/** * Returns the messages in the session flasher * * @param string $type * @param boolean $remove * @return array */ PHP_METHOD(Phalcon_Flash_Session, getMessages){ zval *type = NULL, *remove = NULL, *messages, *return_messages; zval *do_remove; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 2, &type, &remove); if (!type) { PHALCON_INIT_VAR(type); } if (!remove) { PHALCON_INIT_VAR(remove); ZVAL_BOOL(remove, 1); } if (Z_TYPE_P(type) != IS_NULL) { PHALCON_INIT_VAR(do_remove); ZVAL_FALSE(do_remove); } else { do_remove = remove; } PHALCON_INIT_VAR(messages); phalcon_call_method_p1(messages, this_ptr, "_getsessionmessages", do_remove); if (Z_TYPE_P(messages) == IS_ARRAY) { if (likely(Z_TYPE_P(type) != IS_NULL)) { if (phalcon_array_isset(messages, type)) { PHALCON_OBS_VAR(return_messages); phalcon_array_fetch(&return_messages, messages, type, PH_NOISY); RETVAL_ZVAL(return_messages, 1, 0); if (zend_is_true(remove)) { phalcon_array_unset(&messages, type, 0); phalcon_call_method_p1_noret(this_ptr, "_setsessionmessages", messages); } PHALCON_MM_RESTORE(); return; } RETURN_MM_EMPTY_ARRAY(); } RETURN_CCTOR(messages); } RETURN_MM_EMPTY_ARRAY(); }
/** * Adds a role to the ACL list. Second parameter allows inheriting access data from other existing role * * Example: * <code> * $acl->addRole(new Phalcon\Acl\Role('administrator'), 'consultant'); * $acl->addRole('administrator', 'consultant'); * </code> * * @param Phalcon\Acl\RoleInterface $role * @param array|string $accessInherits * @return boolean */ PHP_METHOD(Phalcon_Acl_Adapter_Memory, addRole){ zval *role, *access_inherits = NULL, *role_name = NULL, *object = NULL; zval *roles_names, *exists, *default_access; zval *key; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &role, &access_inherits); if (!access_inherits) { PHALCON_INIT_VAR(access_inherits); } if (Z_TYPE_P(role) == IS_OBJECT) { PHALCON_INIT_VAR(role_name); phalcon_call_method(role_name, role, "getname"); PHALCON_CPY_WRT(object, role); } else { PHALCON_CPY_WRT(role_name, role); PHALCON_INIT_NVAR(object); object_init_ex(object, phalcon_acl_role_ce); phalcon_call_method_p1_noret(object, "__construct", role); } PHALCON_OBS_VAR(roles_names); phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC); if (phalcon_array_isset(roles_names, role_name)) { RETURN_MM_FALSE; } PHALCON_INIT_VAR(exists); ZVAL_BOOL(exists, 1); phalcon_update_property_array_append(this_ptr, SL("_roles"), object TSRMLS_CC); phalcon_update_property_array(this_ptr, SL("_rolesNames"), role_name, exists TSRMLS_CC); PHALCON_OBS_VAR(default_access); phalcon_read_property_this(&default_access, this_ptr, SL("_defaultAccess"), PH_NOISY_CC); PHALCON_INIT_VAR(key); PHALCON_CONCAT_VS(key, role_name, "!*!*"); phalcon_update_property_array(this_ptr, SL("_access"), key, default_access TSRMLS_CC); if (Z_TYPE_P(access_inherits) != IS_NULL) { phalcon_call_method_p2(return_value, this_ptr, "addinherit", role_name, access_inherits); RETURN_MM(); } RETURN_MM_TRUE; }
/** * Stops the active profile * * @return Phalcon\Db\Profiler */ PHP_METHOD(Phalcon_Db_Profiler, stopProfile){ zval *micro, *final_time, *active_profile, *initial_time; zval *diference, *total_seconds, *new_total_seconds; PHALCON_MM_GROW(); PHALCON_INIT_VAR(micro); ZVAL_BOOL(micro, 1); PHALCON_INIT_VAR(final_time); phalcon_call_func_p1(final_time, "microtime", micro); PHALCON_OBS_VAR(active_profile); phalcon_read_property_this(&active_profile, this_ptr, SL("_activeProfile"), PH_NOISY_CC); phalcon_call_method_p1_noret(active_profile, "setfinaltime", final_time); PHALCON_INIT_VAR(initial_time); phalcon_call_method(initial_time, active_profile, "getinitialtime"); PHALCON_INIT_VAR(diference); sub_function(diference, final_time, initial_time TSRMLS_CC); PHALCON_OBS_VAR(total_seconds); phalcon_read_property_this(&total_seconds, this_ptr, SL("_totalSeconds"), PH_NOISY_CC); PHALCON_INIT_VAR(new_total_seconds); phalcon_add_function(new_total_seconds, total_seconds, diference TSRMLS_CC); phalcon_update_property_this(this_ptr, SL("_totalSeconds"), new_total_seconds TSRMLS_CC); phalcon_update_property_array_append(this_ptr, SL("_allProfiles"), active_profile TSRMLS_CC); if (phalcon_method_exists_ex(this_ptr, SS("afterendprofile") TSRMLS_CC) == SUCCESS) { phalcon_call_method_p1_noret(this_ptr, "afterendprofile", active_profile); } RETURN_THIS(); }
/** * Return the default identity value to insert in an identity column * * @return Phalcon\Db\RawValue */ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, getDefaultIdValue){ zval *null_value, *default_value; PHALCON_MM_GROW(); PHALCON_INIT_VAR(null_value); ZVAL_STRING(null_value, "default", 1); PHALCON_INIT_VAR(default_value); object_init_ex(default_value, phalcon_db_rawvalue_ce); phalcon_call_method_p1_noret(default_value, "__construct", null_value); RETURN_CTOR(default_value); }
/** * Rollbacks active transactions within the manager * Collect will remove transaction from the manager * * @param boolean $collect */ PHP_METHOD(Phalcon_Mvc_Model_Transaction_Manager, rollback){ zval *collect = NULL, *transactions, *transaction = NULL, *connection = NULL; zval *is_under_transaction = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &collect); if (!collect) { PHALCON_INIT_VAR(collect); ZVAL_BOOL(collect, 1); } PHALCON_OBS_VAR(transactions); phalcon_read_property_this(&transactions, this_ptr, SL("_transactions"), PH_NOISY_CC); if (Z_TYPE_P(transactions) == IS_ARRAY) { phalcon_is_iterable(transactions, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(transaction); PHALCON_INIT_NVAR(connection); phalcon_call_method(connection, transaction, "getconnection"); PHALCON_INIT_NVAR(is_under_transaction); phalcon_call_method(is_under_transaction, connection, "isundertransaction"); if (zend_is_true(is_under_transaction)) { phalcon_call_method_noret(connection, "rollback"); phalcon_call_method_noret(connection, "close"); } if (zend_is_true(collect)) { phalcon_call_method_p1_noret(this_ptr, "_collecttransaction", transaction); } zend_hash_move_forward_ex(ah0, &hp0); } } PHALCON_MM_RESTORE(); }
/** * Gets row in a specific position of the resultset * * @param int $index * @return Phalcon\Mvc\ModelInterface */ PHP_METHOD(Phalcon_Mvc_Model_Resultset, offsetGet){ zval *index, *count, *exists, *pointer, *current = NULL, *valid; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &index); PHALCON_INIT_VAR(count); phalcon_call_method(count, this_ptr, "count"); PHALCON_INIT_VAR(exists); is_smaller_function(exists, index, count TSRMLS_CC); if (PHALCON_IS_TRUE(exists)) { /** * Check if the last record returned is the current requested */ PHALCON_OBS_VAR(pointer); phalcon_read_property_this(&pointer, this_ptr, SL("_pointer"), PH_NOISY_CC); if (PHALCON_IS_EQUAL(pointer, index)) { PHALCON_INIT_VAR(current); phalcon_call_method(current, this_ptr, "current"); RETURN_CCTOR(current); } /** * Move the cursor to the specific position */ phalcon_call_method_p1_noret(this_ptr, "seek", index); /** * Check if the last record returned is the requested */ PHALCON_INIT_VAR(valid); phalcon_call_method(valid, this_ptr, "valid"); if (PHALCON_IS_NOT_FALSE(valid)) { PHALCON_INIT_NVAR(current); phalcon_call_method(current, this_ptr, "current"); RETURN_CCTOR(current); } RETURN_MM_FALSE; } PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The index does not exist in the cursor"); return; }
/** * Constructor for Phalcon\Db\Adapter\Pdo * * @param array $descriptor */ PHP_METHOD(Phalcon_Db_Adapter_Pdo, __construct){ zval *descriptor; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &descriptor); if (Z_TYPE_P(descriptor) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The descriptor must be an array"); return; } phalcon_call_method_p1_noret(this_ptr, "connect", descriptor); PHALCON_CALL_PARENT_PARAMS_1_NORETURN(this_ptr, "Phalcon\\Db\\Adapter\\Pdo", "__construct", descriptor); PHALCON_MM_RESTORE(); }
/** * Phalcon\Mvc\Model\Resultset\Complex constructor * * @param array $columnsTypes * @param Phalcon\Db\ResultInterface $result * @param Phalcon\Cache\BackendInterface $cache */ PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, __construct){ zval *columns_types, *result, *cache = NULL, *fetch_assoc; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 1, &columns_types, &result, &cache); if (!cache) { PHALCON_INIT_VAR(cache); } /** * Column types, tell the resultset how to build the result */ phalcon_update_property_this(this_ptr, SL("_columnTypes"), columns_types TSRMLS_CC); /** * Valid resultsets are Phalcon\Db\ResultInterface instances */ phalcon_update_property_this(this_ptr, SL("_result"), result TSRMLS_CC); /** * Update the related cache if any */ if (Z_TYPE_P(cache) != IS_NULL) { phalcon_update_property_this(this_ptr, SL("_cache"), cache TSRMLS_CC); } /** * Resultsets type 1 are traversed one-by-one */ phalcon_update_property_long(this_ptr, SL("_type"), 1 TSRMLS_CC); /** * If the database result is an object, change it to fetch assoc */ if (Z_TYPE_P(result) == IS_OBJECT) { PHALCON_INIT_VAR(fetch_assoc); ZVAL_LONG(fetch_assoc, 1); phalcon_call_method_p1_noret(result, "setfetchmode", fetch_assoc); } PHALCON_MM_RESTORE(); }
/** * Phalcon\Mvc\Micro constructor * * @param Phalcon\DiInterface $dependencyInjector */ PHP_METHOD(Phalcon_Mvc_Micro, __construct){ zval *dependency_injector = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &dependency_injector); if (!dependency_injector) { PHALCON_INIT_VAR(dependency_injector); } if (Z_TYPE_P(dependency_injector) == IS_OBJECT) { phalcon_call_method_p1_noret(this_ptr, "setdi", dependency_injector); } PHALCON_MM_RESTORE(); }
/** * Destroyes the session bag * *<code> * $user->destroy(); *</code> */ PHP_METHOD(Phalcon_Session_Bag, destroy){ zval *initalized, *name, *session; PHALCON_MM_GROW(); PHALCON_OBS_VAR(initalized); phalcon_read_property_this(&initalized, this_ptr, SL("_initalized"), PH_NOISY_CC); if (PHALCON_IS_FALSE(initalized)) { phalcon_call_method_noret(this_ptr, "initialize"); } PHALCON_OBS_VAR(name); phalcon_read_property_this(&name, this_ptr, SL("_name"), PH_NOISY_CC); PHALCON_OBS_VAR(session); phalcon_read_property_this(&session, this_ptr, SL("_session"), PH_NOISY_CC); phalcon_call_method_p1_noret(session, "remove", name); PHALCON_MM_RESTORE(); }
/** * Adds a resource to the ACL list * * Access names can be a particular action, by example * search, update, delete, etc or a list of them * * Example: * <code> * //Add a resource to the the list allowing access to an action * $acl->addResource(new Phalcon\Acl\Resource('customers'), 'search'); * $acl->addResource('customers', 'search'); * * //Add a resource with an access list * $acl->addResource(new Phalcon\Acl\Resource('customers'), array('create', 'search')); * $acl->addResource('customers', array('create', 'search')); * </code> * * @param Phalcon\Acl\Resource $resource * @param array $accessList * @return boolean */ PHP_METHOD(Phalcon_Acl_Adapter_Memory, addResource){ zval *resource, *access_list = NULL, *resource_name = NULL; zval *object = NULL, *resources_names, *exists, *status; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &resource, &access_list); if (!access_list) { PHALCON_INIT_VAR(access_list); } if (Z_TYPE_P(resource) == IS_OBJECT) { PHALCON_INIT_VAR(resource_name); phalcon_call_method(resource_name, resource, "getname"); PHALCON_CPY_WRT(object, resource); } else { PHALCON_CPY_WRT(resource_name, resource); PHALCON_INIT_NVAR(object); object_init_ex(object, phalcon_acl_resource_ce); phalcon_call_method_p1_noret(object, "__construct", resource_name); } PHALCON_OBS_VAR(resources_names); phalcon_read_property_this(&resources_names, this_ptr, SL("_resourcesNames"), PH_NOISY_CC); if (!phalcon_array_isset(resources_names, resource_name)) { PHALCON_INIT_VAR(exists); ZVAL_BOOL(exists, 1); phalcon_update_property_array_append(this_ptr, SL("_resources"), object TSRMLS_CC); phalcon_update_property_array(this_ptr, SL("_resourcesNames"), resource_name, exists TSRMLS_CC); } PHALCON_INIT_VAR(status); phalcon_call_method_p2(status, this_ptr, "addresourceaccess", resource_name, access_list); RETURN_CCTOR(status); }
/** * This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. * Call it when you need to restore a database connection. * * Support set search_path after connectted if schema is specified in config. * * @param array $descriptor * @return boolean */ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Postgresql, connect){ zval *descriptor = NULL, *schema = NULL, *sql; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &descriptor); if (!descriptor) { PHALCON_INIT_VAR(descriptor); } else { PHALCON_SEPARATE_PARAM(descriptor); } if (!zend_is_true(descriptor)) { PHALCON_OBS_NVAR(descriptor); phalcon_read_property_this(&descriptor, this_ptr, SL("_descriptor"), PH_NOISY_CC); } PHALCON_INIT_VAR(schema); if (phalcon_array_isset_string(descriptor, SS("schema"))) { PHALCON_OBS_NVAR(schema); phalcon_array_fetch_string(&schema, descriptor, SL("schema"), PH_NOISY); phalcon_array_unset_string(&descriptor, SS("schema"), PH_SEPARATE); } PHALCON_CALL_PARENT_PARAMS_1_NORETURN(this_ptr, "Phalcon\\Db\\Adapter\\Pdo\\Postgresql", "connect", descriptor); /** * Execute the search path in the after connect */ if (Z_TYPE_P(schema) == IS_STRING) { PHALCON_INIT_VAR(sql); PHALCON_CONCAT_SVS(sql, "SET search_path TO '", schema, "'"); phalcon_call_method_p1_noret(this_ptr, "execute", sql); } PHALCON_MM_RESTORE(); }
/** * Appends a message to the internal message list * * @param Phalcon\Validation\Message $message * @return Phalcon\Forms\ElementInterface */ PHP_METHOD(Phalcon_Forms_Element, appendMessage){ zval *message, *messages = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &message); PHALCON_OBS_VAR(messages); phalcon_read_property_this(&messages, this_ptr, SL("_messages"), PH_NOISY_CC); if (Z_TYPE_P(messages) != IS_OBJECT) { PHALCON_INIT_NVAR(messages); object_init_ex(messages, phalcon_validation_message_group_ce); phalcon_call_method_noret(messages, "__construct"); phalcon_update_property_this(this_ptr, SL("_messages"), messages TSRMLS_CC); } phalcon_call_method_p1_noret(messages, "appendmessage", message); RETURN_THIS(); }
/** * Resolves the service based on its configuration * * @param string $name * @param array $parameters * @return mixed */ PHP_METHOD(Phalcon_DI, get){ zval *name, *parameters = NULL, *services, *service, *instance = NULL; zval *exception_message; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &name, ¶meters); if (!parameters) { PHALCON_INIT_VAR(parameters); } /** * A valid service alias is a string */ if (Z_TYPE_P(name) != IS_STRING) { PHALCON_THROW_EXCEPTION_STR(phalcon_di_exception_ce, "The service alias must be a string"); return; } PHALCON_OBS_VAR(services); phalcon_read_property_this(&services, this_ptr, SL("_services"), PH_NOISY_CC); if (phalcon_array_isset(services, name)) { /** * The service is registered in the DI */ PHALCON_OBS_VAR(service); phalcon_array_fetch(&service, services, name, PH_NOISY); PHALCON_INIT_VAR(instance); phalcon_call_method_p2(instance, service, "resolve", parameters, this_ptr); } else { /** * The DI also acts as builder for any class even if it isn't defined in the DI */ if (phalcon_class_exists(name, 1 TSRMLS_CC)) { if (Z_TYPE_P(parameters) == IS_ARRAY) { if (phalcon_fast_count_ev(parameters TSRMLS_CC)) { PHALCON_INIT_NVAR(instance); if (phalcon_create_instance_params(instance, name, parameters TSRMLS_CC) == FAILURE) { return; } } else { PHALCON_INIT_NVAR(instance); if (phalcon_create_instance(instance, name TSRMLS_CC) == FAILURE) { return; } } } else { PHALCON_INIT_NVAR(instance); if (phalcon_create_instance(instance, name TSRMLS_CC) == FAILURE) { return; } } } else { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Service '", name, "' wasn't found in the dependency injection container"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_di_exception_ce, exception_message); return; } } /** * Pass the DI itself if the instance implements Phalcon\DI\InjectionAwareInterface */ if (Z_TYPE_P(instance) == IS_OBJECT) { if (phalcon_method_exists_ex(instance, SS("setdi") TSRMLS_CC) == SUCCESS) { phalcon_call_method_p1_noret(instance, "setdi", this_ptr); } } RETURN_CCTOR(instance); }
/** * Changes internal pointer to a specific position in the resultset * * @param int $position */ PHP_METHOD(Phalcon_Mvc_Model_Resultset, seek){ long i; zval *type, *result, *rows, *position; zval *pointer, *is_different; HashTable *ah0; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &position) == FAILURE) { PHALCON_MM_RESTORE(); RETURN_NULL(); } PHALCON_OBS_VAR(pointer); phalcon_read_property(&pointer, this_ptr, SL("_pointer"), PH_NOISY_CC); /** * We only seek the records if the current position is diferent than the passed one */ PHALCON_INIT_VAR(is_different); is_not_equal_function(is_different, pointer, position TSRMLS_CC); if (PHALCON_IS_TRUE(is_different)) { PHALCON_OBS_VAR(type); phalcon_read_property(&type, this_ptr, SL("_type"), PH_NOISY_CC); if (zend_is_true(type)) { /** * Here, the resultset is fetched one by one because is large */ PHALCON_OBS_VAR(result); phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC); phalcon_call_method_p1_noret(result, "dataseek", position); } else { /** * Here, the resultset is a small array */ PHALCON_OBS_VAR(rows); phalcon_read_property(&rows, this_ptr, SL("_rows"), PH_NOISY_CC); /** * We need to fetch the records because rows is null */ if (Z_TYPE_P(rows) == IS_NULL) { PHALCON_OBS_VAR(result); phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC); if (PHALCON_IS_NOT_FALSE(result)) { PHALCON_INIT_NVAR(rows); phalcon_call_method(rows, result, "fetchall"); phalcon_update_property_zval(this_ptr, SL("_rows"), rows TSRMLS_CC); } } convert_to_long(position); if(Z_TYPE_P(rows) == IS_ARRAY){ ah0 = Z_ARRVAL_P(rows); zend_hash_internal_pointer_reset(ah0); i = 0; while (1) { if (i >= Z_LVAL_P(position)) { break; } zend_hash_move_forward(ah0); i++; } } phalcon_update_property_zval(this_ptr, SL("_pointer"), position TSRMLS_CC); } } PHALCON_MM_RESTORE();}
/** * 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); }
/** * Shows a backtrace item * * @param int $n * @param array $trace */ PHP_METHOD(Phalcon_Debug, showTraceItem){ zval *n, *trace, *space, *two_spaces, *underscore; zval *minus, *html, *class_name, *pattern, *is_phalcon_class; zval *namespace_separator, *prepare_uri_class; zval *class_reflection, *is_internal = NULL, *lower_class_name; zval *prepare_internal_class, *type, *function_name = NULL; zval *function_reflection, *prepared_function_name; zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL; zval *span_argument = NULL, *joined_arguments, *one; zval *file, *line, *show_files, *lines, *number_lines; zval *show_file_fragment, *seven, *before_line; zval *first_line = NULL, *five, *after_line, *last_line = NULL; zval *comment_pattern, *utf8, *ent_compat, *tab; zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL; zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL; zval *escaped_line = NULL; HashTable *ah0; HashPosition hp0; zval **hd; zend_class_entry *ce0, *ce1; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &n, &trace); PHALCON_INIT_VAR(space); ZVAL_STRING(space, " ", 1); PHALCON_INIT_VAR(two_spaces); ZVAL_STRING(two_spaces, " ", 1); PHALCON_INIT_VAR(underscore); ZVAL_STRING(underscore, "_", 1); PHALCON_INIT_VAR(minus); ZVAL_STRING(minus, "-", 1); /** * Every trace in the backtrace have a unique number */ PHALCON_INIT_VAR(html); PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>"); if (phalcon_array_isset_string(trace, SS("class"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "/^Phalcon/", 1); PHALCON_INIT_VAR(is_phalcon_class); phalcon_preg_match(is_phalcon_class, pattern, class_name, NULL TSRMLS_CC); /** * We assume that classes starting by Phalcon are framework's classes */ if (zend_is_true(is_phalcon_class)) { PHALCON_INIT_VAR(namespace_separator); ZVAL_STRING(namespace_separator, "\\", 1); /** * Prepare the class name according to the Phalcon's conventions */ PHALCON_INIT_VAR(prepare_uri_class); phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name); /** * Generate a link to the official docs */ PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>"); } else { ce0 = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_VAR(class_reflection); object_init_ex(class_reflection, ce0); if (phalcon_has_constructor(class_reflection TSRMLS_CC)) { phalcon_call_method_p1_noret(class_reflection, "__construct", class_name); } /** * Check if classes are PHP's classes */ PHALCON_INIT_VAR(is_internal); phalcon_call_method(is_internal, class_reflection, "isinternal"); if (zend_is_true(is_internal)) { PHALCON_INIT_VAR(lower_class_name); phalcon_fast_strtolower(lower_class_name, class_name); PHALCON_INIT_VAR(prepare_internal_class); phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name); /** * Generate a link to the official docs */ PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>"); } else { PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>"); } } /** * Object access operator: static/instance */ PHALCON_OBS_VAR(type); phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY); phalcon_concat_self(&html, type TSRMLS_CC); } /** * Normally the backtrace contains only classes */ if (phalcon_array_isset_string(trace, SS("class"))) { PHALCON_OBS_VAR(function_name); phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY); PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>"); } else { PHALCON_OBS_NVAR(function_name); phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY); /** * Check if the function exists */ if (phalcon_function_exists(function_name TSRMLS_CC) == SUCCESS) { ce1 = zend_fetch_class(SL("ReflectionFunction"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_VAR(function_reflection); object_init_ex(function_reflection, ce1); if (phalcon_has_constructor(function_reflection TSRMLS_CC)) { phalcon_call_method_p1_noret(function_reflection, "__construct", function_name); } PHALCON_INIT_NVAR(is_internal); phalcon_call_method(is_internal, function_reflection, "isinternal"); /** * Internal functions links to the PHP documentation */ if (zend_is_true(is_internal)) { /** * Prepare function's name according to the conventions in the docs */ PHALCON_INIT_VAR(prepared_function_name); phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name); PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>"); } else { PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>"); } } else { PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>"); } } /** * Check for arguments in the function */ if (phalcon_array_isset_string(trace, SS("args"))) { PHALCON_OBS_VAR(trace_args); phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY); if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) { PHALCON_INIT_VAR(arguments); array_init(arguments); phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(argument); /** * Every argument is generated using _getVarDump */ PHALCON_INIT_NVAR(dumped_argument); phalcon_call_method_p1(dumped_argument, this_ptr, "_getvardump", argument); PHALCON_INIT_NVAR(span_argument); PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>"); /** * Append the HTML generated to the argument's list */ phalcon_array_append(&arguments, span_argument, PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); } /** * Join all the arguments */ PHALCON_INIT_VAR(joined_arguments); phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC); PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")"); } else { phalcon_concat_self_str(&html, SL("()") TSRMLS_CC); } } /** * When 'file' is present, it usually means the function is provided by the user */ if (phalcon_array_isset_string(trace, SS("file"))) { PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_OBS_VAR(file); phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY); PHALCON_OBS_VAR(line); phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY); /** * Realpath to the file and its line using a special header */ PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", file, " (", line, ")</div>"); PHALCON_OBS_VAR(show_files); phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY_CC); /** * The developer can change if the files must be opened or not */ if (zend_is_true(show_files)) { /** * Open the file to an array using 'file', this respects the openbase-dir directive */ PHALCON_INIT_VAR(lines); phalcon_call_func_p1(lines, "file", file); PHALCON_INIT_VAR(number_lines); phalcon_fast_count(number_lines, lines TSRMLS_CC); PHALCON_OBS_VAR(show_file_fragment); phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY_CC); /** * File fragments just show a piece of the file where the exception is located */ if (zend_is_true(show_file_fragment)) { /** * Take seven lines back to the current exception's line, @TODO add an option for * this */ PHALCON_INIT_VAR(seven); ZVAL_LONG(seven, 7); PHALCON_INIT_VAR(before_line); sub_function(before_line, line, seven TSRMLS_CC); /** * Check for overflows */ if (PHALCON_LT_LONG(before_line, 1)) { PHALCON_CPY_WRT(first_line, one); } else { PHALCON_CPY_WRT(first_line, before_line); } /** * Take five lines after the current exception's line, @TODO add an option for this */ PHALCON_INIT_VAR(five); ZVAL_LONG(five, 5); PHALCON_INIT_VAR(after_line); phalcon_add_function(after_line, line, five TSRMLS_CC); /** * Check for overflows */ if (PHALCON_GT(after_line, number_lines)) { PHALCON_CPY_WRT(last_line, number_lines); } else { PHALCON_CPY_WRT(last_line, after_line); } PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>"); } else { PHALCON_CPY_WRT(first_line, one); PHALCON_CPY_WRT(last_line, number_lines); PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>"); } PHALCON_INIT_VAR(comment_pattern); ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1); /** * We assume the file is utf-8 encoded, @TODO add an option for this */ PHALCON_INIT_VAR(utf8); ZVAL_STRING(utf8, "UTF-8", 1); /** * Don't escape quotes */ PHALCON_INIT_VAR(ent_compat); ZVAL_LONG(ent_compat, 2); PHALCON_INIT_VAR(tab); ZVAL_STRING(tab, "\t", 1); PHALCON_INIT_VAR(comment); ZVAL_STRING(comment, "* /", 1); PHALCON_CPY_WRT(i, first_line); while (1) { if (PHALCON_LE(i, last_line)) { } else { break; } /** * Current line in the file */ PHALCON_INIT_NVAR(line_position); sub_function(line_position, i, one TSRMLS_CC); /** * Current line content in the piece of file */ PHALCON_OBS_NVAR(current_line); phalcon_array_fetch(¤t_line, lines, line_position, PH_NOISY); /** * File fragments are cleaned, removing tabs and comments */ if (zend_is_true(show_file_fragment)) { if (PHALCON_IS_EQUAL(i, first_line)) { PHALCON_INIT_NVAR(trimmed); phalcon_fast_trim(trimmed, current_line, PHALCON_TRIM_RIGHT TSRMLS_CC); PHALCON_INIT_NVAR(is_comment); phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC); if (zend_is_true(is_comment)) { PHALCON_INIT_NVAR(spaced_current_line); phalcon_fast_str_replace(spaced_current_line, comment, space, current_line); PHALCON_CPY_WRT(current_line, spaced_current_line); } } } /** * Print a non break space if the current line is a line break, this allows to show * the html zebra properly */ if (PHALCON_IS_STRING(current_line, "\n")) { phalcon_concat_self_str(&html, SL(" \n") TSRMLS_CC); } else { if (PHALCON_IS_STRING(current_line, "\r\n")) { phalcon_concat_self_str(&html, SL(" \n") TSRMLS_CC); } else { PHALCON_INIT_NVAR(spaced_current_line); phalcon_fast_str_replace(spaced_current_line, tab, two_spaces, current_line); PHALCON_INIT_NVAR(escaped_line); phalcon_call_func_p3(escaped_line, "htmlentities", spaced_current_line, ent_compat, utf8); phalcon_concat_self(&html, escaped_line TSRMLS_CC); } } PHALCON_SEPARATE(i); phalcon_increment(i); } phalcon_concat_self_str(&html, SL("</pre>") TSRMLS_CC); } } phalcon_concat_self_str(&html, SL("</td></tr>") TSRMLS_CC); RETURN_CTOR(html); }
/** * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST * * @param Phalcon\DiInterface $dependencyInjector * @param string $modelName * @param array $data * @return static */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){ zval *dependency_injector, *model_name, *data; zval *conditions, *service, *meta_data, *model; zval *data_types, *bind, *value = NULL, *field = NULL, *type = NULL, *condition = NULL; zval *value_pattern = NULL, *criteria, *join_conditions; HashTable *ah0; HashPosition hp0; zval **hd; zend_class_entry *ce0; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data); if (Z_TYPE_P(data) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array"); return; } if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services"); return; } PHALCON_INIT_VAR(conditions); array_init(conditions); if (phalcon_fast_count_ev(data TSRMLS_CC)) { PHALCON_INIT_VAR(service); ZVAL_STRING(service, "modelsMetadata", 1); PHALCON_INIT_VAR(meta_data); phalcon_call_method_p1(meta_data, dependency_injector, "getshared", service); ce0 = phalcon_fetch_class(model_name TSRMLS_CC); PHALCON_INIT_VAR(model); object_init_ex(model, ce0); if (phalcon_has_constructor(model TSRMLS_CC)) { phalcon_call_method_noret(model, "__construct"); } PHALCON_INIT_VAR(data_types); phalcon_call_method_p1(data_types, meta_data, "getdatatypes", model); PHALCON_INIT_VAR(bind); array_init(bind); /** * We look for attributes in the array passed as data */ phalcon_is_iterable(data, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(field, ah0, hp0); PHALCON_GET_HVALUE(value); if (phalcon_array_isset(data_types, field)) { if (Z_TYPE_P(value) != IS_NULL) { if (!PHALCON_IS_STRING(value, "")) { PHALCON_OBS_NVAR(type); phalcon_array_fetch(&type, data_types, field, PH_NOISY_CC); if (PHALCON_IS_LONG(type, 2)) { /** * For varchar types we use LIKE operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":"); PHALCON_INIT_NVAR(value_pattern); PHALCON_CONCAT_SVS(value_pattern, "%", value, "%"); phalcon_array_update_zval(&bind, field, &value_pattern, PH_COPY | PH_SEPARATE TSRMLS_CC); } else { /** * For the rest of data types we use a plain = operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":"); phalcon_array_update_zval(&bind, field, &value, PH_COPY | PH_SEPARATE TSRMLS_CC); } phalcon_array_append(&conditions, condition, PH_SEPARATE TSRMLS_CC); } } } zend_hash_move_forward_ex(ah0, &hp0); } } /** * Create an object instance and pass the paramaters to it */ PHALCON_INIT_VAR(criteria); object_init_ex(criteria, phalcon_mvc_model_criteria_ce); if (phalcon_fast_count_ev(conditions TSRMLS_CC)) { PHALCON_INIT_VAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC); phalcon_call_method_p1_noret(criteria, "where", join_conditions); phalcon_call_method_p1_noret(criteria, "bind", bind); } RETURN_CTOR(criteria); }
/** * Create/Returns a new transaction or an existing one * * @param boolean $autoBegin * @return Phalcon\Mvc\Model\TransactionInterface */ PHP_METHOD(Phalcon_Mvc_Model_Transaction_Manager, getOrCreateTransaction){ zval *auto_begin = NULL, *dependency_injector, *number; zval *transactions, *transaction = NULL, *false_value = NULL; zval *service; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 1, &auto_begin); if (!auto_begin) { PHALCON_INIT_VAR(auto_begin); ZVAL_BOOL(auto_begin, 1); } 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_model_transaction_exception_ce, "A dependency injector container is required to obtain the services related to the ORM"); return; } PHALCON_OBS_VAR(number); phalcon_read_property_this(&number, this_ptr, SL("_number"), PH_NOISY_CC); if (zend_is_true(number)) { PHALCON_OBS_VAR(transactions); phalcon_read_property_this(&transactions, this_ptr, SL("_transactions"), PH_NOISY_CC); if (Z_TYPE_P(transactions) == IS_ARRAY) { phalcon_is_iterable(transactions, &ah0, &hp0, 0, 1); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(transaction); if (Z_TYPE_P(transaction) == IS_OBJECT) { PHALCON_INIT_NVAR(false_value); ZVAL_BOOL(false_value, 0); phalcon_call_method_p1_noret(transaction, "setisnewtransaction", false_value); RETURN_CCTOR(transaction); } zend_hash_move_backwards_ex(ah0, &hp0); } } } PHALCON_OBS_VAR(service); phalcon_read_property_this(&service, this_ptr, SL("_service"), PH_NOISY_CC); PHALCON_INIT_VAR(transaction); object_init_ex(transaction, phalcon_mvc_model_transaction_ce); phalcon_call_method_p3_noret(transaction, "__construct", dependency_injector, auto_begin, service); phalcon_call_method_p1_noret(transaction, "settransactionmanager", this_ptr); phalcon_update_property_array_append(this_ptr, SL("_transactions"), transaction TSRMLS_CC); phalcon_property_incr(this_ptr, SL("_number") TSRMLS_CC); RETURN_CTOR(transaction); }
/** * 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); }
/** * Returns a PHQL statement built based on the builder parameters * * @return string */ PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, getPhql){ zval *dependency_injector = NULL, *models, *conditions = NULL; zval *one, *number_models, *invalid_condition; zval *model = NULL, *service_name, *meta_data, *model_instance; zval *no_primary = NULL, *primary_keys, *first_primary_key; zval *column_map = NULL, *attribute_field = NULL, *exception_message; zval *primary_key_condition, *phql, *columns; zval *selected_columns = NULL, *column = NULL, *column_alias = NULL; zval *aliased_column = NULL, *joined_columns = NULL, *model_column_alias = NULL; zval *selected_column = NULL, *selected_models, *model_alias = NULL; zval *selected_model = NULL, *joined_models, *joins; zval *join = NULL, *join_model = NULL, *join_conditions = NULL, *join_alias = NULL; zval *join_type = NULL, *group, *group_items, *group_item = NULL; zval *escaped_item = NULL, *joined_items = NULL, *having, *order; zval *order_items, *order_item = NULL, *limit, *number; zval *offset = NULL; HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5; HashPosition hp0, hp1, hp2, hp3, hp4, hp5; zval **hd; zend_class_entry *ce0; PHALCON_MM_GROW(); 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(dependency_injector); PHALCON_CALL_STATIC(dependency_injector, "phalcon\\di", "getdefault"); phalcon_update_property_this(this_ptr, SL("_dependencyInjector"), dependency_injector TSRMLS_CC); } PHALCON_OBS_VAR(models); phalcon_read_property_this(&models, this_ptr, SL("_models"), PH_NOISY_CC); if (Z_TYPE_P(models) == IS_ARRAY) { if (!phalcon_fast_count_ev(models TSRMLS_CC)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "At least one model is required to build the query"); return; } } else { if (!zend_is_true(models)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "At least one model is required to build the query"); return; } } PHALCON_OBS_VAR(conditions); phalcon_read_property_this(&conditions, this_ptr, SL("_conditions"), PH_NOISY_CC); if (phalcon_is_numeric(conditions)) { /** * If the conditions is a single numeric field. We internally create a condition * using the related primary key */ if (Z_TYPE_P(models) == IS_ARRAY) { PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_INIT_VAR(number_models); phalcon_fast_count(number_models, models TSRMLS_CC); PHALCON_INIT_VAR(invalid_condition); is_smaller_function(invalid_condition, one, number_models TSRMLS_CC); if (PHALCON_IS_TRUE(invalid_condition)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Cannot build the query. Invalid condition"); return; } PHALCON_OBS_VAR(model); phalcon_array_fetch_long(&model, models, 0, PH_NOISY); } else { PHALCON_CPY_WRT(model, models); } PHALCON_INIT_VAR(service_name); ZVAL_STRING(service_name, "modelsMetadata", 1); /** * Get the models metadata service to obtain the column names, column map and * primary key */ PHALCON_INIT_VAR(meta_data); phalcon_call_method_p1(meta_data, dependency_injector, "getshared", service_name); ce0 = phalcon_fetch_class(model TSRMLS_CC); PHALCON_INIT_VAR(model_instance); object_init_ex(model_instance, ce0); if (phalcon_has_constructor(model_instance TSRMLS_CC)) { phalcon_call_method_p1_noret(model_instance, "__construct", dependency_injector); } PHALCON_INIT_VAR(no_primary); ZVAL_BOOL(no_primary, 1); PHALCON_INIT_VAR(primary_keys); phalcon_call_method_p1(primary_keys, meta_data, "getprimarykeyattributes", model_instance); if (phalcon_fast_count_ev(primary_keys TSRMLS_CC)) { if (phalcon_array_isset_long(primary_keys, 0)) { PHALCON_OBS_VAR(first_primary_key); phalcon_array_fetch_long(&first_primary_key, primary_keys, 0, PH_NOISY); /** * The PHQL contains the renamed columns if available */ if (PHALCON_GLOBAL(orm).column_renaming) { PHALCON_INIT_VAR(column_map); phalcon_call_method_p1(column_map, meta_data, "getcolumnmap", model_instance); } else { PHALCON_INIT_NVAR(column_map); } if (Z_TYPE_P(column_map) == IS_ARRAY) { if (phalcon_array_isset(column_map, first_primary_key)) { PHALCON_OBS_VAR(attribute_field); phalcon_array_fetch(&attribute_field, column_map, first_primary_key, PH_NOISY); } else { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Column '", first_primary_key, "\" isn't part of the column map"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } } else { PHALCON_CPY_WRT(attribute_field, first_primary_key); } PHALCON_INIT_VAR(primary_key_condition); PHALCON_CONCAT_SVSVSV(primary_key_condition, "[", model, "].[", attribute_field, "] = ", conditions); PHALCON_CPY_WRT(conditions, primary_key_condition); ZVAL_BOOL(no_primary, 0); } } /** * A primary key is mandatory in these cases */ if (PHALCON_IS_TRUE(no_primary)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Source related to this model does not have a primary key defined"); return; } } PHALCON_INIT_VAR(phql); ZVAL_STRING(phql, "SELECT ", 1); PHALCON_OBS_VAR(columns); phalcon_read_property_this(&columns, this_ptr, SL("_columns"), PH_NOISY_CC); if (Z_TYPE_P(columns) != IS_NULL) { /** * Generate PHQL for columns */ if (Z_TYPE_P(columns) == IS_ARRAY) { PHALCON_INIT_VAR(selected_columns); array_init(selected_columns); phalcon_is_iterable(columns, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(column_alias, ah0, hp0); PHALCON_GET_HVALUE(column); if (Z_TYPE_P(column_alias) == IS_LONG) { phalcon_array_append(&selected_columns, column, PH_SEPARATE); } else { PHALCON_INIT_NVAR(aliased_column); PHALCON_CONCAT_VSV(aliased_column, column, " AS ", column_alias); phalcon_array_append(&selected_columns, aliased_column, PH_SEPARATE); } zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(joined_columns); phalcon_fast_join_str(joined_columns, SL(", "), selected_columns TSRMLS_CC); phalcon_concat_self(&phql, joined_columns TSRMLS_CC); } else { phalcon_concat_self(&phql, columns TSRMLS_CC); } } else { /** * Automatically generate an array of models */ if (Z_TYPE_P(models) == IS_ARRAY) { PHALCON_INIT_NVAR(selected_columns); array_init(selected_columns); phalcon_is_iterable(models, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HKEY(model_column_alias, ah1, hp1); PHALCON_GET_HVALUE(model); if (Z_TYPE_P(model_column_alias) == IS_LONG) { PHALCON_INIT_NVAR(selected_column); PHALCON_CONCAT_SVS(selected_column, "[", model, "].*"); } else { PHALCON_INIT_NVAR(selected_column); PHALCON_CONCAT_SVS(selected_column, "[", model_column_alias, "].*"); } phalcon_array_append(&selected_columns, selected_column, PH_SEPARATE); zend_hash_move_forward_ex(ah1, &hp1); } PHALCON_INIT_NVAR(joined_columns); phalcon_fast_join_str(joined_columns, SL(", "), selected_columns TSRMLS_CC); phalcon_concat_self(&phql, joined_columns TSRMLS_CC); } else { PHALCON_SCONCAT_SVS(phql, "[", models, "].*"); } } /** * Join multiple models or use a single one if it is a string */ if (Z_TYPE_P(models) == IS_ARRAY) { PHALCON_INIT_VAR(selected_models); array_init(selected_models); phalcon_is_iterable(models, &ah2, &hp2, 0, 0); while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) { PHALCON_GET_HKEY(model_alias, ah2, hp2); PHALCON_GET_HVALUE(model); if (Z_TYPE_P(model_alias) == IS_STRING) { PHALCON_INIT_NVAR(selected_model); PHALCON_CONCAT_SVSVS(selected_model, "[", model, "] AS [", model_alias, "]"); } else { PHALCON_INIT_NVAR(selected_model); PHALCON_CONCAT_SVS(selected_model, "[", model, "]"); } phalcon_array_append(&selected_models, selected_model, PH_SEPARATE); zend_hash_move_forward_ex(ah2, &hp2); } PHALCON_INIT_VAR(joined_models); phalcon_fast_join_str(joined_models, SL(", "), selected_models TSRMLS_CC); PHALCON_SCONCAT_SV(phql, " FROM ", joined_models); } else { PHALCON_SCONCAT_SVS(phql, " FROM [", models, "]"); } /** * Check if joins were passed to the builders */ PHALCON_OBS_VAR(joins); phalcon_read_property_this(&joins, this_ptr, SL("_joins"), PH_NOISY_CC); if (Z_TYPE_P(joins) == IS_ARRAY) { phalcon_is_iterable(joins, &ah3, &hp3, 0, 0); while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) { PHALCON_GET_HVALUE(join); /** * The joined table is in the first place of the array */ PHALCON_OBS_NVAR(join_model); phalcon_array_fetch_long(&join_model, join, 0, PH_NOISY); /** * The join conditions are in the second place of the array */ PHALCON_OBS_NVAR(join_conditions); phalcon_array_fetch_long(&join_conditions, join, 1, PH_NOISY); /** * The join alias is in the second place of the array */ PHALCON_OBS_NVAR(join_alias); phalcon_array_fetch_long(&join_alias, join, 2, PH_NOISY); /** * Join type */ PHALCON_OBS_NVAR(join_type); phalcon_array_fetch_long(&join_type, join, 3, PH_NOISY); /** * Create the join according to the type */ if (zend_is_true(join_type)) { PHALCON_SCONCAT_SVSVS(phql, " ", join_type, " JOIN [", join_model, "]"); } else { PHALCON_SCONCAT_SVS(phql, " JOIN [", join_model, "]"); } /** * Alias comes first */ if (zend_is_true(join_alias)) { PHALCON_SCONCAT_SVS(phql, " AS [", join_alias, "]"); } /** * Conditions then */ if (zend_is_true(join_conditions)) { PHALCON_SCONCAT_SV(phql, " ON ", join_conditions); } zend_hash_move_forward_ex(ah3, &hp3); } } /** * Only append conditions if it's string */ if (Z_TYPE_P(conditions) == IS_STRING) { if (PHALCON_IS_NOT_EMPTY(conditions)) { PHALCON_SCONCAT_SV(phql, " WHERE ", conditions); } } /** * Process group parameters */ PHALCON_OBS_VAR(group); phalcon_read_property_this(&group, this_ptr, SL("_group"), PH_NOISY_CC); if (Z_TYPE_P(group) != IS_NULL) { if (Z_TYPE_P(group) == IS_ARRAY) { PHALCON_INIT_VAR(group_items); array_init(group_items); phalcon_is_iterable(group, &ah4, &hp4, 0, 0); while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) { PHALCON_GET_HVALUE(group_item); if (phalcon_is_numeric(group_item)) { phalcon_array_append(&group_items, group_item, PH_SEPARATE); } else { if (phalcon_memnstr_str(group_item, SL("."))) { phalcon_array_append(&group_items, group_item, PH_SEPARATE); } else { PHALCON_INIT_NVAR(escaped_item); PHALCON_CONCAT_SVS(escaped_item, "[", group_item, "]"); phalcon_array_append(&group_items, escaped_item, PH_SEPARATE); } } zend_hash_move_forward_ex(ah4, &hp4); } PHALCON_INIT_VAR(joined_items); phalcon_fast_join_str(joined_items, SL(", "), group_items TSRMLS_CC); PHALCON_SCONCAT_SV(phql, " GROUP BY ", joined_items); } else { if (phalcon_is_numeric(group)) { PHALCON_SCONCAT_SV(phql, " GROUP BY ", group); } else { if (phalcon_memnstr_str(group, SL("."))) { PHALCON_SCONCAT_SV(phql, " GROUP BY ", group); } else { PHALCON_SCONCAT_SVS(phql, " GROUP BY [", group, "]"); } } } PHALCON_OBS_VAR(having); phalcon_read_property_this(&having, this_ptr, SL("_having"), PH_NOISY_CC); if (Z_TYPE_P(having) != IS_NULL) { if (PHALCON_IS_NOT_EMPTY(having)) { PHALCON_SCONCAT_SV(phql, " HAVING ", having); } } } /** * Process order clause */ PHALCON_OBS_VAR(order); phalcon_read_property_this(&order, this_ptr, SL("_order"), PH_NOISY_CC); if (Z_TYPE_P(order) != IS_NULL) { if (Z_TYPE_P(order) == IS_ARRAY) { PHALCON_INIT_VAR(order_items); array_init(order_items); phalcon_is_iterable(order, &ah5, &hp5, 0, 0); while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) { PHALCON_GET_HVALUE(order_item); if (phalcon_is_numeric(order_item)) { phalcon_array_append(&order_items, order_item, PH_SEPARATE); } else { if (phalcon_memnstr_str(order_item, SL("."))) { phalcon_array_append(&order_items, order_item, PH_SEPARATE); } else { PHALCON_INIT_NVAR(escaped_item); PHALCON_CONCAT_SVS(escaped_item, "[", order_item, "]"); phalcon_array_append(&order_items, escaped_item, PH_SEPARATE); } } zend_hash_move_forward_ex(ah5, &hp5); } PHALCON_INIT_NVAR(joined_items); phalcon_fast_join_str(joined_items, SL(", "), order_items TSRMLS_CC); PHALCON_SCONCAT_SV(phql, " ORDER BY ", joined_items); } else { PHALCON_SCONCAT_SV(phql, " ORDER BY ", order); } } /** * Process limit parameters */ PHALCON_OBS_VAR(limit); phalcon_read_property_this(&limit, this_ptr, SL("_limit"), PH_NOISY_CC); if (Z_TYPE_P(limit) != IS_NULL) { if (Z_TYPE_P(limit) == IS_ARRAY) { PHALCON_OBS_VAR(number); phalcon_array_fetch_string(&number, limit, SL("number"), PH_NOISY); if (phalcon_array_isset_string(limit, SS("offset"))) { PHALCON_OBS_VAR(offset); phalcon_array_fetch_string(&offset, limit, SL("offset"), PH_NOISY); if (phalcon_is_numeric(offset)) { PHALCON_SCONCAT_SVSV(phql, " LIMIT ", number, " OFFSET ", offset); } else { PHALCON_SCONCAT_SVS(phql, " LIMIT ", number, " OFFSET 0"); } } else { PHALCON_SCONCAT_SV(phql, " LIMIT ", number); } } else { if (phalcon_is_numeric(limit)) { PHALCON_SCONCAT_SV(phql, " LIMIT ", limit); PHALCON_OBS_NVAR(offset); phalcon_read_property_this(&offset, this_ptr, SL("_offset"), PH_NOISY_CC); if (Z_TYPE_P(offset) != IS_NULL) { if (phalcon_is_numeric(offset)) { PHALCON_SCONCAT_SV(phql, " OFFSET ", offset); } else { phalcon_concat_self_str(&phql, SL(" OFFSET 0") TSRMLS_CC); } } } } } RETURN_CTOR(phql); }
/** * Stores cached content into the Mongo backend and stops the frontend * * @param int|string $keyName * @param string $content * @param long $lifetime * @param boolean $stopBuffer */ PHP_METHOD(Phalcon_Cache_Backend_Mongo, save){ zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL; zval *last_key = NULL, *prefix, *frontend, *cached_content = NULL; zval *prepared_content, *ttl = NULL, *collection, *timestamp; zval *conditions, *document, *data, *is_buffering; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 4, &key_name, &content, &lifetime, &stop_buffer); if (!key_name) { PHALCON_INIT_VAR(key_name); } if (!content) { PHALCON_INIT_VAR(content); } if (!lifetime) { PHALCON_INIT_VAR(lifetime); } if (!stop_buffer) { PHALCON_INIT_VAR(stop_buffer); ZVAL_BOOL(stop_buffer, 1); } if (Z_TYPE_P(key_name) == IS_NULL) { PHALCON_OBS_VAR(last_key); phalcon_read_property_this(&last_key, this_ptr, SL("_lastKey"), PH_NOISY_CC); } else { PHALCON_OBS_VAR(prefix); phalcon_read_property_this(&prefix, this_ptr, SL("_prefix"), PH_NOISY_CC); PHALCON_INIT_NVAR(last_key); PHALCON_CONCAT_VV(last_key, prefix, key_name); } if (!zend_is_true(last_key)) { PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The cache must be started first"); return; } PHALCON_OBS_VAR(frontend); phalcon_read_property_this(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC); if (Z_TYPE_P(content) == IS_NULL) { PHALCON_INIT_VAR(cached_content); phalcon_call_method(cached_content, frontend, "getcontent"); } else { PHALCON_CPY_WRT(cached_content, content); } PHALCON_INIT_VAR(prepared_content); phalcon_call_method_p1(prepared_content, frontend, "beforestore", cached_content); if (Z_TYPE_P(lifetime) == IS_NULL) { PHALCON_INIT_VAR(ttl); phalcon_call_method(ttl, frontend, "getlifetime"); } else { PHALCON_CPY_WRT(ttl, lifetime); } PHALCON_INIT_VAR(collection); phalcon_call_method(collection, this_ptr, "_getcollection"); PHALCON_INIT_VAR(timestamp); ZVAL_LONG(timestamp, (long) time(NULL)); PHALCON_INIT_NVAR(ttl); phalcon_add_function(ttl, lifetime, timestamp TSRMLS_CC); PHALCON_INIT_VAR(conditions); array_init_size(conditions, 1); phalcon_array_update_string(&conditions, SL("key"), &last_key, PH_COPY | PH_SEPARATE); PHALCON_INIT_VAR(document); phalcon_call_method_p1(document, collection, "findone", conditions); if (Z_TYPE_P(document) == IS_ARRAY) { phalcon_array_update_string(&document, SL("time"), &ttl, PH_COPY | PH_SEPARATE); phalcon_array_update_string(&document, SL("data"), &prepared_content, PH_COPY | PH_SEPARATE); phalcon_call_method_p1_noret(collection, "save", document); } else { PHALCON_INIT_VAR(data); array_init_size(data, 3); phalcon_array_update_string(&data, SL("key"), &last_key, PH_COPY | PH_SEPARATE); phalcon_array_update_string(&data, SL("time"), &ttl, PH_COPY | PH_SEPARATE); phalcon_array_update_string(&data, SL("data"), &prepared_content, PH_COPY | PH_SEPARATE); phalcon_call_method_p1_noret(collection, "save", data); } PHALCON_INIT_VAR(is_buffering); phalcon_call_method(is_buffering, frontend, "isbuffering"); if (PHALCON_IS_TRUE(stop_buffer)) { phalcon_call_method_noret(frontend, "stop"); } if (PHALCON_IS_TRUE(is_buffering)) { zend_print_zval(cached_content, 0); } phalcon_update_property_bool(this_ptr, SL("_started"), 0 TSRMLS_CC); PHALCON_MM_RESTORE(); }
/** * Executes the validation * * @param Phalcon\Validation $validator * @param string $attribute * @return boolean */ PHP_METHOD(Phalcon_Validation_Validator_StringLength, validate){ zval *validator, *attribute, *option = NULL, *is_set_min; zval *is_set_max, *value, *length = NULL, *invalid_maximum = NULL; zval *invalid_minimum = NULL, *maximum, *message_str = NULL; zval *type = NULL, *message = NULL, *minimum; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &validator, &attribute); /** * At least one of 'min' or 'max' must be set */ PHALCON_INIT_VAR(option); ZVAL_STRING(option, "min", 1); PHALCON_INIT_VAR(is_set_min); phalcon_call_method_p1(is_set_min, this_ptr, "issetoption", option); PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "max", 1); PHALCON_INIT_VAR(is_set_max); phalcon_call_method_p1(is_set_max, this_ptr, "issetoption", option); if (!zend_is_true(is_set_min)) { if (!zend_is_true(is_set_max)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A minimum or maximum must be set"); return; } } PHALCON_INIT_VAR(value); phalcon_call_method_p1(value, validator, "getvalue", attribute); /** * Check if mbstring is available to calculate the correct length */ if (phalcon_function_exists_ex(SS("mb_strlen") TSRMLS_CC) == SUCCESS) { PHALCON_INIT_VAR(length); phalcon_call_func_p1(length, "mb_strlen", value); } else { PHALCON_INIT_NVAR(length); phalcon_fast_strlen(length, value); } PHALCON_INIT_VAR(invalid_maximum); ZVAL_BOOL(invalid_maximum, 0); PHALCON_INIT_VAR(invalid_minimum); ZVAL_BOOL(invalid_minimum, 0); /** * Maximum length */ if (zend_is_true(is_set_max)) { PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "max", 1); PHALCON_INIT_VAR(maximum); phalcon_call_method_p1(maximum, this_ptr, "getoption", option); is_smaller_function(invalid_maximum, maximum, length TSRMLS_CC); if (PHALCON_IS_TRUE(invalid_maximum)) { /** * Check if the developer has defined a custom message */ PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "messageMaximum", 1); PHALCON_INIT_VAR(message_str); phalcon_call_method_p1(message_str, this_ptr, "getoption", option); if (!zend_is_true(message_str)) { PHALCON_INIT_NVAR(message_str); PHALCON_CONCAT_SVSVS(message_str, "Value of field '", attribute, "' exceeds the maximum ", maximum, " characters"); } PHALCON_INIT_VAR(type); ZVAL_STRING(type, "TooLong", 1); PHALCON_INIT_VAR(message); object_init_ex(message, phalcon_validation_message_ce); phalcon_call_method_p3_noret(message, "__construct", message_str, attribute, type); phalcon_call_method_p1_noret(validator, "appendmessage", message); RETURN_MM_FALSE; } } /** * Minimum length */ if (zend_is_true(is_set_min)) { PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "min", 1); PHALCON_INIT_VAR(minimum); phalcon_call_method_p1(minimum, this_ptr, "getoption", option); is_smaller_function(invalid_minimum, length, minimum TSRMLS_CC); if (PHALCON_IS_TRUE(invalid_minimum)) { /** * Check if the developer has defined a custom message */ PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "messageMinimum", 1); PHALCON_INIT_NVAR(message_str); phalcon_call_method_p1(message_str, this_ptr, "getoption", option); if (!zend_is_true(message_str)) { PHALCON_INIT_NVAR(message_str); PHALCON_CONCAT_SVSVS(message_str, "Value of field '", attribute, "' is less than the minimum ", minimum, " characters"); } PHALCON_INIT_NVAR(type); ZVAL_STRING(type, "TooShort", 1); PHALCON_INIT_NVAR(message); object_init_ex(message, phalcon_validation_message_ce); phalcon_call_method_p3_noret(message, "__construct", message_str, attribute, type); phalcon_call_method_p1_noret(validator, "appendmessage", message); RETURN_MM_FALSE; } } RETURN_MM_TRUE; }
/** * Reads annotations from the class dockblocks, its methods and/or properties * * @param string $className * @return array */ PHP_METHOD(Phalcon_Annotations_Reader, parse){ zval *class_name, *annotations, *reflection; zval *comment = NULL, *file = NULL, *line = NULL, *class_annotations; zval *properties, *annotations_properties; zval *property = NULL, *property_annotations = NULL, *name = NULL; zval *methods, *annotations_methods, *method = NULL; zval *method_annotations = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; zend_class_entry *ce0; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &class_name); if (unlikely(Z_TYPE_P(class_name) != IS_STRING)) { PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "The class name must be an object"); return; } PHALCON_INIT_VAR(annotations); array_init(annotations); /** * A ReflectionClass is used to obtain the class dockblock */ ce0 = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_VAR(reflection); object_init_ex(reflection, ce0); if (phalcon_has_constructor(reflection TSRMLS_CC)) { phalcon_call_method_p1_noret(reflection, "__construct", class_name); } PHALCON_INIT_VAR(comment); phalcon_call_method(comment, reflection, "getdoccomment"); if (Z_TYPE_P(comment) == IS_STRING) { /** * Get the file where the class was declared */ PHALCON_INIT_VAR(file); phalcon_call_method(file, reflection, "getfilename"); /** * Get the line where the class was declared */ PHALCON_INIT_VAR(line); phalcon_call_method(line, reflection, "getstartline"); /** * Read annotations from class */ PHALCON_INIT_VAR(class_annotations); if (phannot_parse_annotations(class_annotations, comment, file, line TSRMLS_CC) == FAILURE) { return; } /** * Append the class annotations to the annotations var */ if (Z_TYPE_P(class_annotations) == IS_ARRAY) { phalcon_array_update_string(&annotations, SL("class"), &class_annotations, PH_COPY | PH_SEPARATE); } } /** * Get the class properties */ PHALCON_INIT_VAR(properties); phalcon_call_method(properties, reflection, "getproperties"); if (phalcon_fast_count_ev(properties TSRMLS_CC)) { /** * Line declaration for properties isn't available */ PHALCON_INIT_NVAR(line); ZVAL_LONG(line, 1); PHALCON_INIT_VAR(annotations_properties); array_init(annotations_properties); phalcon_is_iterable(properties, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(property); /** * Read comment from method */ PHALCON_INIT_NVAR(comment); phalcon_call_method(comment, property, "getdoccomment"); if (Z_TYPE_P(comment) == IS_STRING) { /** * Get the file where the property was declared */ PHALCON_INIT_NVAR(file); phalcon_call_method(file, reflection, "getfilename"); /** * Read annotations from the docblock */ PHALCON_INIT_NVAR(property_annotations); if (phannot_parse_annotations(property_annotations, comment, file, line TSRMLS_CC) == FAILURE) { return; } if (Z_TYPE_P(property_annotations) == IS_ARRAY) { PHALCON_OBS_NVAR(name); phalcon_read_property(&name, property, SL("name"), PH_NOISY_CC); phalcon_array_update_zval(&annotations_properties, name, &property_annotations, PH_COPY | PH_SEPARATE); } } zend_hash_move_forward_ex(ah0, &hp0); } if (phalcon_fast_count_ev(annotations_properties TSRMLS_CC)) { phalcon_array_update_string(&annotations, SL("properties"), &annotations_properties, PH_COPY | PH_SEPARATE); } } /** * Get the class methods */ PHALCON_INIT_VAR(methods); phalcon_call_method(methods, reflection, "getmethods"); if (phalcon_fast_count_ev(methods TSRMLS_CC)) { PHALCON_INIT_VAR(annotations_methods); array_init(annotations_methods); phalcon_is_iterable(methods, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HVALUE(method); /** * Read comment from method */ PHALCON_INIT_NVAR(comment); phalcon_call_method(comment, method, "getdoccomment"); if (Z_TYPE_P(comment) == IS_STRING) { /** * Get the file where the method was declared */ PHALCON_INIT_NVAR(file); phalcon_call_method(file, method, "getfilename"); /** * Get the line where the method was declared */ PHALCON_INIT_NVAR(line); phalcon_call_method(line, method, "getstartline"); /** * Read annotations from class */ PHALCON_INIT_NVAR(method_annotations); if (phannot_parse_annotations(method_annotations, comment, file, line TSRMLS_CC) == FAILURE) { return; } if (Z_TYPE_P(method_annotations) == IS_ARRAY) { PHALCON_OBS_NVAR(name); phalcon_read_property(&name, method, SL("name"), PH_NOISY_CC); phalcon_array_update_zval(&annotations_methods, name, &method_annotations, PH_COPY | PH_SEPARATE); } } zend_hash_move_forward_ex(ah1, &hp1); } if (phalcon_fast_count_ev(annotations_methods TSRMLS_CC)) { phalcon_array_update_string(&annotations, SL("methods"), &annotations_methods, PH_COPY | PH_SEPARATE); } } RETURN_CTOR(annotations); }
/** * Returns a MongoDb collection based on the backend parameters * * @return MongoCollection */ PHP_METHOD(Phalcon_Cache_Backend_Mongo, _getCollection){ zval *mongo_collection = NULL, *options, *mongo = NULL, *server; zval *database, *collection, *mongo_database; zend_class_entry *ce0; PHALCON_MM_GROW(); PHALCON_OBS_VAR(mongo_collection); phalcon_read_property_this(&mongo_collection, this_ptr, SL("_collection"), PH_NOISY_CC); if (Z_TYPE_P(mongo_collection) != IS_OBJECT) { PHALCON_OBS_VAR(options); phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY_CC); /** * If mongo is defined a valid Mongo object must be passed */ if (phalcon_array_isset_string(options, SS("mongo"))) { PHALCON_OBS_VAR(mongo); phalcon_array_fetch_string(&mongo, options, SL("mongo"), PH_NOISY); if (Z_TYPE_P(mongo) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The 'mongo' parameter must be a valid Mongo instance"); return; } } else { /** * Server must be defined otherwise */ PHALCON_OBS_VAR(server); phalcon_array_fetch_string(&server, options, SL("server"), PH_NOISY); if (Z_TYPE_P(server) != IS_STRING) { PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB connection string"); return; } ce0 = zend_fetch_class(SL("Mongo"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_NVAR(mongo); object_init_ex(mongo, ce0); if (phalcon_has_constructor(mongo TSRMLS_CC)) { phalcon_call_method_p1_noret(mongo, "__construct", server); } } /** * Check if the database name is a string */ PHALCON_OBS_VAR(database); phalcon_array_fetch_string(&database, options, SL("db"), PH_NOISY); if (Z_TYPE_P(database) != IS_STRING) { PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB db"); return; } /** * Retrieve the connection name */ PHALCON_OBS_VAR(collection); phalcon_array_fetch_string(&collection, options, SL("collection"), PH_NOISY); if (Z_TYPE_P(collection) != IS_STRING) { PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB collection"); return; } /** * Make the connection and get the collection */ PHALCON_INIT_VAR(mongo_database); phalcon_call_method_p1(mongo_database, mongo, "selectdb", database); PHALCON_INIT_NVAR(mongo_collection); phalcon_call_method_p1(mongo_collection, mongo_database, "selectcollection", collection); } RETURN_CCTOR(mongo_collection); }
/** * Listens for notifications from the models manager * * @param string $type * @param Phalcon\Mvc\ModelInterface $model */ PHP_METHOD(Phalcon_Mvc_Model_Behavior_SoftDelete, notify){ zval *type, *model, *options, *skip, *value, *field, *actual_value; zval *update_model, *status, *messages, *message = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &type, &model); if (PHALCON_IS_STRING(type, "beforeDelete")) { PHALCON_INIT_VAR(options); phalcon_call_method(options, this_ptr, "getoptions"); if (!phalcon_array_isset_string(options, SS("value"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The option 'value' is required"); return; } if (!phalcon_array_isset_string(options, SS("field"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The option 'field' is required"); return; } PHALCON_INIT_VAR(skip); ZVAL_BOOL(skip, 1); /** * Skip the current operation */ phalcon_call_method_p1_noret(model, "skipoperation", skip); /** * 'value' is the value to be updated instead of delete the record */ PHALCON_OBS_VAR(value); phalcon_array_fetch_string(&value, options, SL("value"), PH_NOISY_CC); /** * 'field' is the attribute to be updated instead of delete the record */ PHALCON_OBS_VAR(field); phalcon_array_fetch_string(&field, options, SL("field"), PH_NOISY_CC); PHALCON_INIT_VAR(actual_value); phalcon_call_method_p1(actual_value, model, "readattribute", field); /** * If the record is already flagged as 'deleted' we don't delete it again */ if (!PHALCON_IS_EQUAL(actual_value, value)) { /** * Clone the current model to make a clean new operation */ PHALCON_INIT_VAR(update_model); if (phalcon_clone(update_model, model TSRMLS_CC) == FAILURE) { return; } phalcon_call_method_p2_noret(update_model, "writeattribute", field, value); /** * Update the cloned model */ PHALCON_INIT_VAR(status); phalcon_call_method(status, update_model, "save"); if (!zend_is_true(status)) { /** * Transfer the messages from the cloned model to the original model */ PHALCON_INIT_VAR(messages); phalcon_call_method(messages, update_model, "getmessages"); phalcon_is_iterable(messages, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(message); phalcon_call_method_p1_noret(model, "appendmessage", message); zend_hash_move_forward_ex(ah0, &hp0); } RETURN_MM_FALSE; } /** * Update the original model too */ phalcon_call_method_p2_noret(model, "writeattribute", field, value); } } PHALCON_MM_RESTORE(); }
/** * Returns a slice of the resultset to show in the pagination * * @return stdClass */ PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){ zval *one, *zero, *show, *config, *items, *page_number = NULL; zval *n, *page, *last_show_page, *start; zval *possible_pages = NULL, *total_pages, *page_items; zval *i, *valid = NULL, *current = NULL, *maximum_pages, *next = NULL, *additional_page; zval *before = NULL, *remainder, *pages_total = NULL; zval *r0 = NULL; PHALCON_MM_GROW(); PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_INIT_VAR(zero); ZVAL_LONG(zero, 0); PHALCON_OBS_VAR(show); phalcon_read_property_this(&show, this_ptr, SL("_limitRows"), PH_NOISY_CC); PHALCON_OBS_VAR(config); phalcon_read_property_this(&config, this_ptr, SL("_config"), PH_NOISY_CC); PHALCON_OBS_VAR(items); phalcon_array_fetch_string(&items, config, SL("data"), PH_NOISY); PHALCON_OBS_VAR(page_number); phalcon_read_property_this(&page_number, this_ptr, SL("_page"), PH_NOISY_CC); if (Z_TYPE_P(page_number) == IS_NULL) { PHALCON_CPY_WRT(page_number, one); } if (PHALCON_LT(show, zero)) { PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "The start page number is zero or less"); return; } PHALCON_INIT_VAR(n); phalcon_fast_count(n, items TSRMLS_CC); PHALCON_INIT_VAR(page); object_init(page); PHALCON_INIT_VAR(last_show_page); sub_function(last_show_page, page_number, one TSRMLS_CC); PHALCON_INIT_VAR(start); mul_function(start, show, last_show_page TSRMLS_CC); PHALCON_INIT_VAR(possible_pages); div_function(possible_pages, n, show TSRMLS_CC); PHALCON_INIT_VAR(total_pages); phalcon_call_func_p1(total_pages, "ceil", possible_pages); if (Z_TYPE_P(items) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "Invalid data for paginator"); return; } if (!zend_is_true(page_number)) { PHALCON_CPY_WRT(page_number, one); } PHALCON_INIT_VAR(page_items); array_init(page_items); if (PHALCON_GT(n, zero)) { /** * Seek to the desired position */ if (PHALCON_LE(start, n)) { phalcon_call_method_p1_noret(items, "seek", start); } else { phalcon_call_method_p1_noret(items, "seek", one); PHALCON_CPY_WRT(page_number, one); } /** * The record must be iterable */ PHALCON_INIT_VAR(i); ZVAL_LONG(i, 1); while (1) { PHALCON_INIT_NVAR(r0); phalcon_call_method(r0, items, "valid"); PHALCON_CPY_WRT(valid, r0); if (PHALCON_IS_NOT_FALSE(valid)) { } else { break; } PHALCON_INIT_NVAR(current); phalcon_call_method(current, items, "current"); phalcon_array_append(&page_items, current, PH_SEPARATE); if (PHALCON_GE(i, show)) { break; } phalcon_increment(i); } } phalcon_update_property_zval(page, SL("items"), page_items TSRMLS_CC); PHALCON_INIT_VAR(maximum_pages); phalcon_add_function(maximum_pages, start, show TSRMLS_CC); if (PHALCON_LT(maximum_pages, n)) { PHALCON_INIT_VAR(next); phalcon_add_function(next, page_number, one TSRMLS_CC); } else { if (PHALCON_IS_EQUAL(maximum_pages, n)) { PHALCON_CPY_WRT(next, n); } else { div_function(possible_pages, n, show TSRMLS_CC); PHALCON_INIT_VAR(additional_page); phalcon_add_function(additional_page, possible_pages, one TSRMLS_CC); PHALCON_INIT_NVAR(next); ZVAL_LONG(next, phalcon_get_intval(additional_page)); } } if (PHALCON_GT(next, total_pages)) { PHALCON_CPY_WRT(next, total_pages); } phalcon_update_property_zval(page, SL("next"), next TSRMLS_CC); if (PHALCON_GT(page_number, one)) { PHALCON_INIT_VAR(before); sub_function(before, page_number, one TSRMLS_CC); } else { PHALCON_CPY_WRT(before, one); } phalcon_update_property_zval(page, SL("first"), one TSRMLS_CC); phalcon_update_property_zval(page, SL("before"), before TSRMLS_CC); phalcon_update_property_zval(page, SL("current"), page_number TSRMLS_CC); PHALCON_INIT_VAR(remainder); mod_function(remainder, n, show TSRMLS_CC); PHALCON_INIT_NVAR(possible_pages); div_function(possible_pages, n, show TSRMLS_CC); if (!PHALCON_IS_LONG(remainder, 0)) { PHALCON_INIT_NVAR(next); phalcon_add_function(next, possible_pages, one TSRMLS_CC); PHALCON_INIT_VAR(pages_total); ZVAL_LONG(pages_total, phalcon_get_intval(next)); } else { PHALCON_CPY_WRT(pages_total, possible_pages); } phalcon_update_property_zval(page, SL("last"), pages_total TSRMLS_CC); phalcon_update_property_zval(page, SL("total_pages"), pages_total TSRMLS_CC); phalcon_update_property_zval(page, SL("total_items"), n TSRMLS_CC); RETURN_CTOR(page); }
/** * Attach a listener to the events manager * * @param string $eventType * @param object|callable $handler * @param int $priority */ PHP_METHOD(Phalcon_Events_Manager, attach){ zval *event_type, *handler, *priority = NULL, *events = NULL; zval *enable_priorities, *priority_queue = NULL; zval *mode; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 1, &event_type, &handler, &priority); if (!priority) { PHALCON_INIT_VAR(priority); ZVAL_LONG(priority, 100); } if (unlikely(Z_TYPE_P(event_type) != IS_STRING)) { PHALCON_THROW_EXCEPTION_STR(phalcon_events_exception_ce, "Event type must be a string"); return; } if (unlikely(Z_TYPE_P(handler) != IS_OBJECT)) { PHALCON_THROW_EXCEPTION_STR(phalcon_events_exception_ce, "Event handler must be an Object"); return; } PHALCON_OBS_VAR(events); phalcon_read_property_this(&events, this_ptr, SL("_events"), PH_NOISY_CC); if (Z_TYPE_P(events) != IS_ARRAY) { PHALCON_INIT_NVAR(events); array_init(events); } if (!phalcon_array_isset(events, event_type)) { PHALCON_OBS_VAR(enable_priorities); phalcon_read_property_this(&enable_priorities, this_ptr, SL("_enablePriorities"), PH_NOISY_CC); if (zend_is_true(enable_priorities)) { /** * Create a SplPriorityQueue to store the events with priorities */ PHALCON_INIT_VAR(priority_queue); object_init_ex(priority_queue, spl_ce_SplPriorityQueue); if (phalcon_has_constructor(priority_queue TSRMLS_CC)) { phalcon_call_method_noret(priority_queue, "__construct"); } /** * Extract only the Data */ PHALCON_INIT_VAR(mode); ZVAL_LONG(mode, 1); /** * Set extraction flags */ phalcon_call_method_p1_noret(priority_queue, "setextractflags", mode); /** * Append the events to the queue */ phalcon_array_update_zval(&events, event_type, &priority_queue, PH_COPY | PH_SEPARATE); phalcon_update_property_this(this_ptr, SL("_events"), events TSRMLS_CC); } else { PHALCON_INIT_NVAR(priority_queue); array_init(priority_queue); } } else { /** * Get the current SplPriorityQueue */ PHALCON_OBS_NVAR(priority_queue); phalcon_array_fetch(&priority_queue, events, event_type, PH_NOISY); } /** * Insert the handler in the queue */ if (unlikely(Z_TYPE_P(priority_queue) == IS_OBJECT)) { phalcon_call_method_p2_noret(priority_queue, "insert", handler, priority); } else { phalcon_array_append(&priority_queue, handler, PH_SEPARATE); /** * Append the events to the queue */ phalcon_array_update_zval(&events, event_type, &priority_queue, PH_COPY | PH_SEPARATE); phalcon_update_property_this(this_ptr, SL("_events"), events TSRMLS_CC); } PHALCON_MM_RESTORE(); }