Пример #1
0
/**
 * Converts strings to camelize style
 *
 * 
 *
 * @param string $str
 * @return string
 */
PHP_METHOD(Phalcon_Text, camelize){

	zval *str = NULL;
	zval *c0 = NULL, *c1 = NULL, *c2 = NULL, *c3 = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL;

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

	PHALCON_INIT_VAR(c0);
	ZVAL_STRING(c0, " ", 1);
	PHALCON_INIT_VAR(c1);
	ZVAL_STRING(c1, "", 1);
	PHALCON_INIT_VAR(c2);
	ZVAL_STRING(c2, "_", 1);
	PHALCON_INIT_VAR(c3);
	ZVAL_STRING(c3, " ", 1);
	PHALCON_ALLOC_ZVAL_MM(r0);
	PHALCON_CALL_FUNC_PARAMS_1(r0, "strtolower", str);
	PHALCON_ALLOC_ZVAL_MM(r1);
	phalcon_fast_str_replace(r1, c2, c3, r0 TSRMLS_CC);
	PHALCON_ALLOC_ZVAL_MM(r2);
	PHALCON_CALL_FUNC_PARAMS_1(r2, "ucwords", r1);
	PHALCON_ALLOC_ZVAL_MM(r3);
	phalcon_fast_str_replace(r3, c0, c1, r2 TSRMLS_CC);
	RETURN_DZVAL(r3);
}
Пример #2
0
/**
 * Applies a format to a message before sent it to the internal log
 *
 * @param string $message
 * @param int $type
 * @param int $timestamp
 * @return string
 */
PHP_METHOD(Phalcon_Logger_Formatter_Line, format) {

    zval *message, *type, *timestamp, *format = NULL, *date_format;
    zval *date, *date_wildcard, *new_format = NULL, *type_string;
    zval *type_wildcard, *message_wildcard, *eol;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 3, 0, &message, &type, &timestamp);

    PHALCON_OBS_VAR(format);
    phalcon_read_property_this(&format, this_ptr, SL("_format"), PH_NOISY_CC);

    /**
     * Check if the format has the %date% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%date%"))) {
        PHALCON_OBS_VAR(date_format);
        phalcon_read_property_this(&date_format, this_ptr, SL("_dateFormat"), PH_NOISY_CC);

        PHALCON_INIT_VAR(date);
        phalcon_call_func_p2(date, "date", date_format, timestamp);

        PHALCON_INIT_VAR(date_wildcard);
        ZVAL_STRING(date_wildcard, "%date%", 1);

        PHALCON_INIT_VAR(new_format);
        phalcon_fast_str_replace(new_format, date_wildcard, date, format);
    } else {
        PHALCON_CPY_WRT(new_format, format);
    }

    /**
     * Check if the format has the %type% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%type%"))) {
        PHALCON_INIT_VAR(type_string);
        phalcon_call_method_p1(type_string, this_ptr, "gettypestring", type);

        PHALCON_INIT_VAR(type_wildcard);
        ZVAL_STRING(type_wildcard, "%type%", 1);

        PHALCON_INIT_NVAR(format);
        phalcon_fast_str_replace(format, type_wildcard, type_string, new_format);
    } else {
        PHALCON_CPY_WRT(format, new_format);
    }

    PHALCON_INIT_VAR(message_wildcard);
    ZVAL_STRING(message_wildcard, "%message%", 1);

    PHALCON_INIT_NVAR(new_format);
    phalcon_fast_str_replace(new_format, message_wildcard, message, format);

    PHALCON_INIT_VAR(eol);
    ZVAL_STRING(eol, PHP_EOL, 1);
    PHALCON_CONCAT_VV(return_value, new_format, eol);

    RETURN_MM();
}
Пример #3
0
/**
 * Applies the internal format to the message
 *
 * @param  string $message
 * @param  int $type
 * @param  int $time
 * @return string
 */
PHP_METHOD(Phalcon_Logger_Adapter_File, _applyFormat){

	zval *message = NULL, *type = NULL, *time = NULL, *format = NULL, *date_format = NULL;
	zval *date = NULL, *new_format = NULL, *type_string = NULL;
	zval *c0 = NULL, *c1 = NULL, *c2 = NULL;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &message, &type, &time) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!time) {
		PHALCON_ALLOC_ZVAL_MM(time);
		ZVAL_LONG(time, 0);
	} else {
		PHALCON_SEPARATE_PARAM(time);
	}
	
	if (!zend_is_true(time)) {
		PHALCON_INIT_VAR(time);
		PHALCON_CALL_FUNC(time, "time");
	}
	
	PHALCON_INIT_VAR(format);
	phalcon_read_property(&format, this_ptr, SL("_format"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(date_format);
	phalcon_read_property(&date_format, this_ptr, SL("_dateFormat"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(date);
	PHALCON_CALL_FUNC_PARAMS_2(date, "date", date_format, time);
	
	PHALCON_INIT_VAR(c0);
	ZVAL_STRING(c0, "%date%", 1);
	
	PHALCON_INIT_VAR(new_format);
	phalcon_fast_str_replace(new_format, c0, date, format TSRMLS_CC);
	
	PHALCON_INIT_VAR(type_string);
	PHALCON_CALL_METHOD_PARAMS_1(type_string, this_ptr, "gettypestring", type, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(c1);
	ZVAL_STRING(c1, "%type%", 1);
	
	PHALCON_INIT_VAR(format);
	phalcon_fast_str_replace(format, c1, type_string, new_format TSRMLS_CC);
	
	PHALCON_INIT_VAR(c2);
	ZVAL_STRING(c2, "%message%", 1);
	
	PHALCON_INIT_VAR(new_format);
	phalcon_fast_str_replace(new_format, c2, message, format TSRMLS_CC);
	
	RETURN_CTOR(new_format);
}
Пример #4
0
/**
 * Escapes a string with htmlentities
 *
 * @param string $value
 * @return string
 */
PHP_METHOD(Phalcon_Debug, _escapeString){

	zval *value, *charset, *replaced_value;

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

	if (Z_TYPE_P(value) == IS_STRING) {
		zval line_break;
		zval escaped_line_break;

		charset = phalcon_fetch_static_property_ce(phalcon_debug_ce, SL("_charset") TSRMLS_CC);

		INIT_ZVAL(line_break);
		ZVAL_STRING(&line_break, "\n", 0);

		INIT_ZVAL(escaped_line_break);
		ZVAL_STRING(&escaped_line_break, "\\n", 0);

		ALLOC_INIT_ZVAL(replaced_value);
		phalcon_fast_str_replace(replaced_value, &line_break, &escaped_line_break, value);
		phalcon_htmlentities(return_value, replaced_value, NULL, charset TSRMLS_CC);
		phalcon_ptr_dtor(&replaced_value);
		return;
	}

	RETURN_ZVAL(value, 1, 0);
}
Пример #5
0
/**
 * Escapes a string with htmlentities
 *
 * @param string $value
 * @return string
 */
PHP_METHOD(Phalcon_Debug, _escapeString){

	zval *value, *charset, *ent_compat, *line_break;
	zval *escaped_line_break, *replaced_value;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &value);
	
	if (Z_TYPE_P(value) == IS_STRING) {
		PHALCON_INIT_VAR(charset);
		ZVAL_STRING(charset, "utf-8", 1);
	
		PHALCON_INIT_VAR(ent_compat);
		ZVAL_LONG(ent_compat, 2);
	
		PHALCON_INIT_VAR(line_break);
		ZVAL_STRING(line_break, "\n", 1);
	
		PHALCON_INIT_VAR(escaped_line_break);
		ZVAL_STRING(escaped_line_break, "\\n", 1);
	
		PHALCON_INIT_VAR(replaced_value);
		phalcon_fast_str_replace(replaced_value, line_break, escaped_line_break, value);
		phalcon_call_func_p3(return_value, "htmlentities", replaced_value, ent_compat, charset);
		RETURN_MM();
	}
	
	RETURN_CCTOR(value);
}
Пример #6
0
/**
 * Converts strings to camelize style
 *
 *<code>
 *	echo Phalcon\Text::camelize('coco_bongo'); //CocoBongo
 *</code>
 *
 * @param string $str
 * @return string
 */
PHP_METHOD(Phalcon_Text, camelize){

	zval *str = NULL, *space = NULL, *lower_str = NULL, *underscore = NULL, *no_underscore_str = NULL;
	zval *dash = NULL, *no_dash_str = NULL, *empty_str = NULL, *uc_string = NULL;
	zval *camelized = NULL;

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

	PHALCON_INIT_VAR(space);
	ZVAL_STRING(space, " ", 1);
	
	PHALCON_INIT_VAR(lower_str);
	PHALCON_CALL_FUNC_PARAMS_1(lower_str, "strtolower", str);
	
	PHALCON_INIT_VAR(underscore);
	ZVAL_STRING(underscore, "_", 1);
	
	PHALCON_INIT_VAR(no_underscore_str);
	phalcon_fast_str_replace(no_underscore_str, underscore, space, lower_str TSRMLS_CC);
	
	PHALCON_INIT_VAR(dash);
	ZVAL_STRING(dash, "-", 1);
	
	PHALCON_INIT_VAR(no_dash_str);
	phalcon_fast_str_replace(no_dash_str, dash, space, no_underscore_str TSRMLS_CC);
	
	PHALCON_INIT_VAR(empty_str);
	ZVAL_STRING(empty_str, "", 1);
	
	PHALCON_INIT_VAR(uc_string);
	PHALCON_CALL_FUNC_PARAMS_1(uc_string, "ucwords", no_dash_str);
	
	PHALCON_INIT_VAR(camelized);
	phalcon_fast_str_replace(camelized, space, empty_str, uc_string TSRMLS_CC);
	
	RETURN_CTOR(camelized);
}
Пример #7
0
/**
 * Appends a message to the validator
 *
 * @param string $message
 * @param string $field
 * @param string $type
 */
PHP_METHOD(Phalcon_Mvc_Model_Validator, appendMessage){

	zval *message = NULL, *field = NULL, *type = NULL, *class_name = NULL, *suffix = NULL;
	zval *empty_string = NULL, *model_message = NULL;
	zval *t0 = NULL;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|zz", &message, &field, &type) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!field) {
		PHALCON_ALLOC_ZVAL_MM(field);
		ZVAL_NULL(field);
	}
	
	if (!type) {
		PHALCON_ALLOC_ZVAL_MM(type);
		ZVAL_NULL(type);
	} else {
		PHALCON_SEPARATE_PARAM(type);
	}
	
	if (!zend_is_true(type)) {
		PHALCON_INIT_VAR(class_name);
		phalcon_get_class(class_name, this_ptr TSRMLS_CC);
		
		PHALCON_INIT_VAR(suffix);
		ZVAL_STRING(suffix, "Validator", 1);
		
		PHALCON_INIT_VAR(empty_string);
		ZVAL_STRING(empty_string, "", 1);
		
		PHALCON_INIT_VAR(type);
		phalcon_fast_str_replace(type, suffix, empty_string, class_name TSRMLS_CC);
	}
	
	PHALCON_INIT_VAR(model_message);
	object_init_ex(model_message, phalcon_mvc_model_message_ce);
	PHALCON_CALL_METHOD_PARAMS_3_NORETURN(model_message, "__construct", message, field, type, PH_CHECK);
	
	PHALCON_ALLOC_ZVAL_MM(t0);
	phalcon_read_property(&t0, this_ptr, SL("_messages"), PH_NOISY_CC);
	phalcon_array_append(&t0, model_message, 0 TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_messages"), t0 TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
Пример #8
0
/**
 * Returns the translation related to the given key
 *
 * @param string $index
 * @param array $placeholders
 * @return string
 */
PHP_METHOD(Phalcon_Translate_Adapter_NativeArray, query){

	zval *index, *placeholders = NULL, *translate, *translation = NULL;
	zval *value = NULL, *key = NULL, *key_placeholder = NULL, *replaced = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &index, &placeholders);
	
	if (!placeholders) {
		PHALCON_INIT_VAR(placeholders);
	}
	
	PHALCON_OBS_VAR(translate);
	phalcon_read_property_this(&translate, this_ptr, SL("_translate"), PH_NOISY_CC);
	if (phalcon_array_isset(translate, index)) {
	
		PHALCON_OBS_VAR(translation);
		phalcon_array_fetch(&translation, translate, index, PH_NOISY);
		if (Z_TYPE_P(placeholders) == IS_ARRAY) { 
			if (phalcon_fast_count_ev(placeholders TSRMLS_CC)) {
	
				phalcon_is_iterable(placeholders, &ah0, &hp0, 0, 0);
	
				while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
					PHALCON_GET_HKEY(key, ah0, hp0);
					PHALCON_GET_HVALUE(value);
	
					PHALCON_INIT_NVAR(key_placeholder);
					PHALCON_CONCAT_SVS(key_placeholder, "%", key, "%");
	
					PHALCON_INIT_NVAR(replaced);
					phalcon_fast_str_replace(replaced, key_placeholder, value, translation);
					PHALCON_CPY_WRT(translation, replaced);
	
					zend_hash_move_forward_ex(ah0, &hp0);
				}
	
			}
		}
	
		RETURN_CCTOR(translation);
	}
	
	RETURN_CCTOR(index);
}
Пример #9
0
/**
 * Returns the translation related to the given key
 *
 * @param string $index
 * @param array $placeholders
 * @param string $domain
 * @return string
 */
PHP_METHOD(Phalcon_Translate_Adapter_Gettext, query){

	zval *index, *placeholders = NULL, *domain = NULL;
	zval *translation, *key = NULL, *value = NULL, *key_placeholder = NULL, *replaced = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *msgstr;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 2, &index, &placeholders, &domain);
	
	if (!domain) {
		msgstr = gettext(Z_STRVAL_P(index));
	} else {
		msgstr = dgettext(Z_STRVAL_P(domain), Z_STRVAL_P(index));
	}

	PHALCON_INIT_VAR(translation);
	ZVAL_STRING(translation, msgstr, 1);

	if (placeholders && Z_TYPE_P(placeholders) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(placeholders))) {
		phalcon_is_iterable(placeholders, &ah0, &hp0, 0, 0);
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
			PHALCON_GET_HKEY(key, ah0, hp0);
			PHALCON_GET_HVALUE(value);

			PHALCON_INIT_NVAR(key_placeholder);
			PHALCON_CONCAT_SVS(key_placeholder, "%", key, "%");

			PHALCON_INIT_NVAR(replaced);
			phalcon_fast_str_replace(replaced, key_placeholder, value, translation);

			PHALCON_CPY_WRT(translation, replaced);

			zend_hash_move_forward_ex(ah0, &hp0);
		}
	}

	RETURN_CTOR(translation);
}
Пример #10
0
/**
 * Appends a message to the validator
 *
 * @param string $message
 * @param string $field
 * @param string $type
 */
