Skip to content

Commit

Permalink
Migrating the beforeRedirect callback to the CakeEventManager, reorga…
Browse files Browse the repository at this point in the history
…nizing how events are triggered in controller
  • Loading branch information
lorenzo committed Dec 26, 2011
1 parent ffa12f4 commit 7fdc1cc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Controller/ComponentCollection.php
Expand Up @@ -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'),
);
}
}
30 changes: 14 additions & 16 deletions lib/Cake/Controller/Controller.php
Expand Up @@ -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'
);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions lib/Cake/Test/Case/Controller/ControllerTest.php
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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')));
Expand All @@ -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(
Expand All @@ -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')
Expand All @@ -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));
Expand Down
6 changes: 4 additions & 2 deletions lib/Cake/Utility/ObjectCollection.php
Expand Up @@ -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()));
}
Expand Down

0 comments on commit 7fdc1cc

Please sign in to comment.