Migrating Controller events to use the CakeEventManager

This commit is contained in:
Jose Lorenzo Rodriguez 2011-12-24 21:00:39 -04:30
parent 0a49bd987c
commit 5d67195bf7
4 changed files with 131 additions and 27 deletions

View file

@ -18,6 +18,7 @@
App::uses('ObjectCollection', 'Utility'); App::uses('ObjectCollection', 'Utility');
App::uses('Component', 'Controller'); App::uses('Component', 'Controller');
App::uses('CakeEventListener', 'Event');
/** /**
* Components collection is used as a registry for loaded components and handles loading * Components collection is used as a registry for loaded components and handles loading
@ -25,7 +26,7 @@ App::uses('Component', 'Controller');
* *
* @package Cake.Controller * @package Cake.Controller
*/ */
class ComponentCollection extends ObjectCollection { class ComponentCollection extends ObjectCollection implements CakeEventListener {
/** /**
* The controller that this collection was initialized with. * The controller that this collection was initialized with.
@ -108,4 +109,21 @@ class ComponentCollection extends ObjectCollection {
return $this->_loaded[$alias]; return $this->_loaded[$alias];
} }
/**
* Returns the implemented events that will get routed to the trigger function
* in order to dispatch them separately on each component
*
* @return array
*/
public function implementedEvents() {
return array(
'Controller.startup' => array(
array('callable' => 'trigger', 'priority' => 9),
array('callable' => 'trigger')
),
'Controller.beforeRender' => array('callable' => 'trigger'),
'Controller.beforeRedirect' => array('callable' => 'trigger'),
'Controller.shutdown' => array('callable' => 'trigger', 'priority' => 9),
);
}
} }

View file

