From 7fdc1cc8d4e42728413e9a4903b6e052ec4aa610 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 25 Dec 2011 22:48:27 -0430 Subject: [PATCH] Migrating the beforeRedirect callback to the CakeEventManager, reorganizing how events are triggered in controller --- lib/Cake/Controller/ComponentCollection.php | 2 +- lib/Cake/Controller/Controller.php | 30 +++++++++---------- .../Test/Case/Controller/ControllerTest.php | 14 ++++----- lib/Cake/Utility/ObjectCollection.php | 6 ++-- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/Cake/Controller/ComponentCollection.php b/lib/Cake/Controller/ComponentCollection.php index 6d65dd6b1bf..530ec6f7894 100644 --- a/lib/Cake/Controller/ComponentCollection.php +++ b/lib/Cake/Controller/ComponentCollection.php @@ -121,7 +121,7 @@ public function implementedEvents() { 'Controller.startup' => array('callable' => 'trigger'), 'Controller.beforeRender' => array('callable' => 'trigger'), 'Controller.beforeRedirect' => array('callable' => 'trigger'), - 'Controller.shutdown' => array('callable' => 'trigger', 'priority' => 9), + 'Controller.shutdown' => array('callable' => 'trigger'), ); } } \ No newline at end of file diff --git a/lib/Cake/Controller/Controller.php b/lib/Cake/Controller/Controller.php index f90cf0c613b..a5b912feb85 100644 --- a/lib/Cake/Controller/Controller.php +++ b/lib/Cake/Controller/Controller.php @@ -589,8 +589,9 @@ protected function _mergeControllerVars() { */ public function implementedEvents() { return array( - 'Controller.startup' => 'beforeFilter', + 'Controller.initialize' => 'beforeFilter', 'Controller.beforeRender' => 'beforeRender', + 'Controller.beforeRedirect' => array('callable' => 'beforeRedirect', 'passParams' => true), 'Controller.shutdown' => 'afterFilter' ); } @@ -625,8 +626,8 @@ public function constructClasses() { public function getEventManager() { if (empty($this->_eventManager)) { $this->_eventManager = new CakeEventManager(); - $this->_eventManager->attach($this); $this->_eventManager->attach($this->Components); + $this->_eventManager->attach($this); } return $this->_eventManager; } @@ -730,21 +731,16 @@ public function redirect($url, $status = null, $exit = true) { if (is_array($status)) { extract($status, EXTR_OVERWRITE); } - $response = $this->Components->trigger( - 'beforeRedirect', - array(&$this, $url, $status, $exit), - array('break' => true, 'breakOn' => false, 'collectReturn' => true) - ); + $event = new CakeEvent('Controller.beforeRedirect', $this, array($url, $status, $exit)); + //TODO: Remove the following two lines when the events are fully migrated to the CakeEventManager + $event->breakOn = false; + $event->collectReturn = true; + $this->getEventManager()->dispatch($event); - if ($response === false) { - return; - } - extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE); - - $response = $this->beforeRedirect($url, $status, $exit); - if ($response === false) { + if ($event->isStopped()) { return; } + $response = $event->result; extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE); if (function_exists('session_write_close')) { @@ -1094,11 +1090,13 @@ public function beforeRender() { * or an absolute URL * @param integer $status Optional HTTP status code (eg: 404) * @param boolean $exit If true, exit() will be called after the redirect - * @return boolean + * @return mixed + * false to stop redirection event, + * string controllers a new redirection url or + * array with the keys url, status and exit to be used by the redirect method. * @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks */ public function beforeRedirect($url, $status = null, $exit = true) { - return true; } /** diff --git a/lib/Cake/Test/Case/Controller/ControllerTest.php b/lib/Cake/Test/Case/Controller/ControllerTest.php index e7bbf9c0d92..dc73e86aace 100644 --- a/lib/Cake/Test/Case/Controller/ControllerTest.php +++ b/lib/Cake/Test/Case/Controller/ControllerTest.php @@ -712,7 +712,7 @@ public function testRedirectByCode($code, $msg) { $Controller = new Controller(null); $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode')); - $Controller->Components = $this->getMock('ComponentCollection'); + $Controller->Components = $this->getMock('ComponentCollection', array('trigger')); $Controller->response->expects($this->once())->method('statusCode') ->with($code); @@ -733,7 +733,7 @@ public function testRedirectByMessage($code, $msg) { $Controller = new Controller(null); $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode')); - $Controller->Components = $this->getMock('ComponentCollection'); + $Controller->Components = $this->getMock('ComponentCollection', array('trigger')); $Controller->response->expects($this->once())->method('statusCode') ->with($code); @@ -753,7 +753,7 @@ public function testRedirectByMessage($code, $msg) { public function testRedirectTriggeringComponentsReturnNull() { $Controller = new Controller(null); $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode')); - $Controller->Components = $this->getMock('ComponentCollection'); + $Controller->Components = $this->getMock('ComponentCollection', array('trigger')); $Controller->Components->expects($this->once())->method('trigger') ->will($this->returnValue(null)); @@ -775,7 +775,7 @@ public function testRedirectTriggeringComponentsReturnNull() { public function testRedirectBeforeRedirectModifyingParams() { $Controller = new Controller(null); $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode')); - $Controller->Components = $this->getMock('ComponentCollection'); + $Controller->Components = $this->getMock('ComponentCollection', array('trigger')); $Controller->Components->expects($this->once())->method('trigger') ->will($this->returnValue(array('http://book.cakephp.org'))); @@ -797,7 +797,7 @@ public function testRedirectBeforeRedirectModifyingParams() { public function testRedirectBeforeRedirectModifyingParamsArrayReturn() { $Controller = $this->getMock('Controller', array('header', '_stop')); $Controller->response = $this->getMock('CakeResponse'); - $Controller->Components = $this->getMock('ComponentCollection'); + $Controller->Components = $this->getMock('ComponentCollection', array('trigger')); $return = array( array( @@ -812,7 +812,7 @@ public function testRedirectBeforeRedirectModifyingParamsArrayReturn() { $Controller->Components->expects($this->once())->method('trigger') ->will($this->returnValue($return)); - $Controller->response->expects($this->at(0))->method('header') + $Controller->response->expects($this->once())->method('header') ->with('Location', 'http://example.com/test/2'); $Controller->response->expects($this->at(1))->method('statusCode') @@ -830,7 +830,7 @@ public function testRedirectBeforeRedirectModifyingParamsArrayReturn() { public function testRedirectBeforeRedirectInController() { $Controller = $this->getMock('Controller', array('_stop', 'beforeRedirect')); $Controller->response = $this->getMock('CakeResponse', array('header')); - $Controller->Components = $this->getMock('ComponentCollection'); + $Controller->Components = $this->getMock('ComponentCollection', array('trigger')); $Controller->expects($this->once())->method('beforeRedirect') ->will($this->returnValue(false)); diff --git a/lib/Cake/Utility/ObjectCollection.php b/lib/Cake/Utility/ObjectCollection.php index e72ed577672..4c575592dd2 100644 --- a/lib/Cake/Utility/ObjectCollection.php +++ b/lib/Cake/Utility/ObjectCollection.php @@ -103,8 +103,10 @@ public function trigger($callback, $params = array(), $options = array()) { $params = array($event->subject()); } //TODO: Temporary BC check, while we move all the triggers system into the CakeEventManager - if (isset($event->modParams)) { - $options['modParams'] = $event->modParams; + foreach (array('breakOn', 'collectReturn', 'modParams') as $opt) { + if (isset($event->{$opt})) { + $options[$opt] = $event->{$opt}; + } } $callback = array_pop(explode('.', $event->name())); }