PHP_METHOD(Phalcon_Mvc_Model_Validator, appendMessage){

	zval *message, *field = NULL, *type = NULL, *class_name, *suffix;
	zval *empty_string, *model_message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 2, &message, &field, &type);
	
	if (!field) {
		PHALCON_INIT_VAR(field);
	}
	
	if (!type) {
		PHALCON_INIT_VAR(type);
	} else {
		PHALCON_SEPARATE_PARAM(type);
	}
	
	if (!zend_is_true(type)) {
		PHALCON_INIT_VAR(class_name);
		phalcon_get_class(class_name, this_ptr, 0 TSRMLS_CC);
	
		PHALCON_INIT_VAR(suffix);
		ZVAL_STRING(suffix, "Validator", 1);
	
		PHALCON_INIT_VAR(empty_string);
		ZVAL_STRING(empty_string, "", 1);
	
		PHALCON_INIT_NVAR(type);
		phalcon_fast_str_replace(type, suffix, empty_string, class_name TSRMLS_CC);
	}
	
	PHALCON_INIT_VAR(model_message);
	object_init_ex(model_message, phalcon_mvc_model_message_ce);
	phalcon_call_method_p3_noret(model_message, "__construct", message, field, type);
	
	phalcon_update_property_array_append(this_ptr, SL("_messages"), model_message TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
Пример #11
0
/**
 * Makes the work of autoload registered classes
 *
 * @param string $className
 * @return boolean
 */
PHP_METHOD(Phalcon_Loader, autoLoad){

	zval *class_name = NULL, *file_name = NULL, *directory = NULL, *preffix = NULL;
	zval *path = NULL;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL, *t4 = 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;
	zval *c0 = NULL, *c1 = NULL, *c2 = 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, "z", &class_name) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_ALLOC_ZVAL_MM(t0);
	phalcon_read_property(&t0, this_ptr, SL("_classes"), PHALCON_NOISY TSRMLS_CC);
	eval_int = phalcon_array_isset(t0, class_name);
	if (eval_int) {
		PHALCON_ALLOC_ZVAL_MM(t1);
		phalcon_read_property(&t1, this_ptr, SL("_classes"), PHALCON_NOISY TSRMLS_CC);
		PHALCON_ALLOC_ZVAL_MM(r0);
		phalcon_array_fetch(&r0, t1, class_name, PHALCON_NOISY TSRMLS_CC);
		PHALCON_CPY_WRT(file_name, r0);
		if (phalcon_require(file_name TSRMLS_CC) == FAILURE) {
			return;
		}
		PHALCON_MM_RESTORE();
		RETURN_TRUE;
	}
	
	PHALCON_ALLOC_ZVAL_MM(t2);
	phalcon_read_property(&t2, this_ptr, SL("_namespaces"), PHALCON_NOISY TSRMLS_CC);
	if (phalcon_valid_foreach(t2 TSRMLS_CC)) {
		ah0 = Z_ARRVAL_P(t2);
		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;
		} else {
			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);
		is_smaller_function(r2, preffix, r1 TSRMLS_CC);
		if (zend_is_true(r2)) {
			PHALCON_INIT_VAR(c0);
			ZVAL_LONG(c0, 0);
			PHALCON_INIT_VAR(r3);
			PHALCON_CALL_FUNC_PARAMS_1(r3, "strlen", preffix);
			PHALCON_INIT_VAR(r4);
			PHALCON_CALL_FUNC_PARAMS_3(r4, "substr", class_name, c0, r3);
			PHALCON_INIT_VAR(r5);
			is_equal_function(r5, r4, preffix TSRMLS_CC);
			if (zend_is_true(r5)) {
				PHALCON_INIT_VAR(r6);
				PHALCON_CONCAT_VS(r6, preffix, "\\");
				PHALCON_INIT_VAR(c1);
				ZVAL_STRING(c1, "", 1);
				PHALCON_INIT_VAR(r7);
				phalcon_fast_str_replace(r7, r6, c1, class_name TSRMLS_CC);
				PHALCON_CPY_WRT(file_name, r7);
				if (zend_is_true(file_name)) {
					PHALCON_INIT_VAR(c2);
					ZVAL_STRING(c2, "\\", 1);
					PHALCON_INIT_VAR(t3);
					zend_get_constant("DIRECTORY_SEPARATOR", strlen("DIRECTORY_SEPARATOR"), t3 TSRMLS_CC);
					PHALCON_INIT_VAR(r8);
					PHALCON_CONCAT_VVS(r8, directory, file_name, ".php");
					PHALCON_INIT_VAR(r9);
					phalcon_fast_str_replace(r9, c2, t3, r8 TSRMLS_CC);
					PHALCON_CPY_WRT(path, r9);
					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(ah0, &hp0);
		goto fes_0c08_0;
		fee_0c08_0:
		if(0){}
	} else {
		return;
	}
	
	PHALCON_ALLOC_ZVAL_MM(t4);
	phalcon_read_property(&t4, this_ptr, SL("_directories"), PHALCON_NOISY TSRMLS_CC);
	if (phalcon_valid_foreach(t4 TSRMLS_CC)) {
		ah1 = Z_ARRVAL_P(t4);
		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(directory);
		ZVAL_ZVAL(directory, *hd, 1, 0);
		PHALCON_INIT_VAR(r10);
		PHALCON_CONCAT_VVS(r10, directory, class_name, ".php");
		PHALCON_CPY_WRT(path, r10);
		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(ah1, &hp1);
		goto fes_0c08_1;
		fee_0c08_1:
		if(0){}
	} else {
		return;
	}
	
	PHALCON_MM_RESTORE();
}
Пример #12
0
/**
 * Bind params to SQL select
 *
 * @param string $sqlSelect
 * @param array $params
 */
PHP_METHOD(Phalcon_Db_Adapter_Mysql, bindParams){

	zval *sql_select = NULL, *params = NULL, *select = NULL, *id_connection = NULL;
	zval *bind_value = NULL, *index = NULL, *value = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL;
	zval *r7 = NULL;
	zval *t0 = NULL;
	zval *c0 = NULL, *c1 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &sql_select, &params) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_ALLOC_ZVAL_MM(r0);
	phalcon_fast_count(r0, params TSRMLS_CC);
	if (zend_is_true(r0)) {
		PHALCON_CPY_WRT(select, sql_select);
		
		PHALCON_ALLOC_ZVAL_MM(t0);
		phalcon_read_property(&t0, this_ptr, SL("_idConnection"), PHALCON_NOISY TSRMLS_CC);
		PHALCON_CPY_WRT(id_connection, t0);
		if (phalcon_valid_foreach(params TSRMLS_CC)) {
			ah0 = Z_ARRVAL_P(params);
			zend_hash_internal_pointer_reset_ex(ah0, &hp0);
			fes_321f_0:
			if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
				goto fee_321f_0;
			} else {
				PHALCON_INIT_VAR(index);
				PHALCON_GET_FOREACH_KEY(index, ah0, hp0);
			}
			PHALCON_INIT_VAR(bind_value);
			ZVAL_ZVAL(bind_value, *hd, 1, 0);
			PHALCON_INIT_VAR(r1);
			PHALCON_CALL_FUNC_PARAMS_1(r1, "is_numeric", bind_value);
			if (zend_is_true(r1)) {
				PHALCON_CPY_WRT(value, bind_value);
			} else {
				PHALCON_INIT_VAR(r2);
				PHALCON_INIT_VAR(value);
				PHALCON_CALL_FUNC_PARAMS_2(value, "mysqli_real_escape_string", id_connection, bind_value);
				PHALCON_CONCAT_SVS(r2, "!¡", value, "!¡");
				PHALCON_CPY_WRT(value, r2);
			}
			if (Z_TYPE_P(index) == IS_LONG) {
				PHALCON_INIT_VAR(r3);
				PHALCON_CONCAT_SV(r3, "?", index);
				PHALCON_INIT_VAR(r4);
				phalcon_fast_str_replace(r4, r3, value, select TSRMLS_CC);
				PHALCON_CPY_WRT(select, r4);
			} else {
				if (Z_TYPE_P(index) == IS_STRING) {
					PHALCON_INIT_VAR(r5);
					PHALCON_CONCAT_SVS(r5, ":", index, ":");
					PHALCON_INIT_VAR(r6);
					phalcon_fast_str_replace(r6, r5, value, select TSRMLS_CC);
					PHALCON_CPY_WRT(select, r6);
				} else {
					PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid bind parameter");
					return;
				}
			}
			zend_hash_move_forward_ex(ah0, &hp0);
			goto fes_321f_0;
			fee_321f_0:
			if(0){}
		} else {
			return;
		}
		
		PHALCON_INIT_VAR(c0);
		ZVAL_STRING(c0, "!¡", 1);
		
		PHALCON_INIT_VAR(c1);
		ZVAL_STRING(c1, "'", 1);
		
		PHALCON_ALLOC_ZVAL_MM(r7);
		phalcon_fast_str_replace(r7, c0, c1, select TSRMLS_CC);
		RETURN_DZVAL(r7);
	}
	
	RETURN_CHECK_CTOR(sql_select);
}
Пример #13
0
/**
 * 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);
}
Пример #14
0
/**
 * Returns the translation related to the given key
 *
 * @param string $index
 * @param array $placeholders
 * @return string
 */
