示例#1
0
/**
 * Starts the profile of a SQL sentence
 *
 * @param string $sqlStatement
 * @return Phalcon\Db\Profiler
 */
PHP_METHOD(Phalcon_Db_Profiler, startProfile){

	zval *sql_statement, *active_profile, *micro;
	zval *time;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &sql_statement);
	
	PHALCON_INIT_VAR(active_profile);
	object_init_ex(active_profile, phalcon_db_profiler_item_ce);
	phalcon_call_method_p1_noret(active_profile, "setsqlstatement", sql_statement);
	
	PHALCON_INIT_VAR(micro);
	ZVAL_BOOL(micro, 1);
	
	PHALCON_INIT_VAR(time);
	phalcon_call_func_p1(time, "microtime", micro);
	phalcon_call_method_p1_noret(active_profile, "setinitialtime", time);
	if (phalcon_method_exists_ex(this_ptr, SS("beforestartprofile") TSRMLS_CC) == SUCCESS) {
		phalcon_call_method_p1_noret(this_ptr, "beforestartprofile", active_profile);
	}
	
	phalcon_update_property_this(this_ptr, SL("_activeProfile"), active_profile TSRMLS_CC);
	
	RETURN_THIS();
}
示例#2
0
/**
 * Phalcon\Validation constructor
 *
 * @param array $validators
 */
PHP_METHOD(Phalcon_Validation, __construct){

	zval *validators = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &validators);
	
	if (!validators) {
		PHALCON_INIT_VAR(validators);
	}
	
	if (Z_TYPE_P(validators) != IS_NULL) {
		if (Z_TYPE_P(validators) != IS_ARRAY) { 
			PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "Validators must be an array");
			return;
		}
		phalcon_update_property_this(this_ptr, SL("_validators"), validators TSRMLS_CC);
	}
	
	/** 
	 * Check for an 'initialize' method
	 */
	if (phalcon_method_exists_ex(this_ptr, SS("initialize") TSRMLS_CC) == SUCCESS) {
		phalcon_call_method_noret(this_ptr, "initialize");
	}
	
	PHALCON_MM_RESTORE();
}
示例#3
0
/**
 * Stops the active profile
 *
 * @return Phalcon\Db\Profiler
 */
PHP_METHOD(Phalcon_Db_Profiler, stopProfile){

	zval active_profile = {}, final_time = {}, initial_time = {}, difference = {}, total_seconds = {}, new_total_seconds = {};

	phalcon_read_property(&active_profile, getThis(), SL("_activeProfile"), PH_NOISY);

	PHALCON_CALL_FUNCTIONW(&final_time, "microtime", &PHALCON_GLOBAL(z_true));
	PHALCON_CALL_METHODW(NULL, &active_profile, "setfinaltime", &final_time);

	PHALCON_CALL_METHODW(&initial_time, &active_profile, "getinitialtime");

	phalcon_sub_function(&difference, &final_time, &initial_time);

	phalcon_read_property(&total_seconds, getThis(), SL("_totalSeconds"), PH_NOISY);

	phalcon_add_function(&new_total_seconds, &total_seconds, &difference);

	phalcon_update_property_zval(getThis(), SL("_totalSeconds"), &new_total_seconds);
	phalcon_update_property_array_append(getThis(), SL("_allProfiles"), &active_profile);

	if (phalcon_method_exists_ex(getThis(), SL("afterendprofile")) == SUCCESS) {
		PHALCON_CALL_METHODW(NULL, getThis(), "afterendprofile", &active_profile);
	}

	RETURN_THISW();
}
示例#4
0
文件: form.c 项目: BlueShark/cphalcon
/**
 * Phalcon\Forms\Form constructor
 *
 * @param object $entity
 */
PHP_METHOD(Phalcon_Forms_Form, __construct){

	zval *entity = NULL;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &entity) == FAILURE) {
		RETURN_MM_NULL();
	}

	if (!entity) {
		PHALCON_INIT_VAR(entity);
	}
	
	if (Z_TYPE_P(entity) != IS_NULL) {
		if (Z_TYPE_P(entity) != IS_OBJECT) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_forms_exception_ce, "The base entity is not valid");
			return;
		}
		phalcon_update_property_zval(this_ptr, SL("_entity"), entity TSRMLS_CC);
	}
	
	/** 
	 * Check for an 'initialize' method and call it
	 */
	if (phalcon_method_exists_ex(this_ptr, SS("initialize") TSRMLS_CC) == SUCCESS) {
		PHALCON_CALL_METHOD_NORETURN(this_ptr, "initialize");
	}
	
	PHALCON_MM_RESTORE();
}
示例#5
0
/**
 * Starts the profile of a SQL sentence
 *
 * @param string $sqlStatement
 * @return Phalcon\Db\Profiler
 */
PHP_METHOD(Phalcon_Db_Profiler, startProfile){

	zval *sql_statement, *active_profile, *micro;
	zval *time;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &sql_statement) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_INIT_VAR(active_profile);
	object_init_ex(active_profile, phalcon_db_profiler_item_ce);
	PHALCON_CALL_METHOD_PARAMS_1_NORETURN(active_profile, "setsqlstatement", sql_statement, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(micro);
	ZVAL_BOOL(micro, 1);
	
	PHALCON_INIT_VAR(time);
	PHALCON_CALL_FUNC_PARAMS_1(time, "microtime", micro);
	PHALCON_CALL_METHOD_PARAMS_1_NORETURN(active_profile, "setinitialtime", time, PH_NO_CHECK);
	if (phalcon_method_exists_ex(this_ptr, SS("beforestartprofile") TSRMLS_CC) == SUCCESS) {
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(this_ptr, "beforestartprofile", active_profile, PH_NO_CHECK);
	}
	
	phalcon_update_property_zval(this_ptr, SL("_activeProfile"), active_profile TSRMLS_CC);
	
	RETURN_CTOR(this_ptr);
}
示例#6
0
/**
 * Stops the active profile
 *
 * @return Phalcon\Db\Profiler
 */
PHP_METHOD(Phalcon_Db_Profiler, stopProfile){

	zval *final_time = NULL, *active_profile, *initial_time = NULL;
	zval *difference, *total_seconds, *new_total_seconds;

	PHALCON_MM_GROW();

	PHALCON_CALL_FUNCTION(&final_time, "microtime", PHALCON_GLOBAL(z_true));
	
	active_profile = phalcon_fetch_nproperty_this(this_ptr, SL("_activeProfile"), PH_NOISY TSRMLS_CC);
	PHALCON_CALL_METHOD(NULL, active_profile, "setfinaltime", final_time);
	
	PHALCON_CALL_METHOD(&initial_time, active_profile, "getinitialtime");
	
	PHALCON_INIT_VAR(difference);
	sub_function(difference, final_time, initial_time TSRMLS_CC);
	
	total_seconds = phalcon_fetch_nproperty_this(this_ptr, SL("_totalSeconds"), PH_NOISY TSRMLS_CC);
	
	PHALCON_INIT_VAR(new_total_seconds);
	phalcon_add_function(new_total_seconds, total_seconds, difference 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(NULL, this_ptr, "afterendprofile", active_profile);
	}
	
	RETURN_THIS();
}
示例#7
0
/**
 * Starts the profile of a SQL sentence
 *
 * @param string $sqlStatement
 * @param $sqlVariables
 * @param $sqlBindTypes
 * @return Phalcon\Db\Profiler
 */
PHP_METHOD(Phalcon_Db_Profiler, startProfile){

	zval *sql_statement, *sql_variables = NULL, *sql_bindtypes = NULL, *active_profile;
	zval *time = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 2, &sql_statement, &sql_variables, &sql_bindtypes);
	
	PHALCON_INIT_VAR(active_profile);
	object_init_ex(active_profile, phalcon_db_profiler_item_ce);
	PHALCON_CALL_METHOD(NULL, active_profile, "setsqlstatement", sql_statement);

	if (sql_variables) {
	    PHALCON_CALL_METHOD(NULL, active_profile, "setsqlvariables", sql_variables);
	}

	if (sql_bindtypes) {
	    PHALCON_CALL_METHOD(NULL, active_profile, "setsqlbindtypes", sql_bindtypes);
	}

	PHALCON_CALL_FUNCTION(&time, "microtime", PHALCON_GLOBAL(z_true));
	PHALCON_CALL_METHOD(NULL, active_profile, "setinitialtime", time);

	if (phalcon_method_exists_ex(this_ptr, SS("beforestartprofile") TSRMLS_CC) == SUCCESS) {
		PHALCON_CALL_METHOD(NULL, this_ptr, "beforestartprofile", active_profile);
	}
	
	phalcon_update_property_this(this_ptr, SL("_activeProfile"), active_profile TSRMLS_CC);
	
	RETURN_THIS();
}
示例#8
0
/**
 * Starts the profile of a SQL sentence
 *
 * @param string $sqlStatement
 * @param $sqlVariables
 * @param $sqlBindTypes
 * @return Phalcon\Db\Profiler
 */
