Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving the event stack into a separate object.
- Loading branch information
Florian Krämer
committed
May 22, 2016
1 parent
769e0b8
commit 7764965
Showing
4 changed files
with
218 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,136 @@ | |||
<?php | |||
/** | |||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | |||
* | |||
* Licensed under The MIT License | |||
* For full copyright and license information, please see the LICENSE.txt | |||
* Redistributions of files must retain the above copyright notice. | |||
* | |||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | |||
* @link http://cakephp.org CakePHP(tm) Project | |||
* @since 3.3.0 | |||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | |||
*/ | |||
namespace Cake\Event; | |||
|
|||
use Cake\Event\Event; | |||
|
|||
/** | |||
* The Event Stack | |||
*/ | |||
class EventStack implements \ArrayAccess, \Countable | |||
{ | |||
|
|||
/** | |||
* Events list | |||
* | |||
* @var array | |||
*/ | |||
protected $_events = []; | |||
|
|||
/** | |||
* Empties the stack of dispatched events. | |||
* | |||
* @return void | |||
*/ | |||
public function flush() | |||
{ | |||
$this->_events = []; | |||
} | |||
|
|||
/** | |||
* Adds an event to the stack when stacking is enabled. | |||
* | |||
* @param \Cake\Event\Event $event An event to stack. | |||
* @return void | |||
*/ | |||
public function add(Event $event) | |||
{ | |||
$this->_events[] = $event; | |||
} | |||
|
|||
/** | |||
* Whether a offset exists | |||
* | |||
* @link http://php.net/manual/en/arrayaccess.offsetexists.php | |||
* @param mixed $offset <p> | |||
* An offset to check for. | |||
* </p> | |||
* @return boolean true on success or false on failure. | |||
* </p> | |||
* <p> | |||
* The return value will be casted to boolean if non-boolean was returned. | |||
* @since 5.0.0 | |||
*/ | |||
public function offsetExists($offset) | |||
{ | |||
return isset($this->_events[$offset]); | |||
} | |||
|
|||
/** | |||
* Offset to retrieve | |||
* | |||
* @link http://php.net/manual/en/arrayaccess.offsetget.php | |||
* @param mixed $offset <p> | |||
* The offset to retrieve. | |||
* </p> | |||
* @return mixed Can return all value types. | |||
* @since 5.0.0 | |||
*/ | |||
public function offsetGet($offset) | |||
{ | |||
if ($this->offsetExists($offset)) { | |||
return $this->_events[$offset]; | |||
} | |||
return null; | |||
} | |||
|
|||
/** | |||
* Offset to set | |||
* | |||
* @link http://php.net/manual/en/arrayaccess.offsetset.php | |||
* @param mixed $offset <p> | |||
* The offset to assign the value to. | |||
* </p> | |||
* @param mixed $value <p> | |||
* The value to set. | |||
* </p> | |||
* @return void | |||
* @since 5.0.0 | |||
*/ | |||
public function offsetSet($offset, $value) | |||
{ | |||
$this->_events[$offset] = $value; | |||
} | |||
|
|||
/** | |||
* Offset to unset | |||
* | |||
* @link http://php.net/manual/en/arrayaccess.offsetunset.php | |||
* @param mixed $offset <p> | |||
* The offset to unset. | |||
* </p> | |||
* @return void | |||
* @since 5.0.0 | |||
*/ | |||
public function offsetUnset($offset) | |||
{ | |||
unset($this->_events[$offset]); | |||
} | |||
|
|||
/** | |||
* Count elements of an object | |||
* | |||
* @link http://php.net/manual/en/countable.count.php | |||
* @return int The custom count as an integer. | |||
* </p> | |||
* <p> | |||
* The return value is cast to an integer. | |||
* @since 5.1.0 | |||
*/ | |||
public function count() | |||
{ | |||
return count($this->_events); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
<?php | |||
/** | |||
* CakePHP : Rapid Development Framework (http://cakephp.org) | |||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | |||
* | |||
* Licensed under The MIT License | |||
* For full copyright and license information, please see the LICENSE.txt | |||
* Redistributions of files must retain the above copyright notice. | |||
* | |||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | |||
* @link http://cakephp.org CakePHP Project | |||
* @since 3.3.0 | |||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | |||
*/ | |||
namespace Cake\Test\TestCase\Event; | |||
|
|||
use Cake\Event\Event; | |||
use Cake\Event\EventStack; | |||
use Cake\TestSuite\TestCase; | |||
|
|||
/** | |||
* Tests the Cake\Event\EvenStack class functionality | |||
*/ | |||
class EvenStackTest extends TestCase | |||
{ | |||
|
|||
/** | |||
* testAddEventAndFlush | |||
* | |||
* @return void | |||
*/ | |||
public function testAddEventAndFlush() | |||
{ | |||
$eventStack = new EventStack(); | |||
$event = new Event('my_event', $this); | |||
$event2 = new Event('my_second_event', $this); | |||
|
|||
$eventStack->add($event); | |||
$eventStack->add($event2); | |||
$this->assertCount(2, $eventStack); | |||
|
|||
$this->assertEquals($eventStack[0], $event); | |||
$this->assertEquals($eventStack[1], $event2); | |||
|
|||
$eventStack->flush(); | |||
|
|||
$this->assertCount(0, $eventStack); | |||
} | |||
} |