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
lib/Cake/Controller

View file

@ -24,6 +24,9 @@ App::uses('CakeResponse', 'Network');
App::uses('ClassRegistry', 'Utility');
App::uses('ComponentCollection', 'Controller');
App::uses('View', 'View');
App::uses('CakeEvent', 'Event');
App::uses('CakeEventListener', 'Event');
App::uses('CakeEventManager', 'Event');
/**
* Application controller class for organization of business logic.
@ -57,7 +60,7 @@ App::uses('View', 'View');
* @property SessionComponent $Session
* @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.
@ -294,6 +297,14 @@ class Controller extends Object {
*/
protected $_mergeParent = 'AppController';
/**
* Instance of the CakeEventManager this controller is using
* to dispatch inner events.
*
* @var CakeEventManager
*/
protected $_eventManager = null;
/**
* 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
* see Controller::loadModel(); for more info.
@ -590,6 +616,22 @@ class Controller extends Object {
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.
* Fire the Components and Controller callbacks in the correct order.
@ -601,9 +643,7 @@ class Controller extends Object {
* @return void
*/
public function startupProcess() {
$this->Components->trigger('initialize', array(&$this));
$this->beforeFilter();
$this->Components->trigger('startup', array(&$this));
$this->getEventManager()->dispatch(new CakeEvent('Controller.startup', $this));
}
/**
@ -616,8 +656,7 @@ class Controller extends Object {
* @return void
*/
public function shutdownProcess() {
$this->Components->trigger('shutdown', array(&$this));
$this->afterFilter();
$this->getEventManager()->dispatch(new CakeEvent('Controller.shutdown', $this));
}
/**
@ -862,8 +901,7 @@ class Controller extends Object {
* @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
*/
public function render($view = null, $layout = null) {
$this->beforeRender();
$this->Components->trigger('beforeRender', array(&$this));
$this->getEventManager()->dispatch(new CakeEvent('Controller.beforeRender', $this));
$viewClass = $this->viewClass;
if ($this->viewClass != 'View') {