PHP_METHOD(Phalcon_Db_Profiler, startProfile){

	zval *sql_statement, *sql_variables = NULL, *sql_bindtypes = NULL, active_profile = {}, time = {};

	phalcon_fetch_params(0, 1, 2, &sql_statement, &sql_variables, &sql_bindtypes);

	object_init_ex(&active_profile, phalcon_db_profiler_item_ce);
	PHALCON_CALL_METHODW(NULL, &active_profile, "setsqlstatement", sql_statement);

	if (sql_variables) {
	    PHALCON_CALL_METHODW(NULL, &active_profile, "setsqlvariables", sql_variables);
	}

	if (sql_bindtypes) {
	    PHALCON_CALL_METHODW(NULL, &active_profile, "setsqlbindtypes", sql_bindtypes);
	}

	PHALCON_CALL_FUNCTIONW(&time, "microtime", &PHALCON_GLOBAL(z_true));
	PHALCON_CALL_METHODW(NULL, &active_profile, "setinitialtime", &time);

	if (phalcon_method_exists_ex(getThis(), SL("beforestartprofile")) == SUCCESS) {
		PHALCON_CALL_METHODW(NULL, getThis(), "beforestartprofile", &active_profile);
	}

	phalcon_update_property_zval(getThis(), SL("_activeProfile"), &active_profile);

	RETURN_THISW();
}
示例#9
0
/**
 * Read the model's column map, this can't be infered
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param Phalcon\DiInterface $dependencyInjector
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Introspection, getColumnMaps) {

    zval *model, *dependency_injector, *ordered_column_map = NULL;
    zval *reversed_column_map = NULL, *user_column_map;
    zval *user_name = NULL, *name = NULL, *model_column_map;
    HashTable *ah0;
    HashPosition hp0;
    zval **hd;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 2, 0, &model, &dependency_injector);

    PHALCON_INIT_VAR(ordered_column_map);

    PHALCON_INIT_VAR(reversed_column_map);

    /**
     * Check for a columnMap() method on the model
     */
    if (phalcon_method_exists_ex(model, SS("columnmap") TSRMLS_CC) == SUCCESS) {

        PHALCON_INIT_VAR(user_column_map);
        phalcon_call_method(user_column_map, model, "columnmap");
        if (Z_TYPE_P(user_column_map) != IS_ARRAY) {
            PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "columnMap() not returned an array");
            return;
        }

        array_init(reversed_column_map);
        PHALCON_CPY_WRT(ordered_column_map, user_column_map);

        phalcon_is_iterable(user_column_map, &ah0, &hp0, 0, 0);

        while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

            PHALCON_GET_HKEY(name, ah0, hp0);
            PHALCON_GET_HVALUE(user_name);

            phalcon_array_update_zval(&reversed_column_map, user_name, &name, PH_COPY | PH_SEPARATE);

            zend_hash_move_forward_ex(ah0, &hp0);
        }

    }

    /**
     * Store the column map
     */
    PHALCON_INIT_VAR(model_column_map);
    array_init(model_column_map);
    phalcon_array_update_long(&model_column_map, 0, &ordered_column_map, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_column_map, 1, &reversed_column_map, PH_COPY | PH_SEPARATE);

    RETURN_CTOR(model_column_map);
}
示例#10
0
/**
 * Stops the active profile
 *
 * @access public
 */
PHP_METHOD(Phalcon_Db_Profiler, stopProfile){

	zval *final_time = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL;
	zval *c0 = NULL;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL, *t4 = NULL, *t5 = NULL;

	PHALCON_MM_GROW();
	PHALCON_ALLOC_ZVAL_MM(r0);
	PHALCON_INIT_VAR(c0);
	ZVAL_BOOL(c0, 1);
	PHALCON_CALL_FUNC_PARAMS_1(r0, "microtime", c0, 0x034);
	PHALCON_CPY_WRT(final_time, r0);
	
	PHALCON_ALLOC_ZVAL_MM(t0);
	phalcon_read_property(&t0, this_ptr, "_activeProfile", sizeof("_activeProfile")-1, PHALCON_NOISY TSRMLS_CC);
	PHALCON_CALL_METHOD_PARAMS_1_NORETURN(t0, "setfinaltime", final_time, PHALCON_NO_CHECK);
	
	PHALCON_ALLOC_ZVAL_MM(t1);
	phalcon_read_property(&t1, this_ptr, "_totalSeconds", sizeof("_totalSeconds")-1, PHALCON_NOISY TSRMLS_CC);
	
	PHALCON_ALLOC_ZVAL_MM(r1);
	
	PHALCON_ALLOC_ZVAL_MM(t2);
	phalcon_read_property(&t2, this_ptr, "_activeProfile", sizeof("_activeProfile")-1, PHALCON_NOISY TSRMLS_CC);
	PHALCON_CALL_METHOD(r1, t2, "getinitialtime", PHALCON_NO_CHECK);
	
	PHALCON_ALLOC_ZVAL_MM(r2);
	sub_function(r2, final_time, r1 TSRMLS_CC);
	
	PHALCON_ALLOC_ZVAL_MM(r3);
	phalcon_add_function(r3, t1, r2 TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, "_totalSeconds", strlen("_totalSeconds"), r3 TSRMLS_CC);
	
	PHALCON_ALLOC_ZVAL_MM(t3);
	phalcon_read_property(&t3, this_ptr, "_activeProfile", sizeof("_activeProfile")-1, PHALCON_NOISY TSRMLS_CC);
	
	PHALCON_ALLOC_ZVAL_MM(t4);
	phalcon_read_property(&t4, this_ptr, "_allProfiles", sizeof("_allProfiles")-1, PHALCON_NOISY TSRMLS_CC);
	phalcon_array_append(&t4, t3, PHALCON_NO_SEPARATE_THX TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, "_allProfiles", strlen("_allProfiles"), t4 TSRMLS_CC);
	if (phalcon_method_exists_ex(this_ptr, "afterendprofile", strlen("afterendprofile") TSRMLS_CC) == SUCCESS) {
		PHALCON_ALLOC_ZVAL_MM(t5);
		phalcon_read_property(&t5, this_ptr, "_activeProfile", sizeof("_activeProfile")-1, PHALCON_NOISY TSRMLS_CC);
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(this_ptr, "afterendprofile", t5, PHALCON_NO_CHECK);
	}
	
	PHALCON_MM_RESTORE();
}
示例#11
0
/**
 * Phalcon\Mvc\Router\Group constructor
 *
 * @param array $paths
 */
PHP_METHOD(Phalcon_Mvc_Router_Group, __construct){

	zval *paths = NULL;

	phalcon_fetch_params(0, 0, 1, &paths);

	if (!paths) {
		paths = &PHALCON_GLOBAL(z_null);
	}

	if (Z_TYPE_P(paths) == IS_ARRAY || Z_TYPE_P(paths) == IS_STRING) {
		phalcon_update_property_zval(getThis(), SL("_paths"), paths);
	}

	if (phalcon_method_exists_ex(getThis(), SL("initialize")) == SUCCESS) {
		PHALCON_CALL_METHODW(NULL, getThis(), "initialize", paths);
	}
}
示例#12
0
/**
 * Initializes a model in the models manager
 *
 * @param Phalcon\Mvc\CollectionInterface $model
 */