PHP_METHOD(Phalcon_Translate_Adapter_NativeArray, query){

	zval *index = NULL, *placeholders = NULL, *translate = NULL, *translation = NULL;
	zval *value = NULL, *key = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	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, "zz", &index, &placeholders) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_INIT_VAR(translate);
	phalcon_read_property(&translate, this_ptr, SL("_translate"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(translate, index);
	if (eval_int) {
		PHALCON_INIT_VAR(translation);
		phalcon_array_fetch(&translation, translate, index, PH_NOISY_CC);
		if (zend_is_true(placeholders)) {
			PHALCON_ALLOC_ZVAL_MM(r0);
			phalcon_fast_count(r0, placeholders TSRMLS_CC);
			if (zend_is_true(r0)) {
				if (!phalcon_valid_foreach(placeholders TSRMLS_CC)) {
					return;
				}
				
				ah0 = Z_ARRVAL_P(placeholders);
				zend_hash_internal_pointer_reset_ex(ah0, &hp0);
				fes_101a_0:
					if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
						goto fee_101a_0;
					}
					
					PHALCON_INIT_VAR(key);
					PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
					PHALCON_INIT_VAR(value);
					ZVAL_ZVAL(value, *hd, 1, 0);
					PHALCON_INIT_VAR(r1);
					PHALCON_CONCAT_SVS(r1, "%", key, "%");
					PHALCON_INIT_VAR(r2);
					phalcon_fast_str_replace(r2, r1, value, translation TSRMLS_CC);
					PHALCON_CPY_WRT(translation, r2);
					zend_hash_move_forward_ex(ah0, &hp0);
					goto fes_101a_0;
				fee_101a_0:
				if(0){}
				
			}
		}
		
		
		RETURN_CCTOR(translation);
	}
	
	
	RETURN_CCTOR(index);
}
Пример #15
0
/**
 * Returns the translation related to the given key
 *
 * @param string $index
 * @param array $placeholders
 * @return string
 */