@ -24,6 +24,9 @@ App::uses('CakeResponse', 'Network');
App::uses('ClassRegistry', 'Utility'); App::uses('ClassRegistry', 'Utility');
App::uses('ComponentCollection', 'Controller'); App::uses('ComponentCollection', 'Controller');
App::uses('View', 'View'); App::uses('View', 'View');
App::uses('CakeEvent', 'Event');
App::uses('CakeEventListener', 'Event');
App::uses('CakeEventManager', 'Event');
/** /**
* Application controller class for organization of business logic. * Application controller class for organization of business logic.
@ -57,7 +60,7 @@ App::uses('View', 'View');
* @property SessionComponent $Session * @property SessionComponent $Session
* @link http://book.cakephp.org/2.0/en/controllers.html * @link http://book.cakephp.org/2.0/en/controllers.html
*/ */
class Controller extends Object { class Controller extends Object implements CakeEventListener {
/** /**
* The name of this controller. Controller names are plural, named after the model they manipulate. * The name of this controller. Controller names are plural, named after the model they manipulate.
@ -294,6 +297,14 @@ class Controller extends Object {
*/ */
protected $_mergeParent = 'AppController'; protected $_mergeParent = 'AppController';
/**
* Instance of the CakeEventManager this controller is using
* to dispatch inner events.
*
* @var CakeEventManager
*/
protected $_eventManager = null;
/** /**
* Constructor. * Constructor.
* *
@ -570,6 +581,21 @@ class Controller extends Object {
} }
} }
/**
* Returns a list of all events that will fire in the controller during it's lifecycle.
* You can override this function to add you own listener callbacks
*
* @return array
*/
public function implementedEvents() {
return array(
'Controller.startup' => 'beforeFilter',
'Controller.beforeRender' => 'beforeRender',
'Controller.beforeRedirect' => array('callable' => 'beforeRedirect', 'passParams' => true),
'Controller.shutdown' => 'afterFilter'
);
}
/** /**
* Loads Model classes based on the uses property * Loads Model classes based on the uses property
* see Controller::loadModel(); for more info. * see Controller::loadModel(); for more info.
@ -590,6 +616,22 @@ class Controller extends Object {
return true; return true;
} }
/**
* Returns the CakeEventManager manager instance that is handling any callbacks.
* You can use this instance to register any new listeners or callbacks to the
* controller events, or create your own events and trigger them at will.
*
* @return CakeEventManager
*/
public function getEventManager() {
if (empty($this->_eventManager)) {
$this->_eventManager = new CakeEventManager();
$this->_eventManager->attach($this);
$this->_eventManager->attach($this->Components);
}
return $this->_eventManager;
}
/** /**
* Perform the startup process for this controller. * Perform the startup process for this controller.
* Fire the Components and Controller callbacks in the correct order. * Fire the Components and Controller callbacks in the correct order.
@ -601,9 +643,7 @@ class Controller extends Object {
* @return void * @return void
*/ */
public function startupProcess() { public function startupProcess() {
$this->Components->trigger('initialize', array(&$this)); $this->getEventManager()->dispatch(new CakeEvent('Controller.startup', $this));
$this->beforeFilter();
$this->Components->trigger('startup', array(&$this));
} }
/** /**
@ -616,8 +656,7 @@ class Controller extends Object {
* @return void * @return void
*/ */
public function shutdownProcess() { public function shutdownProcess() {
$this->Components->trigger('shutdown', array(&$this)); $this->getEventManager()->dispatch(new CakeEvent('Controller.shutdown', $this));
$this->afterFilter();
} }
/** /**
@ -862,8 +901,7 @@ class Controller extends Object {
* @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
*/ */
public function render($view = null, $layout = null) { public function render($view = null, $layout = null) {
$this->beforeRender(); $this->getEventManager()->dispatch(new CakeEvent('Controller.beforeRender', $this));
$this->Components->trigger('beforeRender', array(&$this));
$viewClass = $this->viewClass; $viewClass = $this->viewClass;
if ($this->viewClass != 'View') { if ($this->viewClass != 'View') {

View file

@ -313,7 +313,7 @@ class TestComponent extends Object {
* *
* @return void * @return void
*/ */
public function initialize(&$controller) { public function initialize($controller) {
} }
/** /**
@ -321,7 +321,7 @@ class TestComponent extends Object {
* *
* @return void * @return void
*/ */
public function startup(&$controller) { public function startup($controller) {
} }
/** /**
@ -329,7 +329,7 @@ class TestComponent extends Object {
* *
* @return void * @return void
*/ */
public function shutdown(&$controller) { public function shutdown($controller) {
} }
/** /**
@ -337,7 +337,7 @@ class TestComponent extends Object {
* *
* @return void * @return void
*/ */
public function beforeRender(&$controller) { public function beforeRender($controller) {
if ($this->viewclass) { if ($this->viewclass) {
$controller->viewClass = $this->viewclass; $controller->viewClass = $this->viewclass;
} }
@ -1111,17 +1111,35 @@ class ControllerTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testStartupProcess() { public function testStartupProcess() {
$Controller = $this->getMock('Controller', array('beforeFilter', 'afterFilter')); $Controller = $this->getMock('Controller', array('getEventManager'));
$Controller->components = array('MockStartup'); $eventManager = $this->getMock('CakeEventManager');
$Controller->Components = $this->getMock('ComponentCollection'); $eventManager->expects($this->once())->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('CakeEvent'),
$this->attributeEqualTo('_name', 'Controller.startup'),
$this->attributeEqualTo('_subject', $Controller)
)
);
$Controller->expects($this->once())->method('getEventManager')
->will($this->returnValue($eventManager));
$Controller->startupProcess();
}
/**
* Tests that the shutdown process calls the correct functions
*
* @return void
*/
public function testStartupProcessIndirect() {
$Controller = $this->getMock('Controller', array('beforeFilter'));
$Controller->components = array('MockShutdown');
$Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
$Controller->expects($this->once())->method('beforeFilter'); $Controller->expects($this->once())->method('beforeFilter');
$Controller->Components->expects($this->at(0))->method('trigger') $Controller->Components->expects($this->exactly(2))->method('trigger')->with($this->isInstanceOf('CakeEvent'));
->with('initialize', array(&$Controller));
$Controller->Components->expects($this->at(1))->method('trigger')
->with('startup', array(&$Controller));
$Controller->startupProcess(); $Controller->startupProcess();
} }
@ -1132,14 +1150,35 @@ class ControllerTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testShutdownProcess() { public function testShutdownProcess() {
$Controller = $this->getMock('Controller', array('beforeFilter', 'afterFilter')); $Controller = $this->getMock('Controller', array('getEventManager'));
$eventManager = $this->getMock('CakeEventManager');
$eventManager->expects($this->once())->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('CakeEvent'),
$this->attributeEqualTo('_name', 'Controller.shutdown'),
$this->attributeEqualTo('_subject', $Controller)
)
);
$Controller->expects($this->once())->method('getEventManager')
->will($this->returnValue($eventManager));
$Controller->shutdownProcess();
}
/**
* Tests that the shutdown process calls the correct functions
*
* @return void
*/
public function testShutdownProcessIndirect() {
$Controller = $this->getMock('Controller', array('afterFilter'));
$Controller->components = array('MockShutdown'); $Controller->components = array('MockShutdown');
$Controller->Components = $this->getMock('ComponentCollection'); $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
$Controller->expects($this->once())->method('afterFilter'); $Controller->expects($this->once())->method('afterFilter');
$Controller->Components->expects($this->once())->method('trigger') $Controller->Components->expects($this->exactly(1))->method('trigger')->with($this->isInstanceOf('CakeEvent'));
->with('shutdown', array(&$Controller));
$Controller->shutdownProcess(); $Controller->shutdownProcess();
} }

View file

@ -82,8 +82,10 @@ abstract class ObjectCollection {
* Defaults to false. * Defaults to false.
* *
* *
* @param string $callback Method to fire on all the objects. Its assumed all the objects implement * @param string $callback|CakeEvent Method to fire on all the objects. Its assumed all the objects implement
* the method you are calling. * the method you are calling. If an instance of CakeEvent is provided, then then Event name will parsed to
* get the callback name. This is done by getting the last word after any dot in the event name
* (eg. `Model.afterSave` event will trigger the `afterSave` callback)
* @param array $params Array of parameters for the triggered callback. * @param array $params Array of parameters for the triggered callback.
* @param array $options Array of options. * @param array $options Array of options.
* @return mixed Either the last result or all results if collectReturn is on. * @return mixed Either the last result or all results if collectReturn is on.
@ -93,6 +95,13 @@ abstract class ObjectCollection {
if (empty($this->_enabled)) { if (empty($this->_enabled)) {
return true; return true;
} }
if ($callback instanceof CakeEvent) {
if (is_array($callback->data)) {
$params = $callback->data;
}
$params = array($callback->subject());
$callback = array_pop(explode('.', $callback->name()));
}
$options = array_merge( $options = array_merge(
array( array(
'break' => false, 'break' => false,