PHP_METHOD(Phalcon_Mvc_Collection_Manager, initialize){

	zval *model, *class_name, *initialized, *events_manager;
	zval *event_name;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &model);
	
	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model, 1 TSRMLS_CC);
	
	PHALCON_OBS_VAR(initialized);
	phalcon_read_property_this(&initialized, this_ptr, SL("_initialized"), PH_NOISY_CC);
	
	/** 
	 * Models are just initialized once per request
	 */
	if (!phalcon_array_isset(initialized, class_name)) {
	
		/** 
		 * Call the 'initialize' method if it's implemented
		 */
		if (phalcon_method_exists_ex(model, SS("initialize") TSRMLS_CC) == SUCCESS) {
			PHALCON_CALL_METHOD_NORETURN(model, "initialize");
		}
	
		/** 
		 * If an EventsManager is available we pass to it every initialized model
		 */
		PHALCON_OBS_VAR(events_manager);
		phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
		if (Z_TYPE_P(events_manager) == IS_OBJECT) {
			PHALCON_INIT_VAR(event_name);
			ZVAL_STRING(event_name, "collectionManager:afterInitialize", 1);
			PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", event_name, this_ptr);
		}
	
		phalcon_update_property_array(this_ptr, SL("_initialized"), class_name, model TSRMLS_CC);
		phalcon_update_property_this(this_ptr, SL("_lastInitialized"), model TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
示例#13
0
/**
 * Initializes a model in the model manager
 *
 * @param Phalcon\Mvc\Model $model
 */
PHP_METHOD(Phalcon_Mvc_Model_Manager, initialize){

	zval *model = NULL, *class_name = NULL, *initialized = NULL, *events_manager = NULL;
	zval *event_name = NULL;
	zval *t0 = NULL;
	int eval_int;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &model) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model TSRMLS_CC);
	
	PHALCON_INIT_VAR(initialized);
	phalcon_read_property(&initialized, this_ptr, SL("_initialized"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(initialized, class_name);
	if (!eval_int) {
		if (phalcon_method_exists_ex(model, SL("initialize") TSRMLS_CC) == SUCCESS) {
			PHALCON_CALL_METHOD_NORETURN(model, "initialize", PH_NO_CHECK);
		}
		
		PHALCON_ALLOC_ZVAL_MM(t0);
		phalcon_read_property(&t0, this_ptr, SL("_initialized"), PH_NOISY_CC);
		phalcon_array_update_zval_bool(&t0, class_name, 1, 0 TSRMLS_CC);
		phalcon_update_property_zval(this_ptr, SL("_initialized"), t0 TSRMLS_CC);
		phalcon_update_property_zval(this_ptr, SL("_lastInitialized"), model TSRMLS_CC);
		
		PHALCON_INIT_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, "modelsManager:afterInitialize", 1);
			PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", event_name, this_ptr, PH_NO_CHECK);
		}
	}
	
	PHALCON_MM_RESTORE();
}
示例#14
0
/**
 * 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;
	zval *t0 = NULL;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(micro);
	ZVAL_BOOL(micro, 1);
	
	PHALCON_INIT_VAR(final_time);
	PHALCON_CALL_FUNC_PARAMS_1(final_time, "microtime", micro);
	
	PHALCON_INIT_VAR(active_profile);
	phalcon_read_property(&active_profile, this_ptr, SL("_activeProfile"), PH_NOISY_CC);
	PHALCON_CALL_METHOD_PARAMS_1_NORETURN(active_profile, "setfinaltime", final_time, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(initial_time);
	PHALCON_CALL_METHOD(initial_time, active_profile, "getinitialtime", PH_NO_CHECK);
	
	PHALCON_INIT_VAR(diference);
	sub_function(diference, final_time, initial_time TSRMLS_CC);
	
	PHALCON_INIT_VAR(total_seconds);
	phalcon_read_property(&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_zval(this_ptr, SL("_totalSeconds"), new_total_seconds TSRMLS_CC);
	
	PHALCON_INIT_VAR(t0);
	phalcon_read_property(&t0, this_ptr, SL("_allProfiles"), PH_NOISY_CC);
	phalcon_array_append(&t0, active_profile, 0 TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_allProfiles"), t0 TSRMLS_CC);
	if (phalcon_method_exists_ex(this_ptr, SS("afterendprofile") TSRMLS_CC) == SUCCESS) {
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(this_ptr, "afterendprofile", active_profile, PH_NO_CHECK);
	}
	
	
	RETURN_CTOR(this_ptr);
}
示例#15
0
/**
 * Starts the profile of a SQL sentence
 *
 * @param string $sqlStatement
 */
PHP_METHOD(Phalcon_Db_Profiler, startProfile){

	zval *sql_statement = NULL;
	zval *i0 = NULL;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL;
	zval *r0 = NULL;
	zval *c0 = NULL;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &sql_statement) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_ALLOC_ZVAL_MM(i0);
	object_init_ex(i0, phalcon_db_profiler_item_ce);
	PHALCON_CALL_METHOD_NORETURN(i0, "__construct", PHALCON_CHECK);
	phalcon_update_property_zval(this_ptr, "_activeProfile", strlen("_activeProfile"), i0 TSRMLS_CC);
	
	PHALCON_ALLOC_ZVAL_MM(t0);
	phalcon_read_property(&t0, this_ptr, "_activeProfile", sizeof("_activeProfile")-1, PHALCON_NOISY TSRMLS_CC);
	PHALCON_CALL_METHOD_PARAMS_1_NORETURN(t0, "setsqlstatement", sql_statement, PHALCON_NO_CHECK);
	
	PHALCON_ALLOC_ZVAL_MM(t1);
	phalcon_read_property(&t1, this_ptr, "_activeProfile", sizeof("_activeProfile")-1, PHALCON_NOISY TSRMLS_CC);
	
	PHALCON_ALLOC_ZVAL_MM(r0);
	
	PHALCON_INIT_VAR(c0);
	ZVAL_BOOL(c0, 1);
	PHALCON_CALL_FUNC_PARAMS_1(r0, "microtime", c0, 0x034);
	PHALCON_CALL_METHOD_PARAMS_1_NORETURN(t1, "setinitialtime", r0, PHALCON_NO_CHECK);
	if (phalcon_method_exists_ex(this_ptr, "beforestartprofile", strlen("beforestartprofile") TSRMLS_CC) == SUCCESS) {
		PHALCON_ALLOC_ZVAL_MM(t2);
		phalcon_read_property(&t2, this_ptr, "_activeProfile", sizeof("_activeProfile")-1, PHALCON_NOISY TSRMLS_CC);
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(this_ptr, "beforestartprofile", t2, PHALCON_NO_CHECK);
	}
	
	PHALCON_MM_RESTORE();
}
示例#16
0
/**
 * 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();
}
示例#17
0
/**
 * Phalcon\Mvc\Router\Group constructor
 *
 * @param array $paths
 */
PHP_METHOD(Phalcon_Mvc_Router_Group, __construct){

	zval *paths = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &paths);
	
	if (!paths) {
		PHALCON_INIT_VAR(paths);
	}
	
	if (Z_TYPE_P(paths) != IS_ARRAY) { 
		if (Z_TYPE_P(paths) != IS_STRING) {
			RETURN_MM_NULL();
		}
	}
	phalcon_update_property_this(this_ptr, SL("_paths"), paths TSRMLS_CC);
	if (phalcon_method_exists_ex(this_ptr, SS("initialize") TSRMLS_CC) == SUCCESS) {
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(this_ptr, "initialize", paths);
	}
	
	PHALCON_MM_RESTORE();
}
示例#18
0
/**
 * Gets the a value to validate in the array/object data source
 *
 * @param string $attribute
 * @return mixed
 */