PHP_METHOD(Phalcon_Translate_Adapter_NativeArray, query){

	zval *index, *placeholders = NULL, *translate, *translation = NULL;
	zval *value = NULL, *key = NULL, *key_placeholder = NULL, *replaced = NULL;
	HashTable *ah0;
	HashPosition hp0;
	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|z", &index, &placeholders) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!placeholders) {
		PHALCON_INIT_NVAR(placeholders);
	}
	
	PHALCON_INIT_VAR(translate);
	phalcon_read_property(&translate, this_ptr, SL("_translate"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(translate, index);
	if (eval_int) {
		PHALCON_INIT_VAR(translation);
		phalcon_array_fetch(&translation, translate, index, PH_NOISY_CC);
		if (Z_TYPE_P(placeholders) == IS_ARRAY) { 
			if (phalcon_fast_count_ev(placeholders TSRMLS_CC)) {
				
				if (!phalcon_valid_foreach(placeholders TSRMLS_CC)) {
					return;
				}
				
				ah0 = Z_ARRVAL_P(placeholders);
				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_KEY(key, ah0, hp0);
					PHALCON_GET_FOREACH_VALUE(value);
					
					PHALCON_INIT_NVAR(key_placeholder);
					PHALCON_CONCAT_SVS(key_placeholder, "%", key, "%");
					
					PHALCON_INIT_NVAR(replaced);
					phalcon_fast_str_replace(replaced, key_placeholder, value, translation TSRMLS_CC);
					PHALCON_CPY_WRT(translation, replaced);
					
					zend_hash_move_forward_ex(ah0, &hp0);
					goto ph_cycle_start_0;
					
				ph_cycle_end_0:
				if(0){}
				
			}
		}
		
		
		RETURN_CCTOR(translation);
	}
	
	
	RETURN_CCTOR(index);
}
Пример #16
0
/**
 * Check whether internal resource has rows to fetch
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){

	zval *type = NULL, *result, *row = NULL, *rows, *hydrate_mode, *underscore;
	zval *empty_str, *active_row = NULL, *columns_types;
	zval *dirty_state, *column = NULL, *alias = NULL, *source = NULL, *attributes = NULL;
	zval *column_map = NULL, *row_model = NULL, *attribute = NULL, *column_alias = NULL;
	zval *column_value = NULL, *keep_snapshots = NULL, *instance = NULL;
	zval *value = NULL, *sql_alias = NULL, *n_alias = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(type);
	phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
	if (zend_is_true(type)) {
	
		/** 
		 * The result is bigger than 32 rows so it's retrieved one by one
		 */
		PHALCON_OBS_VAR(result);
		phalcon_read_property_this(&result, this_ptr, SL("_result"), PH_NOISY_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
			PHALCON_INIT_VAR(row);
			PHALCON_CALL_METHOD_PARAMS_1(row, result, "fetch", result);
		} else {
			PHALCON_INIT_NVAR(row);
			ZVAL_BOOL(row, 0);
		}
	} else {
		/** 
		 * The full rows are dumped in this_ptr->rows
		 */
		PHALCON_OBS_VAR(rows);
		phalcon_read_property_this(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
		if (Z_TYPE_P(rows) == IS_ARRAY) { 
	
			PHALCON_INIT_NVAR(row);
			phalcon_array_get_current(row, rows TSRMLS_CC);
			if (Z_TYPE_P(row) == IS_OBJECT) {
				phalcon_array_next(rows);
			}
		} else {
			PHALCON_INIT_NVAR(row);
			ZVAL_BOOL(row, 0);
		}
	}
	
	/** 
	 * Valid records are arrays
	 */
	if (Z_TYPE_P(row) == IS_ARRAY || Z_TYPE_P(row) == IS_OBJECT) {
	
		/** 
		 * The result type=1 so we need to build every row
		 */
		if (zend_is_true(type)) {
	
			/** 
			 * Get current hydration mode
			 */
			PHALCON_OBS_VAR(hydrate_mode);
			phalcon_read_property_this(&hydrate_mode, this_ptr, SL("_hydrateMode"), PH_NOISY_CC);
	
			PHALCON_INIT_VAR(underscore);
			ZVAL_STRING(underscore, "_", 1);
	
			PHALCON_INIT_VAR(empty_str);
			ZVAL_STRING(empty_str, "", 1);
	
			/** 
			 * Each row in a complex result is a Phalcon\Mvc\Model\Row instance
			 */
	
			switch (phalcon_get_intval(hydrate_mode)) {
	
				case 0:
					PHALCON_INIT_VAR(active_row);
					object_init_ex(active_row, phalcon_mvc_model_row_ce);
					break;
	
				case 1:
					PHALCON_INIT_NVAR(active_row);
					array_init(active_row);
					break;
	
				case 2:
					PHALCON_INIT_NVAR(active_row);
					object_init(active_row);
					break;
	
			}
	
			/** 
			 * Create every record according to the column types
			 */
			PHALCON_OBS_VAR(columns_types);
			phalcon_read_property_this(&columns_types, this_ptr, SL("_columnTypes"), PH_NOISY_CC);
	
			/** 
			 * Set records as dirty state PERSISTENT by default
			 */
			PHALCON_INIT_VAR(dirty_state);
			ZVAL_LONG(dirty_state, 0);
	
			if (!phalcon_is_iterable(columns_types, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(alias, ah0, hp0);
				PHALCON_GET_FOREACH_VALUE(column);
	
				PHALCON_OBS_NVAR(type);
				phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY_CC);
				if (PHALCON_IS_STRING(type, "object")) {
	
					/** 
					 * Object columns are assigned column by column
					 */
					PHALCON_OBS_NVAR(source);
					phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY_CC);
	
					PHALCON_OBS_NVAR(attributes);
					phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY_CC);
	
					PHALCON_OBS_NVAR(column_map);
					phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY_CC);
	
					/** 
					 * Assign the values from the _source_attribute notation to its real column name
					 */
					PHALCON_INIT_NVAR(row_model);
					array_init(row_model);
	
					if (!phalcon_is_iterable(attributes, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
						return;
					}
	
					while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
						PHALCON_GET_FOREACH_VALUE(attribute);
	
						/** 
						 * Columns are supposed to be in the form _table_field
						 */
						PHALCON_INIT_NVAR(column_alias);
						PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute);
	
						PHALCON_OBS_NVAR(column_value);
						phalcon_array_fetch(&column_value, row, column_alias, PH_NOISY_CC);
						phalcon_array_update_zval(&row_model, attribute, &column_value, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
						zend_hash_move_forward_ex(ah1, &hp1);
					}
	
					/** 
					 * Generate the column value according to the hydration type
					 */
	
					switch (phalcon_get_intval(hydrate_mode)) {
	
						case 0:
							/** 
							 * Check if the resultset must keep snapshots
							 */
							if (phalcon_array_isset_string(column, SS("keepSnapshots"))) {
								PHALCON_OBS_NVAR(keep_snapshots);
								phalcon_array_fetch_string(&keep_snapshots, column, SL("keepSnapshots"), PH_NOISY_CC);
							} else {
								PHALCON_INIT_NVAR(keep_snapshots);
								ZVAL_BOOL(keep_snapshots, 0);
							}
	
							/** 
							 * Get the base instance
							 */
							PHALCON_OBS_NVAR(instance);
							phalcon_array_fetch_string(&instance, column, SL("instance"), PH_NOISY_CC);
	
							/** 
							 * Assign the values to the attributes using a column map
							 */
							PHALCON_INIT_NVAR(value);
							PHALCON_CALL_STATIC_PARAMS_5(value, "phalcon\\mvc\\model", "cloneresultmap", instance, row_model, column_map, dirty_state, keep_snapshots);
							break;
	
						default:
							/** 
							 * Other kinds of hydrations
							 */
							PHALCON_INIT_NVAR(value);
							PHALCON_CALL_STATIC_PARAMS_3(value, "phalcon\\mvc\\model", "cloneresultmaphydrate", row_model, column_map, hydrate_mode);
							break;
	
					}
	
					/** 
					 * The complete object is assigned to an attribute with the name of the alias or
					 * the model name
					 */
					PHALCON_OBS_NVAR(attribute);
					phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY_CC);
				} else {
					/** 
					 * Scalar columns are simply assigned to the result object
					 */
					if (phalcon_array_isset_string(column, SS("sqlAlias"))) {
						PHALCON_OBS_NVAR(sql_alias);
						phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY_CC);
	
						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch(&value, row, sql_alias, PH_NOISY_CC);
					} else {
						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch(&value, row, alias, PH_NOISY_CC);
					}
	
					/** 
					 * If a 'balias' is defined is not an unnamed scalar
					 */
					if (phalcon_array_isset_string(column, SS("balias"))) {
						PHALCON_CPY_WRT(attribute, alias);
					} else {
						PHALCON_INIT_NVAR(n_alias);
						phalcon_fast_str_replace(n_alias, underscore, empty_str, alias TSRMLS_CC);
						PHALCON_CPY_WRT(attribute, n_alias);
					}
				}
	
				/** 
				 * Assign the instance according to the hydration type
				 */
	
				switch (phalcon_get_intval(hydrate_mode)) {
	
					case 1:
						phalcon_array_update_zval(&active_row, attribute, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
						break;
	
					default:
						phalcon_update_property_zval_zval(active_row, attribute, value TSRMLS_CC);
						break;
	
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			/** 
			 * Store the generated row in this_ptr->activeRow to be retrieved by 'current'
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), active_row TSRMLS_CC);
		} else {
			/** 
			 * The row is already built so we just assign it to the activeRow
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), row TSRMLS_CC);
		}
		RETURN_MM_TRUE;
	}
	
	/** 
	 * There are no results to retrieve so we update this_ptr->activeRow as false
	 */
	phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC);
	RETURN_MM_FALSE;
}
Пример #17
0
/**
 * Replaces placeholders from pattern returning a valid PCRE regular expression
 *
 * @param string $pattern
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Router_Route, compilePattern){

	zval *pattern, *compiled_pattern = NULL, *id_pattern;
	zval wildcard, *pattern_copy = NULL, *params_pattern;
	zval *int_pattern;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &pattern);
	
	PHALCON_CPY_WRT(compiled_pattern, pattern);
	
	/** 
	 * If a pattern contains ':', maybe there are placeholders to replace
	 */
	if (phalcon_memnstr_str(pattern, SL(":"))) {
	
		/** 
		 * This is a pattern for valid identifiers
		 */
		PHALCON_INIT_VAR(id_pattern);
		ZVAL_STRING(id_pattern, "/([a-zA-Z0-9\\_\\-]+)", 1);
	
		/** 
		 * Replace the module part
		 */
		if (phalcon_memnstr_str(pattern, SL("/:module"))) {
			INIT_ZVAL(wildcard);
			ZVAL_STRING(&wildcard, "/:module", 0);
			PHALCON_CPY_WRT(pattern_copy, compiled_pattern);
	
			PHALCON_INIT_NVAR(compiled_pattern);
			phalcon_fast_str_replace(compiled_pattern, &wildcard, id_pattern, pattern_copy);
		}
	
		/** 
		 * Replace the controller placeholder
		 */
		if (phalcon_memnstr_str(pattern, SL("/:controller"))) {
			INIT_ZVAL(wildcard);
			ZVAL_STRING(&wildcard, "/:controller", 0);
			PHALCON_CPY_WRT(pattern_copy, compiled_pattern);
	
			PHALCON_INIT_NVAR(compiled_pattern);
			phalcon_fast_str_replace(compiled_pattern, &wildcard, id_pattern, pattern_copy);
		}
	
		/** 
		 * Replace the namespace placeholder
		 */
		if (phalcon_memnstr_str(pattern, SL("/:namespace"))) {
			INIT_ZVAL(wildcard)
			ZVAL_STRING(&wildcard, "/:namespace", 0);
			PHALCON_CPY_WRT(pattern_copy, compiled_pattern);
	
			PHALCON_INIT_NVAR(compiled_pattern);
			phalcon_fast_str_replace(compiled_pattern, &wildcard, id_pattern, pattern_copy);
		}
	
		/** 
		 * Replace the action placeholder
		 */
		if (phalcon_memnstr_str(pattern, SL("/:action"))) {
			INIT_ZVAL(wildcard);
			ZVAL_STRING(&wildcard, "/:action", 0);
			PHALCON_CPY_WRT(pattern_copy, compiled_pattern);
	
			PHALCON_INIT_NVAR(compiled_pattern);
			phalcon_fast_str_replace(compiled_pattern, &wildcard, id_pattern, pattern_copy);
		}
	
		/** 
		 * Replace the params placeholder
		 */
		if (phalcon_memnstr_str(pattern, SL("/:params"))) {
			INIT_ZVAL(wildcard);
			ZVAL_STRING(&wildcard, "/:params", 0);
	
			PHALCON_INIT_VAR(params_pattern);
			ZVAL_STRING(params_pattern, "(/.*)*", 1);
			PHALCON_CPY_WRT(pattern_copy, compiled_pattern);
	
			PHALCON_INIT_NVAR(compiled_pattern);
			phalcon_fast_str_replace(compiled_pattern, &wildcard, params_pattern, pattern_copy);
		}
	
		/** 
		 * Replace the int placeholder
		 */
		if (phalcon_memnstr_str(pattern, SL("/:int"))) {
			INIT_ZVAL(wildcard);
			ZVAL_STRING(&wildcard, "/:int", 0);
	
			PHALCON_INIT_VAR(int_pattern);
			ZVAL_STRING(int_pattern, "/([0-9]+)", 1);
			PHALCON_CPY_WRT(pattern_copy, compiled_pattern);
	
			PHALCON_INIT_NVAR(compiled_pattern);
			phalcon_fast_str_replace(compiled_pattern, &wildcard, int_pattern, pattern_copy);
		}
	}
	
	/** 
	 * Check if the pattern has parantheses in order to add the regex delimiters
	 */
	if (phalcon_memnstr_str(compiled_pattern, SL("("))) {
		PHALCON_CONCAT_SVS(return_value, "#^", compiled_pattern, "$#");
		RETURN_MM();
	}
	
	/** 
	 * Square brackets are also checked
	 */
	if (phalcon_memnstr_str(compiled_pattern, SL("["))) {
		PHALCON_CONCAT_SVS(return_value, "#^", compiled_pattern, "$#");
		RETURN_MM();
	}
	
	RETURN_CCTOR(compiled_pattern);
}
Пример #18
0
/**
 * Replaces placeholders from pattern returning a valid PCRE regular expression
 *
 * @param string $pattern
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Router, compilePattern){

	zval *pattern = NULL, *id_pattern = NULL, *compiled_pattern = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL;
	zval *c0 = NULL, *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL, *c5 = NULL, *c6 = NULL;

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

	PHALCON_INIT_VAR(id_pattern);
	ZVAL_STRING(id_pattern, "/([a-zA-Z0-9\\_]+)", 1);
	
	PHALCON_ALLOC_ZVAL_MM(r0);
	phalcon_fast_strpos_str(r0, pattern, SL(":") TSRMLS_CC);
	if (zend_is_true(r0)) {
		PHALCON_INIT_VAR(c0);
		ZVAL_STRING(c0, "/:module", 1);
		PHALCON_INIT_VAR(compiled_pattern);
		phalcon_fast_str_replace(compiled_pattern, c0, id_pattern, pattern TSRMLS_CC);
		
		PHALCON_INIT_VAR(c1);
		ZVAL_STRING(c1, "/:controller", 1);
		
		PHALCON_INIT_VAR(compiled_pattern);
		phalcon_fast_str_replace(compiled_pattern, c1, id_pattern, pattern TSRMLS_CC);
		
		PHALCON_INIT_VAR(c2);
		ZVAL_STRING(c2, "/:action", 1);
		
		PHALCON_ALLOC_ZVAL_MM(r1);
		phalcon_fast_str_replace(r1, c2, id_pattern, compiled_pattern TSRMLS_CC);
		PHALCON_CPY_WRT(compiled_pattern, r1);
		
		PHALCON_INIT_VAR(c3);
		ZVAL_STRING(c3, "/:params", 1);
		
		PHALCON_INIT_VAR(c4);
		ZVAL_STRING(c4, "(/.*)*", 1);
		
		PHALCON_ALLOC_ZVAL_MM(r2);
		phalcon_fast_str_replace(r2, c3, c4, compiled_pattern TSRMLS_CC);
		PHALCON_CPY_WRT(compiled_pattern, r2);
		
		PHALCON_INIT_VAR(c5);
		ZVAL_STRING(c5, "/:int", 1);
		
		PHALCON_INIT_VAR(c6);
		ZVAL_STRING(c6, "/([0-9]+)", 1);
		
		PHALCON_ALLOC_ZVAL_MM(r3);
		phalcon_fast_str_replace(r3, c5, c6, compiled_pattern TSRMLS_CC);
		PHALCON_CPY_WRT(compiled_pattern, r3);
	} else {
		PHALCON_CPY_WRT(compiled_pattern, pattern);
	}
	
	PHALCON_ALLOC_ZVAL_MM(r4);
	PHALCON_CONCAT_SVS(r4, "#^", compiled_pattern, "$#");
	
	RETURN_CTOR(r4);
}
Пример #19
0
/**
 * Manually bind params to a SQL statement. This method requires an active connection to a database system
 *
 *<code>
 *	$sql = $connection->bindParams('SELECT * FROM robots WHERE name = ?0', array('Bender'));
 *  echo $sql; // SELECT * FROM robots WHERE name = 'Bender'
 *</code>
 *
 * @param string $sqlStatement
 * @param array $params
 * @return string
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, bindParams){

	zval *sql_statement, *params, *sql = NULL, *pdo, *bind_value = NULL;
	zval *index = NULL, *value = NULL, *place_key = NULL, *replaced_sql = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &sql_statement, &params) == FAILURE) {
		RETURN_MM_NULL();
	}

	if (Z_TYPE_P(params) == IS_ARRAY) { 
		if (phalcon_fast_count_ev(params TSRMLS_CC)) {
			PHALCON_CPY_WRT(sql, sql_statement);
	
			PHALCON_OBS_VAR(pdo);
			phalcon_read_property(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC);
	
			if (!phalcon_is_iterable(params, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(index, ah0, hp0);
				PHALCON_GET_FOREACH_VALUE(bind_value);
	
				if (phalcon_is_numeric(bind_value)) {
					PHALCON_CPY_WRT(value, bind_value);
				} else {
					if (Z_TYPE_P(bind_value) == IS_OBJECT) {
						PHALCON_INIT_NVAR(value);
						PHALCON_CALL_FUNC_PARAMS_1(value, "strval", bind_value);
					} else {
						PHALCON_INIT_NVAR(value);
						PHALCON_CALL_METHOD_PARAMS_1(value, pdo, "quote", bind_value);
					}
				}
	
				/** 
				 * Handle long parameters as numeric placeholders: ?0, ?1
				 */
				if (Z_TYPE_P(index) == IS_LONG) {
					PHALCON_INIT_NVAR(place_key);
					PHALCON_CONCAT_SV(place_key, "?", index);
	
					PHALCON_INIT_NVAR(replaced_sql);
					phalcon_fast_str_replace(replaced_sql, place_key, value, sql TSRMLS_CC);
					PHALCON_CPY_WRT(sql, replaced_sql);
					zend_hash_move_forward_ex(ah0, &hp0);
					continue;
				}
	
				/** 
				 * Handle long parameters as string placeholders: :name:, :other:
				 */
				if (Z_TYPE_P(index) == IS_STRING) {
					PHALCON_INIT_NVAR(place_key);
					PHALCON_CONCAT_SVS(place_key, ":", index, ":");
	
					PHALCON_INIT_NVAR(replaced_sql);
					phalcon_fast_str_replace(replaced_sql, place_key, value, sql TSRMLS_CC);
					PHALCON_CPY_WRT(sql, replaced_sql);
					zend_hash_move_forward_ex(ah0, &hp0);
					continue;
				}
	
				/** 
				 * Unrecognized parameter type
				 */
				PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid bind parameter");
				return;
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
	
			RETURN_CCTOR(sql);
		}
	}
	
	RETURN_CCTOR(sql_statement);
}
Пример #20
0
/**
 * Generates a URL
 *
 * @param string|array $uri
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Url, get){

	zval *uri = NULL, *base_uri = NULL, *dependency_injector = NULL, *service = NULL;
	zval *router = NULL, *route_name = NULL, *route = NULL, *exception_message = NULL;
	zval *exception = NULL, *pattern = NULL, *replaced_pattern = NULL;
	zval *controller_name = NULL, *wildcard = NULL, *action_name = NULL;
	zval *have_bracket = NULL, *matches = NULL, *match_position = NULL;
	zval *set_order = NULL, *names_pattern = NULL, *have_variables = NULL;
	zval *match = NULL, *match_zero = NULL, *match_one = NULL, *value = NULL, *new_pcre_pattern = NULL;
	zval *final_uri = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL;
	zval *t0 = NULL;
	zval *p0[] = { NULL, NULL, NULL, NULL };
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	int eval_int;

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

	if (!uri) {
		PHALCON_ALLOC_ZVAL_MM(uri);
		ZVAL_NULL(uri);
	}
	
	PHALCON_INIT_VAR(base_uri);
	PHALCON_CALL_METHOD(base_uri, this_ptr, "getbaseuri", PH_NO_CHECK);
	if (Z_TYPE_P(uri) == IS_ARRAY) { 
		eval_int = phalcon_array_isset_string(uri, SL("for")+1);
		if (!eval_int) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "It's necessary to define the route name with the parameter \"for\"");
			return;
		}
		
		PHALCON_INIT_VAR(dependency_injector);
		phalcon_read_property(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
		if (!zend_is_true(dependency_injector)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "A dependency injector container is required to obtain the \"url\" service");
			return;
		}
		
		PHALCON_INIT_VAR(service);
		ZVAL_STRING(service, "router", 1);
		
		PHALCON_INIT_VAR(router);
		PHALCON_CALL_METHOD_PARAMS_1(router, dependency_injector, "getshared", service, PH_NO_CHECK);
		
		PHALCON_INIT_VAR(route_name);
		phalcon_array_fetch_string(&route_name, uri, SL("for"), PH_NOISY_CC);
		
		PHALCON_INIT_VAR(route);
		PHALCON_CALL_METHOD_PARAMS_1(route, router, "getroutebyname", route_name, PH_NO_CHECK);
		if (Z_TYPE_P(route) != IS_OBJECT) {
			PHALCON_INIT_VAR(exception_message);
			PHALCON_CONCAT_SVS(exception_message, "Cannot obtain a route using the name \"", route_name, "\"");
			
			PHALCON_INIT_VAR(exception);
			object_init_ex(exception, phalcon_mvc_url_exception_ce);
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(exception, "__construct", exception_message, PH_CHECK);
			phalcon_throw_exception(exception TSRMLS_CC);
			return;
		}
		
		PHALCON_INIT_VAR(pattern);
		PHALCON_CALL_METHOD(pattern, route, "getpattern", PH_NO_CHECK);
		PHALCON_CPY_WRT(replaced_pattern, pattern);
		eval_int = phalcon_array_isset_string(uri, SL("controller")+1);
		if (eval_int) {
			PHALCON_INIT_VAR(controller_name);
			phalcon_array_fetch_string(&controller_name, uri, SL("controller"), PH_NOISY_CC);
			
			PHALCON_INIT_VAR(wildcard);
			ZVAL_STRING(wildcard, ":controller", 1);
			
			PHALCON_ALLOC_ZVAL_MM(r0);
			phalcon_fast_str_replace(r0, wildcard, controller_name, replaced_pattern TSRMLS_CC);
			PHALCON_CPY_WRT(replaced_pattern, r0);
		}
		
		eval_int = phalcon_array_isset_string(uri, SL("action")+1);
		if (eval_int) {
			PHALCON_INIT_VAR(action_name);
			phalcon_array_fetch_string(&action_name, uri, SL("action"), PH_NOISY_CC);
			
			PHALCON_INIT_VAR(wildcard);
			ZVAL_STRING(wildcard, ":action", 1);
			
			PHALCON_ALLOC_ZVAL_MM(r1);
			phalcon_fast_str_replace(r1, wildcard, action_name, replaced_pattern TSRMLS_CC);
			PHALCON_CPY_WRT(replaced_pattern, r1);
		}
		
		PHALCON_INIT_VAR(have_bracket);
		phalcon_fast_strpos_str(have_bracket, replaced_pattern, SL("{") TSRMLS_CC);
		if (Z_TYPE_P(have_bracket) != IS_BOOL || (Z_TYPE_P(have_bracket) == IS_BOOL && Z_BVAL_P(have_bracket))) {
			PHALCON_INIT_VAR(matches);
			ZVAL_NULL(matches);
			
			PHALCON_INIT_VAR(match_position);
			ZVAL_LONG(match_position, 1);
			
			PHALCON_INIT_VAR(t0);
			ZVAL_LONG(t0, 2);
			PHALCON_CPY_WRT(set_order, t0);
			
			PHALCON_INIT_VAR(names_pattern);
			ZVAL_STRING(names_pattern, "#{([a-zA-Z][a-zA-Z0-9\\_\\-]+)(:([^}]+))*}#", 1);
			p0[0] = names_pattern;
			p0[1] = replaced_pattern;
			Z_SET_ISREF_P(matches);
			p0[2] = matches;
			p0[3] = set_order;
			
			PHALCON_ALLOC_ZVAL_MM(r2);
			PHALCON_CALL_FUNC_PARAMS(r2, "preg_match_all", 4, p0);
			Z_UNSET_ISREF_P(p0[2]);
			PHALCON_CPY_WRT(have_variables, r2);
			if (zend_is_true(have_variables)) {
				if (!phalcon_valid_foreach(matches TSRMLS_CC)) {
					return;
				}
				
				ah0 = Z_ARRVAL_P(matches);
				zend_hash_internal_pointer_reset_ex(ah0, &hp0);
				fes_0306_0:
					if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
						goto fee_0306_0;
					}
					
					PHALCON_INIT_VAR(match);
					ZVAL_ZVAL(match, *hd, 1, 0);
					PHALCON_INIT_VAR(match_zero);
					phalcon_array_fetch_long(&match_zero, match, 0, PH_NOISY_CC);
					
					PHALCON_INIT_VAR(match_one);
					phalcon_array_fetch_long(&match_one, match, 1, PH_NOISY_CC);
					eval_int = phalcon_array_isset(uri, match_one);
					if (eval_int) {
						PHALCON_INIT_VAR(value);
						phalcon_array_fetch(&value, uri, match_one, PH_NOISY_CC);
						
						PHALCON_INIT_VAR(new_pcre_pattern);
						phalcon_fast_str_replace(new_pcre_pattern, match_zero, value, replaced_pattern TSRMLS_CC);
						PHALCON_CPY_WRT(replaced_pattern, new_pcre_pattern);
					}
					zend_hash_move_forward_ex(ah0, &hp0);
					goto fes_0306_0;
				fee_0306_0:
				if(0){}
				
			}
		}
		
		
		RETURN_CCTOR(replaced_pattern);
	} else {
		PHALCON_INIT_VAR(final_uri);
		PHALCON_CONCAT_VV(final_uri, base_uri, uri);
		
		RETURN_CTOR(final_uri);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #21
0
/**
 * Bind params to a SQL statement
 *
 * @param string $sql
 * @param array $params
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, bindParams){

	zval *sql_statement = NULL, *params = NULL, *number_params = NULL;
	zval *sql = NULL, *pdo = NULL, *bind_value = NULL, *index = NULL, *is_numeric = NULL;
	zval *value = NULL, *place_key = NULL, *replaced_sql = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;

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

	if (Z_TYPE_P(params) == IS_ARRAY) { 
		PHALCON_INIT_VAR(number_params);
		phalcon_fast_count(number_params, params TSRMLS_CC);
		if (zend_is_true(number_params)) {
			PHALCON_CPY_WRT(sql, sql_statement);
			
			PHALCON_INIT_VAR(pdo);
			phalcon_read_property(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC);
			
			if (!phalcon_valid_foreach(params TSRMLS_CC)) {
				return;
			}
			
			ah0 = Z_ARRVAL_P(params);
			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_INIT_VAR(index);
				PHALCON_GET_FOREACH_KEY(index, ah0, hp0);
				PHALCON_GET_FOREACH_VALUE(bind_value);
				
				PHALCON_INIT_VAR(is_numeric);
				PHALCON_CALL_FUNC_PARAMS_1(is_numeric, "is_numeric", bind_value);
				if (PHALCON_IS_TRUE(is_numeric)) {
					PHALCON_CPY_WRT(value, bind_value);
				} else {
					PHALCON_INIT_VAR(value);
					PHALCON_CALL_METHOD_PARAMS_1(value, pdo, "quote", bind_value, PH_NO_CHECK);
				}
				
				if (Z_TYPE_P(index) == IS_LONG) {
					PHALCON_INIT_VAR(place_key);
					PHALCON_CONCAT_SV(place_key, "?", index);
					
					PHALCON_INIT_VAR(replaced_sql);
					phalcon_fast_str_replace(replaced_sql, place_key, value, sql TSRMLS_CC);
					PHALCON_CPY_WRT(sql, replaced_sql);
				} else {
					if (Z_TYPE_P(index) == IS_STRING) {
						PHALCON_INIT_VAR(place_key);
						PHALCON_CONCAT_SVS(place_key, ":", index, ":");
						
						PHALCON_INIT_VAR(replaced_sql);
						phalcon_fast_str_replace(replaced_sql, place_key, value, sql TSRMLS_CC);
						PHALCON_CPY_WRT(sql, replaced_sql);
					} else {
						PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid bind parameter");
						return;
					}
				}
				
				zend_hash_move_forward_ex(ah0, &hp0);
				goto ph_cycle_start_0;
			
			ph_cycle_end_0:
			
			
			RETURN_CCTOR(sql);
		}
	}
	
	RETURN_CCTOR(sql_statement);
}
Пример #22
0
/**
 * Check whether internal resource has rows to fetch
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){

	zval *type = NULL, *result, *row = NULL, *rows, *underscore, *empty_str;
	zval *active_row, *columns_types, *column = NULL, *alias = NULL;
	zval *source = NULL, *instance = NULL, *attributes = NULL, *column_map = NULL;
	zval *row_model = NULL, *attribute = NULL, *column_alias = NULL, *value = NULL;
	zval *model_attribute = NULL, *sql_alias = NULL, *n_alias = 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();

	PHALCON_INIT_VAR(type);
	phalcon_read_property(&type, this_ptr, SL("_type"), PH_NOISY_CC);
	if (zend_is_true(type)) {
		PHALCON_INIT_VAR(result);
		phalcon_read_property(&result, this_ptr, SL("_result"), PH_NOISY_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
			PHALCON_INIT_VAR(row);
			PHALCON_CALL_METHOD_PARAMS_1(row, result, "fetch", result, PH_NO_CHECK);
		} else {
			PHALCON_INIT_NVAR(row);
			ZVAL_BOOL(row, 0);
		}
	} else {
		PHALCON_INIT_VAR(rows);
		phalcon_read_property(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
		Z_SET_ISREF_P(rows);
	
		PHALCON_INIT_NVAR(row);
		PHALCON_CALL_FUNC_PARAMS_1(row, "current", rows);
		Z_UNSET_ISREF_P(rows);
		if (zend_is_true(row)) {
			Z_SET_ISREF_P(rows);
			PHALCON_CALL_FUNC_PARAMS_1_NORETURN("next", rows);
			Z_UNSET_ISREF_P(rows);
		}
	}
	
	if (PHALCON_IS_NOT_FALSE(row)) {
		PHALCON_INIT_VAR(underscore);
		ZVAL_STRING(underscore, "_", 1);
	
		PHALCON_INIT_VAR(empty_str);
		ZVAL_STRING(empty_str, "", 1);
	
		PHALCON_INIT_VAR(active_row);
		object_init_ex(active_row, phalcon_mvc_model_row_ce);
	
		PHALCON_INIT_VAR(columns_types);
		phalcon_read_property(&columns_types, this_ptr, SL("_columnTypes"), PH_NOISY_CC);
	
		if (!phalcon_valid_foreach(columns_types TSRMLS_CC)) {
			return;
		}
	
		ah0 = Z_ARRVAL_P(columns_types);
		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_KEY(alias, ah0, hp0);
			PHALCON_GET_FOREACH_VALUE(column);
	
			PHALCON_INIT_NVAR(type);
			phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY_CC);
			if (PHALCON_COMPARE_STRING(type, "object")) {
				/** 
				 * Object columns are assigned column by column
				 */
				PHALCON_INIT_NVAR(source);
				phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY_CC);
	
				PHALCON_INIT_NVAR(instance);
				phalcon_array_fetch_string(&instance, column, SL("instance"), PH_NOISY_CC);
	
				PHALCON_INIT_NVAR(attributes);
				phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY_CC);
	
				PHALCON_INIT_NVAR(column_map);
				phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY_CC);
	
				/** 
				 * Assign the values from the _source_attribute notation to its real column name
				 */
				PHALCON_INIT_NVAR(row_model);
				array_init(row_model);
	
				if (!phalcon_valid_foreach(attributes TSRMLS_CC)) {
					return;
				}
	
				ah1 = Z_ARRVAL_P(attributes);
				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_VALUE(attribute);
	
					PHALCON_INIT_NVAR(column_alias);
					PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute);
	
					PHALCON_INIT_NVAR(value);
					phalcon_array_fetch(&value, row, column_alias, PH_NOISY_CC);
					phalcon_array_update_zval(&row_model, attribute, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
					zend_hash_move_forward_ex(ah1, &hp1);
					goto ph_cycle_start_1;
	
				ph_cycle_end_1:
	
				/** 
				 * Assign the values to the attributes using a column map
				 */
				PHALCON_INIT_NVAR(model_attribute);
				PHALCON_CALL_STATIC_PARAMS_3(model_attribute, "phalcon\\mvc\\model", "dumpresultmap", instance, row_model, column_map);
	
				/** 
				 * The complete object is assigned to an attribute with the name of the alias or
				 * the model name
				 */
				PHALCON_INIT_NVAR(attribute);
				phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY_CC);
				phalcon_update_property_zval_zval(active_row, attribute, model_attribute TSRMLS_CC);
			} else {
				/** 
				 * Scalar columns are simply assigned to the result object
				 */
				eval_int = phalcon_array_isset_string(column, SS("sqlAlias"));
				if (eval_int) {
					PHALCON_INIT_NVAR(sql_alias);
					phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY_CC);
	
					PHALCON_INIT_NVAR(value);
					phalcon_array_fetch(&value, row, sql_alias, PH_NOISY_CC);
				} else {
					PHALCON_INIT_NVAR(value);
					phalcon_array_fetch(&value, row, alias, PH_NOISY_CC);
				}
	
				/** 
				 * If a 'balias' is defined is not an unnamed scalar
				 */
				eval_int = phalcon_array_isset_string(column, SS("balias"));
				if (eval_int) {
					phalcon_update_property_zval_zval(active_row, alias, value TSRMLS_CC);
				} else {
					PHALCON_INIT_NVAR(n_alias);
					phalcon_fast_str_replace(n_alias, underscore, empty_str, alias TSRMLS_CC);
					phalcon_update_property_zval_zval(active_row, n_alias, value TSRMLS_CC);
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
			goto ph_cycle_start_0;
	
		ph_cycle_end_0:
	
		phalcon_update_property_zval(this_ptr, SL("_activeRow"), active_row TSRMLS_CC);
		PHALCON_MM_RESTORE();
		RETURN_TRUE;
	} else {
		phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
	RETURN_FALSE;
}
Пример #23
0
/**
 * Internal sanizite wrapper to filter_var
 *
 * @param  mixed $value
 * @param  string $filter
 * @return mixed
 */
