/** * Makes the work of autoload registered classes * * @param string $className * @return boolean */ PHP_METHOD(Phalcon_Loader, autoLoad){ zval *class_name = NULL, *events_manager = NULL, *classes = NULL; zval *file_path = NULL, *extensions = NULL, *namespaces = NULL, *ds = NULL; zval *directory = NULL, *preffix = NULL, *file_name = NULL, *extension = NULL; zval *path = NULL; zval *c0 = NULL, *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL, *c5 = NULL, *c6 = NULL; zval *c7 = NULL, *c8 = NULL; zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL; zval *r7 = NULL, *r8 = NULL; zval *t0 = NULL; HashTable *ah0, *ah1, *ah2, *ah3; HashPosition hp0, hp1, hp2, hp3; zval **hd; char *hash_index; uint hash_index_len; ulong hash_num; int hash_type; int eval_int; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &class_name) == FAILURE) { PHALCON_MM_RESTORE(); RETURN_NULL(); } PHALCON_INIT_VAR(events_manager); phalcon_read_property(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC); if (zend_is_true(events_manager)) { PHALCON_INIT_VAR(c0); ZVAL_STRING(c0, "loader:beforeCheckClass", 1); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", c0, this_ptr, PH_NO_CHECK); } PHALCON_INIT_VAR(classes); phalcon_read_property(&classes, this_ptr, SL("_classes"), PH_NOISY_CC); eval_int = phalcon_array_isset(classes, class_name); if (eval_int) { PHALCON_INIT_VAR(file_path); phalcon_array_fetch(&file_path, classes, class_name, PH_NOISY_CC); if (zend_is_true(events_manager)) { phalcon_update_property_zval(this_ptr, SL("_foundPath"), file_path TSRMLS_CC); PHALCON_INIT_VAR(c1); ZVAL_STRING(c1, "loader:pathFound", 1); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", c1, this_ptr, PH_NO_CHECK); } if (phalcon_require(file_path TSRMLS_CC) == FAILURE) { return; } PHALCON_MM_RESTORE(); RETURN_TRUE; } PHALCON_INIT_VAR(extensions); phalcon_read_property(&extensions, this_ptr, SL("_extensions"), PH_NOISY_CC); PHALCON_INIT_VAR(namespaces); phalcon_read_property(&namespaces, this_ptr, SL("_namespaces"), PH_NOISY_CC); PHALCON_ALLOC_ZVAL_MM(r0); phalcon_fast_count(r0, namespaces TSRMLS_CC); if (zend_is_true(r0)) { PHALCON_INIT_VAR(ds); zend_get_constant(SL("DIRECTORY_SEPARATOR"), ds TSRMLS_CC); if (!phalcon_valid_foreach(namespaces TSRMLS_CC)) { return; } ah0 = Z_ARRVAL_P(namespaces); zend_hash_internal_pointer_reset_ex(ah0, &hp0); fes_0c08_0: if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){ goto fee_0c08_0; } PHALCON_INIT_VAR(preffix); PHALCON_GET_FOREACH_KEY(preffix, ah0, hp0); PHALCON_INIT_VAR(directory); ZVAL_ZVAL(directory, *hd, 1, 0); PHALCON_INIT_VAR(r1); PHALCON_CALL_FUNC_PARAMS_1(r1, "strlen", class_name); PHALCON_INIT_VAR(r2); PHALCON_CALL_FUNC_PARAMS_1(r2, "strlen", preffix); PHALCON_INIT_VAR(r3); is_smaller_function(r3, r2, r1 TSRMLS_CC); if (zend_is_true(r3)) { PHALCON_INIT_VAR(c2); ZVAL_LONG(c2, 0); PHALCON_INIT_VAR(r4); PHALCON_CALL_FUNC_PARAMS_1(r4, "strlen", preffix); PHALCON_INIT_VAR(r5); PHALCON_CALL_FUNC_PARAMS_3(r5, "substr", class_name, c2, r4); PHALCON_INIT_VAR(r6); is_equal_function(r6, r5, preffix TSRMLS_CC); if (zend_is_true(r6)) { PHALCON_INIT_VAR(r7); PHALCON_CONCAT_VS(r7, preffix, "\\"); PHALCON_INIT_VAR(c3); ZVAL_STRING(c3, "", 1); PHALCON_INIT_VAR(file_name); phalcon_fast_str_replace(file_name, r7, c3, class_name TSRMLS_CC); if (zend_is_true(file_name)) { if (!phalcon_valid_foreach(extensions TSRMLS_CC)) { return; } ah1 = Z_ARRVAL_P(extensions); zend_hash_internal_pointer_reset_ex(ah1, &hp1); fes_0c08_1: if(zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) != SUCCESS){ goto fee_0c08_1; } PHALCON_INIT_VAR(extension); ZVAL_ZVAL(extension, *hd, 1, 0); PHALCON_INIT_VAR(c4); ZVAL_STRING(c4, "\\", 1); PHALCON_INIT_VAR(r8); PHALCON_CONCAT_VVSV(r8, directory, file_name, ".", extension); PHALCON_INIT_VAR(path); phalcon_fast_str_replace(path, c4, ds, r8 TSRMLS_CC); if (zend_is_true(events_manager)) { phalcon_update_property_zval(this_ptr, SL("_checkedPath"), path TSRMLS_CC); PHALCON_INIT_VAR(c5); ZVAL_STRING(c5, "loader:beforeCheckPath", 1); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", c5, this_ptr, PH_NO_CHECK); } if (phalcon_file_exists(path TSRMLS_CC) == SUCCESS) { if (zend_is_true(events_manager)) { phalcon_update_property_zval(this_ptr, SL("_foundPath"), file_path TSRMLS_CC); PHALCON_INIT_VAR(c6); ZVAL_STRING(c6, "loader:pathFound", 1); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", c6, this_ptr, PH_NO_CHECK); } if (phalcon_require(path TSRMLS_CC) == FAILURE) { return; } PHALCON_MM_RESTORE(); RETURN_TRUE; } zend_hash_move_forward_ex(ah1, &hp1); goto fes_0c08_1; fee_0c08_1: if(0){} } } } zend_hash_move_forward_ex(ah0, &hp0); goto fes_0c08_0; fee_0c08_0: if(0){} } PHALCON_ALLOC_ZVAL_MM(t0); phalcon_read_property(&t0, this_ptr, SL("_directories"), PH_NOISY_CC); if (!phalcon_valid_foreach(t0 TSRMLS_CC)) { return; } ah2 = Z_ARRVAL_P(t0); zend_hash_internal_pointer_reset_ex(ah2, &hp2); fes_0c08_2: if(zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) != SUCCESS){ goto fee_0c08_2; } PHALCON_INIT_VAR(directory); ZVAL_ZVAL(directory, *hd, 1, 0); if (!phalcon_valid_foreach(extensions TSRMLS_CC)) { return; } ah3 = Z_ARRVAL_P(extensions); zend_hash_internal_pointer_reset_ex(ah3, &hp3); fes_0c08_3: if(zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) != SUCCESS){ goto fee_0c08_3; } PHALCON_INIT_VAR(extension); ZVAL_ZVAL(extension, *hd, 1, 0); PHALCON_INIT_VAR(path); PHALCON_CONCAT_VVSV(path, directory, class_name, ".", extension); if (zend_is_true(events_manager)) { phalcon_update_property_zval(this_ptr, SL("_checkedPath"), path TSRMLS_CC); PHALCON_INIT_VAR(c7); ZVAL_STRING(c7, "loader:beforeCheckPath", 1); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", c7, this_ptr, PH_NO_CHECK); } if (phalcon_file_exists(path TSRMLS_CC) == SUCCESS) { if (phalcon_require(path TSRMLS_CC) == FAILURE) { return; } PHALCON_MM_RESTORE(); RETURN_TRUE; } zend_hash_move_forward_ex(ah3, &hp3); goto fes_0c08_3; fee_0c08_3: if(0){} zend_hash_move_forward_ex(ah2, &hp2); goto fes_0c08_2; fee_0c08_2: if(0){} if (zend_is_true(events_manager)) { PHALCON_INIT_VAR(c8); ZVAL_STRING(c8, "loader:afterCheckClass", 1); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", c8, this_ptr, PH_NO_CHECK); } RETURN_CCTOR(this_ptr); }
/** * Makes the work of autoload registered classes * * @param string $className * @return boolean */ PHP_METHOD(Phalcon_Loader, autoLoad){ zval *class_name, *events_manager, *event_name = NULL; zval *classes, *file_path = NULL, *extensions, *ds, *namespace_separator; zval *empty_str, *zero, *namespaces, *directory = NULL; zval *prefix = NULL, *prefix_namespace = NULL, *file_name = NULL; zval *fixed_directory = NULL, *extension = NULL, *complete_path = NULL; zval *pseudo_separator, *prefixes, *no_prefix_class = NULL; zval *ds_class_name, *ns_class_name, *directories; HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5; HashPosition hp0, hp1, hp2, hp3, hp4, hp5; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &class_name) == FAILURE) { RETURN_MM_NULL(); } PHALCON_OBS_VAR(events_manager); phalcon_read_property(&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, "loader:beforeCheckClass", 1); PHALCON_CALL_METHOD_PARAMS_3_NORETURN(events_manager, "fire", event_name, this_ptr, class_name); } /** * First we check for static paths for classes */ PHALCON_OBS_VAR(classes); phalcon_read_property(&classes, this_ptr, SL("_classes"), PH_NOISY_CC); if (Z_TYPE_P(classes) == IS_ARRAY) { if (phalcon_array_isset(classes, class_name)) { PHALCON_OBS_VAR(file_path); phalcon_array_fetch(&file_path, classes, class_name, PH_NOISY_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_zval(this_ptr, SL("_foundPath"), file_path TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "loader:pathFound", 1); PHALCON_CALL_METHOD_PARAMS_3_NORETURN(events_manager, "fire", event_name, this_ptr, file_path); } if (phalcon_require(file_path TSRMLS_CC) == FAILURE) { return; } RETURN_MM_TRUE; } } PHALCON_OBS_VAR(extensions); phalcon_read_property(&extensions, this_ptr, SL("_extensions"), PH_NOISY_CC); PHALCON_INIT_VAR(ds); zend_get_constant(SL("DIRECTORY_SEPARATOR"), ds TSRMLS_CC); PHALCON_INIT_VAR(namespace_separator); ZVAL_STRING(namespace_separator, "\\", 1); PHALCON_INIT_VAR(empty_str); ZVAL_STRING(empty_str, "", 1); PHALCON_INIT_VAR(zero); ZVAL_LONG(zero, 0); /** * Checking in namespaces */ PHALCON_OBS_VAR(namespaces); phalcon_read_property(&namespaces, this_ptr, SL("_namespaces"), PH_NOISY_CC); if (Z_TYPE_P(namespaces) == IS_ARRAY) { if (!phalcon_is_iterable(namespaces, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_KEY(prefix, ah0, hp0); PHALCON_GET_FOREACH_VALUE(directory); /** * The class name must start with the current namespace */ if (phalcon_start_with(class_name, prefix, NULL)) { /** * Append the namespace separator to the prefix */ PHALCON_INIT_NVAR(prefix_namespace); PHALCON_CONCAT_VV(prefix_namespace, prefix, namespace_separator); PHALCON_INIT_NVAR(file_name); phalcon_fast_str_replace(file_name, prefix_namespace, empty_str, class_name TSRMLS_CC); if (zend_is_true(file_name)) { /** * Add a trailing directory separator if the user forgot to do that */ PHALCON_INIT_NVAR(fixed_directory); phalcon_fix_path(&fixed_directory, directory, ds TSRMLS_CC); if (!phalcon_is_iterable(extensions, &ah1, &hp1, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(extension); PHALCON_INIT_NVAR(complete_path); PHALCON_CONCAT_VVSV(complete_path, fixed_directory, file_name, ".", extension); PHALCON_INIT_NVAR(file_path); phalcon_fast_str_replace(file_path, namespace_separator, ds, complete_path TSRMLS_CC); /** * Check if a events manager is available */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_zval(this_ptr, SL("_checkedPath"), file_path TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "loader:beforeCheckPath", 1); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", event_name, this_ptr); } /** * This is probably a good path, let's check if the file exist */ if (phalcon_file_exists(file_path TSRMLS_CC) == SUCCESS) { if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_zval(this_ptr, SL("_foundPath"), file_path TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "loader:pathFound", 1); PHALCON_CALL_METHOD_PARAMS_3_NORETURN(events_manager, "fire", event_name, this_ptr, file_path); } /** * Simulate a require */ if (phalcon_require(file_path TSRMLS_CC) == FAILURE) { return; } /** * Return true mean success */ RETURN_MM_TRUE; } zend_hash_move_forward_ex(ah1, &hp1); } } } zend_hash_move_forward_ex(ah0, &hp0); } } PHALCON_INIT_VAR(pseudo_separator); ZVAL_STRING(pseudo_separator, "_", 1); /** * Checking in prefixes */ PHALCON_OBS_VAR(prefixes); phalcon_read_property(&prefixes, this_ptr, SL("_prefixes"), PH_NOISY_CC); if (Z_TYPE_P(prefixes) == IS_ARRAY) { if (!phalcon_is_iterable(prefixes, &ah2, &hp2, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) { PHALCON_GET_FOREACH_KEY(prefix, ah2, hp2); PHALCON_GET_FOREACH_VALUE(directory); /** * The class name starts with the prefix? */ if (phalcon_start_with(class_name, prefix, NULL)) { /** * Remove the prefix from the class */ PHALCON_INIT_NVAR(no_prefix_class); phalcon_fast_str_replace(no_prefix_class, prefix, empty_str, class_name TSRMLS_CC); /** * Change the pseudo-separator '_' by the directory separator */ PHALCON_INIT_NVAR(file_name); phalcon_fast_str_replace(file_name, pseudo_separator, ds, no_prefix_class TSRMLS_CC); if (zend_is_true(file_name)) { /** * Add a trailing directory separator if the user forgot to do that */ PHALCON_INIT_NVAR(fixed_directory); phalcon_fix_path(&fixed_directory, directory, ds TSRMLS_CC); if (!phalcon_is_iterable(extensions, &ah3, &hp3, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(extension); PHALCON_INIT_NVAR(complete_path); PHALCON_CONCAT_VVSV(complete_path, fixed_directory, file_name, ".", extension); PHALCON_INIT_NVAR(file_path); phalcon_fast_str_replace(file_path, namespace_separator, ds, complete_path TSRMLS_CC); if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_zval(this_ptr, SL("_checkedPath"), file_path TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "loader:beforeCheckPath", 1); PHALCON_CALL_METHOD_PARAMS_3_NORETURN(events_manager, "fire", event_name, this_ptr, file_path); } if (phalcon_file_exists(file_path TSRMLS_CC) == SUCCESS) { /** * Call 'pathFound' event */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_zval(this_ptr, SL("_foundPath"), file_path TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "loader:pathFound", 1); PHALCON_CALL_METHOD_PARAMS_3_NORETURN(events_manager, "fire", event_name, this_ptr, file_path); } if (phalcon_require(file_path TSRMLS_CC) == FAILURE) { return; } RETURN_MM_TRUE; } zend_hash_move_forward_ex(ah3, &hp3); } } } zend_hash_move_forward_ex(ah2, &hp2); } } /** * Change the pseudo-separator by the directory separator in the class name */ PHALCON_INIT_VAR(ds_class_name); phalcon_fast_str_replace(ds_class_name, pseudo_separator, ds, class_name TSRMLS_CC); /** * And change the namespace separator by directory separator too */ PHALCON_INIT_VAR(ns_class_name); phalcon_fast_str_replace(ns_class_name, namespace_separator, ds, ds_class_name TSRMLS_CC); /** * Checking in directories */ PHALCON_OBS_VAR(directories); phalcon_read_property(&directories, this_ptr, SL("_directories"), PH_NOISY_CC); if (Z_TYPE_P(directories) == IS_ARRAY) { if (!phalcon_is_iterable(directories, &ah4, &hp4, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(directory); /** * Add a trailing directory separator if the user forgot to do that */ PHALCON_INIT_NVAR(fixed_directory); phalcon_fix_path(&fixed_directory, directory, ds TSRMLS_CC); if (!phalcon_is_iterable(extensions, &ah5, &hp5, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(extension); /** * Create a possible path for the file */ PHALCON_INIT_NVAR(file_path); PHALCON_CONCAT_VVSV(file_path, fixed_directory, ns_class_name, ".", extension); if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_zval(this_ptr, SL("_checkedPath"), file_path TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "loader:beforeCheckPath", 1); PHALCON_CALL_METHOD_PARAMS_3_NORETURN(events_manager, "fire", event_name, this_ptr, file_path); } /** * Check in every directory if the class exists here */ if (phalcon_file_exists(file_path TSRMLS_CC) == SUCCESS) { /** * Call 'pathFound' event */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { phalcon_update_property_zval(this_ptr, SL("_foundPath"), file_path TSRMLS_CC); PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "loader:pathFound", 1); PHALCON_CALL_METHOD_PARAMS_3_NORETURN(events_manager, "fire", event_name, this_ptr, file_path); } /** * Simulate a require */ if (phalcon_require(file_path TSRMLS_CC) == FAILURE) { return; } /** * Return true meaning success */ RETURN_MM_TRUE; } zend_hash_move_forward_ex(ah5, &hp5); } zend_hash_move_forward_ex(ah4, &hp4); } } /** * Call 'afterCheckClass' event */ if (Z_TYPE_P(events_manager) == IS_OBJECT) { PHALCON_INIT_NVAR(event_name); ZVAL_STRING(event_name, "loader:afterCheckClass", 1); PHALCON_CALL_METHOD_PARAMS_3_NORETURN(events_manager, "fire", event_name, this_ptr, class_name); } /** * Cannot find the class return false */ RETURN_MM_FALSE; }