PHP_METHOD(Phalcon_Validation, getValue){

	zval *attribute, *entity, *method, *value = NULL, *data, *values;
	zval *filters, *field_filters, *service_name;
	zval *dependency_injector = NULL, *filter_service;
	zval *filtered;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &attribute);
	
	PHALCON_OBS_VAR(entity);
	phalcon_read_property_this(&entity, this_ptr, SL("_entity"), PH_NOISY_CC);
	
	/** 
	 * If the entity is an object use it to retrieve the values
	 */
	if (Z_TYPE_P(entity) == IS_OBJECT) {
	
		PHALCON_INIT_VAR(method);
		PHALCON_CONCAT_SV(method, "get", attribute);
		if (phalcon_method_exists(entity, method TSRMLS_CC) == SUCCESS) {
			PHALCON_INIT_VAR(value);
			phalcon_call_method_zval(value, entity, method);
		} else {
			if (phalcon_method_exists_ex(entity, SS("readattribute") TSRMLS_CC) == SUCCESS) {
				PHALCON_INIT_NVAR(value);
				phalcon_call_method(value, entity, "readattribute");
			} else {
				if (phalcon_isset_property_zval(entity, attribute TSRMLS_CC)) {
					PHALCON_OBS_NVAR(value);
					phalcon_read_property_zval(&value, entity, attribute, PH_NOISY_CC);
				} else {
					PHALCON_INIT_NVAR(value);
				}
			}
		}
	
		RETURN_CCTOR(value);
	}
	
	PHALCON_OBS_VAR(data);
	phalcon_read_property_this(&data, this_ptr, SL("_data"), PH_NOISY_CC);
	if (Z_TYPE_P(data) != IS_ARRAY) { 
		if (Z_TYPE_P(data) != IS_OBJECT) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "There is no data to validate");
			return;
		}
	}
	
	/** 
	 * Check if there is a calculated value
	 */
	PHALCON_OBS_VAR(values);
	phalcon_read_property_this(&values, this_ptr, SL("_values"), PH_NOISY_CC);
	if (phalcon_array_isset(values, attribute)) {
		PHALCON_OBS_NVAR(value);
		phalcon_array_fetch(&value, values, attribute, PH_NOISY_CC);
		RETURN_CCTOR(value);
	}
	
	PHALCON_INIT_NVAR(value);
	
	if (Z_TYPE_P(data) == IS_ARRAY) { 
		if (phalcon_array_isset(data, attribute)) {
			PHALCON_OBS_NVAR(value);
			phalcon_array_fetch(&value, data, attribute, PH_NOISY_CC);
		}
	} else {
		if (Z_TYPE_P(data) == IS_OBJECT) {
			if (phalcon_isset_property_zval(data, attribute TSRMLS_CC)) {
				PHALCON_OBS_NVAR(value);
				phalcon_read_property_zval(&value, data, attribute, PH_NOISY_CC);
			}
		}
	}
	
	if (Z_TYPE_P(value) != IS_NULL) {
	
		PHALCON_OBS_VAR(filters);
		phalcon_read_property_this(&filters, this_ptr, SL("_filters"), PH_NOISY_CC);
		if (Z_TYPE_P(filters) == IS_ARRAY) { 
			if (phalcon_array_isset(filters, attribute)) {
	
				PHALCON_OBS_VAR(field_filters);
				phalcon_array_fetch(&field_filters, filters, attribute, PH_NOISY_CC);
				if (zend_is_true(field_filters)) {
	
					PHALCON_INIT_VAR(service_name);
					ZVAL_STRING(service_name, "filter", 1);
	
					PHALCON_INIT_VAR(dependency_injector);
					phalcon_call_method(dependency_injector, this_ptr, "getdi");
					if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
	
						PHALCON_INIT_NVAR(dependency_injector);
						PHALCON_CALL_STATIC(dependency_injector, "phalcon\\di", "getdefault");
	
						if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
							PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "A dependency injector is required to obtain the 'filter' service");
							return;
						}
					}
	
					PHALCON_INIT_VAR(filter_service);
					phalcon_call_method_p1(filter_service, dependency_injector, "getshared", service_name);
					if (Z_TYPE_P(filter_service) != IS_OBJECT) {
						PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "Returned 'filter' service is invalid");
						return;
					}
	
					PHALCON_INIT_VAR(filtered);
					phalcon_call_method_p2(filtered, filter_service, "sanitize", value, field_filters);
	
					RETURN_CCTOR(filtered);
				}
			}
		}
	
		/** 
		 * Cache the calculated value
		 */
		phalcon_update_property_array(this_ptr, SL("_values"), attribute, value TSRMLS_CC);
	
		RETURN_CCTOR(value);
	}
	
	RETURN_MM_NULL();
}
示例#19
0
/**
 * Initialize the metadata for certain table
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param string $key
 * @param string $table
 * @param string $schema
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, _initialize){

	zval *model, *key, *table, *schema, *class_name = NULL, *meta_data = NULL;
	zval *data = NULL, *table_metadata = NULL, *exception_message = NULL;
	zval *connection, *exists, *complete_table = NULL, *columns;
	zval *attributes, *primary_keys, *non_primary_keys;
	zval *numeric_typed, *not_null, *field_types;
	zval *field_bind_types, *automatic_default;
	zval *identity_field = NULL, *column = NULL, *field_name = NULL, *feature = NULL;
	zval *type = NULL, *bind_type = NULL, *column_map = NULL, *ordered_column_map = NULL;
	zval *reversed_column_map = NULL, *user_column_map;
	zval *user_name = NULL, *name = NULL, *model_column_map;
	zval *t0 = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	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, "zzzz", &model, &key, &table, &schema) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model TSRMLS_CC);
	if (Z_TYPE_P(key) != IS_NULL) {
		PHALCON_INIT_VAR(meta_data);
		phalcon_read_property(&meta_data, this_ptr, SL("_metaData"), PH_NOISY_CC);
		eval_int = phalcon_array_isset(meta_data, key);
		if (!eval_int) {
			/** 
			 * The meta-data is read from the adapter always
			 */
			PHALCON_INIT_VAR(data);
			PHALCON_CALL_METHOD_PARAMS_1(data, this_ptr, "read", key, PH_NO_CHECK);
			if (Z_TYPE_P(data) != IS_NULL) {
				if (Z_TYPE_P(meta_data) != IS_ARRAY) { 
					PHALCON_INIT_NVAR(meta_data);
					array_init(meta_data);
				}
				phalcon_array_update_zval(&meta_data, key, &data, PH_COPY | PH_SEPARATE TSRMLS_CC);
				phalcon_update_property_zval(this_ptr, SL("_metaData"), meta_data TSRMLS_CC);
			} else {
				/** 
				 * Check if there is a method 'metaData' in the model and retrieve data from it
				 */
				if (phalcon_method_exists_ex(model, SS("metadata") TSRMLS_CC) == SUCCESS) {
					PHALCON_INIT_VAR(table_metadata);
					PHALCON_CALL_METHOD(table_metadata, model, "metadata", PH_NO_CHECK);
					if (Z_TYPE_P(table_metadata) != IS_ARRAY) { 
						PHALCON_INIT_VAR(exception_message);
						PHALCON_CONCAT_SV(exception_message, "Invalid meta-data for model ", class_name);
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
				} else {
					/** 
					 * Check if the mapped table exists on the database
					 */
					PHALCON_INIT_VAR(connection);
					PHALCON_CALL_METHOD(connection, model, "getconnection", PH_NO_CHECK);
	
					PHALCON_INIT_VAR(exists);
					PHALCON_CALL_METHOD_PARAMS_2(exists, connection, "tableexists", table, schema, PH_NO_CHECK);
					if (!zend_is_true(exists)) {
						if (zend_is_true(schema)) {
							PHALCON_INIT_VAR(complete_table);
							PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
						} else {
							PHALCON_CPY_WRT(complete_table, table);
						}
	
						PHALCON_INIT_NVAR(exception_message);
						PHALCON_CONCAT_SVSV(exception_message, "Table \"", complete_table, "\" doesn't exist on database when dumping meta-data for ", class_name);
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
	
					/** 
					 * Try to describe the table
					 */
					PHALCON_INIT_VAR(columns);
					PHALCON_CALL_METHOD_PARAMS_2(columns, connection, "describecolumns", table, schema, PH_NO_CHECK);
					if (!phalcon_fast_count_ev(columns TSRMLS_CC)) {
						if (zend_is_true(schema)) {
							PHALCON_INIT_NVAR(complete_table);
							PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
						} else {
							PHALCON_CPY_WRT(complete_table, table);
						}
	
						PHALCON_INIT_NVAR(exception_message);
						PHALCON_CONCAT_SVSV(exception_message, "Cannot obtain table columns for the mapped source \"", complete_table, "\" used in model ", class_name);
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
	
					/** 
					 * Initialize meta-data
					 */
					PHALCON_INIT_VAR(attributes);
					array_init(attributes);
	
					PHALCON_INIT_VAR(primary_keys);
					array_init(primary_keys);
	
					PHALCON_INIT_VAR(non_primary_keys);
					array_init(non_primary_keys);
	
					PHALCON_INIT_VAR(numeric_typed);
					array_init(numeric_typed);
	
					PHALCON_INIT_VAR(not_null);
					array_init(not_null);
	
					PHALCON_INIT_VAR(field_types);
					array_init(field_types);
	
					PHALCON_INIT_VAR(field_bind_types);
					array_init(field_bind_types);
	
					PHALCON_INIT_VAR(automatic_default);
					array_init(automatic_default);
	
					PHALCON_INIT_VAR(identity_field);
					ZVAL_BOOL(identity_field, 0);
	
					if (!phalcon_valid_foreach(columns TSRMLS_CC)) {
						return;
					}
	
					ah0 = Z_ARRVAL_P(columns);
					zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	
					ph_cycle_start_0:
	
						if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
							goto ph_cycle_end_0;
						}
	
						PHALCON_GET_FOREACH_VALUE(column);
	
						PHALCON_INIT_NVAR(field_name);
						PHALCON_CALL_METHOD(field_name, column, "getname", PH_NO_CHECK);
						phalcon_array_append(&attributes, field_name, PH_SEPARATE TSRMLS_CC);
	
						PHALCON_INIT_NVAR(feature);
						PHALCON_CALL_METHOD(feature, column, "isprimary", PH_NO_CHECK);
						if (PHALCON_IS_TRUE(feature)) {
							phalcon_array_append(&primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
						} else {
							phalcon_array_append(&non_primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
						}
	
						PHALCON_INIT_NVAR(feature);
						PHALCON_CALL_METHOD(feature, column, "isnumeric", PH_NO_CHECK);
						if (PHALCON_IS_TRUE(feature)) {
							phalcon_array_update_zval_bool(&numeric_typed, field_name, 1, PH_SEPARATE TSRMLS_CC);
						}
	
						PHALCON_INIT_NVAR(feature);
						PHALCON_CALL_METHOD(feature, column, "isnotnull", PH_NO_CHECK);
						if (PHALCON_IS_TRUE(feature)) {
							phalcon_array_append(&not_null, field_name, PH_SEPARATE TSRMLS_CC);
						}
	
						PHALCON_INIT_NVAR(feature);
						PHALCON_CALL_METHOD(feature, column, "isautoincrement", PH_NO_CHECK);
						if (PHALCON_IS_TRUE(feature)) {
							PHALCON_CPY_WRT(identity_field, field_name);
						}
	
						PHALCON_INIT_NVAR(type);
						PHALCON_CALL_METHOD(type, column, "gettype", PH_NO_CHECK);
						phalcon_array_update_zval(&field_types, field_name, &type, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
						PHALCON_INIT_NVAR(bind_type);
						PHALCON_CALL_METHOD(bind_type, column, "getbindtype", PH_NO_CHECK);
						phalcon_array_update_zval(&field_bind_types, field_name, &bind_type, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
						zend_hash_move_forward_ex(ah0, &hp0);
						goto ph_cycle_start_0;
	
					ph_cycle_end_0:
	
					/** 
					 * Create an array using the MODELS_* constants as indexes
					 */
					PHALCON_INIT_NVAR(table_metadata);
					array_init(table_metadata);
					phalcon_array_update_long(&table_metadata, 0, &attributes, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 3, &not_null, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 4, &field_types, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 9, &field_bind_types, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 10, &automatic_default, PH_COPY | PH_SEPARATE TSRMLS_CC);
					phalcon_array_update_long(&table_metadata, 11, &automatic_default, PH_COPY | PH_SEPARATE TSRMLS_CC);
				}
	
				PHALCON_INIT_VAR(t0);
				phalcon_read_property(&t0, this_ptr, SL("_metaData"), PH_NOISY_CC);
				phalcon_array_update_zval(&t0, key, &table_metadata, PH_COPY TSRMLS_CC);
				phalcon_update_property_zval(this_ptr, SL("_metaData"), t0 TSRMLS_CC);
				PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "write", key, table_metadata, PH_NO_CHECK);
			}
		}
	}
	
	/** 
	 * Check for a column map, store in _columnMap in order and reversed order
	 */
	PHALCON_INIT_NVAR(class_name);
	phalcon_get_class(class_name, model TSRMLS_CC);
	
	PHALCON_INIT_VAR(column_map);
	phalcon_read_property(&column_map, this_ptr, SL("_columnMap"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(column_map, class_name);
	if (!eval_int) {
		if (Z_TYPE_P(column_map) != IS_ARRAY) { 
			PHALCON_INIT_NVAR(column_map);
			array_init(column_map);
		}
	
		PHALCON_INIT_NVAR(data);
		PHALCON_CALL_METHOD_PARAMS_1(data, this_ptr, "read", class_name, PH_NO_CHECK);
		if (Z_TYPE_P(data) != IS_NULL) {
			phalcon_array_update_zval(&column_map, class_name, &data, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_columnMap"), column_map TSRMLS_CC);
		} else {
			PHALCON_INIT_VAR(ordered_column_map);
	
			PHALCON_INIT_VAR(reversed_column_map);
	
			/** 
			 * Check for a columnMap() method on the model
			 */
			if (phalcon_method_exists_ex(model, SS("columnmap") TSRMLS_CC) == SUCCESS) {
				PHALCON_INIT_VAR(user_column_map);
				PHALCON_CALL_METHOD(user_column_map, model, "columnmap", PH_NO_CHECK);
				if (Z_TYPE_P(user_column_map) != IS_ARRAY) { 
					PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "columnMap() not returned an array");
					return;
				}
	
				array_init(reversed_column_map);
				PHALCON_CPY_WRT(ordered_column_map, user_column_map);
	
				if (!phalcon_valid_foreach(user_column_map TSRMLS_CC)) {
					return;
				}
	
				ah1 = Z_ARRVAL_P(user_column_map);
				zend_hash_internal_pointer_reset_ex(ah1, &hp1);
	
				ph_cycle_start_1:
	
					if (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) != SUCCESS) {
						goto ph_cycle_end_1;
					}
	
					PHALCON_GET_FOREACH_KEY(name, ah1, hp1);
					PHALCON_GET_FOREACH_VALUE(user_name);
	
					phalcon_array_update_zval(&reversed_column_map, user_name, &name, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
					zend_hash_move_forward_ex(ah1, &hp1);
					goto ph_cycle_start_1;
	
				ph_cycle_end_1:
				if(0){}
	
			}
	
			PHALCON_INIT_VAR(model_column_map);
			array_init(model_column_map);
			phalcon_array_update_long(&model_column_map, 0, &ordered_column_map, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&model_column_map, 1, &reversed_column_map, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_zval(&column_map, class_name, &model_column_map, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_columnMap"), column_map TSRMLS_CC);
			PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "write", class_name, model_column_map, PH_NO_CHECK);
		}
	}
	
	PHALCON_MM_RESTORE();
}
示例#20
0
/**
 * Initialize the metadata for certain table
 *
 * @param Phalcon\Mvc\Model $model
 * @param string $key
 * @param string $table
 * @param string $schema
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, _initializeMetaData){

	zval *model, *key, *table, *schema, *meta_data, *data;
	zval *table_metadata = NULL, *class_name = NULL, *exception_message = NULL;
	zval *connection, *exists, *complete_table = NULL, *attributes;
	zval *primary_keys, *non_primary_keys, *numeric_typed;
	zval *not_null, *field_types, *field_bind_types;
	zval *automatic_default, *identity_field = NULL;
	zval *columns, *column = NULL, *field_name = NULL, *feature = NULL, *type = NULL;
	zval *bind_type = NULL;
	zval *t0 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzz", &model, &key, &table, &schema) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_INIT_VAR(meta_data);
	phalcon_read_property(&meta_data, this_ptr, SL("_metaData"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(meta_data, key);
	if (!eval_int) {
		PHALCON_INIT_VAR(data);
		PHALCON_CALL_METHOD_PARAMS_1(data, this_ptr, "read", key, PH_NO_CHECK);
		if (Z_TYPE_P(data) != IS_NULL) {
			phalcon_array_update_zval(&meta_data, key, &data, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_metaData"), meta_data TSRMLS_CC);
			PHALCON_MM_RESTORE();
			RETURN_NULL();
		}
		
		if (phalcon_method_exists_ex(model, SS("metadata") TSRMLS_CC) == SUCCESS) {
			PHALCON_INIT_VAR(table_metadata);
			PHALCON_CALL_METHOD(table_metadata, model, "metadata", PH_NO_CHECK);
			if (Z_TYPE_P(table_metadata) != IS_ARRAY) { 
				PHALCON_INIT_VAR(class_name);
				phalcon_get_class(class_name, model TSRMLS_CC);
				
				PHALCON_INIT_VAR(exception_message);
				PHALCON_CONCAT_SV(exception_message, "Invalid meta-data for model ", class_name);
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
				return;
			}
		} else {
			PHALCON_INIT_VAR(connection);
			PHALCON_CALL_METHOD(connection, model, "getconnection", PH_NO_CHECK);
			
			PHALCON_INIT_VAR(exists);
			PHALCON_CALL_METHOD_PARAMS_2(exists, connection, "tableexists", table, schema, PH_NO_CHECK);
			if (!zend_is_true(exists)) {
				if (zend_is_true(schema)) {
					PHALCON_INIT_VAR(complete_table);
					PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
				} else {
					PHALCON_CPY_WRT(complete_table, table);
				}
				
				PHALCON_INIT_NVAR(class_name);
				phalcon_get_class(class_name, model TSRMLS_CC);
				
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVSV(exception_message, "Table \"", complete_table, "\" doesn't exist on database when dumping meta-data for ", class_name);
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
				return;
			}
			
			PHALCON_INIT_VAR(attributes);
			array_init(attributes);
			
			PHALCON_INIT_VAR(primary_keys);
			array_init(primary_keys);
			
			PHALCON_INIT_VAR(non_primary_keys);
			array_init(non_primary_keys);
			
			PHALCON_INIT_VAR(numeric_typed);
			array_init(numeric_typed);
			
			PHALCON_INIT_VAR(not_null);
			array_init(not_null);
			
			PHALCON_INIT_VAR(field_types);
			array_init(field_types);
			
			PHALCON_INIT_VAR(field_bind_types);
			array_init(field_bind_types);
			
			PHALCON_INIT_VAR(automatic_default);
			array_init(automatic_default);
			
			PHALCON_INIT_VAR(identity_field);
			ZVAL_BOOL(identity_field, 0);
			
			PHALCON_INIT_VAR(columns);
			PHALCON_CALL_METHOD_PARAMS_2(columns, connection, "describecolumns", table, schema, PH_NO_CHECK);
			if (!phalcon_fast_count_ev(columns TSRMLS_CC)) {
				if (zend_is_true(schema)) {
					PHALCON_INIT_NVAR(complete_table);
					PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
				} else {
					PHALCON_CPY_WRT(complete_table, table);
				}
				
				PHALCON_INIT_NVAR(class_name);
				phalcon_get_class(class_name, model TSRMLS_CC);
				
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVSV(exception_message, "Cannot obtain table columns for the mapped source \"", complete_table, "\" used in model ", class_name);
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
				return;
			}
			
			
			if (!phalcon_valid_foreach(columns TSRMLS_CC)) {
				return;
			}
			
			ah0 = Z_ARRVAL_P(columns);
			zend_hash_internal_pointer_reset_ex(ah0, &hp0);
			
			ph_cycle_start_0:
			
				if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
					goto ph_cycle_end_0;
				}
				
				PHALCON_GET_FOREACH_VALUE(column);
				
				PHALCON_INIT_NVAR(field_name);
				PHALCON_CALL_METHOD(field_name, column, "getname", PH_NO_CHECK);
				phalcon_array_append(&attributes, field_name, PH_SEPARATE TSRMLS_CC);
				
				PHALCON_INIT_NVAR(feature);
				PHALCON_CALL_METHOD(feature, column, "isprimary", PH_NO_CHECK);
				if (PHALCON_IS_TRUE(feature)) {
					phalcon_array_append(&primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
				} else {
					phalcon_array_append(&non_primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
				}
				
				PHALCON_INIT_NVAR(feature);
				PHALCON_CALL_METHOD(feature, column, "isnumeric", PH_NO_CHECK);
				if (PHALCON_IS_TRUE(feature)) {
					phalcon_array_update_zval_bool(&numeric_typed, field_name, 1, PH_SEPARATE TSRMLS_CC);
				}
				
				PHALCON_INIT_NVAR(feature);
				PHALCON_CALL_METHOD(feature, column, "isnotnull", PH_NO_CHECK);
				if (PHALCON_IS_TRUE(feature)) {
					phalcon_array_append(&not_null, field_name, PH_SEPARATE TSRMLS_CC);
				}
				
				PHALCON_INIT_NVAR(feature);
				PHALCON_CALL_METHOD(feature, column, "isautoincrement", PH_NO_CHECK);
				if (PHALCON_IS_TRUE(feature)) {
					PHALCON_CPY_WRT(identity_field, field_name);
				}
				
				PHALCON_INIT_NVAR(type);
				PHALCON_CALL_METHOD(type, column, "gettype", PH_NO_CHECK);
				phalcon_array_update_zval(&field_types, field_name, &type, PH_COPY | PH_SEPARATE TSRMLS_CC);
				
				PHALCON_INIT_NVAR(bind_type);
				PHALCON_CALL_METHOD(bind_type, column, "getbindtype", PH_NO_CHECK);
				phalcon_array_update_zval(&field_bind_types, field_name, &bind_type, PH_COPY | PH_SEPARATE TSRMLS_CC);
				
				zend_hash_move_forward_ex(ah0, &hp0);
				goto ph_cycle_start_0;
				
			ph_cycle_end_0:
			
			PHALCON_INIT_NVAR(table_metadata);
			array_init(table_metadata);
			phalcon_array_update_long(&table_metadata, 0, &attributes, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 3, &not_null, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 4, &field_types, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 9, &field_bind_types, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 10, &automatic_default, PH_COPY | PH_SEPARATE TSRMLS_CC);
			phalcon_array_update_long(&table_metadata, 11, &automatic_default, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
		
		PHALCON_INIT_VAR(t0);
		phalcon_read_property(&t0, this_ptr, SL("_metaData"), PH_NOISY_CC);
		phalcon_array_update_zval(&t0, key, &table_metadata, PH_COPY TSRMLS_CC);
		phalcon_update_property_zval(this_ptr, SL("_metaData"), t0 TSRMLS_CC);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "write", key, table_metadata, PH_NO_CHECK);
	}
	
	PHALCON_MM_RESTORE();
}
示例#21
0
/**
 * Validate a set of data according to a set of rules
 *
 * @param array|object $data
 * @param object $entity
 * @return Phalcon\Validation\Message\Group
 */
PHP_METHOD(Phalcon_Validation, validate){

	zval *data = NULL, *entity = NULL, *validators, *messages = NULL, *status = NULL;
	zval *cancel_on_fail, *scope = NULL, *attribute = NULL, *validator = NULL;
	zval *must_cancel = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 2, &data, &entity);
	
	if (!data) {
		PHALCON_INIT_VAR(data);
	}
	
	if (!entity) {
		PHALCON_INIT_VAR(entity);
	}
	
	PHALCON_OBS_VAR(validators);
	phalcon_read_property_this(&validators, this_ptr, SL("_validators"), PH_NOISY_CC);
	if (Z_TYPE_P(validators) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "There are no validators to validate");
		return;
	}
	
	/** 
	 * Clear pre-calculated values
	 */
	phalcon_update_property_null(this_ptr, SL("_values") TSRMLS_CC);
	
	/** 
	 * Implicitly creates a Phalcon\Validation\Message\Group object
	 */
	PHALCON_INIT_VAR(messages);
	object_init_ex(messages, phalcon_validation_message_group_ce);
	phalcon_call_method_noret(messages, "__construct");
	
	/** 
	 * Validation classes can implement the 'beforeValidation' callback
	 */
	if (phalcon_method_exists_ex(this_ptr, SS("beforevalidation") TSRMLS_CC) == SUCCESS) {
	
		PHALCON_INIT_VAR(status);
		phalcon_call_method_p3(status, this_ptr, "beforevalidation", data, entity, messages);
		if (PHALCON_IS_FALSE(status)) {
			RETURN_CCTOR(status);
		}
	}
	
	phalcon_update_property_this(this_ptr, SL("_messages"), messages TSRMLS_CC);
	if (Z_TYPE_P(data) == IS_ARRAY) { 
		phalcon_update_property_this(this_ptr, SL("_data"), data TSRMLS_CC);
	} else {
		if (Z_TYPE_P(data) == IS_OBJECT) {
			phalcon_update_property_this(this_ptr, SL("_data"), data TSRMLS_CC);
		}
	}
	
	PHALCON_INIT_VAR(cancel_on_fail);
	ZVAL_STRING(cancel_on_fail, "cancelOnFail", 1);
	
	phalcon_is_iterable(validators, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HVALUE(scope);
	
		if (Z_TYPE_P(scope) != IS_ARRAY) { 
			PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "The validator scope is not valid");
			return;
		}
	
		PHALCON_OBS_NVAR(attribute);
		phalcon_array_fetch_long(&attribute, scope, 0, PH_NOISY_CC);
	
		PHALCON_OBS_NVAR(validator);
		phalcon_array_fetch_long(&validator, scope, 1, PH_NOISY_CC);
		if (Z_TYPE_P(validator) != IS_OBJECT) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "One of the validators is not valid");
			return;
		}
	
		PHALCON_INIT_NVAR(status);
		phalcon_call_method_p2(status, validator, "validate", this_ptr, attribute);
	
		/** 
		 * Check if the validation must be canceled if this validator fails
		 */
		if (PHALCON_IS_FALSE(status)) {
	
			PHALCON_INIT_NVAR(must_cancel);
			phalcon_call_method_p1(must_cancel, validator, "getoption", cancel_on_fail);
			if (zend_is_true(must_cancel)) {
				break;
			}
		}
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	/** 
	 * Get the messages generated by the validators
	 */
	PHALCON_OBS_NVAR(messages);
	phalcon_read_property_this(&messages, this_ptr, SL("_messages"), PH_NOISY_CC);
	if (phalcon_method_exists_ex(this_ptr, SS("aftervalidation") TSRMLS_CC) == SUCCESS) {
		phalcon_call_method_p3_noret(this_ptr, "aftervalidation", data, entity, messages);
	}
	
	RETURN_CCTOR(messages);
}
示例#22
0
/**
 * Read the model's column map, this can't be infered
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param Phalcon\DiInterface $dependencyInjector
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Introspection, getColumnMaps){

	zval *model, *dependency_injector;
	zval *ordered_column_map = NULL, *columns = NULL, *column_name = NULL, *reversed_column_map = NULL;
	zval *user_name = NULL, *name = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &model, &dependency_injector);

	if (!PHALCON_GLOBAL(orm).column_renaming) {
		RETURN_MM_NULL();
	}

	/** 
	 * Check for a columnMap() method on the model
	 */
	if (phalcon_method_exists_ex(model, SS("columnmap") TSRMLS_CC) == SUCCESS) {
	
		PHALCON_CALL_METHOD(&ordered_column_map, model, "columnmap");
		if (Z_TYPE_P(ordered_column_map) != IS_ARRAY) { 
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "columnMap() not returned an array");
			return;
		}

		PHALCON_CALL_METHOD(&columns, model, "getcolumns");

		if (Z_TYPE_P(columns) == IS_ARRAY) {

			phalcon_is_iterable(columns, &ah0, &hp0, 0, 0);	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

				PHALCON_GET_HVALUE(column_name);

				if (!phalcon_array_isset(ordered_column_map, column_name)) {
					phalcon_array_update_zval(&ordered_column_map, column_name, column_name, PH_COPY);
				}
		
				zend_hash_move_forward_ex(ah0, &hp0);
			}
		}

		PHALCON_INIT_VAR(reversed_column_map);
		array_init(reversed_column_map);

		phalcon_is_iterable(ordered_column_map, &ah1, &hp1, 0, 0);	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {

			PHALCON_GET_HKEY(name, ah1, hp1);
			PHALCON_GET_HVALUE(user_name);

			phalcon_array_update_zval(&reversed_column_map, user_name, name, PH_COPY);
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	} else {
		PHALCON_INIT_NVAR(ordered_column_map);
		PHALCON_INIT_VAR(reversed_column_map);
	}
	
	/** 
	 * Store the column map
	 */
	array_init_size(return_value, 2);
	phalcon_array_update_long(&return_value, 0, ordered_column_map, PH_COPY);
	phalcon_array_update_long(&return_value, 1, reversed_column_map, PH_COPY);
	
	PHALCON_MM_RESTORE();
}
示例#23
0
文件: di.c 项目: 11mariom/cphalcon
/**
 * 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, &parameters);
	
	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);
}
示例#24
0
/**
 * Produces an string representation of a variable
 *
 * @param mixed $variable
 * @return string
 */
