Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
- Implemented afterInitialize event (#782)
- Phalcon\Dispatcher optimizations (#782)
- Added getPreviousControllerName(), getPreviousActionName() (#1462)
- If an event handler throws an exception, call dispatch:beforeException and break the dispatch loop (#1763)
- Phalcon\Element:
- Phalcon\Element::addFilter() incorrectly prepends NULL as the first element (#1019)
- Phalcon\Escaper:
Expand Down
24 changes: 23 additions & 1 deletion ext/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ PHP_METHOD(Phalcon_Dispatcher, getReturnedValue){
RETURN_MEMBER(this_ptr, "_returnedValue");
}

static inline int phalcon_dispatcher_fire_event(zval *return_value, zval *mgr, const char *event, zval *source, zval *data TSRMLS_DC)
static int phalcon_dispatcher_fire_event(zval *return_value, zval *mgr, const char *event, zval *source, zval *data TSRMLS_DC)
{
if (mgr) {
zval *event_name;
Expand All @@ -514,6 +514,28 @@ static inline int phalcon_dispatcher_fire_event(zval *return_value, zval *mgr, c

status = phalcon_call_method_params(return_value, NULL, mgr, SL("fire"), zend_inline_hash_func(SS("fire")) TSRMLS_CC, (data ? 3 : 2), event_name, source, data);

if (EG(exception)) {
zval *exception = EG(exception);
Z_ADDREF_P(exception);

zend_clear_exception(TSRMLS_C);

assert(Z_REFCOUNT_P(exception) == 1);
/* exception will be destroyed automatically after return from _handleexception */
Z_DELREF_P(exception);

/* source == this_ptr */
assert(Z_TYPE_P(source) == IS_OBJECT && instanceof_function_ex(Z_OBJCE_P(source), phalcon_dispatcherinterface_ce, 1 TSRMLS_CC));
if (Z_OBJCE_P(source)->type == ZEND_INTERNAL_CLASS) {
/* Shortcut, save one method call */
ZVAL_STRING(event_name, "dispatch:beforeException", 0);
phalcon_call_method_params(NULL, NULL, mgr, SL("fire"), zend_inline_hash_func(SS("fire")) TSRMLS_CC, 3, event_name, source, exception);
}
else {
phalcon_call_method_params(NULL, NULL, source, SL("_handleexception"), zend_inline_hash_func(SS("_handleexception")) TSRMLS_CC, 1, exception);
}
}

ZVAL_NULL(event_name);
zval_ptr_dtor(&event_name);
return status;
Expand Down
3 changes: 3 additions & 0 deletions ext/mvc/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ PHP_METHOD(Phalcon_Mvc_Dispatcher, _throwDispatchException){
* Handles a user exception
*
* @param \Exception $exception
*
* @warning If any additional logic is to be implemented here, please check
* phalcon_dispatcher_fire_event() first
*/
PHP_METHOD(Phalcon_Mvc_Dispatcher, _handleException){

Expand Down
39 changes: 39 additions & 0 deletions ext/tests/issue-1763.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
Fire dispatch:beforeException on exceptions from event handlers - https://github.com/phalcon/cphalcon/issues/1763
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php

$di = new \Phalcon\DI\FactoryDefault();
$di->set('dispatcher', function() {
$eventsManager = new Phalcon\Events\Manager;

$eventsManager->attach('dispatch:beforeDispatchLoop', function($event, $dispatcher) {
throw new Exception('Just because.');
});

$eventsManager->attach('dispatch:beforeException', function($event, $dispatcher, $exception) {
while (ob_get_level()) {
ob_end_clean();
}

echo 'dispatch:beforeException', PHP_EOL;
ob_start();
});

$dispatcher = new Phalcon\Mvc\Dispatcher;
$dispatcher->setEventsManager($eventsManager);

return $dispatcher;
});

$di->set('view', 'Phalcon\\Mvc\\View');

$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
echo 'After $application->handle()->getContent()', PHP_EOL;
?>
--EXPECT--
dispatch:beforeException
After $application->handle()->getContent()