PHP_METHOD(Phalcon_Filter, _sanitize){

	zval *value, *filter, *filters, *filter_object;
	zval *arguments, *filtered = NULL, *type = NULL, *quote, *empty_str;
	zval *escaped, *allow_fraction, *options, *exception_message;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &value, &filter) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_INIT_VAR(filters);
	phalcon_read_property(&filters, this_ptr, SL("_filters"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(filters, filter);
	if (eval_int) {
		PHALCON_INIT_VAR(filter_object);
		phalcon_array_fetch(&filter_object, filters, filter, PH_NOISY_CC);
	
		/** 
		 * If the filter is a closure we call it in the PHP userland
		 */
		if (phalcon_is_instance_of(filter_object, SL("Closure") TSRMLS_CC)) {
			PHALCON_INIT_VAR(arguments);
			array_init(arguments);
			phalcon_array_append(&arguments, value, PH_SEPARATE TSRMLS_CC);
	
			PHALCON_INIT_VAR(filtered);
			PHALCON_CALL_USER_FUNC_ARRAY(filtered, filter_object, arguments);
		} else {
			PHALCON_INIT_NVAR(filtered);
			PHALCON_CALL_METHOD_PARAMS_1(filtered, filter_object, "filter", value, PH_NO_CHECK);
		}
	
	
		RETURN_CCTOR(filtered);
	}
	
	PHALCON_INIT_NVAR(filtered);
	
	if (PHALCON_COMPARE_STRING(filter, "email")) {
		/** 
		 * The 'email' filter uses the filter extension
		 */
		PHALCON_INIT_VAR(type);
		ZVAL_LONG(type, 517);
	
		PHALCON_INIT_VAR(quote);
		ZVAL_STRING(quote, "'", 1);
	
		PHALCON_INIT_VAR(empty_str);
		ZVAL_STRING(empty_str, "", 1);
	
		PHALCON_INIT_VAR(escaped);
		phalcon_fast_str_replace(escaped, quote, empty_str, value TSRMLS_CC);
	
		PHALCON_INIT_NVAR(filtered);
		PHALCON_CALL_FUNC_PARAMS_2(filtered, "filter_var", escaped, type);
		goto ph_end_0;
	}
	
	if (PHALCON_COMPARE_STRING(filter, "int")) {
		/** 
		 * 'int' filter sanitizes a numeric input
		 */
		PHALCON_INIT_NVAR(type);
		ZVAL_LONG(type, 519);
	
		PHALCON_INIT_NVAR(filtered);
		PHALCON_CALL_FUNC_PARAMS_2(filtered, "filter_var", value, type);
		goto ph_end_0;
	}
	
	if (PHALCON_COMPARE_STRING(filter, "string")) {
		PHALCON_INIT_NVAR(type);
		ZVAL_LONG(type, 513);
	
		PHALCON_INIT_NVAR(filtered);
		PHALCON_CALL_FUNC_PARAMS_2(filtered, "filter_var", value, type);
		goto ph_end_0;
	}
	
	if (PHALCON_COMPARE_STRING(filter, "float")) {
		/** 
		 * The 'float' filter uses the filter extension
		 */
		PHALCON_INIT_VAR(allow_fraction);
		ZVAL_LONG(allow_fraction, 4096);
	
		PHALCON_INIT_VAR(options);
		array_init(options);
		phalcon_array_update_string(&options, SL("flags"), &allow_fraction, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
		PHALCON_INIT_NVAR(type);
		ZVAL_LONG(type, 520);
	
		PHALCON_INIT_NVAR(filtered);
		PHALCON_CALL_FUNC_PARAMS_3(filtered, "filter_var", value, type, options);
		goto ph_end_0;
	}
	
	if (PHALCON_COMPARE_STRING(filter, "alphanum")) {
		PHALCON_INIT_NVAR(filtered);
		phalcon_filter_alphanum(filtered, value);
		goto ph_end_0;
	}
	
	if (PHALCON_COMPARE_STRING(filter, "trim")) {
		PHALCON_INIT_NVAR(filtered);
		PHALCON_CALL_FUNC_PARAMS_1(filtered, "trim", value);
		goto ph_end_0;
	}
	
	if (PHALCON_COMPARE_STRING(filter, "striptags")) {
		PHALCON_INIT_NVAR(filtered);
		PHALCON_CALL_FUNC_PARAMS_1(filtered, "strip_tags", value);
		goto ph_end_0;
	}
	
	if (PHALCON_COMPARE_STRING(filter, "lower")) {
		if (phalcon_function_exists_ex(SS("mb_strtolower") TSRMLS_CC) == SUCCESS) {
			/** 
			 * 'lower' checks for the mbstring extension to make a correct lowercase
			 * transformation
			 */
			PHALCON_INIT_NVAR(filtered);
			PHALCON_CALL_FUNC_PARAMS_1(filtered, "mb_strtolower", value);
		} else {
			PHALCON_INIT_NVAR(filtered);
			PHALCON_CALL_FUNC_PARAMS_1(filtered, "strtolower", value);
		}
		goto ph_end_0;
	}
	
	if (PHALCON_COMPARE_STRING(filter, "upper")) {
		if (phalcon_function_exists_ex(SS("mb_strtoupper") TSRMLS_CC) == SUCCESS) {
			/** 
			 * 'upper' checks for the mbstring extension to make a correct lowercase
			 * transformation
			 */
			PHALCON_INIT_NVAR(filtered);
			PHALCON_CALL_FUNC_PARAMS_1(filtered, "mb_strtoupper", value);
		} else {
			PHALCON_INIT_NVAR(filtered);
			PHALCON_CALL_FUNC_PARAMS_1(filtered, "strtoupper", value);
		}
		goto ph_end_0;
	}
	
	PHALCON_INIT_VAR(exception_message);
	PHALCON_CONCAT_SVS(exception_message, "Sanitize filter ", filter, " is not supported");
	PHALCON_THROW_EXCEPTION_ZVAL(phalcon_filter_exception_ce, exception_message);
	return;
	
	ph_end_0:
	
	RETURN_CCTOR(filtered);
}
Пример #24
0
/**
 * Bind params to SQL select
 *
 * @param string $sqlSelect
 * @param array $params
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, bindParams){

	zval *sql_select = NULL, *params = NULL, *select = NULL, *pdo = NULL, *bind_value = NULL;
	zval *index = NULL, *value = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &sql_select, &params) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_ALLOC_ZVAL_MM(r0);
	phalcon_fast_count(r0, params TSRMLS_CC);
	if (zend_is_true(r0)) {
		PHALCON_CPY_WRT(select, sql_select);
		
		PHALCON_INIT_VAR(pdo);
		phalcon_read_property(&pdo, this_ptr, SL("_pdo"), PH_NOISY_CC);
		if (!phalcon_valid_foreach(params TSRMLS_CC)) {
			return;
		}
		
		ah0 = Z_ARRVAL_P(params);
		zend_hash_internal_pointer_reset_ex(ah0, &hp0);
		fes_7f5d_2:
			if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
				goto fee_7f5d_2;
			}
			
			PHALCON_INIT_VAR(index);
			PHALCON_GET_FOREACH_KEY(index, ah0, hp0);
			PHALCON_INIT_VAR(bind_value);
			ZVAL_ZVAL(bind_value, *hd, 1, 0);
			PHALCON_INIT_VAR(r1);
			PHALCON_CALL_FUNC_PARAMS_1(r1, "is_numeric", bind_value);
			if (zend_is_true(r1)) {
				PHALCON_CPY_WRT(value, bind_value);
			} else {
				PHALCON_INIT_VAR(value);
				PHALCON_CALL_METHOD_PARAMS_1(value, pdo, "quote", bind_value, PH_NO_CHECK);
			}
			if (Z_TYPE_P(index) == IS_LONG) {
				PHALCON_INIT_VAR(r2);
				PHALCON_CONCAT_SV(r2, "?", index);
				PHALCON_INIT_VAR(r3);
				phalcon_fast_str_replace(r3, r2, value, select TSRMLS_CC);
				PHALCON_CPY_WRT(select, r3);
			} else {
				if (Z_TYPE_P(index) == IS_STRING) {
					PHALCON_INIT_VAR(r4);
					PHALCON_CONCAT_SVS(r4, ":", index, ":");
					PHALCON_INIT_VAR(r5);
					phalcon_fast_str_replace(r5, r4, value, select TSRMLS_CC);
					PHALCON_CPY_WRT(select, r5);
				} else {
					PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid bind parameter");
					return;
				}
			}
			zend_hash_move_forward_ex(ah0, &hp0);
			goto fes_7f5d_2;
		fee_7f5d_2:
		if(0){}
		
		
		RETURN_CCTOR(select);
	}
	
	RETURN_CCTOR(sql_select);
}
Пример #25
0
/**
 * Check whether internal resource has rows to fetch
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){

	zval *type = NULL, *row = NULL, *underscore;
	zval *empty_str, *active_row = NULL;
	zval *dirty_state, *column = NULL, *alias = NULL, *source = NULL, *attributes = NULL;
	zval *column_map = NULL, *row_model = NULL, *attribute = NULL, *column_alias = NULL;
	zval *column_value = NULL;
	zval *value = NULL, *sql_alias = NULL, *n_alias = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	int i_type, is_partial;

	PHALCON_MM_GROW();

	type       = phalcon_fetch_nproperty_this(this_ptr, SL("_type"), PH_NOISY TSRMLS_CC);
	i_type     = (Z_TYPE_P(type) == IS_LONG) ? Z_LVAL_P(type) : phalcon_get_intval(type);
	is_partial = (i_type == PHALCON_MVC_MODEL_RESULTSET_TYPE_PARTIAL);
	type       = NULL;

	PHALCON_INIT_VAR(row);
	if (is_partial) {
		/** 
		 * The result is bigger than 32 rows so it's retrieved one by one
		 */
		zval *result = phalcon_fetch_nproperty_this(this_ptr, SL("_result"), PH_NOISY TSRMLS_CC);
		if (PHALCON_IS_NOT_FALSE(result)) {
			PHALCON_CALL_METHOD(&row, result, "fetch", result);
		} else {
			ZVAL_FALSE(row);
		}
	} else {
		/** 
		 * The full rows are dumped in this_ptr->rows
		 */
		zval *rows = phalcon_fetch_nproperty_this(this_ptr, SL("_rows"), PH_NOISY TSRMLS_CC);
		if (Z_TYPE_P(rows) == IS_ARRAY) { 
			phalcon_array_get_current(row, rows);
			if (Z_TYPE_P(row) == IS_OBJECT) {
				zend_hash_move_forward(Z_ARRVAL_P(rows));
			}
		} else {
			ZVAL_FALSE(row);
		}
	}
	
	/** 
	 * Valid records are arrays
	 */
	if (Z_TYPE_P(row) == IS_ARRAY || Z_TYPE_P(row) == IS_OBJECT) {
	
		/** 
		 * The result type=1 so we need to build every row
		 */
		if (is_partial) {
	
			/** 
			 * Get current hydration mode
			 */
			zval *hydrate_mode  = phalcon_fetch_nproperty_this(this_ptr, SL("_hydrateMode"), PH_NOISY TSRMLS_CC);
			zval *columns_types = phalcon_fetch_nproperty_this(this_ptr, SL("_columnTypes"), PH_NOISY TSRMLS_CC);
			int i_hydrate_mode  = phalcon_get_intval(hydrate_mode);
	
			PHALCON_INIT_VAR(underscore);
			ZVAL_STRING(underscore, "_", 1);
	
			PHALCON_INIT_VAR(empty_str);
			ZVAL_EMPTY_STRING(empty_str);
	
			/** 
			 * Each row in a complex result is a Phalcon\Mvc\Model\Row instance
			 */
			PHALCON_INIT_VAR(active_row);
			switch (i_hydrate_mode) {
				case 0:
					object_init_ex(active_row, phalcon_mvc_model_row_ce);
					break;
	
				case 1:
					array_init(active_row);
					break;
	
				case 2:
				default:
					object_init(active_row);
					break;
			}
	
			/** 
			 * Create every record according to the column types
			 */
	
			/** 
			 * Set records as dirty state PERSISTENT by default
			 */
			PHALCON_INIT_VAR(dirty_state);
			ZVAL_LONG(dirty_state, 0);
	
			phalcon_is_iterable(columns_types, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HKEY(alias, ah0, hp0);
				PHALCON_GET_HVALUE(column);
	
				PHALCON_OBS_NVAR(type);
				phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY);
				if (PHALCON_IS_STRING(type, "object")) {
	
					/** 
					 * Object columns are assigned column by column
					 */
					PHALCON_OBS_NVAR(source);
					phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY);
	
					PHALCON_OBS_NVAR(attributes);
					phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY);
	
					PHALCON_OBS_NVAR(column_map);
					phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY);
	
					/** 
					 * Assign the values from the _source_attribute notation to its real column name
					 */
					PHALCON_INIT_NVAR(row_model);
					array_init(row_model);
	
					phalcon_is_iterable(attributes, &ah1, &hp1, 0, 0);
	
					while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
						PHALCON_GET_HVALUE(attribute);
	
						/** 
						 * Columns are supposed to be in the form _table_field
						 */
						PHALCON_INIT_NVAR(column_alias);
						PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute);
	
						PHALCON_OBS_NVAR(column_value);
						phalcon_array_fetch(&column_value, row, column_alias, PH_NOISY);
						phalcon_array_update_zval(&row_model, attribute, column_value, PH_COPY | PH_SEPARATE);
	
						zend_hash_move_forward_ex(ah1, &hp1);
					}
	
					/** 
					 * Generate the column value according to the hydration type
					 */
					switch (phalcon_get_intval(hydrate_mode)) {
	
						case 0: {
							zval *keep_snapshots, *instance;

							/** 
							 * Check if the resultset must keep snapshots
							 */
							if (!phalcon_array_isset_string_fetch(&keep_snapshots, column, SS("keepSnapshots"))) {
								keep_snapshots = PHALCON_GLOBAL(z_false);
							}
	
							/** 
							 * Get the base instance
							 */
							if (!phalcon_array_isset_string_fetch(&instance, column, SS("instance"))) {
								php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined index: instance");
								instance = PHALCON_GLOBAL(z_null);
							}
	
							/** 
							 * Assign the values to the attributes using a column map
							 */
							PHALCON_CALL_CE_STATIC(&value, phalcon_mvc_model_ce, "cloneresultmap", instance, row_model, column_map, dirty_state, keep_snapshots);
							break;
						}
	
						default:
							/** 
							 * Other kinds of hydrations
							 */
							PHALCON_CALL_CE_STATIC(&value, phalcon_mvc_model_ce, "cloneresultmaphydrate", row_model, column_map, hydrate_mode);
							break;
	
					}
	
					/** 
					 * The complete object is assigned to an attribute with the name of the alias or
					 * the model name
					 */
					PHALCON_OBS_NVAR(attribute);
					if (phalcon_array_isset_string(column, SS("balias"))) {
						phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY);
					}
				} else {
					/** 
					 * Scalar columns are simply assigned to the result object
					 */
					if (phalcon_array_isset_string(column, SS("sqlAlias"))) {
						PHALCON_OBS_NVAR(sql_alias);
						phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY);
	
						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch(&value, row, sql_alias, PH_NOISY);
					} else {
						PHALCON_OBS_NVAR(value);
						if (phalcon_array_isset(row, alias)) {
							phalcon_array_fetch(&value, row, alias, PH_NOISY);
						}
					}
	
					/** 
					 * If a 'balias' is defined is not an unnamed scalar
					 */
					if (phalcon_array_isset_string(column, SS("balias"))) {
						PHALCON_CPY_WRT(attribute, alias);
					} else {
						PHALCON_INIT_NVAR(n_alias);
						phalcon_fast_str_replace(n_alias, underscore, empty_str, alias);
						PHALCON_CPY_WRT(attribute, n_alias);
					}

					assert(attribute != NULL);
				}
	
				/** 
				 * Assign the instance according to the hydration type
				 */
				if (unlikely(!attribute)) {
					zend_throw_exception_ex(phalcon_mvc_model_exception_ce, 0 TSRMLS_CC, "Unexpected inconsistency: attribute is NULL");
					RETURN_MM();
				}
	
				switch (phalcon_get_intval(hydrate_mode)) {
	
					case 1:
						phalcon_array_update_zval(&active_row, attribute, value, PH_COPY | PH_SEPARATE);
						break;
	
					default:
						phalcon_update_property_zval_zval(active_row, attribute, value TSRMLS_CC);
						break;
	
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			/** 
			 * Store the generated row in this_ptr->activeRow to be retrieved by 'current'
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), active_row TSRMLS_CC);
		} else {
			/** 
			 * The row is already built so we just assign it to the activeRow
			 */
			phalcon_update_property_this(this_ptr, SL("_activeRow"), row TSRMLS_CC);
		}
		RETURN_MM_TRUE;
	}
	
	/** 
	 * There are no results to retrieve so we update this_ptr->activeRow as false
	 */
	phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC);
	RETURN_MM_FALSE;
}
Пример #26
0
/**
 * Applies a format to a message before sent it to the internal log
 *
 * @param string $message
 * @param int $type
 * @param int $timestamp
 * @param array $context
 * @return string
 */
