/** * Writes the log to the stream itself * * @param string $message * @param int $type * @param int $time */ PHP_METHOD(Phalcon_Logger_Adapter_Syslog, logInternal){ zval *message, *type, *time, *formatter, *applied_format; zval *syslog_type, *syslog_message; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &message, &type, &time); PHALCON_INIT_VAR(formatter); phalcon_call_method(formatter, this_ptr, "getformatter"); PHALCON_INIT_VAR(applied_format); phalcon_call_method_p3(applied_format, formatter, "format", message, type, time); if (Z_TYPE_P(applied_format) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "The formatted message is not valid"); return; } PHALCON_OBS_VAR(syslog_type); phalcon_array_fetch_long(&syslog_type, applied_format, 0, PH_NOISY); PHALCON_OBS_VAR(syslog_message); phalcon_array_fetch_long(&syslog_message, applied_format, 1, PH_NOISY); phalcon_call_func_p2_noret("syslog", syslog_type, syslog_message); PHALCON_MM_RESTORE(); }
/** * Throws an internal exception * * @param string $message * @param int $exceptionCode */ PHP_METHOD(Phalcon_CLI_Dispatcher, _throwDispatchException) { zval *message, *exception_code = NULL, *exception, *events_manager; zval *event_name, *status; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &message, &exception_code); if (!exception_code) { PHALCON_INIT_VAR(exception_code); ZVAL_LONG(exception_code, 0); } PHALCON_INIT_VAR(exception); object_init_ex(exception, phalcon_cli_dispatcher_exception_ce); phalcon_call_method_p2_noret(exception, "__construct", message, exception_code); PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "dispatch:beforeException", 1); PHALCON_INIT_VAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, exception); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } phalcon_throw_exception(exception TSRMLS_CC); return; }
/** * Handles a user exception * * @param \Exception $exception */ PHP_METHOD(Phalcon_Mvc_Dispatcher, _handleException){ zval *exception, *events_manager, *event_name; zval *status; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &exception); PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "dispatch:beforeException", 1); PHALCON_INIT_VAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, exception); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } PHALCON_MM_RESTORE(); }
/** * Writes the log to the file itself * * @param string $message * @param int $type * @param int $time */ PHP_METHOD(Phalcon_Logger_Adapter_File, logInternal){ zval *message, *type, *time, *file_handler, *formatter; zval *applied_format; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &message, &type, &time); PHALCON_OBS_VAR(file_handler); phalcon_read_property_this(&file_handler, this_ptr, SL("_fileHandler"), PH_NOISY_CC); if (!zend_is_true(file_handler)) { PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "Cannot send message to the log because it is invalid"); return; } PHALCON_INIT_VAR(formatter); phalcon_call_method(formatter, this_ptr, "getformatter"); PHALCON_INIT_VAR(applied_format); phalcon_call_method_p3(applied_format, formatter, "format", message, type, time); phalcon_call_func_p2_noret("fwrite", file_handler, applied_format); PHALCON_MM_RESTORE(); }
/** * Maps a route to a handler * * @param string $routePattern * @param callable $handler * @return Phalcon\Mvc\Router\RouteInterface */ PHP_METHOD(Phalcon_Mvc_Micro_Collection, map){ zval *route_pattern, *handler, *method; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &route_pattern, &handler); PHALCON_INIT_VAR(method); phalcon_call_method_p3(return_value, this_ptr, "_addmap", method, route_pattern, handler); RETURN_MM(); }
/** * Maps a route to a handler that only matches if the HTTP method is HEAD * * @param string $routePattern * @param callable $handler * @return Phalcon\Mvc\Router\RouteInterface */ PHP_METHOD(Phalcon_Mvc_Micro_Collection, head){ zval *route_pattern, *handler, *method, *route; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &route_pattern, &handler); PHALCON_INIT_VAR(method); ZVAL_STRING(method, "HEAD", 1); PHALCON_INIT_VAR(route); phalcon_call_method_p3(route, this_ptr, "_addmap", method, route_pattern, handler); RETURN_CCTOR(route); }
/** * Validate a set of data according to a set of rules * * @param array|object $data * @param object $entity * @return Phalcon\Validation\Message\Group */ PHP_METHOD(Phalcon_Validation, validate){ zval *data = NULL, *entity = NULL, *validators, *messages = NULL, *status = NULL; zval *cancel_on_fail, *scope = NULL, *attribute = NULL, *validator = NULL; zval *must_cancel = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 2, &data, &entity); if (!data) { PHALCON_INIT_VAR(data); } if (!entity) { PHALCON_INIT_VAR(entity); } PHALCON_OBS_VAR(validators); phalcon_read_property_this(&validators, this_ptr, SL("_validators"), PH_NOISY_CC); if (Z_TYPE_P(validators) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "There are no validators to validate"); return; } /** * Clear pre-calculated values */ phalcon_update_property_null(this_ptr, SL("_values") TSRMLS_CC); /** * Implicitly creates a Phalcon\Validation\Message\Group object */ PHALCON_INIT_VAR(messages); object_init_ex(messages, phalcon_validation_message_group_ce); phalcon_call_method_noret(messages, "__construct"); /** * Validation classes can implement the 'beforeValidation' callback */ if (phalcon_method_exists_ex(this_ptr, SS("beforevalidation") TSRMLS_CC) == SUCCESS) { PHALCON_INIT_VAR(status); phalcon_call_method_p3(status, this_ptr, "beforevalidation", data, entity, messages); if (PHALCON_IS_FALSE(status)) { RETURN_CCTOR(status); } } phalcon_update_property_this(this_ptr, SL("_messages"), messages TSRMLS_CC); if (Z_TYPE_P(data) == IS_ARRAY) { phalcon_update_property_this(this_ptr, SL("_data"), data TSRMLS_CC); } else { if (Z_TYPE_P(data) == IS_OBJECT) { phalcon_update_property_this(this_ptr, SL("_data"), data TSRMLS_CC); } } PHALCON_INIT_VAR(cancel_on_fail); ZVAL_STRING(cancel_on_fail, "cancelOnFail", 1); phalcon_is_iterable(validators, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(scope); if (Z_TYPE_P(scope) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "The validator scope is not valid"); return; } PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, scope, 0, PH_NOISY_CC); PHALCON_OBS_NVAR(validator); phalcon_array_fetch_long(&validator, scope, 1, PH_NOISY_CC); if (Z_TYPE_P(validator) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "One of the validators is not valid"); return; } PHALCON_INIT_NVAR(status); phalcon_call_method_p2(status, validator, "validate", this_ptr, attribute); /** * Check if the validation must be canceled if this validator fails */ if (PHALCON_IS_FALSE(status)) { PHALCON_INIT_NVAR(must_cancel); phalcon_call_method_p1(must_cancel, validator, "getoption", cancel_on_fail); if (zend_is_true(must_cancel)) { break; } } zend_hash_move_forward_ex(ah0, &hp0); } /** * Get the messages generated by the validators */ PHALCON_OBS_NVAR(messages); phalcon_read_property_this(&messages, this_ptr, SL("_messages"), PH_NOISY_CC); if (phalcon_method_exists_ex(this_ptr, SS("aftervalidation") TSRMLS_CC) == SUCCESS) { phalcon_call_method_p3_noret(this_ptr, "aftervalidation", data, entity, messages); } RETURN_CCTOR(messages); }
/** * Resolves the service * * @param array $parameters * @param Phalcon\DiInterface $dependencyInjector * @return mixed */ PHP_METHOD(Phalcon_DI_Service, resolve){ zval *parameters = NULL, *dependency_injector = NULL, *shared; zval *shared_instance, *found = NULL, *instance = NULL, *definition; zval *builder, *name, *exception_message; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 2, ¶meters, &dependency_injector); if (!parameters) { PHALCON_INIT_VAR(parameters); } if (!dependency_injector) { PHALCON_INIT_VAR(dependency_injector); } PHALCON_OBS_VAR(shared); phalcon_read_property_this(&shared, this_ptr, SL("_shared"), PH_NOISY_CC); /** * Check if the service is shared */ if (zend_is_true(shared)) { PHALCON_OBS_VAR(shared_instance); phalcon_read_property_this(&shared_instance, this_ptr, SL("_sharedInstance"), PH_NOISY_CC); if (Z_TYPE_P(shared_instance) != IS_NULL) { RETURN_CCTOR(shared_instance); } } PHALCON_INIT_VAR(found); ZVAL_BOOL(found, 1); PHALCON_INIT_VAR(instance); PHALCON_OBS_VAR(definition); phalcon_read_property_this(&definition, this_ptr, SL("_definition"), PH_NOISY_CC); if (Z_TYPE_P(definition) == IS_STRING) { /** * String definitions can be class names without implicit parameters */ if (phalcon_class_exists(definition, 1 TSRMLS_CC)) { if (Z_TYPE_P(parameters) == IS_ARRAY) { if (phalcon_fast_count_ev(parameters TSRMLS_CC)) { if (phalcon_create_instance_params(instance, definition, parameters TSRMLS_CC) == FAILURE) { return; } } else { PHALCON_INIT_NVAR(instance); if (phalcon_create_instance(instance, definition TSRMLS_CC) == FAILURE) { return; } } } else { PHALCON_INIT_NVAR(instance); if (phalcon_create_instance(instance, definition TSRMLS_CC) == FAILURE) { return; } } } else { ZVAL_BOOL(found, 0); } } else { /** * Object definitions can be a Closure or an already resolved instance */ if (likely(Z_TYPE_P(definition) == IS_OBJECT)) { if (phalcon_is_instance_of(definition, SL("Closure") TSRMLS_CC)) { if (Z_TYPE_P(parameters) == IS_ARRAY) { PHALCON_INIT_NVAR(instance); PHALCON_CALL_USER_FUNC_ARRAY(instance, definition, parameters); } else { PHALCON_INIT_NVAR(instance); PHALCON_CALL_USER_FUNC(instance, definition); } } else { PHALCON_CPY_WRT(instance, definition); } } else { /** * Array definitions require a 'className' parameter */ if (Z_TYPE_P(definition) == IS_ARRAY) { PHALCON_INIT_VAR(builder); object_init_ex(builder, phalcon_di_service_builder_ce); PHALCON_INIT_NVAR(instance); phalcon_call_method_p3(instance, builder, "build", dependency_injector, definition, parameters); } else { PHALCON_INIT_NVAR(found); ZVAL_BOOL(found, 0); } } } /** * If the service can't be built, we must throw an exception */ if (PHALCON_IS_FALSE(found)) { PHALCON_OBS_VAR(name); phalcon_read_property_this(&name, this_ptr, SL("_name"), PH_NOISY_CC); PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Service '", name, "' cannot be resolved"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_di_exception_ce, exception_message); return; } /** * Update the shared instance if the service is shared */ if (zend_is_true(shared)) { phalcon_update_property_this(this_ptr, SL("_sharedInstance"), instance TSRMLS_CC); } RETURN_CCTOR(instance); }
/** * Tries to render the view with every engine registered in the component * * @param string $path * @param array $params */ PHP_METHOD(Phalcon_Mvc_View_Simple, _internalRender){ zval *path, *params, *events_manager, *event_name = NULL; zval *status = NULL, *not_exists = NULL, *must_clean, *views_dir; zval *views_dir_path, *engines, *engine = NULL, *extension = NULL; zval *view_engine_path = NULL, *exception_message; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &path, ¶ms); PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_this(this_ptr, SL("_activeRenderPath"), path TSRMLS_CC); } /** * Call beforeRender if there is an events manager */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "view:beforeRender", 1); PHALCON_INIT_VAR(status); phalcon_call_method_p2(status, events_manager, "fire", event_name, this_ptr); if (PHALCON_IS_FALSE(status)) { RETURN_MM_NULL(); } } PHALCON_INIT_VAR(not_exists); ZVAL_BOOL(not_exists, 1); PHALCON_INIT_VAR(must_clean); ZVAL_BOOL(must_clean, 1); PHALCON_OBS_VAR(views_dir); phalcon_read_property_this(&views_dir, this_ptr, SL("_viewsDir"), PH_NOISY_CC); PHALCON_INIT_VAR(views_dir_path); PHALCON_CONCAT_VV(views_dir_path, views_dir, path); /** * Load the template engines */ PHALCON_INIT_VAR(engines); phalcon_call_method(engines, this_ptr, "_loadtemplateengines"); /** * Views are rendered in each engine */ phalcon_is_iterable(engines, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(extension, ah0, hp0); PHALCON_GET_HVALUE(engine); PHALCON_INIT_NVAR(view_engine_path); PHALCON_CONCAT_VV(view_engine_path, views_dir_path, extension); if (phalcon_file_exists(view_engine_path TSRMLS_CC) == SUCCESS) { /** * Call beforeRenderView if there is a events manager available */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "view:beforeRenderView", 1); PHALCON_INIT_NVAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, view_engine_path); if (PHALCON_IS_FALSE(status)) { zend_hash_move_forward_ex(ah0, &hp0); continue; } } phalcon_call_method_p3_noret(engine, "render", view_engine_path, params, must_clean); /** * Call afterRenderView if there is a events manager available */ PHALCON_INIT_NVAR(not_exists); ZVAL_BOOL(not_exists, 0); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "view:afterRenderView", 1); phalcon_call_method_p2_noret(events_manager, "fire", event_name, this_ptr); } break; } zend_hash_move_forward_ex(ah0, &hp0); } /** * Always throw an exception if the view does not exist */ if (PHALCON_IS_TRUE(not_exists)) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "View '", views_dir_path, "' was not found in the views directory"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_view_exception_ce, exception_message); return; } /** * Call afterRender event */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "view:afterRender", 1); phalcon_call_method_p2_noret(events_manager, "fire", event_name, this_ptr); } PHALCON_MM_RESTORE(); }
/** * Sends SQL statements to the database server returning the success state. * Use this method only when the SQL statement sent to the server doesn't return any row * *<code> * //Inserting data * $success = $connection->execute("INSERT INTO robots VALUES (1, 'Astro Boy')"); * $success = $connection->execute("INSERT INTO robots VALUES (?, ?)", array(1, 'Astro Boy')); *</code> * * @param string $sqlStatement * @param array $bindParams * @param array $bindTypes * @return boolean */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, execute){ zval *sql_statement, *bind_params = NULL, *bind_types = NULL; zval *events_manager, *event_name = NULL, *status, *affected_rows = NULL; zval *pdo, *statement, *new_statement; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 2, &sql_statement, &bind_params, &bind_types); if (!bind_params) { PHALCON_INIT_VAR(bind_params); } if (!bind_types) { PHALCON_INIT_VAR(bind_types); } /** * Execute the beforeQuery event if a EventsManager is available */ PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "db:beforeQuery", 1); phalcon_update_property_this(this_ptr, SL("_sqlStatement"), sql_statement TSRMLS_CC); phalcon_update_property_this(this_ptr, SL("_sqlVariables"), bind_params TSRMLS_CC); phalcon_update_property_this(this_ptr, SL("_sqlBindTypes"), bind_types TSRMLS_CC); PHALCON_INIT_VAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, bind_params); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } /** * Initialize affected_rows to 0 */ PHALCON_INIT_VAR(affected_rows); ZVAL_LONG(affected_rows, 0); PHALCON_OBS_VAR(pdo); phalcon_read_property_this(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC); if (Z_TYPE_P(bind_params) == IS_ARRAY) { PHALCON_INIT_VAR(statement); phalcon_call_method_p1(statement, pdo, "prepare", sql_statement); if (Z_TYPE_P(statement) == IS_OBJECT) { PHALCON_INIT_VAR(new_statement); phalcon_call_method_p3(new_statement, this_ptr, "executeprepared", statement, bind_params, bind_types); phalcon_call_method(affected_rows, new_statement, "rowcount"); } } else { PHALCON_INIT_NVAR(affected_rows); phalcon_call_method_p1(affected_rows, pdo, "exec", sql_statement); } /** * Execute the afterQuery event if a EventsManager is available */ if (Z_TYPE_P(affected_rows) == IS_LONG) { phalcon_update_property_this(this_ptr, SL("_affectedRows"), affected_rows TSRMLS_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "db:afterQuery", 1); phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, bind_params); } } RETURN_MM_TRUE; }
/** * Sends SQL statements to the database server returning the success state. * Use this method only when the SQL statement sent to the server is returning rows * *<code> * //Querying data * $resultset = $connection->query("SELECT * FROM robots WHERE type='mechanical'"); * $resultset = $connection->query("SELECT * FROM robots WHERE type=?", array("mechanical")); *</code> * * @param string $sqlStatement * @param array $bindParams * @param array $bindTypes * @return Phalcon\Db\ResultInterface */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, query){ zval *sql_statement, *bind_params = NULL, *bind_types = NULL; zval *events_manager, *event_name = NULL, *status, *pdo; zval *statement = NULL, *new_statement; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 2, &sql_statement, &bind_params, &bind_types); if (!bind_params) { PHALCON_INIT_VAR(bind_params); } if (!bind_types) { PHALCON_INIT_VAR(bind_types); } PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); /** * Execute the beforeQuery event if a EventsManager is available */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "db:beforeQuery", 1); phalcon_update_property_this(this_ptr, SL("_sqlStatement"), sql_statement TSRMLS_CC); phalcon_update_property_this(this_ptr, SL("_sqlVariables"), bind_params TSRMLS_CC); phalcon_update_property_this(this_ptr, SL("_sqlBindTypes"), bind_types TSRMLS_CC); PHALCON_INIT_VAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, bind_params); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } PHALCON_OBS_VAR(pdo); phalcon_read_property_this(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC); if (Z_TYPE_P(bind_params) == IS_ARRAY) { PHALCON_INIT_VAR(statement); phalcon_call_method_p1(statement, pdo, "prepare", sql_statement); if (Z_TYPE_P(statement) == IS_OBJECT) { PHALCON_INIT_VAR(new_statement); phalcon_call_method_p3(new_statement, this_ptr, "executeprepared", statement, bind_params, bind_types); PHALCON_CPY_WRT(statement, new_statement); } } else { PHALCON_INIT_NVAR(statement); phalcon_call_method_p1(statement, pdo, "query", sql_statement); } /** * Execute the afterQuery event if a EventsManager is available */ if (likely(Z_TYPE_P(statement) == IS_OBJECT)) { if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "db:afterQuery", 1); phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, bind_params); } object_init_ex(return_value, phalcon_db_result_pdo_ce); phalcon_call_method_p5_noret(return_value, "__construct", this_ptr, statement, sql_statement, bind_params, bind_types); RETURN_MM(); } RETURN_CCTOR(statement); }
/** * Throws an internal exception * * @param string $message * @param int $exceptionCode */ PHP_METHOD(Phalcon_Mvc_Dispatcher, _throwDispatchException){ zval *message, *exception_code = NULL, *dependency_injector; zval *exception_message, *exception = NULL, *service; zval *response, *status_code, *status_message; zval *events_manager, *event_name, *status; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &message, &exception_code); if (!exception_code) { PHALCON_INIT_VAR(exception_code); ZVAL_LONG(exception_code, 0); } else { PHALCON_SEPARATE_PARAM(exception_code); } PHALCON_OBS_VAR(dependency_injector); phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_INIT_NVAR(exception_code); ZVAL_LONG(exception_code, 0); PHALCON_INIT_VAR(exception_message); ZVAL_STRING(exception_message, "A dependency injection container is required to access the 'response' service", 1); PHALCON_INIT_VAR(exception); object_init_ex(exception, phalcon_mvc_dispatcher_exception_ce); phalcon_call_method_p2_noret(exception, "__construct", exception_message, exception_code); phalcon_throw_exception(exception TSRMLS_CC); return; } PHALCON_INIT_VAR(service); ZVAL_STRING(service, "response", 1); PHALCON_INIT_VAR(response); phalcon_call_method_p1(response, dependency_injector, "getshared", service); /** * Dispatcher exceptions automatically sends 404 status */ PHALCON_INIT_VAR(status_code); ZVAL_LONG(status_code, 404); PHALCON_INIT_VAR(status_message); ZVAL_STRING(status_message, "Not Found", 1); phalcon_call_method_p2_noret(response, "setstatuscode", status_code, status_message); /** * Create the real exception */ PHALCON_INIT_NVAR(exception); object_init_ex(exception, phalcon_mvc_dispatcher_exception_ce); phalcon_call_method_p2_noret(exception, "__construct", message, exception_code); PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "dispatch:beforeException", 1); PHALCON_INIT_VAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, exception); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } /** * Throw the exception if it wasn't handled */ phalcon_throw_exception(exception TSRMLS_CC); return; }
/** * Handle the command-line arguments. * * * <code> * $arguments = array( * 'task' => 'taskname', * 'action' => 'action', * 'params' => array('parameter1', 'parameter2') * ); * $console->handle($arguments); * </code> * * @param array $arguments * @return mixed */ PHP_METHOD(Phalcon_CLI_Console, handle){ zval *arguments = NULL, *dependency_injector, *events_manager; zval *service = NULL, *router, *module_name, *event_name = NULL; zval *status = NULL, *modules, *exception_msg = NULL, *module; zval *path, *class_name = NULL, *module_object, *task_name; zval *action_name, *params, *dispatcher, *task; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &arguments); if (!arguments) { PHALCON_INIT_VAR(arguments); array_init(arguments); } PHALCON_OBS_VAR(dependency_injector); phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_cli_console_exception_ce, "A dependency injection object is required to access internal services"); return; } PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); PHALCON_INIT_VAR(service); ZVAL_STRING(service, "router", 1); PHALCON_INIT_VAR(router); phalcon_call_method_p1(router, dependency_injector, "getshared", service); phalcon_call_method_p1_noret(router, "handle", arguments); PHALCON_INIT_VAR(module_name); phalcon_call_method(module_name, router, "getmodulename"); if (zend_is_true(module_name)) { if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_VAR(event_name); ZVAL_STRING(event_name, "console:beforeStartModule", 1); PHALCON_INIT_VAR(status); phalcon_call_method_p3(status, events_manager, "fire", event_name, this_ptr, module_name); if (PHALCON_IS_FALSE(status)) { RETURN_MM_FALSE; } } PHALCON_OBS_VAR(modules); phalcon_read_property_this(&modules, this_ptr, SL("_modules"), PH_NOISY_CC); if (!phalcon_array_isset(modules, module_name)) { PHALCON_INIT_VAR(exception_msg); PHALCON_CONCAT_SVS(exception_msg, "Module '", module_name, "' isn't registered in the console container"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_cli_console_exception_ce, exception_msg); return; } PHALCON_OBS_VAR(module); phalcon_array_fetch(&module, modules, module_name, PH_NOISY); if (Z_TYPE_P(module) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_cli_console_exception_ce, "Invalid module definition path"); return; } if (phalcon_array_isset_string(module, SS("path"))) { PHALCON_OBS_VAR(path); phalcon_array_fetch_string(&path, module, SL("path"), PH_NOISY); if (phalcon_file_exists(path TSRMLS_CC) == SUCCESS) { if (phalcon_require(path TSRMLS_CC) == FAILURE) { return; } } else { PHALCON_INIT_NVAR(exception_msg); PHALCON_CONCAT_SVS(exception_msg, "Module definition path '", path, "\" doesn't exist"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_cli_console_exception_ce, exception_msg); return; } } if (phalcon_array_isset_string(module, SS("className"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_string(&class_name, module, SL("className"), PH_NOISY); } else { PHALCON_INIT_NVAR(class_name); ZVAL_STRING(class_name, "Module", 1); } PHALCON_INIT_VAR(module_object); phalcon_call_method_p1(module_object, dependency_injector, "get", class_name); phalcon_call_method_noret(module_object, "registerautoloaders"); phalcon_call_method_p1_noret(module_object, "registerservices", dependency_injector); if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_this(this_ptr, SL("_moduleObject"), module_object TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "console: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)) { RETURN_MM_FALSE; } } } PHALCON_INIT_VAR(task_name); phalcon_call_method(task_name, router, "gettaskname"); PHALCON_INIT_VAR(action_name); phalcon_call_method(action_name, router, "getactionname"); PHALCON_INIT_VAR(params); phalcon_call_method(params, router, "getparams"); PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "dispatcher", 1); PHALCON_INIT_VAR(dispatcher); phalcon_call_method_p1(dispatcher, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(dispatcher, phalcon_dispatcherinterface_ce); phalcon_call_method_p1_noret(dispatcher, "settaskname", task_name); phalcon_call_method_p1_noret(dispatcher, "setactionname", action_name); phalcon_call_method_p1_noret(dispatcher, "setparams", params); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "console:beforeHandleTask", 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; } } PHALCON_INIT_VAR(task); phalcon_call_method(task, dispatcher, "dispatch"); if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "console:afterHandleTask", 1); phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, task); } RETURN_CCTOR(task); }
/** * Handles a MVC request * * @param string $uri * @return Phalcon\Http\ResponseInterface */ 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 *module_object = NULL, *modules, *exception_msg = NULL; zval *module, *class_name = NULL, *path, *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; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &uri); if (!uri) { PHALCON_INIT_VAR(uri); } PHALCON_OBS_VAR(dependency_injector); phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "A dependency injection object is required to access internal services"); return; } PHALCON_OBS_VAR(events_manager); phalcon_read_property_this(&events_manager, 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; } } PHALCON_INIT_VAR(service); ZVAL_STRING(service, "router", 1); PHALCON_INIT_VAR(router); phalcon_call_method_p1(router, dependency_injector, "getshared", service); /** * Handle the URI pattern (if any) */ phalcon_call_method_p1_noret(router, "handle", uri); /** * Load module config */ PHALCON_INIT_VAR(module_name); phalcon_call_method(module_name, router, "getmodulename"); /** * If the router doesn't return a valid module we use the default module */ if (!zend_is_true(module_name)) { PHALCON_OBS_NVAR(module_name); phalcon_read_property_this(&module_name, this_ptr, SL("_defaultModule"), PH_NOISY_CC); } PHALCON_INIT_VAR(module_object); /** * 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; } } /** * Check if the module passed by the router is registered in the modules container */ PHALCON_OBS_VAR(modules); phalcon_read_property_this(&modules, this_ptr, SL("_modules"), PH_NOISY_CC); if (!phalcon_array_isset(modules, module_name)) { PHALCON_INIT_VAR(exception_msg); PHALCON_CONCAT_SVS(exception_msg, "Module '", module_name, "' isn't registered in the application container"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_application_exception_ce, exception_msg); return; } /** * A module definition must ne an array or an object */ PHALCON_OBS_VAR(module); phalcon_array_fetch(&module, modules, module_name, PH_NOISY); if (Z_TYPE_P(module) != IS_ARRAY) { if (Z_TYPE_P(module) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "Invalid module definition"); return; } } /** * An array module definition contains a path to a module definition class */ if (Z_TYPE_P(module) == IS_ARRAY) { /** * Class name used to load the module definition */ if (phalcon_array_isset_string(module, SS("className"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_string(&class_name, module, SL("className"), PH_NOISY); } else { PHALCON_INIT_NVAR(class_name); ZVAL_STRING(class_name, "Module", 1); } /** * If developer specify a path try to include the file */ if (phalcon_array_isset_string(module, SS("path"))) { PHALCON_OBS_VAR(path); phalcon_array_fetch_string(&path, module, SL("path"), PH_NOISY); if (!phalcon_class_exists(class_name, 0 TSRMLS_CC)) { if (phalcon_file_exists(path TSRMLS_CC) == SUCCESS) { if (phalcon_require(path TSRMLS_CC) == FAILURE) { return; } } else { PHALCON_INIT_NVAR(exception_msg); PHALCON_CONCAT_SVS(exception_msg, "Module definition path '", path, "' doesn't exist"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_application_exception_ce, exception_msg); return; } } } phalcon_call_method_p1(module_object, dependency_injector, "get", class_name); /** * 'registerAutoloaders' and 'registerServices' are automatically called */ phalcon_call_method_p1_noret(module_object, "registerautoloaders", dependency_injector); phalcon_call_method_p1_noret(module_object, "registerservices", dependency_injector); } else { /** * A module definition object, can be a Closure instance */ if (phalcon_is_instance_of(module, SL("Closure") TSRMLS_CC)) { PHALCON_INIT_VAR(module_params); array_init_size(module_params, 1); phalcon_array_append(&module_params, dependency_injector, PH_SEPARATE); PHALCON_INIT_NVAR(status); PHALCON_CALL_USER_FUNC_ARRAY(status, module, module_params); } else { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_application_exception_ce, "Invalid module definition"); return; } } /** * Calling afterStartModule event */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { 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)) { RETURN_MM_FALSE; } } } /** * Check whether use implicit views or not */ PHALCON_OBS_VAR(implicit_view); phalcon_read_property_this(&implicit_view, this_ptr, SL("_implicitView"), PH_NOISY_CC); if (PHALCON_IS_TRUE(implicit_view)) { PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "view", 1); PHALCON_INIT_VAR(view); phalcon_call_method_p1(view, dependency_injector, "getshared", service); } /** * We get the parameters from the router and assign them to the dispatcher */ PHALCON_INIT_NVAR(module_name); phalcon_call_method(module_name, router, "getmodulename"); PHALCON_INIT_VAR(namespace_name); phalcon_call_method(namespace_name, router, "getnamespacename"); PHALCON_INIT_VAR(controller_name); phalcon_call_method(controller_name, router, "getcontrollername"); PHALCON_INIT_VAR(action_name); phalcon_call_method(action_name, router, "getactionname"); PHALCON_INIT_VAR(params); phalcon_call_method(params, router, "getparams"); PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "dispatcher", 1); PHALCON_INIT_VAR(dispatcher); phalcon_call_method_p1(dispatcher, dependency_injector, "getshared", service); /** * Assign the values passed from the router */ phalcon_call_method_p1_noret(dispatcher, "setmodulename", module_name); phalcon_call_method_p1_noret(dispatcher, "setnamespacename", namespace_name); phalcon_call_method_p1_noret(dispatcher, "setcontrollername", controller_name); phalcon_call_method_p1_noret(dispatcher, "setactionname", action_name); phalcon_call_method_p1_noret(dispatcher, "setparams", params); /** * Start the view component (start output buffering) */ if (PHALCON_IS_TRUE(implicit_view)) { 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; } } /** * The dispatcher must return an object */ PHALCON_INIT_VAR(controller); phalcon_call_method(controller, dispatcher, "dispatch"); PHALCON_INIT_VAR(returned_response); ZVAL_BOOL(returned_response, 0); /** * Get the latest value returned by an action */ PHALCON_INIT_VAR(possible_response); phalcon_call_method(possible_response, dispatcher, "getreturnedvalue"); if (Z_TYPE_P(possible_response) == IS_OBJECT) { /** * Check if the returned object is already a response */ phalcon_instance_of(returned_response, possible_response, phalcon_http_responseinterface_ce TSRMLS_CC); } /** * 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); } /** * If the dispatcher returns an object we try to render the view in auto-rendering * mode */ if (PHALCON_IS_FALSE(returned_response)) { if (PHALCON_IS_TRUE(implicit_view)) { if (Z_TYPE_P(controller) == IS_OBJECT) { PHALCON_INIT_VAR(render_status); ZVAL_BOOL(render_status, 1); /** * 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); } /** * Check if the view process has been treated by the developer */ if (PHALCON_IS_NOT_FALSE(render_status)) { PHALCON_INIT_NVAR(controller_name); phalcon_call_method(controller_name, dispatcher, "getcontrollername"); PHALCON_INIT_NVAR(action_name); phalcon_call_method(action_name, dispatcher, "getactionname"); PHALCON_INIT_NVAR(params); phalcon_call_method(params, dispatcher, "getparams"); /** * Automatic render based on the latest controller executed */ phalcon_call_method_p3_noret(view, "render", controller_name, action_name, params); } } } } /** * Finish the view component (stop output buffering) */ if (PHALCON_IS_TRUE(implicit_view)) { phalcon_call_method_noret(view, "finish"); } if (PHALCON_IS_FALSE(returned_response)) { PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "response", 1); PHALCON_INIT_VAR(response); phalcon_call_method_p1(response, dependency_injector, "getshared", service); if (PHALCON_IS_TRUE(implicit_view)) { /** * The content returned by the view is passed to the response service */ PHALCON_INIT_VAR(content); phalcon_call_method(content, view, "getcontent"); phalcon_call_method_p1_noret(response, "setcontent", content); } } else { /** * We don't need to create a response because there is a one already created */ 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); } /** * Headers are automatically send */ phalcon_call_method_noret(response, "sendheaders"); /** * Cookies are automatically send */ phalcon_call_method_noret(response, "sendcookies"); /** * Return the response */ RETURN_CCTOR(response); }
/** * Writes the log to the stream itself * * @param string $message * @param int $type * @param int $time * @see http://www.firephp.org/Wiki/Reference/Protocol */ PHP_METHOD(Phalcon_Logger_Adapter_Firephp, logInternal){ zval *message, *type, *time, *formatter, *applied_format; zval *initialized, *index; sapi_header_line h = { NULL, 0, 0 }; smart_str str = { NULL, 0, 0 }; int size, offset, num_bytes; const int chunk = 4960; /* If headers has already been sent, we can do nothing. Exit early. */ if (SG(headers_sent)) { RETURN_FALSE; } PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &message, &type, &time); PHALCON_INIT_VAR(formatter); phalcon_call_method(formatter, this_ptr, "getformatter"); PHALCON_OBS_VAR(initialized); phalcon_read_static_property(&initialized, SL("phalcon\\logger\\adapter\\firephp"), SL("_initialized") TSRMLS_CC); if (!zend_is_true(initialized)) { /** * Send the required initialization headers. * Use Zend API here so that the user can see the progress and because * if we delegate this to Phalcon and there will be a fatal errors, * chances are that the headers will never ne sent. */ h.line = "X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2"; h.line_len = sizeof("X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2")-1; sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC); h.line = "X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3"; h.line_len = sizeof("X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3")-1; sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC); h.line = "X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1"; h.line_len = sizeof("X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1")-1; sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC); ZVAL_TRUE(initialized); /* This will also update the property because "initialized" was not separated */ } PHALCON_INIT_VAR(applied_format); phalcon_call_method_p3(applied_format, formatter, "format", message, type, time); if (Z_TYPE_P(applied_format) != IS_STRING) { PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "The formatted message is not valid"); return; } PHALCON_OBS_VAR(index); phalcon_read_static_property(&index, SL("phalcon\\logger\\adapter\\firephp"), SL("_index") TSRMLS_CC); size = Z_STRLEN_P(applied_format); offset = 0; /** * We need to send the data in chunks not exceeding 5,000 bytes. * Allocate the smart string once to avoid performance penalties. */ smart_str_alloc4(&str, (size > chunk ? chunk : size), 0, num_bytes); while (size > 0) { smart_str_appends(&str, "X-Wf-1-1-1-"); smart_str_append_long(&str, Z_LVAL_P(index)); smart_str_appends(&str, ": "); num_bytes = size > chunk ? chunk : size; if (offset) { /* This is not the first chunk, prepend the payload with "|" */ smart_str_appendc(&str, '|'); } /* Grab the chunk from the encoded string */ smart_str_appendl(&str, Z_STRVAL_P(applied_format) + offset, num_bytes); size -= num_bytes; offset += num_bytes; if (size) { /* If we have more data to send, append "|/" */ smart_str_appendl(&str, "|\\", 2); } smart_str_0(&str); /* Not strictly necessary but just to be safe */ /* Send the result */ h.line = str.c; h.line_len = str.len; sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC); /* Update header index; this will update Phalcon\Logger\Adapter\Firephp as well */ ZVAL_LONG(index, Z_LVAL_P(index)+1); /** * Do not free and then reallocate memory. Just pretend the string * is empty. We will take care of deallocation later. */ str.len = 0; } /* Deallocate the smnart string if it is not empty */ if (str.c) { smart_str_free(&str); } PHALCON_MM_RESTORE(); }