mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #9115 from biesbjerg/ticket-7488
Fire Controller.shutdown and Dispatcher.afterDispatch events when exceptions are thrown
This commit is contained in:
commit
b50c06423d
2 changed files with 51 additions and 2 deletions
|
@ -20,9 +20,12 @@
|
|||
*/
|
||||
|
||||
App::uses('Sanitize', 'Utility');
|
||||
App::uses('Dispatcher', 'Routing');
|
||||
App::uses('Router', 'Routing');
|
||||
App::uses('CakeResponse', 'Network');
|
||||
App::uses('Controller', 'Controller');
|
||||
App::uses('CakeRequest', 'Network');
|
||||
App::uses('CakeResponse', 'Network');
|
||||
App::uses('CakeEvent', 'Event');
|
||||
|
||||
/**
|
||||
* Exception Renderer.
|
||||
|
@ -287,7 +290,7 @@ class ExceptionRenderer {
|
|||
protected function _outputMessage($template) {
|
||||
try {
|
||||
$this->controller->render($template);
|
||||
$this->controller->afterFilter();
|
||||
$this->_shutdown();
|
||||
$this->controller->response->send();
|
||||
} catch (MissingViewException $e) {
|
||||
$attributes = $e->getAttributes();
|
||||
|
@ -327,4 +330,23 @@ class ExceptionRenderer {
|
|||
$this->controller->response->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the shutdown events.
|
||||
*
|
||||
* Triggers the afterFilter and afterDispatch events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _shutdown() {
|
||||
$afterFilterEvent = new CakeEvent('Controller.shutdown', $this->controller);
|
||||
$this->controller->getEventManager()->dispatch($afterFilterEvent);
|
||||
|
||||
$Dispatcher = new Dispatcher();
|
||||
$afterDispatchEvent = new CakeEvent('Dispatcher.afterDispatch', $Dispatcher, array(
|
||||
'request' => $this->controller->request,
|
||||
'response' => $this->controller->response
|
||||
));
|
||||
$Dispatcher->getEventManager()->dispatch($afterDispatchEvent);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ App::uses('ExceptionRenderer', 'Error');
|
|||
App::uses('Controller', 'Controller');
|
||||
App::uses('Component', 'Controller');
|
||||
App::uses('Router', 'Routing');
|
||||
App::uses('CakeEventManager', 'Event');
|
||||
|
||||
/**
|
||||
* Short description for class.
|
||||
|
@ -877,4 +878,30 @@ class ExceptionRendererTest extends CakeTestCase {
|
|||
$this->assertContains(h('SELECT * from poo_query < 5 and :seven'), $result);
|
||||
$this->assertContains("'seven' => (int) 7", $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that rendering exceptions triggers shutdown events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderShutdownEvents() {
|
||||
$fired = array();
|
||||
$listener = function ($event) use (&$fired) {
|
||||
$fired[] = $event->name();
|
||||
};
|
||||
|
||||
$EventManager = CakeEventManager::instance();
|
||||
$EventManager->attach($listener, 'Controller.shutdown');
|
||||
$EventManager->attach($listener, 'Dispatcher.afterDispatch');
|
||||
|
||||
$exception = new Exception('Terrible');
|
||||
$ExceptionRenderer = new ExceptionRenderer($exception);
|
||||
|
||||
ob_start();
|
||||
$ExceptionRenderer->render();
|
||||
ob_get_clean();
|
||||
|
||||
$expected = array('Controller.shutdown', 'Dispatcher.afterDispatch');
|
||||
$this->assertEquals($expected, $fired);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue