diff --git a/ext/cli/router.c b/ext/cli/router.c index 972a983a70a..50d4472f2b2 100644 --- a/ext/cli/router.c +++ b/ext/cli/router.c @@ -127,11 +127,8 @@ PHALCON_INIT_CLASS(Phalcon_CLI_Router){ */ PHP_METHOD(Phalcon_CLI_Router, __construct){ - - phalcon_update_property_empty_array(phalcon_cli_router_ce, this_ptr, SL("_params") TSRMLS_CC); - - phalcon_update_property_empty_array(phalcon_cli_router_ce, this_ptr, SL("_defaultParams") TSRMLS_CC); - + phalcon_update_property_empty_array(this_ptr, SL("_params") TSRMLS_CC); + phalcon_update_property_empty_array(this_ptr, SL("_defaultParams") TSRMLS_CC); } /** diff --git a/ext/dispatcher.c b/ext/dispatcher.c index c9435a40ace..9a0c0970423 100644 --- a/ext/dispatcher.c +++ b/ext/dispatcher.c @@ -161,9 +161,7 @@ PHALCON_INIT_CLASS(Phalcon_Dispatcher){ */ PHP_METHOD(Phalcon_Dispatcher, __construct){ - - phalcon_update_property_empty_array(phalcon_dispatcher_ce, this_ptr, SL("_params") TSRMLS_CC); - + phalcon_update_property_empty_array(this_ptr, SL("_params") TSRMLS_CC); } /** diff --git a/ext/kernel/object.c b/ext/kernel/object.c index 080c52c0fb5..b2f56cbbb9f 100644 --- a/ext/kernel/object.c +++ b/ext/kernel/object.c @@ -1131,7 +1131,7 @@ int phalcon_update_property_array_append(zval *object, char *property, unsigned /** * Intializes an object property with an empty array */ -int phalcon_update_property_empty_array(zend_class_entry *ce, zval *object, char *property_name, unsigned int property_length TSRMLS_DC) { +int phalcon_update_property_empty_array(zval *object, char *property_name, unsigned int property_length TSRMLS_DC) { zval *empty_array; int res; diff --git a/ext/kernel/object.h b/ext/kernel/object.h index a3716a30496..0f3aa24ba20 100644 --- a/ext/kernel/object.h +++ b/ext/kernel/object.h @@ -78,7 +78,7 @@ int phalcon_update_property_bool(zval *obj, char *property_name, unsigned int pr int phalcon_update_property_null(zval *obj, char *property_name, unsigned int property_length TSRMLS_DC); int phalcon_update_property_zval(zval *obj, char *property_name, unsigned int property_length, zval *value TSRMLS_DC); int phalcon_update_property_zval_zval(zval *obj, zval *property, zval *value TSRMLS_DC); -int phalcon_update_property_empty_array(zend_class_entry *ce, zval *object, char *property, unsigned int property_length TSRMLS_DC); +int phalcon_update_property_empty_array(zval *object, char *property, unsigned int property_length TSRMLS_DC); /** Updating array properties */ int phalcon_update_property_array(zval *object, char *property, unsigned int property_length, zval *index, zval *value TSRMLS_DC); diff --git a/ext/kernel/require.c b/ext/kernel/require.c index 83c2af9c908..86e6bb03fe4 100644 --- a/ext/kernel/require.c +++ b/ext/kernel/require.c @@ -83,7 +83,9 @@ int phalcon_require_ret(zval **return_value_ptr, const char *require_path TSRMLS return ret; } } + else { + zend_destroy_file_handle(&file_handle TSRMLS_CC); + } - zend_destroy_file_handle(&file_handle TSRMLS_CC); return FAILURE; } diff --git a/ext/mvc/application.c b/ext/mvc/application.c index 95c7d016dd2..0b00910c5e6 100644 --- a/ext/mvc/application.c +++ b/ext/mvc/application.c @@ -26,6 +26,7 @@ #include "mvc/viewinterface.h" #include "di/injectable.h" #include "diinterface.h" +#include "events/managerinterface.h" #include "http/responseinterface.h" #include @@ -269,6 +270,26 @@ PHP_METHOD(Phalcon_Mvc_Application, getDefaultModule){ RETURN_MEMBER(this_ptr, "_defaultModule"); } +static int phalcon_mvc_application_fire_event(zval *mgr, const char *event, zval *this_ptr, zval *params TSRMLS_DC) +{ + if (mgr) { + zval *event_name; + zval *status = NULL; + uint params_cnt = 2 + (params != NULL ? 1 : 0); + + PHALCON_ALLOC_GHOST_ZVAL(event_name); + ZVAL_STRING(event_name, event, 1); + + if (FAILURE == phalcon_call_method_params(status, &status, mgr, SL("fire"), zend_inline_hash_func(SS("fire")) TSRMLS_CC, params_cnt, event_name, this_ptr, params)) { + return FAILURE; + } + + return (Z_TYPE_P(status) == IS_BOOL && !Z_BVAL_P(status)) ? FAILURE : SUCCESS; + } + + return SUCCESS; +} + /** * Handles a MVC request * @@ -278,14 +299,14 @@ PHP_METHOD(Phalcon_Mvc_Application, getDefaultModule){ PHP_METHOD(Phalcon_Mvc_Application, handle){ zval *uri = NULL, *dependency_injector, *events_manager; - zval *event_name = NULL, *status = NULL, *service = NULL, *router, *module_name = NULL; + zval *status = NULL, *service = NULL, *router, *module_name = NULL; zval *module_object = NULL, *modules; zval *module, *class_name = NULL, *module_params; zval *implicit_view, *view, *namespace_name; zval *controller_name = NULL, *action_name = NULL, *params = NULL; zval *dispatcher, *controller, *returned_response = NULL; zval *possible_response, *render_status = NULL, *response = NULL; - zval *content; + zval *content, *path; int f_implicit_view; PHALCON_MM_GROW(); @@ -303,20 +324,16 @@ PHP_METHOD(Phalcon_Mvc_Application, handle){ } events_manager = phalcon_fetch_nproperty_this(this_ptr, SL("_eventsManager"), PH_NOISY_CC); - - /** - * Call boot event, this allow the developer to perform initialization actions - */ - if (Z_TYPE_P(events_manager) == IS_OBJECT) { - - PHALCON_INIT_VAR(event_name); - ZVAL_STRING(event_name, "application:boot", 1); - - PHALCON_INIT_VAR(status); - phalcon_call_method_p2(status, events_manager, "fire", event_name, this_ptr); - if (PHALCON_IS_FALSE(status)) { - RETURN_MM_FALSE; - } + if (Z_TYPE_P(events_manager) != IS_OBJECT) { + events_manager = NULL; + } + else { + PHALCON_VERIFY_INTERFACE_EX(events_manager, phalcon_events_managerinterface_ce, phalcon_mvc_application_exception_ce, 1); + } + + /* Call boot event, this allows the developer to perform initialization actions */ + if (FAILURE == phalcon_mvc_application_fire_event(events_manager, "application:boot", getThis(), NULL TSRMLS_CC)) { + RETURN_MM_FALSE; } PHALCON_INIT_VAR(service); @@ -349,18 +366,10 @@ PHP_METHOD(Phalcon_Mvc_Application, handle){ * Process the module definition */ if (zend_is_true(module_name)) { - if (Z_TYPE_P(events_manager) == IS_OBJECT) { - - PHALCON_INIT_NVAR(event_name); - ZVAL_STRING(event_name, "application:beforeStartModule", 1); - - PHALCON_INIT_NVAR(status); - phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, module_name); - if (PHALCON_IS_FALSE(status)) { - RETURN_MM_FALSE; - } + if (FAILURE == phalcon_mvc_application_fire_event(events_manager, "application:beforeStartModule", getThis(), module_name TSRMLS_CC)) { + RETURN_MM_FALSE; } - + /** * Check if the module passed by the router is registered in the modules container */ @@ -373,7 +382,7 @@ PHP_METHOD(Phalcon_Mvc_Application, handle){ } /** - * A module definition must ne an array or an object + * A module definition must be an array or an object */ if (Z_TYPE_P(module) != IS_ARRAY && Z_TYPE_P(module) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "Invalid module definition"); @@ -386,8 +395,6 @@ PHP_METHOD(Phalcon_Mvc_Application, handle){ * An array module definition contains a path to a module definition class */ if (Z_TYPE_P(module) == IS_ARRAY) { - zval *path; - /** * Class name used to load the module definition */ @@ -437,18 +444,10 @@ PHP_METHOD(Phalcon_Mvc_Application, handle){ return; } - /** - * Calling afterStartModule event - */ - if (Z_TYPE_P(events_manager) == IS_OBJECT) { + /* Calling afterStartModule event */ + if (events_manager) { phalcon_update_property_this(this_ptr, SL("_moduleObject"), module_object TSRMLS_CC); - - PHALCON_INIT_NVAR(event_name); - ZVAL_STRING(event_name, "application:afterStartModule", 1); - - PHALCON_INIT_NVAR(status); - phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, module_name); - if (PHALCON_IS_FALSE(status)) { + if (FAILURE == phalcon_mvc_application_fire_event(events_manager, "application:afterStartModule", getThis(), module_name TSRMLS_CC)) { RETURN_MM_FALSE; } } @@ -516,19 +515,9 @@ PHP_METHOD(Phalcon_Mvc_Application, handle){ phalcon_call_method_noret(view, "start"); } - /** - * Calling beforeHandleRequest - */ - if (Z_TYPE_P(events_manager) == IS_OBJECT) { - - PHALCON_INIT_NVAR(event_name); - ZVAL_STRING(event_name, "application:beforeHandleRequest", 1); - - PHALCON_INIT_NVAR(status); - phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, dispatcher); - if (PHALCON_IS_FALSE(status)) { - RETURN_MM_FALSE; - } + /* Calling beforeHandleRequest */ + if (FAILURE == phalcon_mvc_application_fire_event(events_manager, "application:beforeHandleRequest", getThis(), dispatcher TSRMLS_CC)) { + RETURN_MM_FALSE; } /** @@ -554,13 +543,9 @@ PHP_METHOD(Phalcon_Mvc_Application, handle){ ZVAL_FALSE(returned_response); } - /** - * Calling afterHandleRequest - */ - if (Z_TYPE_P(events_manager) == IS_OBJECT) { - PHALCON_INIT_NVAR(event_name); - ZVAL_STRING(event_name, "application:afterHandleRequest", 1); - phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, controller); + /* Calling afterHandleRequest */ + if (FAILURE == phalcon_mvc_application_fire_event(events_manager, "application:afterHandleRequest", getThis(), controller TSRMLS_CC) && EG(exception)) { + RETURN_MM(); } /** @@ -577,11 +562,17 @@ PHP_METHOD(Phalcon_Mvc_Application, handle){ /** * This allows to make a custom view render */ - if (Z_TYPE_P(events_manager) == IS_OBJECT) { - PHALCON_INIT_NVAR(event_name); - ZVAL_STRING(event_name, "application:viewRender", 1); - - phalcon_call_method_p3(render_status, events_manager, "fire", event_name, this_ptr, view); + if (events_manager) { + if (FAILURE == phalcon_mvc_application_fire_event(events_manager, "application:viewRender", getThis(), view TSRMLS_CC)) { + if (EG(exception)) { + RETURN_MM(); + } + + ZVAL_FALSE(render_status); + } + else { + ZVAL_TRUE(render_status); + } } else { ZVAL_TRUE(render_status); @@ -639,13 +630,10 @@ PHP_METHOD(Phalcon_Mvc_Application, handle){ PHALCON_CPY_WRT(response, possible_response); } - /** - * Calling beforeSendResponse - */ - if (Z_TYPE_P(events_manager) == IS_OBJECT) { - PHALCON_INIT_NVAR(event_name); - ZVAL_STRING(event_name, "application:beforeSendResponse", 1); - phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, response); + + /* Calling beforeSendResponse */ + if (FAILURE == phalcon_mvc_application_fire_event(events_manager, "application:beforeSendResponse", getThis(), response TSRMLS_CC) && EG(exception)) { + RETURN_MM(); } /** diff --git a/ext/mvc/router.c b/ext/mvc/router.c index 74fdbb5ddba..e5230cc000f 100644 --- a/ext/mvc/router.c +++ b/ext/mvc/router.c @@ -217,12 +217,12 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Router){ */ PHP_METHOD(Phalcon_Mvc_Router, __construct){ - zval *default_routes = NULL, *routes, *routes_name_lookup, *paths = NULL, *action_pattern; - zval *route = NULL, *params_pattern, *params; + zval *default_routes = NULL, *routes, *paths = NULL, *action_pattern; + zval *route = NULL, *params_pattern; PHALCON_MM_GROW(); - phalcon_update_property_empty_array(phalcon_mvc_router_ce, this_ptr, SL("_defaultParams") TSRMLS_CC); + phalcon_update_property_empty_array(this_ptr, SL("_defaultParams") TSRMLS_CC); phalcon_fetch_params(1, 0, 1, &default_routes); @@ -267,13 +267,9 @@ PHP_METHOD(Phalcon_Mvc_Router, __construct){ phalcon_array_append(&routes, route, 0); } - PHALCON_INIT_VAR(params); - array_init(params); - PHALCON_INIT_VAR(routes_name_lookup); - array_init(routes_name_lookup); - phalcon_update_property_this(this_ptr, SL("_params"), params TSRMLS_CC); + phalcon_update_property_empty_array(this_ptr, SL("_params") TSRMLS_CC); phalcon_update_property_this(this_ptr, SL("_routes"), routes TSRMLS_CC); - phalcon_update_property_this(this_ptr, SL("_routesNameLookup"), routes_name_lookup TSRMLS_CC); + phalcon_update_property_empty_array(this_ptr, SL("_routesNameLookup") TSRMLS_CC); PHALCON_MM_RESTORE(); } @@ -321,7 +317,7 @@ PHP_METHOD(Phalcon_Mvc_Router, getRewriteUri){ /** * By default we use $_GET['url'] to obtain the rewrite information */ - if (!zend_is_true(uri_source)) { + if (!zend_is_true(uri_source)) { /* FIXME: Compare with URI_SOURCE_SERVER_REQUEST_URI */ _GET = phalcon_get_global(SS("_GET") TSRMLS_CC); if (phalcon_array_isset_string_fetch(&url, _GET, SS("_url"))) { if (PHALCON_IS_NOT_EMPTY(url)) { @@ -357,7 +353,7 @@ PHP_METHOD(Phalcon_Mvc_Router, getRewriteUri){ * $router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); * * - * @param string $uriSource + * @param int $uriSource * @return Phalcon\Mvc\Router */ PHP_METHOD(Phalcon_Mvc_Router, setUriSource){ @@ -545,7 +541,7 @@ PHP_METHOD(Phalcon_Mvc_Router, setDefaults){ } /** - * Returns an array of default paths + * Returns an array of default parameters * * @return array */ @@ -727,6 +723,7 @@ PHP_METHOD(Phalcon_Mvc_Router, handle){ if (phalcon_memnstr_str(hostname, SL("("))) { if (!phalcon_memnstr_str(hostname, SL("#"))) { PHALCON_INIT_NVAR(regex_host_name); + /* FIXME: handle mixed case */ PHALCON_CONCAT_SVS(regex_host_name, "#^", hostname, "$#"); } else { PHALCON_CPY_WRT(regex_host_name, hostname); @@ -734,6 +731,7 @@ PHP_METHOD(Phalcon_Mvc_Router, handle){ RETURN_MM_ON_FAILURE(phalcon_preg_match(matched, regex_host_name, current_host_name, NULL TSRMLS_CC)); } else { + /* FIXME: handle mixed case */ is_equal_function(matched, current_host_name, hostname TSRMLS_CC); } @@ -1221,7 +1219,7 @@ PHP_METHOD(Phalcon_Mvc_Router, mount){ /** * Set a group of paths to be returned when none of the defined routes are matched * - * @param array $paths + * @param array|string $paths * @return Phalcon\Mvc\Router */ PHP_METHOD(Phalcon_Mvc_Router, notFound){ @@ -1361,7 +1359,7 @@ PHP_METHOD(Phalcon_Mvc_Router, getRoutes){ * Returns a route object by its id * * @param string $id - * @return Phalcon\Mvc\Router\Route + * @return Phalcon\Mvc\Router\Route | false */ PHP_METHOD(Phalcon_Mvc_Router, getRouteById){ diff --git a/ext/mvc/router/route.c b/ext/mvc/router/route.c index 93d28c4bd31..d01987d8e8b 100644 --- a/ext/mvc/router/route.c +++ b/ext/mvc/router/route.c @@ -124,7 +124,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Router_Route){ zend_declare_property_null(phalcon_mvc_router_route_ce, SL("_name"), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(phalcon_mvc_router_route_ce, SL("_beforeMatch"), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(phalcon_mvc_router_route_ce, SL("_group"), ZEND_ACC_PROTECTED TSRMLS_CC); - zend_declare_property_null(phalcon_mvc_router_route_ce, SL("_uniqueId"), ZEND_ACC_STATIC|ZEND_ACC_PROTECTED TSRMLS_CC); + zend_declare_property_long(phalcon_mvc_router_route_ce, SL("_uniqueId"), 0, ZEND_ACC_STATIC|ZEND_ACC_PROTECTED TSRMLS_CC); zend_class_implements(phalcon_mvc_router_route_ce TSRMLS_CC, 1, phalcon_mvc_router_routeinterface_ce); diff --git a/ext/tests/issue-1982.phpt b/ext/tests/issue-1982.phpt new file mode 100644 index 00000000000..7bf6bff43ba --- /dev/null +++ b/ext/tests/issue-1982.phpt @@ -0,0 +1,45 @@ +--TEST-- +Crashes in multimodule applications - https://github.com/phalcon/cphalcon/pull/1982 +--SKIPIF-- + +--FILE-- +setDefaultModule('frontend')->notFound(array("controller" => "index", "action" => "index")); + +define('NEED_MODULE', 1); +$application = new \Phalcon\Mvc\Application($di); +$application->registerModules( + array( + 'frontend' => array('className' => 'Module', 'path' => __FILE__), + ) +); +echo $application->handle()->getContent(); + +else : + +class Module +{ + public function registerAutoloaders() + { + } + + public function registerServices() + { + } +} + +endif; +?> +--EXPECT--