PHP_METHOD(Phalcon_Logger_Formatter_Line, format){

	zval *message, *type, *timestamp, *context, *format = NULL, *date_format;
	zval *date, *date_wildcard, *new_format = NULL, *type_string = NULL;
	zval *type_wildcard, *message_wildcard;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 4, 0, &message, &type, &timestamp, &context);
	
	PHALCON_OBS_VAR(format);
	phalcon_read_property_this(&format, this_ptr, SL("_format"), PH_NOISY TSRMLS_CC);
	
	/** 
	 * Check if the format has the %date% placeholder
	 */
	if (phalcon_memnstr_str(format, SL("%date%"))) {
		date_format = phalcon_fetch_nproperty_this(this_ptr, SL("_dateFormat"), PH_NOISY TSRMLS_CC);
	
		PHALCON_INIT_VAR(date);
		phalcon_date(date, date_format, timestamp TSRMLS_CC);
	
		PHALCON_INIT_VAR(date_wildcard);
		ZVAL_STRING(date_wildcard, "%date%", 1);
	
		PHALCON_INIT_VAR(new_format);
		phalcon_fast_str_replace(new_format, date_wildcard, date, format);
	} else {
		PHALCON_CPY_WRT(new_format, format);
	}
	
	/** 
	 * Check if the format has the %type% placeholder
	 */
	if (phalcon_memnstr_str(format, SL("%type%"))) {
		PHALCON_CALL_METHOD(&type_string, this_ptr, "gettypestring", type);
	
		PHALCON_INIT_VAR(type_wildcard);
		ZVAL_STRING(type_wildcard, "%type%", 1);
	
		PHALCON_INIT_NVAR(format);
		phalcon_fast_str_replace(format, type_wildcard, type_string, new_format);
	} else {
		PHALCON_CPY_WRT(format, new_format);
	}
	
	PHALCON_INIT_VAR(message_wildcard);
	ZVAL_STRING(message_wildcard, "%message%", 1);
	
	PHALCON_INIT_NVAR(new_format);
	phalcon_fast_str_replace(new_format, message_wildcard, message, format);
	
	if (Z_TYPE_P(context) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(context)) > 0) {
		PHALCON_CALL_METHOD(&format, this_ptr, "interpolate", new_format, context);
	}
	else {
		PHALCON_CPY_WRT(format, new_format);
	}

	PHALCON_CONCAT_VS(return_value, format, PHP_EOL);
	RETURN_MM();
}
Пример #27
0
/**
 * 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(&current_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("&nbsp;\n") TSRMLS_CC);
				} else {
					if (PHALCON_IS_STRING(current_line, "\r\n")) {
						phalcon_concat_self_str(&html, SL("&nbsp;\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);
}
Пример #28
0
/**
 * Applies a format to a message before sent it to the internal log
 *
 * @param string $message
 * @param int $type
 * @param int $timestamp
 */
