Skip to content

Commit

Permalink
Change to doclean before update that you could add it back in update
Browse files Browse the repository at this point in the history
function
  • Loading branch information
HillLiu committed Aug 16, 2018
1 parent 511b32f commit 3660f5c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
8 changes: 3 additions & 5 deletions dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ public function notify($state,$clean=null)
$this->_notify($state.POST, $clean);
}

private function _notify($state,$clean=null)
private function _notify($state, $clean=null)
{
if(isset($this->_subjects[$state])){
if (isset($this->_subjects[$state])) {
$this->_subjects[$state]->setDoClean($clean);
$this->_subjects[$state]->notify();
if($clean){
$this->_subjects[$state]->removeAll();
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Subject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Subject implements SplSubject
}

private $_name;
private $_doClean;

public function __construct($name)
{
$this->_name = $name;
Expand All @@ -29,6 +31,9 @@ public function notify()
while ($store->valid()) {
$obj = $store->current();
$store->next();
if ($this->_doClean) {
$store->detach($obj);
}
$obj->update($this);
if ($store->contains($obj)) {
$tmp->attach($obj);
Expand All @@ -44,6 +49,11 @@ public function getName()
return $this->_name;
}

public function setDoClean($bool)
{
$this->_doClean = $bool;
}

private function _getThis(SplObserver $observer)
{
return \PMVC\get($observer, \PMVC\THIS, function() use($observer) { return $observer; });
Expand Down
30 changes: 30 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ function testFireEvent()
$dispatcher->notify($event);
}

public function testFireWithoutClean()
{
$event = 'Test';
$dispatcher = PMVC\plug($this->_plug);

$mockObserver = $this->getMockBuilder(__NAMESPACE__.'\MockObserver')
->setMethods(['on'.$event])
->getMock();
$mockObserver->expects($this->once())
->method('on'.$event);
$dispatcher->attach($mockObserver, $event);
$dispatcher->notify($event);
$this->assertTrue($dispatcher->contains($mockObserver, $event));
}

public function testFireWithClean()
{
$event = 'Test';
$dispatcher = PMVC\plug($this->_plug);

$mockObserver = $this->getMockBuilder(__NAMESPACE__.'\MockObserver')
->setMethods(['on'.$event])
->getMock();
$mockObserver->expects($this->once())
->method('on'.$event);
$dispatcher->attach($mockObserver, $event);
$dispatcher->notify($event, true);
$this->assertFalse($dispatcher->contains($mockObserver, $event));
}

function testFireEventBySetOption()
{
$key = 'foo';
Expand Down

0 comments on commit 3660f5c

Please sign in to comment.