PHP_METHOD(Phalcon_Debug, _getVarDump){

	zval *variable, *class_name, *dumped_object;
	zval *array_dump = NULL, *dump = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &variable);
	
	if (PHALCON_IS_SCALAR(variable)) {
	
		/** 
		 * Boolean variables are represented as 'true'/'false'
		 */
		if (Z_TYPE_P(variable) == IS_BOOL) {
			if (zend_is_true(variable)) {
				RETURN_MM_STRING("true", 1);
			} else {
				RETURN_MM_STRING("false", 1);
			}
		}
	
		/** 
		 * String variables are escaped to avoid XSS injections
		 */
		if (Z_TYPE_P(variable) == IS_STRING) {
			phalcon_call_method_p1(return_value, this_ptr, "_escapestring", variable);
			RETURN_MM();
		}
	
		/** 
		 * Other scalar variables are just converted to strings
		 */
	
		RETURN_CCTOR(variable);
	}
	
	/** 
	 * If the variable is an object print its class name
	 */
	if (Z_TYPE_P(variable) == IS_OBJECT) {
	
		PHALCON_INIT_VAR(class_name);
		phalcon_get_class(class_name, variable, 0 TSRMLS_CC);
	
		/** 
		 * Try to check for a 'dump' method, this surely produces a better printable
		 * representation
		 */
		if (phalcon_method_exists_ex(variable, SS("dump") TSRMLS_CC) == SUCCESS) {
			PHALCON_INIT_VAR(dumped_object);
			phalcon_call_method(dumped_object, variable, "dump");
	
			/** 
			 * dump() must return an array, generate a recursive representation using
			 * getArrayDump
			 */
			PHALCON_INIT_VAR(array_dump);
			phalcon_call_method_p1(array_dump, this_ptr, "_getarraydump", dumped_object);
	
			PHALCON_INIT_VAR(dump);
			PHALCON_CONCAT_SVSVS(dump, "Object(", class_name, ": ", array_dump, ")");
		} else {
			/** 
			 * If dump() is not available just print the class name
			 */
			PHALCON_INIT_NVAR(dump);
			PHALCON_CONCAT_SVS(dump, "Object(", class_name, ")</span>");
		}
	
		RETURN_CTOR(dump);
	}
	
	/** 
	 * Recursively process the array and enclose it in Array()
	 */
	if (Z_TYPE_P(variable) == IS_ARRAY) { 
		PHALCON_INIT_NVAR(array_dump);
		phalcon_call_method_p1(array_dump, this_ptr, "_getarraydump", variable);
		PHALCON_CONCAT_SVS(return_value, "Array(", array_dump, ")");
		RETURN_MM();
	}
	
	/** 
	 * Null variables are represented as 'null'
	 * Other types are represented by its type
	 */
	RETURN_MM_STRING(zend_zval_type_name(variable), 1);
}
示例#25
0
/**
 * Initialize the metadata for certain table
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param string $key
 * @param string $table
 * @param string $schema
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, _initialize){

	zval *model, *key, *table, *schema, *strategy = NULL, *class_name;
	zval *meta_data = NULL, *prefix_key = NULL, *data = NULL, *model_metadata = NULL;
	zval *exception_message, *dependency_injector;
	zval *key_name, *column_map = NULL, *model_column_map = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 4, 0, &model, &key, &table, &schema);
	
	dependency_injector = phalcon_fetch_nproperty_this(this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_CC);

	PHALCON_INIT_VAR(strategy);
	
	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model, 0 TSRMLS_CC);
	if (Z_TYPE_P(key) != IS_NULL) {
	
		PHALCON_OBS_VAR(meta_data);
		phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY TSRMLS_CC);
		if (!phalcon_array_isset(meta_data, key)) {
	
			PHALCON_INIT_VAR(prefix_key);
			PHALCON_CONCAT_SV(prefix_key, "meta-", key);
	
			/** 
			 * The meta-data is read from the adapter always
			 */
			PHALCON_CALL_METHOD(&data, this_ptr, "read", prefix_key);
			if (Z_TYPE_P(data) != IS_NULL) {
				if (Z_TYPE_P(meta_data) != IS_ARRAY) { 
					PHALCON_INIT_NVAR(meta_data);
					array_init(meta_data);
				}
				phalcon_array_update_zval(&meta_data, key, data, PH_COPY | PH_SEPARATE);
				phalcon_update_property_this(this_ptr, SL("_metaData"), meta_data TSRMLS_CC);
			} else {
				/** 
				 * Check if there is a method 'metaData' in the model to retrieve meta-data from it
				 */
				if (phalcon_method_exists_ex(model, SS("metadata") TSRMLS_CC) == SUCCESS) {
					PHALCON_CALL_METHOD(&model_metadata, model, "metadata");
					if (Z_TYPE_P(model_metadata) != IS_ARRAY) { 
						PHALCON_INIT_VAR(exception_message);
						PHALCON_CONCAT_SV(exception_message, "Invalid meta-data for model ", class_name);
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
				} else {

					/** 
					 * Get the meta-data extraction strategy
					 */
					PHALCON_CALL_METHOD(&strategy, this_ptr, "getstrategy");
	
					/** 
					 * Get the meta-data
					 */
					PHALCON_CALL_METHOD(&model_metadata, strategy, "getmetadata", model, dependency_injector);
				}
	
				/** 
				 * Store the meta-data locally
				 */
				phalcon_update_property_array(this_ptr, SL("_metaData"), key, model_metadata TSRMLS_CC);
	
				/** 
				 * Store the meta-data in the adapter
				 */
				PHALCON_CALL_METHOD(NULL, this_ptr, "write", prefix_key, model_metadata);
			}
		}
	}
	
	/** 
	 * Check for a column map, store in _columnMap in order and reversed order
	 */
	if (!PHALCON_GLOBAL(orm).column_renaming) {
		RETURN_MM_NULL();
	}
	
	PHALCON_INIT_VAR(key_name);
	phalcon_fast_strtolower(key_name, class_name);
	
	PHALCON_OBS_VAR(column_map);
	phalcon_read_property_this(&column_map, this_ptr, SL("_columnMap"), PH_NOISY TSRMLS_CC);
	if (phalcon_array_isset(column_map, key_name)) {
		RETURN_MM_NULL();
	}
	
	if (Z_TYPE_P(column_map) != IS_ARRAY) { 
		PHALCON_INIT_NVAR(column_map);
		array_init(column_map);
	}
	
	/** 
	 * Create the map key name
	 */
	PHALCON_INIT_NVAR(prefix_key);
	PHALCON_CONCAT_SV(prefix_key, "map-", key_name);
	
	/** 
	 * Check if the meta-data is already in the adapter
	 */
	PHALCON_CALL_METHOD(&data, this_ptr, "read", prefix_key);
	if (Z_TYPE_P(data) != IS_NULL) {
		phalcon_array_update_zval(&column_map, key_name, data, PH_COPY | PH_SEPARATE);
		phalcon_update_property_this(this_ptr, SL("_columnMap"), column_map TSRMLS_CC);
		RETURN_MM_NULL();
	}
	
	/** 
	 * Get the meta-data extraction strategy
	 */
	if (Z_TYPE_P(strategy) != IS_OBJECT) {
		PHALCON_CALL_METHOD(&strategy, this_ptr, "getstrategy");
	}
	
	/** 
	 * Get the meta-data
	 */
	PHALCON_CALL_METHOD(&model_column_map, strategy, "getcolumnmaps", model, dependency_injector);
	
	/** 
	 * Update the column map locally
	 */
	phalcon_update_property_array(this_ptr, SL("_columnMap"), key_name, model_column_map TSRMLS_CC);
	
	/** 
	 * Write the data to the adapter
	 */
	PHALCON_CALL_METHOD(NULL, this_ptr, "write", prefix_key, model_column_map);
	
	PHALCON_MM_RESTORE();
}
示例#26
0
/**
 * Dispatches a controller action taking into account the routing parameters
 *
 * @param Phalcon_Request $request
 * @param Phalcon_Response $response
 * @param Phalcon_View $view
 * @param Phalcon_Model_Manager $model
 * @return Phalcon_Controller
 */