PHP_METHOD(Phalcon_Logger_Formatter_Line, format) {

    zval *message, *type, *timestamp, *format = NULL, *date_format;
    zval *date, *date_wildcard, *new_format = NULL, *type_string;
    zval *type_wildcard, *message_wildcard, *eol = NULL;

    PHALCON_MM_GROW();

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &message, &type, &timestamp) == FAILURE) {
        RETURN_MM_NULL();
    }

    PHALCON_OBS_VAR(format);
    phalcon_read_property(&format, this_ptr, SL("_format"), PH_NOISY_CC);

    /**
     * Check if the format has the %date% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%date%") TSRMLS_CC)) {
        PHALCON_OBS_VAR(date_format);
        phalcon_read_property(&date_format, this_ptr, SL("_dateFormat"), PH_NOISY_CC);

        PHALCON_INIT_VAR(date);
        PHALCON_CALL_FUNC_PARAMS_2(date, "date", date_format, timestamp);

        PHALCON_INIT_VAR(date_wildcard);
        ZVAL_STRING(date_wildcard, "%date%", 1);

        PHALCON_INIT_VAR(new_format);
        phalcon_fast_str_replace(new_format, date_wildcard, date, format TSRMLS_CC);
    } else {
        PHALCON_CPY_WRT(new_format, format);
    }

    /**
     * Check if the format has the %type% placeholder
     */
    if (phalcon_memnstr_str(format, SL("%type%") TSRMLS_CC)) {
        PHALCON_INIT_VAR(type_string);
        PHALCON_CALL_METHOD_PARAMS_1(type_string, this_ptr, "gettypestring", type);

        PHALCON_INIT_VAR(type_wildcard);
        ZVAL_STRING(type_wildcard, "%type%", 1);

        PHALCON_INIT_NVAR(format);
        phalcon_fast_str_replace(format, type_wildcard, type_string, new_format TSRMLS_CC);
    } else {
        PHALCON_CPY_WRT(format, new_format);
    }

    PHALCON_INIT_VAR(message_wildcard);
    ZVAL_STRING(message_wildcard, "%message%", 1);

    PHALCON_INIT_NVAR(new_format);
    phalcon_fast_str_replace(new_format, message_wildcard, message, format TSRMLS_CC);

    PHALCON_INIT_VAR(eol);

    PHALCON_INIT_NVAR(eol);
    ZVAL_STRING(eol, PHP_EOL, 1);

    PHALCON_INIT_NVAR(format);
    PHALCON_CONCAT_VV(format, new_format, eol);

    RETURN_CCTOR(format);
}
Пример #29
0
/**
 * Add a route to the router
 *
 * @param string $pattern
 * @param string/array $paths
 */
PHP_METHOD(Phalcon_Mvc_Router, add){

	zval *pattern = NULL, *paths = NULL, *parts = NULL, *route_paths = NULL, *pcre_pattern = NULL;
	zval *matches = NULL, *match = NULL, *n = NULL;
	zval *c0 = NULL, *c1 = NULL, *c2 = 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;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL;
	zval *a0 = NULL;
	zval *p0[] = { NULL, NULL, NULL, NULL };
	HashTable *ah0;
	HashPosition hp0;
	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, "zz", &pattern, &paths) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (Z_TYPE_P(paths) == IS_STRING) {
		PHALCON_INIT_VAR(c0);
		ZVAL_STRING(c0, "::", 1);
		PHALCON_INIT_VAR(parts);
		phalcon_fast_explode(parts, c0, paths TSRMLS_CC);
		
		PHALCON_INIT_VAR(route_paths);
		array_init(route_paths);
		
		PHALCON_ALLOC_ZVAL_MM(r0);
		phalcon_array_fetch_long(&r0, parts, 0, PH_NOISY_CC);
		
		PHALCON_ALLOC_ZVAL_MM(r1);
		PHALCON_CALL_FUNC_PARAMS_1(r1, "strtolower", r0);
		phalcon_array_update_string(&route_paths, SL("controller"), &r1, PH_COPY | PH_SEPARATE TSRMLS_CC);
		eval_int = phalcon_array_isset_long(parts, 1);
		if (eval_int) {
			PHALCON_ALLOC_ZVAL_MM(r2);
			phalcon_array_fetch_long(&r2, parts, 1, PH_NOISY_CC);
			phalcon_array_update_string(&route_paths, SL("action"), &r2, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	} else {
		PHALCON_CPY_WRT(route_paths, paths);
	}
	
	PHALCON_INIT_VAR(c1);
	ZVAL_STRING(c1, "#", 1);
	
	PHALCON_INIT_VAR(c2);
	ZVAL_STRING(c2, "\\#", 1);
	
	PHALCON_INIT_VAR(pcre_pattern);
	phalcon_fast_str_replace(pcre_pattern, c1, c2, pattern TSRMLS_CC);
	
	PHALCON_ALLOC_ZVAL_MM(r3);
	phalcon_fast_strpos_str(r3, pcre_pattern, SL("{") TSRMLS_CC);
	if (zend_is_true(r3)) {
		PHALCON_INIT_VAR(matches);
		array_init(matches);
		
		PHALCON_INIT_VAR(p0[0]);
		ZVAL_STRING(p0[0], "#{([a-zA-Z0-9\\_\\-]+):([^}]+)}#", 1);
		p0[1] = pcre_pattern;
		Z_SET_ISREF_P(matches);
		p0[2] = matches;
		
		PHALCON_INIT_VAR(t0);
		ZVAL_LONG(t0, 2);
		p0[3] = t0;
		
		PHALCON_ALLOC_ZVAL_MM(r4);
		PHALCON_CALL_FUNC_PARAMS(r4, "preg_match_all", 4, p0);
		Z_UNSET_ISREF_P(p0[2]);
		if (zend_is_true(r4)) {
			if (!phalcon_valid_foreach(matches TSRMLS_CC)) {
				return;
			}
			
			ah0 = Z_ARRVAL_P(matches);
			zend_hash_internal_pointer_reset_ex(ah0, &hp0);
			fes_c9ff_0:
				if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
					goto fee_c9ff_0;
				}
				
				PHALCON_INIT_VAR(n);
				PHALCON_GET_FOREACH_KEY(n, ah0, hp0);
				PHALCON_INIT_VAR(match);
				ZVAL_ZVAL(match, *hd, 1, 0);
				PHALCON_INIT_VAR(r5);
				phalcon_array_fetch_long(&r5, match, 0, PH_NOISY_CC);
				PHALCON_INIT_VAR(r6);
				phalcon_array_fetch_long(&r6, match, 2, PH_NOISY_CC);
				PHALCON_INIT_VAR(r7);
				PHALCON_CONCAT_SVS(r7, "(", r6, ")");
				PHALCON_INIT_VAR(r8);
				phalcon_fast_str_replace(r8, r5, r7, pcre_pattern TSRMLS_CC);
				PHALCON_CPY_WRT(pcre_pattern, r8);
				
				PHALCON_INIT_VAR(t1);
				ZVAL_LONG(t1, 1);
				
				PHALCON_INIT_VAR(r9);
				phalcon_add_function(r9, n, t1 TSRMLS_CC);
				
				PHALCON_INIT_VAR(r10);
				phalcon_array_fetch_long(&r10, match, 1, PH_NOISY_CC);
				phalcon_array_update_zval(&route_paths, r10, &r9, PH_COPY | PH_SEPARATE TSRMLS_CC);
				zend_hash_move_forward_ex(ah0, &hp0);
				goto fes_c9ff_0;
			fee_c9ff_0:
			if(0){}
			
		}
	}
	
	PHALCON_ALLOC_ZVAL_MM(a0);
	array_init(a0);
	
	PHALCON_ALLOC_ZVAL_MM(r11);
	PHALCON_CALL_METHOD_PARAMS_1(r11, this_ptr, "compilepattern", pcre_pattern, PH_NO_CHECK);
	phalcon_array_update_string(&a0, SL("pattern"), &r11, PH_COPY | PH_SEPARATE TSRMLS_CC);
	phalcon_array_update_string(&a0, SL("paths"), &route_paths, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
	PHALCON_ALLOC_ZVAL_MM(t2);
	phalcon_read_property(&t2, this_ptr, SL("_routes"), PH_NOISY_CC);
	phalcon_array_append(&t2, a0, 0 TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_routes"), t2 TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
Пример #30
0
/**
 * Checks for annotations in the public methods of the controller
 *
 * @param string $controller
 * @param string $action
 * @param Phalcon\Annotations\Annotation $annotation
 */
PHP_METHOD(Phalcon_Mvc_Router_Annotations, processActionAnnotation){

	zval *controller, *action, *annotation, *is_route = NULL;
	zval *methods = NULL, *name, *action_sufix, *empty_str;
	zval *real_action_name, *action_name, *route_prefix;
	zval *parameter = NULL, *paths = NULL, *position, *value, *uri = NULL, *route;
	zval *converts, *convert = NULL, *param = NULL, *route_name;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &controller, &action, &annotation) == FAILURE) {
		RETURN_MM_NULL();
	}

	PHALCON_INIT_VAR(is_route);
	ZVAL_BOOL(is_route, 0);
	
	PHALCON_INIT_VAR(methods);
	
	PHALCON_INIT_VAR(name);
	PHALCON_CALL_METHOD(name, annotation, "getname");
	
	/** 
	 * Find if the route is for adding routes
	 */
	if (PHALCON_IS_STRING(name, "Route")) {
		ZVAL_BOOL(is_route, 1);
	} else {
		if (PHALCON_IS_STRING(name, "Get")) {
			ZVAL_BOOL(is_route, 1);
	
			ZVAL_STRING(methods, "GET", 1);
		} else {
			if (PHALCON_IS_STRING(name, "Post")) {
				ZVAL_BOOL(is_route, 1);
	
				PHALCON_INIT_NVAR(methods);
				ZVAL_STRING(methods, "POST", 1);
			} else {
				if (PHALCON_IS_STRING(name, "Put")) {
					ZVAL_BOOL(is_route, 1);
	
					PHALCON_INIT_NVAR(methods);
					ZVAL_STRING(methods, "PUT", 1);
				} else {
					if (PHALCON_IS_STRING(name, "Options")) {
						ZVAL_BOOL(is_route, 1);
	
						PHALCON_INIT_NVAR(methods);
						ZVAL_STRING(methods, "OPTIONS", 1);
					}
				}
			}
		}
	}
	
	if (PHALCON_IS_TRUE(is_route)) {
	
		PHALCON_OBS_VAR(action_sufix);
		phalcon_read_property(&action_sufix, this_ptr, SL("_actionSufix"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(empty_str);
		ZVAL_STRING(empty_str, "", 1);
	
		PHALCON_INIT_VAR(real_action_name);
		phalcon_fast_str_replace(real_action_name, action_sufix, empty_str, action TSRMLS_CC);
	
		PHALCON_INIT_VAR(action_name);
		phalcon_fast_strtolower(action_name, real_action_name);
	
		PHALCON_OBS_VAR(route_prefix);
		phalcon_read_property(&route_prefix, this_ptr, SL("_routePrefix"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(parameter);
		ZVAL_STRING(parameter, "paths", 1);
	
		/** 
		 * Check for existing paths in the annotation
		 */
		PHALCON_INIT_VAR(paths);
		PHALCON_CALL_METHOD_PARAMS_1(paths, annotation, "getnamedparameter", parameter);
		if (Z_TYPE_P(paths) != IS_ARRAY) { 
			PHALCON_INIT_NVAR(paths);
			array_init(paths);
		}
	
		phalcon_array_update_string(&paths, SL("controller"), &controller, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_string(&paths, SL("action"), &action_name, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
		PHALCON_INIT_VAR(position);
		ZVAL_LONG(position, 0);
	
		PHALCON_INIT_VAR(value);
		PHALCON_CALL_METHOD_PARAMS_1(value, annotation, "getargument", position);
	
		/** 
		 * Create the route using the prefix
		 */
		if (Z_TYPE_P(value) != IS_NULL) {
			if (!PHALCON_IS_STRING(value, "/")) {
				PHALCON_INIT_VAR(uri);
				PHALCON_CONCAT_VV(uri, route_prefix, value);
			} else {
				PHALCON_CPY_WRT(uri, route_prefix);
			}
		} else {
			PHALCON_INIT_NVAR(uri);
			PHALCON_CONCAT_VV(uri, route_prefix, action_name);
		}
	
		/** 
		 * Add the route to the router
		 */
		PHALCON_INIT_VAR(route);
		PHALCON_CALL_METHOD_PARAMS_2(route, this_ptr, "add", uri, paths);
		if (Z_TYPE_P(methods) == IS_NULL) {
	
			PHALCON_INIT_NVAR(parameter);
			ZVAL_STRING(parameter, "methods", 1);
	
			PHALCON_INIT_NVAR(methods);
			PHALCON_CALL_METHOD_PARAMS_1(methods, annotation, "getnamedparameter", parameter);
			if (Z_TYPE_P(methods) == IS_ARRAY) { 
				PHALCON_CALL_METHOD_PARAMS_1_NORETURN(route, "via", methods);
			} else {
				if (Z_TYPE_P(methods) == IS_STRING) {
					PHALCON_CALL_METHOD_PARAMS_1_NORETURN(route, "via", methods);
				}
			}
		} else {
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(route, "via", methods);
		}
	
		PHALCON_INIT_NVAR(parameter);
		ZVAL_STRING(parameter, "converts", 1);
	
		PHALCON_INIT_VAR(converts);
		PHALCON_CALL_METHOD_PARAMS_1(converts, annotation, "getnamedparameter", parameter);
		if (Z_TYPE_P(converts) == IS_ARRAY) { 
	
			if (!phalcon_is_iterable(converts, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(param, ah0, hp0);
				PHALCON_GET_FOREACH_VALUE(convert);
	
				PHALCON_CALL_METHOD_PARAMS_2_NORETURN(route, "convert", param, convert);
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
		}
	
		PHALCON_INIT_NVAR(parameter);
		ZVAL_STRING(parameter, "name", 1);
	
		PHALCON_INIT_VAR(route_name);
		PHALCON_CALL_METHOD_PARAMS_1(route_name, annotation, "getnamedparameter", parameter);
		if (Z_TYPE_P(route_name) == IS_STRING) {
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(route, "setname", route_name);
		}
	
		RETURN_MM_TRUE;
	}
	
	PHALCON_MM_RESTORE();
}