PHP_METHOD(Phalcon_Dispatcher, dispatch){

	zval *request = NULL, *response = NULL, *view = NULL, *model = NULL, *controllers_dir = NULL;
	zval *value = NULL, *controller = NULL, *number_dispatches = NULL;
	zval *controller_name = NULL, *controllers = NULL, *controller_class = NULL;
	zval *controller_path = NULL, *params = NULL, *action_name = NULL;
	zval *action_method = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL;
	zval *r7 = NULL, *r8 = NULL, *r9 = NULL, *r10 = NULL, *r11 = NULL, *r12 = NULL;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL, *t4 = NULL, *t5 = NULL, *t6 = NULL;
	zval *t7 = NULL, *t8 = NULL, *t9 = NULL, *t10 = NULL;
	zval *c0 = NULL, *c1 = NULL, *c2 = NULL;
	zval *i0 = NULL;
	zval *a0 = NULL, *a1 = NULL;
	zval *p0[] = { NULL, NULL, NULL, NULL, NULL };
	int eval_int;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|zz", &request, &response, &view, &model) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!view) {
		PHALCON_INIT_VAR(view);
		ZVAL_NULL(view);
	}
	
	if (!model) {
		PHALCON_INIT_VAR(model);
		ZVAL_NULL(model);
	}
	
	PHALCON_ALLOC_ZVAL_MM(r0);
	PHALCON_ALLOC_ZVAL_MM(t0);
	phalcon_read_property(&t0, this_ptr, SL("_basePath"), PHALCON_NOISY TSRMLS_CC);
	PHALCON_ALLOC_ZVAL_MM(t1);
	phalcon_read_property(&t1, this_ptr, SL("_controllersDir"), PHALCON_NOISY TSRMLS_CC);
	PHALCON_CONCAT_VV(r0, t0, t1);
	PHALCON_CPY_WRT(controllers_dir, r0);
	
	PHALCON_INIT_VAR(value);
	ZVAL_NULL(value);
	
	PHALCON_INIT_VAR(controller);
	ZVAL_NULL(controller);
	
	PHALCON_INIT_VAR(number_dispatches);
	ZVAL_LONG(number_dispatches, 0);
	phalcon_update_property_bool(this_ptr, SL("_finished"), 0 TSRMLS_CC);
	ws_e10f_0:
		
		PHALCON_INIT_VAR(t2);
		phalcon_read_property(&t2, this_ptr, SL("_finished"), PHALCON_NOISY TSRMLS_CC);
		if (zend_is_true(t2)) {
			goto we_e10f_0;
		}
		phalcon_update_property_bool(this_ptr, SL("_finished"), 1 TSRMLS_CC);
		
		PHALCON_INIT_VAR(t3);
		phalcon_read_property(&t3, this_ptr, SL("_controllerName"), PHALCON_NOISY TSRMLS_CC);
		PHALCON_CPY_WRT(controller_name, t3);
		if (!zend_is_true(controller_name)) {
			PHALCON_INIT_VAR(t4);
			phalcon_read_property(&t4, this_ptr, SL("_defaultController"), PHALCON_NOISY TSRMLS_CC);
			PHALCON_CPY_WRT(controller_name, t4);
			phalcon_update_property_zval(this_ptr, SL("_controllerName"), controller_name TSRMLS_CC);
		}
		
		PHALCON_INIT_VAR(t5);
		phalcon_read_property(&t5, this_ptr, SL("_controllers"), PHALCON_NOISY TSRMLS_CC);
		PHALCON_CPY_WRT(controllers, t5);
		
		PHALCON_INIT_VAR(r1);
		
		PHALCON_INIT_VAR(r2);
		PHALCON_CALL_STATIC_PARAMS_1(r2, "phalcon_text", "camelize", controller_name);
		PHALCON_CONCAT_VS(r1, r2, "Controller");
		PHALCON_CPY_WRT(controller_class, r1);
		eval_int = phalcon_array_isset(controllers, controller_class);
		if (!eval_int) {
			PHALCON_INIT_VAR(c0);
			ZVAL_BOOL(c0, 0);
			PHALCON_INIT_VAR(r3);
			PHALCON_CALL_FUNC_PARAMS_2(r3, "class_exists", controller_class, c0);
			if (!zend_is_true(r3)) {
				PHALCON_INIT_VAR(r4);
				PHALCON_CONCAT_VVS(r4, controllers_dir, controller_class, ".php");
				PHALCON_CPY_WRT(controller_path, r4);
				if (phalcon_file_exists(controller_path TSRMLS_CC) == SUCCESS) {
					if (phalcon_require(controller_path TSRMLS_CC) == FAILURE) {
						return;
					}
				} else {
					PHALCON_INIT_VAR(r5);
					PHALCON_CONCAT_SVS(r5, "File for controller class ", controller_class, " doesn't exist");
					PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "_throwdispatchexception", response, r5, PHALCON_NO_CHECK);
				}
				
				PHALCON_INIT_VAR(c1);
				ZVAL_BOOL(c1, 0);
				
				PHALCON_INIT_VAR(r6);
				PHALCON_CALL_FUNC_PARAMS_2(r6, "class_exists", controller_class, c1);
				if (!zend_is_true(r6)) {
					PHALCON_INIT_VAR(r7);
					PHALCON_CONCAT_SVS(r7, "Class ", controller_class, " was not found on controller file");
					PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "_throwdispatchexception", response, r7, PHALCON_NO_CHECK);
				}
			}
			ce0 = phalcon_fetch_class(controller_class TSRMLS_CC);
			
			PHALCON_INIT_VAR(i0);
			object_init_ex(i0, ce0);
			p0[0] = this_ptr;
			p0[1] = request;
			p0[2] = response;
			p0[3] = view;
			p0[4] = model;
			PHALCON_CALL_METHOD_PARAMS_NORETURN(i0, "__construct", 5, p0, PHALCON_CHECK);
			PHALCON_CPY_WRT(controller, i0);
			if (phalcon_method_exists_ex(controller, SL("initialize") TSRMLS_CC) == SUCCESS) {
				PHALCON_CALL_METHOD_NORETURN(controller, "initialize", PHALCON_NO_CHECK);
			}
			
			PHALCON_INIT_VAR(t6);
			phalcon_read_property(&t6, this_ptr, SL("_controllers"), PHALCON_NOISY TSRMLS_CC);
			phalcon_array_update(&t6, controller_class, &controller, PHALCON_NO_SEPARATE_THX, PHALCON_COPY, PHALCON_NO_CTOR TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_controllers"), t6 TSRMLS_CC);
		} else {
			PHALCON_INIT_VAR(r8);
			phalcon_array_fetch(&r8, controllers, controller_class, PHALCON_NOISY TSRMLS_CC);
			PHALCON_CPY_WRT(controller, r8);
		}
		
		PHALCON_INIT_VAR(t7);
		phalcon_read_property(&t7, this_ptr, SL("_params"), PHALCON_NOISY TSRMLS_CC);
		PHALCON_CPY_WRT(params, t7);
		
		PHALCON_INIT_VAR(t8);
		phalcon_read_property(&t8, this_ptr, SL("_actionName"), PHALCON_NOISY TSRMLS_CC);
		PHALCON_CPY_WRT(action_name, t8);
		if (!zend_is_true(action_name)) {
			PHALCON_INIT_VAR(t9);
			phalcon_read_property(&t9, this_ptr, SL("_defaultAction"), PHALCON_NOISY TSRMLS_CC);
			PHALCON_CPY_WRT(action_name, t9);
			phalcon_update_property_zval(this_ptr, SL("_actionName"), action_name TSRMLS_CC);
		}
		
		if (phalcon_method_exists_ex(controller, SL("beforedispatch") TSRMLS_CC) == SUCCESS) {
			PHALCON_INIT_VAR(r9);
			PHALCON_CALL_METHOD_PARAMS_3(r9, controller, "beforedispatch", controller_name, action_name, params, PHALCON_NO_CHECK);
			if (Z_TYPE_P(r9) == IS_BOOL && !Z_BVAL_P(r9)) {
				PHALCON_INIT_VAR(value);
				ZVAL_BOOL(value, 0);
				goto we_e10f_0;
			}
		}
		
		PHALCON_INIT_VAR(r10);
		PHALCON_CONCAT_VS(r10, action_name, "Action");
		PHALCON_CPY_WRT(action_method, r10);
		if (phalcon_method_exists(controller, action_method TSRMLS_CC) == SUCCESS) {
			PHALCON_INIT_VAR(a0);
			array_init(a0);
			phalcon_array_append(&a0, controller, PHALCON_SEPARATE_PLZ TSRMLS_CC);
			phalcon_array_append(&a0, action_method, PHALCON_SEPARATE_PLZ TSRMLS_CC);
			PHALCON_INIT_VAR(value);
			PHALCON_CALL_FUNC_PARAMS_2(value, "call_user_func_array", a0, params);
		} else {
			if (phalcon_method_exists_ex(controller, SL("notfoundaction") TSRMLS_CC) == SUCCESS) {
				PHALCON_INIT_VAR(a1);
				array_init(a1);
				phalcon_array_append(&a1, controller, PHALCON_SEPARATE_PLZ TSRMLS_CC);
				add_next_index_stringl(a1, SL("notFoundAction"), 1);
				PHALCON_INIT_VAR(value);
				PHALCON_CALL_FUNC_PARAMS_2(value, "call_user_func_array", a1, params);
			} else {
				PHALCON_INIT_VAR(r11);
				PHALCON_CONCAT_SVSVS(r11, "Action '", action_name, "' was not found on controller '", controller_name, "'");
				PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "_throwdispatchexception", response, r11, PHALCON_NO_CHECK);
			}
		}
		
		if (phalcon_method_exists_ex(controller, SL("afterdispatch") TSRMLS_CC) == SUCCESS) {
			PHALCON_CALL_METHOD_PARAMS_3_NORETURN(controller, "afterdispatch", controller_name, action_name, params, PHALCON_NO_CHECK);
		}
		
		PHALCON_SEPARATE(number_dispatches);
		increment_function(number_dispatches);
		
		PHALCON_INIT_VAR(t10);
		ZVAL_LONG(t10, 256);
		
		PHALCON_INIT_VAR(r12);
		is_smaller_function(r12, t10, number_dispatches TSRMLS_CC);
		if (zend_is_true(r12)) {
			PHALCON_INIT_VAR(c2);
			ZVAL_STRING(c2, "Dispatcher has detected a cyclic routing causing stability problems", 1);
			PHALCON_CALL_METHOD_PARAMS_2_NORETURN(this_ptr, "_throwdispatchexception", response, c2, PHALCON_NO_CHECK);
		}
		goto ws_e10f_0;
	we_e10f_0:
	phalcon_update_property_zval(this_ptr, SL("_returnedValue"), value TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_lastController"), controller TSRMLS_CC);
	
	RETURN_CHECK_CTOR(controller);
}