Пример #1
0
/**
 * Resolves an annotation expression
 *
 * @param array $expr
 * @return mixed
 */
PHP_METHOD(Phalcon_Annotations_Annotation, getExpression){

	zval *expr = NULL, *type, *items, *item = NULL;
	zval *resolved_item = NULL, *exception_message;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &expr);
	
	PHALCON_SEPARATE_PARAM(expr);
	
	if (Z_TYPE_P(expr) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "The expression is not valid");
		return;
	}
	
	PHALCON_OBS_VAR(type);
	phalcon_array_fetch_string(&type, expr, SL("type"), PH_NOISY);
	
	switch (phalcon_get_intval(type)) {
	
		case PHANNOT_T_INTEGER:
		case PHANNOT_T_DOUBLE:
		case PHANNOT_T_STRING:
		case PHANNOT_T_IDENTIFIER:
			if (return_value_ptr) {
				zval_ptr_dtor(return_value_ptr);
				phalcon_array_fetch_string(return_value_ptr, expr, SL("value"), PH_NOISY);
			}
			else {
				phalcon_array_fetch_string(&return_value, expr, SL("value"), PH_NOISY);
			}
			RETURN_MM();
			/* no break because of implicit return */
	
		case PHANNOT_T_NULL:
			RETURN_MM_NULL();
			/* no break because of implicit return */
	
		case PHANNOT_T_FALSE:
			RETURN_MM_FALSE;
			/* no break because of implicit return */
	
		case PHANNOT_T_TRUE:
			RETURN_MM_TRUE;
			/* no break because of implicit return */
	
		case PHANNOT_T_ARRAY:
			PHALCON_OBS_VAR(items);
			phalcon_array_fetch_string(&items, expr, SL("items"), PH_NOISY);
	
			phalcon_is_iterable(items, &ah0, &hp0, 0, 0);
			array_init_size(return_value, zend_hash_num_elements(ah0));
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
				zval *name;
	
				PHALCON_GET_HVALUE(item);
	
				PHALCON_OBS_NVAR(expr);
				phalcon_array_fetch_string(&expr, item, SL("expr"), PH_NOISY);
	
				PHALCON_CALL_METHOD(&resolved_item, this_ptr, "getexpression", expr);
				if (phalcon_array_isset_string_fetch(&name, item, SS("name"))) {
					phalcon_array_update_zval(&return_value, name, resolved_item, PH_COPY);
				} else {
					phalcon_array_append(&return_value, resolved_item, 0);
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			RETURN_MM();
			/* no break because of implicit return */
	
		case PHANNOT_T_ANNOTATION:
			object_init_ex(return_value, phalcon_annotations_annotation_ce);
			PHALCON_CALL_METHOD(NULL, return_value, "__construct", expr);
			RETURN_MM();
			/* no break because of implicit return */
	
		default:
			PHALCON_INIT_VAR(exception_message);
			PHALCON_CONCAT_SVS(exception_message, "The expression ", type, " is unknown");
			PHALCON_THROW_EXCEPTION_ZVAL(phalcon_annotations_exception_ce, exception_message);
			return;
	
	}
	
	PHALCON_MM_RESTORE();
}
Пример #2
0
/**
 * Reads annotations from the class dockblocks, its methods and/or properties
 *
 * @param string $className
 * @return array
 */
PHP_METHOD(Phalcon_Annotations_Reader, parse){

	zval *class_name, *annotations, *reflection;
	zval *comment = NULL, *file = NULL, *line = NULL, *class_annotations;
	zval *properties, *annotations_properties;
	zval *property = NULL, *property_annotations = NULL, *name = NULL;
	zval *methods, *annotations_methods, *method = NULL;
	zval *method_annotations = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &class_name);
	
	if (unlikely(Z_TYPE_P(class_name) != IS_STRING)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "The class name must be an object");
		return;
	}
	
	PHALCON_INIT_VAR(annotations);
	array_init(annotations);
	
	/** 
	 * A ReflectionClass is used to obtain the class dockblock
	 */
	ce0 = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
	
	PHALCON_INIT_VAR(reflection);
	object_init_ex(reflection, ce0);
	if (phalcon_has_constructor(reflection TSRMLS_CC)) {
		phalcon_call_method_p1_noret(reflection, "__construct", class_name);
	}
	
	PHALCON_INIT_VAR(comment);
	phalcon_call_method(comment, reflection, "getdoccomment");
	if (Z_TYPE_P(comment) == IS_STRING) {
	
		/** 
		 * Get the file where the class was declared
		 */
		PHALCON_INIT_VAR(file);
		phalcon_call_method(file, reflection, "getfilename");
	
		/** 
		 * Get the line where the class was declared
		 */
		PHALCON_INIT_VAR(line);
		phalcon_call_method(line, reflection, "getstartline");
	
		/** 
		 * Read annotations from class
		 */
		PHALCON_INIT_VAR(class_annotations);
		if (phannot_parse_annotations(class_annotations, comment, file, line TSRMLS_CC) == FAILURE) {
			return;
		}
	
		/** 
		 * Append the class annotations to the annotations var
		 */
		if (Z_TYPE_P(class_annotations) == IS_ARRAY) { 
			phalcon_array_update_string(&annotations, SL("class"), &class_annotations, PH_COPY | PH_SEPARATE);
		}
	}
	
	/** 
	 * Get the class properties
	 */
	PHALCON_INIT_VAR(properties);
	phalcon_call_method(properties, reflection, "getproperties");
	if (phalcon_fast_count_ev(properties TSRMLS_CC)) {
	
		/** 
		 * Line declaration for properties isn't available
		 */
		PHALCON_INIT_NVAR(line);
		ZVAL_LONG(line, 1);
	
		PHALCON_INIT_VAR(annotations_properties);
		array_init(annotations_properties);
	
		phalcon_is_iterable(properties, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HVALUE(property);
	
			/** 
			 * Read comment from method
			 */
			PHALCON_INIT_NVAR(comment);
			phalcon_call_method(comment, property, "getdoccomment");
			if (Z_TYPE_P(comment) == IS_STRING) {
	
				/** 
				 * Get the file where the property was declared
				 */
				PHALCON_INIT_NVAR(file);
				phalcon_call_method(file, reflection, "getfilename");
	
				/** 
				 * Read annotations from the docblock
				 */
				PHALCON_INIT_NVAR(property_annotations);
				if (phannot_parse_annotations(property_annotations, comment, file, line TSRMLS_CC) == FAILURE) {
					return;
				}
				if (Z_TYPE_P(property_annotations) == IS_ARRAY) { 
					PHALCON_OBS_NVAR(name);
					phalcon_read_property(&name, property, SL("name"), PH_NOISY_CC);
					phalcon_array_update_zval(&annotations_properties, name, &property_annotations, PH_COPY | PH_SEPARATE);
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		if (phalcon_fast_count_ev(annotations_properties TSRMLS_CC)) {
			phalcon_array_update_string(&annotations, SL("properties"), &annotations_properties, PH_COPY | PH_SEPARATE);
		}
	}
	
	/** 
	 * Get the class methods
	 */
	PHALCON_INIT_VAR(methods);
	phalcon_call_method(methods, reflection, "getmethods");
	if (phalcon_fast_count_ev(methods TSRMLS_CC)) {
	
		PHALCON_INIT_VAR(annotations_methods);
		array_init(annotations_methods);
	
		phalcon_is_iterable(methods, &ah1, &hp1, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_HVALUE(method);
	
			/** 
			 * Read comment from method
			 */
			PHALCON_INIT_NVAR(comment);
			phalcon_call_method(comment, method, "getdoccomment");
			if (Z_TYPE_P(comment) == IS_STRING) {
	
				/** 
				 * Get the file where the method was declared
				 */
				PHALCON_INIT_NVAR(file);
				phalcon_call_method(file, method, "getfilename");
	
				/** 
				 * Get the line where the method was declared
				 */
				PHALCON_INIT_NVAR(line);
				phalcon_call_method(line, method, "getstartline");
	
				/** 
				 * Read annotations from class
				 */
				PHALCON_INIT_NVAR(method_annotations);
				if (phannot_parse_annotations(method_annotations, comment, file, line TSRMLS_CC) == FAILURE) {
					return;
				}
				if (Z_TYPE_P(method_annotations) == IS_ARRAY) { 
					PHALCON_OBS_NVAR(name);
					phalcon_read_property(&name, method, SL("name"), PH_NOISY_CC);
					phalcon_array_update_zval(&annotations_methods, name, &method_annotations, PH_COPY | PH_SEPARATE);
				}
			}
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
		if (phalcon_fast_count_ev(annotations_methods TSRMLS_CC)) {
			phalcon_array_update_string(&annotations, SL("methods"), &annotations_methods, PH_COPY | PH_SEPARATE);
		}
	}
	
	RETURN_CTOR(annotations);
}
Пример #3
0
/**
 * Appends a NOT IN condition to the current conditions
 *
 *<code>
 *	$criteria->notInWhere('id', [1, 2, 3]);
 *</code>
 *
 * @param string $expr
 * @param array $values
 * @return Phalcon\Mvc\Model\Query\Builder
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, notInWhere){

	zval *expr, *values, *hidden_param, *bind_params;
	zval *bind_keys, *value = NULL, *key = NULL, *query_key = NULL, *joined_keys;
	zval *conditions;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &expr, &values);
	
	if (Z_TYPE_P(values) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Values must be an array");
		return;
	}
	
	PHALCON_OBS_VAR(hidden_param);
	phalcon_read_property_this(&hidden_param, this_ptr, SL("_hiddenParamNumber"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(bind_params);
	array_init(bind_params);
	
	PHALCON_INIT_VAR(bind_keys);
	array_init(bind_keys);
	
	phalcon_is_iterable(values, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HVALUE(value);
	
		/** 
		 * Key with auto bind-params
		 */
		PHALCON_INIT_NVAR(key);
		PHALCON_CONCAT_SV(key, "phi", hidden_param);
	
		PHALCON_INIT_NVAR(query_key);
		PHALCON_CONCAT_SVS(query_key, ":", key, ":");
		phalcon_array_append(&bind_keys, query_key, PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_zval(&bind_params, key, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
		PHALCON_SEPARATE(hidden_param);
		phalcon_increment(hidden_param);
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	PHALCON_INIT_VAR(joined_keys);
	phalcon_fast_join_str(joined_keys, SL(", "), bind_keys TSRMLS_CC);
	
	/** 
	 * Create a standard IN condition with bind params
	 */
	PHALCON_INIT_VAR(conditions);
	PHALCON_CONCAT_VSVS(conditions, expr, " NOT IN (", joined_keys, ")");
	
	/** 
	 * Append the IN to the current conditions using and 'and'
	 */
	phalcon_call_method_p2_noret(this_ptr, "andwhere", conditions, bind_params);
	phalcon_update_property_this(this_ptr, SL("_hiddenParamNumber"), hidden_param TSRMLS_CC);
	
	RETURN_THIS();
}
Пример #4
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_p1(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);
			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);
	
			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:
							/** 
							 * 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);
							} 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);
	
							/** 
							 * 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);
				} 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);
						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);
					}
				}
	
				/** 
				 * 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);
						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;
}
Пример #5
0
/**
 * The meta-data is obtained by reading the column descriptions from the database information schema
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param Phalcon\DiInterface $dependencyInjector
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Introspection, getMetaData) {

    zval *model, *dependency_injector, *class_name;
    zval *schema, *table, *read_connection, *exists;
    zval *complete_table = NULL, *exception_message = NULL;
    zval *columns, *attributes, *primary_keys, *non_primary_keys;
    zval *numeric_typed, *not_null, *field_types;
    zval *field_bind_types, *automatic_default;
    zval *identity_field = NULL, *column = NULL, *field_name = NULL, *feature = NULL;
    zval *type = NULL, *bind_type = NULL, *model_metadata;
    HashTable *ah0;
    HashPosition hp0;
    zval **hd;

    PHALCON_MM_GROW();

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

    PHALCON_INIT_VAR(class_name);
    phalcon_get_class(class_name, model, 0 TSRMLS_CC);

    PHALCON_INIT_VAR(schema);
    phalcon_call_method(schema, model, "getschema");

    PHALCON_INIT_VAR(table);
    phalcon_call_method(table, model, "getsource");

    /**
     * Check if the mapped table exists on the database
     */
    PHALCON_INIT_VAR(read_connection);
    phalcon_call_method(read_connection, model, "getreadconnection");

    PHALCON_INIT_VAR(exists);
    phalcon_call_method_p2(exists, read_connection, "tableexists", table, schema);
    if (!zend_is_true(exists)) {
        if (zend_is_true(schema)) {
            PHALCON_INIT_VAR(complete_table);
            PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
        } else {
            PHALCON_CPY_WRT(complete_table, table);
        }

        /**
         * The table not exists
         */
        PHALCON_INIT_VAR(exception_message);
        PHALCON_CONCAT_SVSV(exception_message, "Table \"", complete_table, "\" doesn't exist on database when dumping meta-data for ", class_name);
        PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
        return;
    }

    /**
     * Try to describe the table
     */
    PHALCON_INIT_VAR(columns);
    phalcon_call_method_p2(columns, read_connection, "describecolumns", table, schema);
    if (!phalcon_fast_count_ev(columns TSRMLS_CC)) {
        if (zend_is_true(schema)) {
            PHALCON_INIT_NVAR(complete_table);
            PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
        } else {
            PHALCON_CPY_WRT(complete_table, table);
        }

        /**
         * The table not exists
         */
        PHALCON_INIT_NVAR(exception_message);
        PHALCON_CONCAT_SVSV(exception_message, "Cannot obtain table columns for the mapped source \"", complete_table, "\" used in model ", class_name);
        PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
        return;
    }

    /**
     * Initialize meta-data
     */
    PHALCON_INIT_VAR(attributes);
    array_init(attributes);

    PHALCON_INIT_VAR(primary_keys);
    array_init(primary_keys);

    PHALCON_INIT_VAR(non_primary_keys);
    array_init(non_primary_keys);

    PHALCON_INIT_VAR(numeric_typed);
    array_init(numeric_typed);

    PHALCON_INIT_VAR(not_null);
    array_init(not_null);

    PHALCON_INIT_VAR(field_types);
    array_init(field_types);

    PHALCON_INIT_VAR(field_bind_types);
    array_init(field_bind_types);

    PHALCON_INIT_VAR(automatic_default);
    array_init(automatic_default);

    PHALCON_INIT_VAR(identity_field);
    ZVAL_BOOL(identity_field, 0);

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

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

        PHALCON_GET_HVALUE(column);

        PHALCON_INIT_NVAR(field_name);
        phalcon_call_method(field_name, column, "getname");
        phalcon_array_append(&attributes, field_name, PH_SEPARATE);

        /**
         * To mark fields as primary keys
         */
        PHALCON_INIT_NVAR(feature);
        phalcon_call_method(feature, column, "isprimary");
        if (PHALCON_IS_TRUE(feature)) {
            phalcon_array_append(&primary_keys, field_name, PH_SEPARATE);
        } else {
            phalcon_array_append(&non_primary_keys, field_name, PH_SEPARATE);
        }

        /**
         * To mark fields as numeric
         */
        PHALCON_INIT_NVAR(feature);
        phalcon_call_method(feature, column, "isnumeric");
        if (PHALCON_IS_TRUE(feature)) {
            phalcon_array_update_zval_bool(&numeric_typed, field_name, 1, PH_SEPARATE);
        }

        /**
         * To mark fields as not null
         */
        PHALCON_INIT_NVAR(feature);
        phalcon_call_method(feature, column, "isnotnull");
        if (PHALCON_IS_TRUE(feature)) {
            phalcon_array_append(&not_null, field_name, PH_SEPARATE);
        }

        /**
         * To mark fields as identity columns
         */
        PHALCON_INIT_NVAR(feature);
        phalcon_call_method(feature, column, "isautoincrement");
        if (PHALCON_IS_TRUE(feature)) {
            PHALCON_CPY_WRT(identity_field, field_name);
        }

        /**
         * To get the internal types
         */
        PHALCON_INIT_NVAR(type);
        phalcon_call_method(type, column, "gettype");
        phalcon_array_update_zval(&field_types, field_name, &type, PH_COPY | PH_SEPARATE);

        /**
         * To mark how the fields must be escaped
         */
        PHALCON_INIT_NVAR(bind_type);
        phalcon_call_method(bind_type, column, "getbindtype");
        phalcon_array_update_zval(&field_bind_types, field_name, &bind_type, PH_COPY | PH_SEPARATE);

        zend_hash_move_forward_ex(ah0, &hp0);
    }

    /**
     * Create an array using the MODELS_* constants as indexes
     */
    PHALCON_INIT_VAR(model_metadata);
    array_init(model_metadata);
    phalcon_array_update_long(&model_metadata, 0, &attributes, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 3, &not_null, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 4, &field_types, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 9, &field_bind_types, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 10, &automatic_default, PH_COPY | PH_SEPARATE);
    phalcon_array_update_long(&model_metadata, 11, &automatic_default, PH_COPY | PH_SEPARATE);

    RETURN_CTOR(model_metadata);
}
Пример #6
0
/**
 * Process a request header and return an array of values with their qualities
 *
 * @param string $serverIndex
 * @param string $name
 * @return array
 */
PHP_METHOD(Phalcon_Http_Request, _getQualityHeader){

	zval *server_index = NULL, *name = NULL, *quality_one = NULL, *returned_parts = NULL;
	zval *http_server = NULL, *pattern = NULL, *parts = NULL, *part = NULL, *header_parts = NULL;
	zval *quality_part = NULL, *quality = NULL, *header_name = NULL;
	zval *c0 = NULL, *c1 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	int eval_int;

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

	PHALCON_INIT_VAR(quality_one);
	ZVAL_DOUBLE(quality_one, 1);
	
	PHALCON_INIT_VAR(returned_parts);
	array_init(returned_parts);
	
	PHALCON_INIT_VAR(http_server);
	PHALCON_CALL_METHOD_PARAMS_1(http_server, this_ptr, "getserver", server_index, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(pattern);
	ZVAL_STRING(pattern, "/,\\s*/", 1);
	
	PHALCON_INIT_VAR(parts);
	PHALCON_CALL_FUNC_PARAMS_2(parts, "preg_split", pattern, http_server);
	if (!phalcon_valid_foreach(parts TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(parts);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	fes_ac06_2:
		if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
			goto fee_ac06_2;
		}
		
		PHALCON_INIT_VAR(part);
		ZVAL_ZVAL(part, *hd, 1, 0);
		PHALCON_INIT_VAR(c0);
		ZVAL_STRING(c0, ";", 1);
		PHALCON_INIT_VAR(header_parts);
		phalcon_fast_explode(header_parts, c0, part TSRMLS_CC);
		eval_int = phalcon_array_isset_long(header_parts, 1);
		if (eval_int) {
			PHALCON_INIT_VAR(quality_part);
			phalcon_array_fetch_long(&quality_part, header_parts, 1, PH_NOISY_CC);
			
			PHALCON_INIT_VAR(c1);
			ZVAL_LONG(c1, 2);
			
			PHALCON_INIT_VAR(quality);
			PHALCON_CALL_FUNC_PARAMS_2(quality, "substr", quality_part, c1);
		} else {
			PHALCON_CPY_WRT(quality, quality_one);
		}
		
		PHALCON_INIT_VAR(header_name);
		phalcon_array_fetch_long(&header_name, header_parts, 0, PH_NOISY_CC);
		
		PHALCON_INIT_VAR(quality_part);
		array_init(quality_part);
		phalcon_array_update_zval(&quality_part, name, &header_name, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_string(&quality_part, SL("quality"), &quality, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_append(&returned_parts, quality_part, PH_SEPARATE TSRMLS_CC);
		zend_hash_move_forward_ex(ah0, &hp0);
		goto fes_ac06_2;
	fee_ac06_2:
	
	
	RETURN_CTOR(returned_parts);
}
Пример #7
0
/**
 * Attach a listener to the events manager
 *
 * @param string $eventType
 * @param callable $handler
 * @param int $priority
 */
PHP_METHOD(Phalcon_Events_Manager, attach){

	zval *event_type, *handler, *_priority = NULL, priority = {}, events = {}, listener = {}, enable_priorities = {}, priority_queue = {};

	phalcon_fetch_params(0, 2, 1, &event_type, &handler, &_priority);

	if (!_priority) {
		ZVAL_LONG(&priority, 100);
	} else {
		PHALCON_CPY_WRT(&priority, _priority);
	}

	if (unlikely(Z_TYPE_P(event_type) != IS_STRING)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_events_exception_ce, "Event type must be a string");
		return;
	}

	if (Z_TYPE_P(handler) != IS_OBJECT && !phalcon_is_callable(handler)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_events_exception_ce, "Event handler must be an object or callable");
		return;
	}

	if (phalcon_instance_of_ev(handler, phalcon_events_listener_ce)) {
		PHALCON_CPY_WRT(&listener, handler);
		PHALCON_CALL_METHODW(NULL, &listener, "setpriority", &priority);
		PHALCON_CALL_METHODW(NULL, &listener, "setevent", event_type);
	} else {
		object_init_ex(&listener, phalcon_events_listener_ce);
		PHALCON_CALL_METHODW(NULL, &listener, "__construct", handler, &priority, event_type);
	}

	phalcon_return_property(&events, getThis(), SL("_events"));
	if (Z_TYPE(events) != IS_ARRAY) {
		array_init(&events);
	}

	if (!phalcon_array_isset_fetch(&priority_queue, &events, event_type)) {
		phalcon_return_property(&enable_priorities, getThis(), SL("_enablePriorities"));
		if (zend_is_true(&enable_priorities)) {
			/** 
			 * Create a SplPriorityQueue to store the events with priorities
			 */
			object_init_ex(&priority_queue, spl_ce_SplPriorityQueue);
			if (phalcon_has_constructor(&priority_queue)) {
				PHALCON_CALL_METHODW(NULL, &priority_queue, "__construct");
			}

			/** 
			 * Extract only the Data, Set extraction flags
			 */
			PHALCON_CALL_METHODW(NULL, &priority_queue, "setextractflags", &PHALCON_GLOBAL(z_one));

			/** 
			 * Append the events to the queue
			 */
			phalcon_array_update_zval(&events, event_type, &priority_queue, PH_COPY);
			phalcon_update_property_this(getThis(), SL("_events"), &events);
		} else {
			array_init(&priority_queue);
		}
	}

	/** 
	 * Insert the handler in the queue
	 */
	if (unlikely(Z_TYPE(priority_queue) == IS_OBJECT)) {
		PHALCON_CALL_METHODW(NULL, &priority_queue, "insert", &listener, &priority);
	} else {
		phalcon_array_append(&priority_queue, &listener, PH_COPY);
	}

	/** 
	 * Append the events to the queue
	 */
	phalcon_array_update_zval(&events, event_type, &priority_queue, PH_COPY);
	phalcon_update_property_this(getThis(), SL("_events"), &events);
}
Пример #8
0
/**
 * Loads registered template engines, if none is registered it will use Phalcon\Mvc\View\Engine\Php
 *
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_View, _loadTemplateEngines){

	zval *engines = NULL, *dependency_injector = NULL, *registered_engines = NULL;
	zval *number_engines = NULL, *php_engine = NULL, *arguments = NULL;
	zval *engine_service = NULL, *extension = NULL, *engine_object = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;

	PHALCON_MM_GROW();
	PHALCON_INIT_VAR(engines);
	phalcon_read_property(&engines, this_ptr, SL("_engines"), PH_NOISY_CC);
	if (Z_TYPE_P(engines) == IS_BOOL && !Z_BVAL_P(engines)) {
		PHALCON_INIT_VAR(dependency_injector);
		phalcon_read_property(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
		
		PHALCON_INIT_VAR(engines);
		array_init(engines);
		
		PHALCON_INIT_VAR(registered_engines);
		phalcon_read_property(&registered_engines, this_ptr, SL("_registeredEngines"), PH_NOISY_CC);
		
		PHALCON_INIT_VAR(number_engines);
		phalcon_fast_count(number_engines, registered_engines TSRMLS_CC);
		if (phalcon_compare_strict_long(number_engines, 0 TSRMLS_CC)) {
			PHALCON_INIT_VAR(php_engine);
			object_init_ex(php_engine, phalcon_mvc_view_engine_php_ce);
			PHALCON_CALL_METHOD_PARAMS_2_NORETURN(php_engine, "__construct", this_ptr, dependency_injector, PH_CHECK);
			phalcon_array_update_string(&engines, SL(".phtml"), &php_engine, PH_COPY | PH_SEPARATE TSRMLS_CC);
		} else {
			if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_view_exception_ce, "A dependency injector container is required to obtain the application services");
				return;
			}
			
			PHALCON_INIT_VAR(arguments);
			array_init(arguments);
			phalcon_array_append(&arguments, this_ptr, PH_SEPARATE TSRMLS_CC);
			phalcon_array_append(&arguments, dependency_injector, PH_SEPARATE TSRMLS_CC);
			if (!phalcon_valid_foreach(registered_engines TSRMLS_CC)) {
				return;
			}
			
			ah0 = Z_ARRVAL_P(registered_engines);
			zend_hash_internal_pointer_reset_ex(ah0, &hp0);
			fes_ecde_0:
				if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
					goto fee_ecde_0;
				}
				
				PHALCON_INIT_VAR(extension);
				PHALCON_GET_FOREACH_KEY(extension, ah0, hp0);
				PHALCON_INIT_VAR(engine_service);
				ZVAL_ZVAL(engine_service, *hd, 1, 0);
				PHALCON_INIT_VAR(engine_object);
				PHALCON_CALL_METHOD_PARAMS_2(engine_object, dependency_injector, "getshared", engine_service, arguments, PH_NO_CHECK);
				phalcon_array_update_zval(&engines, extension, &engine_object, PH_COPY | PH_SEPARATE TSRMLS_CC);
				zend_hash_move_forward_ex(ah0, &hp0);
				goto fes_ecde_0;
			fee_ecde_0:
			if(0){}
			
		}
		
		phalcon_update_property_zval(this_ptr, SL("_engines"), engines TSRMLS_CC);
	} else {
		PHALCON_INIT_VAR(engines);
		phalcon_read_property(&engines, this_ptr, SL("_engines"), PH_NOISY_CC);
	}
	
	
	RETURN_CCTOR(engines);
}
Пример #9
0
/**
 * Lists table indexes
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Index[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo, describeIndexes){

	zval *table = NULL, *schema = NULL, *dialect = NULL, *fetch_assoc = NULL, *sql = NULL;
	zval *describe = NULL, *indexes = NULL, *index = NULL, *key_name = NULL, *empty_arr = NULL;
	zval *column_name = NULL, *index_objects = NULL, *index_columns = NULL;
	zval *name = 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|z", &table, &schema) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!schema) {
		PHALCON_ALLOC_ZVAL_MM(schema);
		ZVAL_NULL(schema);
	}
	
	PHALCON_INIT_VAR(dialect);
	phalcon_read_property(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(fetch_assoc);
	phalcon_get_class_constant(fetch_assoc, phalcon_db_ce, SL("FETCH_ASSOC") TSRMLS_CC);
	
	PHALCON_INIT_VAR(sql);
	PHALCON_CALL_METHOD_PARAMS_2(sql, dialect, "describeindexes", table, schema, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(describe);
	PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_assoc, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(indexes);
	array_init(indexes);
	
	if (!phalcon_valid_foreach(describe TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(describe);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	
	ph_cycle_start_0:
	
		if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
			goto ph_cycle_end_0;
		}
		
		PHALCON_GET_FOREACH_VALUE(index);
		
		PHALCON_INIT_VAR(key_name);
		phalcon_array_fetch_string(&key_name, index, SL("key_name"), PH_NOISY_CC);
		eval_int = phalcon_array_isset(indexes, key_name);
		if (!eval_int) {
			PHALCON_INIT_VAR(empty_arr);
			array_init(empty_arr);
			phalcon_array_update_zval(&indexes, key_name, &empty_arr, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
		
		PHALCON_INIT_VAR(column_name);
		phalcon_array_fetch_string(&column_name, index, SL("column_name"), PH_NOISY_CC);
		phalcon_array_update_append_multi_2(&indexes, key_name, column_name, 0 TSRMLS_CC);
		
		zend_hash_move_forward_ex(ah0, &hp0);
		goto ph_cycle_start_0;
	
	ph_cycle_end_0:
	
	PHALCON_INIT_VAR(index_objects);
	array_init(index_objects);
	
	if (!phalcon_valid_foreach(indexes TSRMLS_CC)) {
		return;
	}
	
	ah1 = Z_ARRVAL_P(indexes);
	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_INIT_VAR(name);
		PHALCON_GET_FOREACH_KEY(name, ah1, hp1);
		PHALCON_GET_FOREACH_VALUE(index_columns);
		
		PHALCON_INIT_VAR(index);
		object_init_ex(index, phalcon_db_index_ce);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(index, "__construct", name, index_columns, PH_CHECK);
		phalcon_array_update_zval(&index_objects, name, &index, PH_COPY | PH_SEPARATE TSRMLS_CC);
		
		zend_hash_move_forward_ex(ah1, &hp1);
		goto ph_cycle_start_1;
	
	ph_cycle_end_1:
	
	
	RETURN_CTOR(index_objects);
}
Пример #10
0
/**
 * Lists table references
 *
 * @param string $table
 * @param string $schema
 * @return Phalcon\Db\Reference[]
 */
PHP_METHOD(Phalcon_Db_Adapter_Pdo_Sqlite, describeReferences){

	zval *table, *schema = NULL, *dialect, *sql, *fetch_assoc;
	zval *describe, *reference_objects, *reference_describe = NULL;
	zval *number = NULL, *constraint_name = NULL, *referenced_table = NULL;
	zval *from = NULL, *to = NULL, *columns = NULL, *referenced_columns = NULL;
	zval *reference_array = NULL, *reference = 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, "z|z", &table, &schema) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!schema) {
		PHALCON_INIT_NVAR(schema);
	}
	
	PHALCON_INIT_VAR(dialect);
	phalcon_read_property(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(sql);
	PHALCON_CALL_METHOD_PARAMS_2(sql, dialect, "describereferences", table, schema, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(fetch_assoc);
	phalcon_get_class_constant(fetch_assoc, phalcon_db_ce, SS("FETCH_ASSOC") TSRMLS_CC);
	
	PHALCON_INIT_VAR(describe);
	PHALCON_CALL_METHOD_PARAMS_2(describe, this_ptr, "fetchall", sql, fetch_assoc, PH_NO_CHECK);
	
	PHALCON_INIT_VAR(reference_objects);
	array_init(reference_objects);
	
	if (!phalcon_valid_foreach(describe TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(describe);
	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(number, ah0, hp0);
		PHALCON_GET_FOREACH_VALUE(reference_describe);
		
		PHALCON_INIT_NVAR(constraint_name);
		PHALCON_CONCAT_SV(constraint_name, "foreign_key_", number);
		
		PHALCON_INIT_NVAR(referenced_table);
		phalcon_array_fetch_string(&referenced_table, reference_describe, SL("table"), PH_NOISY_CC);
		
		PHALCON_INIT_NVAR(from);
		phalcon_array_fetch_string(&from, reference_describe, SL("from"), PH_NOISY_CC);
		
		PHALCON_INIT_NVAR(to);
		phalcon_array_fetch_string(&to, reference_describe, SL("to"), PH_NOISY_CC);
		
		PHALCON_INIT_NVAR(columns);
		array_init(columns);
		phalcon_array_append(&columns, from, PH_SEPARATE TSRMLS_CC);
		
		PHALCON_INIT_NVAR(referenced_columns);
		array_init(referenced_columns);
		phalcon_array_append(&referenced_columns, to, PH_SEPARATE TSRMLS_CC);
		
		PHALCON_INIT_NVAR(reference_array);
		array_init(reference_array);
		add_assoc_null_ex(reference_array, SS("referencedSchema"));
		phalcon_array_update_string(&reference_array, SL("referencedTable"), &referenced_table, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_string(&reference_array, SL("columns"), &columns, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_string(&reference_array, SL("referencedColumns"), &referenced_columns, PH_COPY | PH_SEPARATE TSRMLS_CC);
		
		PHALCON_INIT_NVAR(reference);
		object_init_ex(reference, phalcon_db_reference_ce);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(reference, "__construct", constraint_name, reference_array, PH_CHECK);
		phalcon_array_update_zval(&reference_objects, constraint_name, &reference, PH_COPY | PH_SEPARATE TSRMLS_CC);
		
		zend_hash_move_forward_ex(ah0, &hp0);
		goto ph_cycle_start_0;
		
	ph_cycle_end_0:
	
	
	RETURN_CTOR(reference_objects);
}
Пример #11
0
/**
 * Cache the actual view render to certain level
 *
 * @param boolean|array $options
 */
PHP_METHOD(Phalcon_Mvc_View, cache){

	zval *options = NULL, *view_options = NULL, *cache_options = NULL;
	zval *value = NULL, *key = NULL, *cache_level = 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", &options) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!options) {
		PHALCON_ALLOC_ZVAL_MM(options);
		ZVAL_BOOL(options, 1);
	}
	
	if (Z_TYPE_P(options) == IS_ARRAY) { 
		PHALCON_INIT_VAR(view_options);
		phalcon_read_property(&view_options, this_ptr, SL("_options"), PH_NOISY_CC);
		eval_int = phalcon_array_isset_string(view_options, SL("cache")+1);
		if (eval_int) {
			PHALCON_INIT_VAR(cache_options);
			phalcon_array_fetch_string(&cache_options, view_options, SL("cache"), PH_NOISY_CC);
		} else {
			PHALCON_INIT_VAR(cache_options);
			array_init(cache_options);
		}
		
		if (!phalcon_valid_foreach(options TSRMLS_CC)) {
			return;
		}
		
		ah0 = Z_ARRVAL_P(options);
		zend_hash_internal_pointer_reset_ex(ah0, &hp0);
		fes_ecde_4:
			if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
				goto fee_ecde_4;
			}
			
			PHALCON_INIT_VAR(key);
			PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
			PHALCON_INIT_VAR(value);
			ZVAL_ZVAL(value, *hd, 1, 0);
			phalcon_array_update_zval(&cache_options, key, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
			zend_hash_move_forward_ex(ah0, &hp0);
			goto fes_ecde_4;
		fee_ecde_4:
		
		eval_int = phalcon_array_isset_string(cache_options, SL("level")+1);
		if (eval_int) {
			PHALCON_INIT_VAR(cache_level);
			phalcon_array_fetch_string(&cache_level, cache_options, SL("level"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_cacheLevel"), cache_level TSRMLS_CC);
		} else {
			phalcon_update_property_long(this_ptr, SL("_cacheLevel"), 5 TSRMLS_CC);
		}
		
		phalcon_update_property_zval(view_options, SL("cache"), cache_options TSRMLS_CC);
		phalcon_update_property_zval(this_ptr, SL("_options"), view_options TSRMLS_CC);
	} else {
		if (zend_is_true(options)) {
			phalcon_update_property_long(this_ptr, SL("_cacheLevel"), 5 TSRMLS_CC);
		} else {
			phalcon_update_property_long(this_ptr, SL("_cacheLevel"), 0 TSRMLS_CC);
		}
	}
	
	PHALCON_MM_RESTORE();
}
Пример #12
0
/**
 * Initialize the metadata for certain table
 *
 * @param Phalcon\Mvc\Model $model
 * @param string $table
 * @param string $schema
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, _initializeMetaData){

	zval *model = NULL, *table = NULL, *schema = NULL, *key = NULL, *connection = NULL, *exists = NULL;
	zval *complete_table = NULL, *attributes = NULL, *primary_keys = NULL;
	zval *non_primary_keys = NULL, *numeric_typed = NULL, *not_null = NULL;
	zval *field_types = NULL, *identity_field = NULL, *columns = NULL;
	zval *column = NULL, *field_name = NULL, *table_metadata = NULL;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL;
	zval *i0 = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL;
	zval *a0 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	int eval_int;

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

	PHALCON_INIT_VAR(key);
	PHALCON_CONCAT_VV(key, schema, table);
	
	PHALCON_ALLOC_ZVAL_MM(t0);
	phalcon_read_property(&t0, this_ptr, SL("_metaData"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(t0, key);
	if (!eval_int) {
		PHALCON_INIT_VAR(connection);
		PHALCON_CALL_METHOD(connection, model, "getconnection", PH_NO_CHECK);
		
		PHALCON_INIT_VAR(exists);
		PHALCON_CALL_METHOD_PARAMS_2(exists, connection, "tableexists", table, schema, PH_NO_CHECK);
		if (!zend_is_true(exists)) {
			if (zend_is_true(schema)) {
				PHALCON_INIT_VAR(complete_table);
				PHALCON_CONCAT_VSV(complete_table, schema, "\".\"", table);
			} else {
				PHALCON_CPY_WRT(complete_table, table);
			}
			
			PHALCON_ALLOC_ZVAL_MM(i0);
			object_init_ex(i0, phalcon_mvc_model_exception_ce);
			
			PHALCON_ALLOC_ZVAL_MM(r0);
			phalcon_get_class(r0, model TSRMLS_CC);
			
			PHALCON_ALLOC_ZVAL_MM(r1);
			PHALCON_CONCAT_SVSV(r1, "Table \"", complete_table, "\" doesn't exist on database when dumping meta-data for ", r0);
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(i0, "__construct", r1, PH_CHECK);
			phalcon_throw_exception(i0 TSRMLS_CC);
			return;
		}
		
		PHALCON_INIT_VAR(attributes);
		array_init(attributes);
		
		PHALCON_INIT_VAR(primary_keys);
		array_init(primary_keys);
		
		PHALCON_INIT_VAR(non_primary_keys);
		array_init(non_primary_keys);
		
		PHALCON_INIT_VAR(numeric_typed);
		array_init(numeric_typed);
		
		PHALCON_INIT_VAR(not_null);
		array_init(not_null);
		
		PHALCON_INIT_VAR(field_types);
		array_init(field_types);
		
		PHALCON_INIT_VAR(identity_field);
		ZVAL_BOOL(identity_field, 0);
		
		PHALCON_INIT_VAR(columns);
		PHALCON_CALL_METHOD_PARAMS_2(columns, connection, "describecolumns", table, schema, PH_NO_CHECK);
		if (!phalcon_valid_foreach(columns TSRMLS_CC)) {
			return;
		}
		
		ah0 = Z_ARRVAL_P(columns);
		zend_hash_internal_pointer_reset_ex(ah0, &hp0);
		fes_c40c_0:
			if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
				goto fee_c40c_0;
			}
			
			PHALCON_INIT_VAR(column);
			ZVAL_ZVAL(column, *hd, 1, 0);
			PHALCON_INIT_VAR(field_name);
			PHALCON_CALL_METHOD(field_name, column, "getname", PH_NO_CHECK);
			phalcon_array_append(&attributes, field_name, PH_SEPARATE TSRMLS_CC);
			
			PHALCON_INIT_VAR(r2);
			PHALCON_CALL_METHOD(r2, column, "isprimary", PH_NO_CHECK);
			if (zend_is_true(r2)) {
				phalcon_array_append(&primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
			} else {
				phalcon_array_append(&non_primary_keys, field_name, PH_SEPARATE TSRMLS_CC);
			}
			
			PHALCON_INIT_VAR(r3);
			PHALCON_CALL_METHOD(r3, column, "isnumeric", PH_NO_CHECK);
			if (zend_is_true(r3)) {
				phalcon_array_update_zval_bool(&numeric_typed, field_name, 1, PH_SEPARATE TSRMLS_CC);
			}
			
			PHALCON_INIT_VAR(r4);
			PHALCON_CALL_METHOD(r4, column, "isnotnull", PH_NO_CHECK);
			if (zend_is_true(r4)) {
				phalcon_array_append(&not_null, field_name, PH_SEPARATE TSRMLS_CC);
			}
			
			PHALCON_INIT_VAR(r5);
			PHALCON_CALL_METHOD(r5, column, "isautoincrement", PH_NO_CHECK);
			if (zend_is_true(r5)) {
				PHALCON_CPY_WRT(identity_field, field_name);
			}
			
			PHALCON_INIT_VAR(r6);
			PHALCON_CALL_METHOD(r6, column, "gettype", PH_NO_CHECK);
			phalcon_array_update_zval(&field_types, field_name, &r6, PH_COPY | PH_SEPARATE TSRMLS_CC);
			zend_hash_move_forward_ex(ah0, &hp0);
			goto fes_c40c_0;
		fee_c40c_0:
		if(0){}
		
		PHALCON_INIT_VAR(table_metadata);
		array_init(table_metadata);
		phalcon_array_update_long(&table_metadata, 0, &attributes, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_long(&table_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_long(&table_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_long(&table_metadata, 3, &not_null, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_long(&table_metadata, 4, &field_types, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_long(&table_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_long(&table_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE TSRMLS_CC);
		
		PHALCON_ALLOC_ZVAL_MM(t1);
		phalcon_read_property(&t1, this_ptr, SL("_metaData"), PH_NOISY_CC);
		phalcon_array_update_zval(&t1, key, &table_metadata, PH_COPY TSRMLS_CC);
		phalcon_update_property_zval(this_ptr, SL("_metaData"), t1 TSRMLS_CC);
		
		PHALCON_ALLOC_ZVAL_MM(t2);
		phalcon_read_property(&t2, this_ptr, SL("_changed"), PH_NOISY_CC);
		if (!zend_is_true(t2)) {
			PHALCON_ALLOC_ZVAL_MM(a0);
			array_init(a0);
			phalcon_array_append(&a0, this_ptr, PH_SEPARATE TSRMLS_CC);
			add_next_index_stringl(a0, SL("storeMetaData"), 1);
			PHALCON_CALL_FUNC_PARAMS_1_NORETURN("register_shutdown_function", a0);
			phalcon_update_property_bool(this_ptr, SL("_changed"), 1 TSRMLS_CC);
		}
	}
	
	PHALCON_MM_RESTORE();
}
Пример #13
0
/**
 * Stores cached content into the Memcached backend and stops the frontend
 *
 * @param int|string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, save){

	zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL;
	zval *cached_content = NULL, *prepared_content = NULL, *ttl = NULL, *flags, *success = NULL;
	zval *keys = NULL, *is_buffering = NULL;
	zval *last_key, *frontend, *memcache, *options, *special_key;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 4, &key_name, &content, &lifetime, &stop_buffer);
	
	if (!key_name || Z_TYPE_P(key_name) == IS_NULL) {
		last_key = phalcon_fetch_nproperty_this(this_ptr, SL("_lastKey"), PH_NOISY TSRMLS_CC);
	} else {
		zval *prefix = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY TSRMLS_CC);
	
		PHALCON_INIT_VAR(last_key);
		PHALCON_CONCAT_VV(last_key, prefix, key_name);
	}

	if (!zend_is_true(last_key)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The cache must be started first");
		return;
	}
	
	frontend = phalcon_fetch_nproperty_this(this_ptr, SL("_frontend"), PH_NOISY TSRMLS_CC);
	
	/** 
	 * Check if a connection is created or make a new one
	 */
	memcache = phalcon_fetch_nproperty_this(this_ptr, SL("_memcache"), PH_NOISY TSRMLS_CC);
	if (Z_TYPE_P(memcache) != IS_OBJECT) {
		memcache = NULL;
		PHALCON_CALL_METHOD(&memcache, this_ptr, "_connect");
	}

	if (!content || Z_TYPE_P(content) == IS_NULL) {
		PHALCON_CALL_METHOD(&cached_content, frontend, "getcontent");
	} else {
		cached_content = content;
	}
	
	/** 
	 * Prepare the content in the frontend
	 */
	PHALCON_CALL_METHOD(&prepared_content, frontend, "beforestore", cached_content);

	if (!lifetime || Z_TYPE_P(lifetime) == IS_NULL) {
		zval *tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_lastLifetime"), PH_NOISY TSRMLS_CC);

		if (Z_TYPE_P(tmp) == IS_NULL) {
			PHALCON_CALL_METHOD(&ttl, frontend, "getlifetime");
		}
		else {
			ttl = tmp;
		}
	} else {
		ttl = lifetime;
	}
	
	PHALCON_INIT_VAR(flags);
	ZVAL_LONG(flags, 0);
	
	/** 
	 * We store without flags
	 */
	if (phalcon_is_numeric(cached_content)) {
		PHALCON_CALL_METHOD(&success, memcache, "set", last_key, cached_content, flags, ttl);
	} else {
		PHALCON_CALL_METHOD(&success, memcache, "set", last_key, prepared_content, flags, ttl);
	}

	if (!zend_is_true(success)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Failed to store data in memcached");
		return;
	}
	
	options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);

	if (unlikely(!phalcon_array_isset_string_fetch(&special_key, options, SS("statsKey")))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	if (Z_TYPE_P(special_key) != IS_NULL) {
		/* Update the stats key */
		PHALCON_CALL_METHOD(&keys, memcache, "get", special_key);
		if (Z_TYPE_P(keys) != IS_ARRAY) {
			PHALCON_INIT_NVAR(keys);
			array_init(keys);
		}

		if (!phalcon_array_isset(keys, last_key)) {
			phalcon_array_update_zval(&keys, last_key, ttl, PH_COPY);
			PHALCON_CALL_METHOD(NULL, memcache, "set", special_key, keys);
		}
	}
	
	PHALCON_CALL_METHOD(&is_buffering, frontend, "isbuffering");

	if (!stop_buffer || PHALCON_IS_TRUE(stop_buffer)) {
		PHALCON_CALL_METHOD(NULL, frontend, "stop");
	}
	
	if (PHALCON_IS_TRUE(is_buffering)) {
		zend_print_zval(cached_content, 0);
	}
	
	phalcon_update_property_bool(this_ptr, SL("_started"), 0 TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
Пример #14
0
/**
 * Stores cached content into the Memcached backend and stops the frontend
 *
 * @param int|string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, save){

	zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL, last_key = {}, prefix = {}, cached_content = {}, prepared_content = {}, ttl = {}, flags = {}, success = {};
	zval keys = {}, is_buffering = {}, frontend = {}, memcache = {}, options = {}, special_key = {};

	phalcon_fetch_params(0, 0, 4, &key_name, &content, &lifetime, &stop_buffer);

	if (!key_name || Z_TYPE_P(key_name) == IS_NULL) {
		phalcon_return_property(&last_key, getThis(), SL("_lastKey"));
	} else {
		phalcon_return_property(&prefix, getThis(), SL("_prefix"));
		PHALCON_CONCAT_VV(&last_key, &prefix, key_name);
	}

	if (!zend_is_true(&last_key)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "The cache must be started first");
		return;
	}

	phalcon_return_property(&frontend, getThis(), SL("_frontend"));

	/** 
	 * Check if a connection is created or make a new one
	 */
	phalcon_return_property(&memcache, getThis(), SL("_memcache"));
	if (Z_TYPE(memcache) != IS_OBJECT) {
		PHALCON_CALL_METHODW(&memcache, getThis(), "_connect");
	}

	if (!content || Z_TYPE_P(content) == IS_NULL) {
		PHALCON_CALL_METHODW(&cached_content, &frontend, "getcontent");
	} else {
		PHALCON_CPY_WRT(&cached_content, content);
	}

	/** 
	 * Prepare the content in the frontend
	 */
	PHALCON_CALL_METHODW(&prepared_content, &frontend, "beforestore", &cached_content);

	if (!lifetime || Z_TYPE_P(lifetime) == IS_NULL) {
		phalcon_return_property(&ttl, getThis(), SL("_lastLifetime"));

		if (Z_TYPE(ttl) == IS_NULL) {
			PHALCON_CALL_METHODW(&ttl, &frontend, "getlifetime");
		}
	} else {
		PHALCON_CPY_WRT(&ttl, lifetime);
	}

	ZVAL_LONG(&flags, 0);

	/** 
	 * We store without flags
	 */
	if (phalcon_is_numeric(&cached_content)) {
		PHALCON_CALL_METHODW(&success, &memcache, "set", &last_key, &cached_content, &flags, &ttl);
	} else {
		PHALCON_CALL_METHODW(&success, &memcache, "set", &last_key, &prepared_content, &flags, &ttl);
	}

	if (!zend_is_true(&success)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "Failed to store data in memcached");
		return;
	}

	phalcon_return_property(&options, getThis(), SL("_options"));

	if (unlikely(!phalcon_array_isset_fetch_str(&special_key, &options, SL("statsKey")))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	if (Z_TYPE(special_key) != IS_NULL) {
		/* Update the stats key */
		PHALCON_CALL_METHODW(&keys, &memcache, "get", &special_key);
		if (Z_TYPE(keys) != IS_ARRAY) {
			array_init(&keys);
		}

		if (!phalcon_array_isset(&keys, &last_key)) {
			phalcon_array_update_zval(&keys, &last_key, &ttl, PH_COPY);
			PHALCON_CALL_METHODW(NULL, &memcache, "set", &special_key, &keys);
		}
	}

	PHALCON_CALL_METHODW(&is_buffering, &frontend, "isbuffering");

	if (!stop_buffer || PHALCON_IS_TRUE(stop_buffer)) {
		PHALCON_CALL_METHODW(NULL, &frontend, "stop");
	}

	if (PHALCON_IS_TRUE(&is_buffering)) {
		zend_print_zval(&cached_content, 0);
	}

	phalcon_update_property_bool(getThis(), SL("_started"), 0);
}
Пример #15
0
/**
 * Writes meta-data for certain model using a MODEL_* constant
 *
 *<code>
 *	print_r($metaData->writeColumnMapIndex(new Robots(), MetaData::MODELS_REVERSE_COLUMN_MAP, array('leName' => 'name')));
 *</code>
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param int $index
 * @param mixed $data
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, writeMetaDataIndex){

	zval *model, *index, *data, *replace, *table = NULL, *schema = NULL, *class_name;
	zval *key, *meta_data = NULL, *arr, *value;
	HashTable *ah2;
	HashPosition hp2;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 4, 0, &model, &index, &data, &replace);
	PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1);

	if (Z_TYPE_P(index) != IS_LONG) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Index must be a valid integer constant");
		return;
	}
	
	if (Z_TYPE_P(data) != IS_ARRAY && Z_TYPE_P(data) != IS_STRING && Z_TYPE_P(data) != IS_BOOL) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Invalid data for index");
		return;
	}
	
	PHALCON_CALL_METHOD(&table, model, "getsource");
	PHALCON_CALL_METHOD(&schema, model, "getschema");
	
	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model, 1 TSRMLS_CC);
	
	/** 
	 * Unique key for meta-data is created using class-name-schema-table
	 */
	PHALCON_INIT_VAR(key);
	PHALCON_CONCAT_VSVV(key, class_name, "-", schema, table);
	
	PHALCON_OBS_VAR(meta_data);
	phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY TSRMLS_CC);
	if (!phalcon_array_isset(meta_data, key)) {
		PHALCON_CALL_METHOD(NULL, this_ptr, "_initialize", model, key, table, schema);
	
		PHALCON_OBS_NVAR(meta_data);
		phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY TSRMLS_CC);
	} else if (!zend_is_true(replace)) {
		PHALCON_OBS_VAR(arr);
		phalcon_array_fetch(&arr, meta_data, key, PH_NOISY);

		PHALCON_OBS_VAR(value);
		phalcon_array_fetch(&value, arr, index, PH_NOISY);

		PHALCON_SEPARATE_PARAM(data);
		phalcon_is_iterable(value, &ah2, &hp2, 0, 0);

		while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
			zval key2 = phalcon_get_current_key_w(ah2, &hp2);

			if (!phalcon_array_isset(data, &key2)) {
				phalcon_array_update_zval(&data, &key2, *hd, PH_COPY | PH_SEPARATE);
			}

			zend_hash_move_forward_ex(ah2, &hp2);
		}
	}
	
	phalcon_array_update_multi_2(&meta_data, key, index, data, 0);
	phalcon_update_property_this(this_ptr, SL("_metaData"), meta_data TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
Пример #16
0
/**
 * Gets the type of PHQL queries
 *
 *
 * @return int
 */
PHP_METHOD(Phalcon_Mvc_Model_Query_Builder_Where, setConditions){

	zval *conditions, *bind_params = NULL, *bind_types = NULL, *type = NULL, merge = {}, merged_conditions = {}, merged_bind_params = {}, merged_bind_types;
	zval joind_condition = {}, *single_condition_array = NULL, current_conditions = {}, new_conditions = {};
	zend_string *str_key;
	ulong idx;

	phalcon_fetch_params(0, 1, 3, &conditions, &bind_params, &bind_types, &type);

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

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

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

	ZVAL_BOOL(&merge, Z_TYPE_P(type) != IS_NULL ? 1 : 0);

	if (Z_TYPE_P(conditions) == IS_ARRAY) {
		/* ----------- INITIALIZING LOOP VARIABLES ----------- */

		/*
		 * array containing single condition for example:
		 * array(
		 *      array(
		 *           'status = :status:',
		 *           array('status' => 5),
		 *           array('status' => PDO::PARAM_INT),
		 *      ),
		 *      'name' => 'Dreamsxin',
		 * )
		 */
		array_init(&merged_conditions);
		array_init(&merged_bind_params);
		array_init(&merged_bind_types);

		ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(conditions), idx, str_key, single_condition_array) {
			zval single_condition_key = {}, condition_string = {}, tmp_bind_params = {}, tmp_bind_types = {};
			if (str_key) {
				ZVAL_STR(&single_condition_key, str_key);
			} else {
				ZVAL_LONG(&single_condition_key, idx);
			}
			if (Z_TYPE_P(single_condition_array) == IS_ARRAY
				&& phalcon_array_isset_fetch_long(&condition_string, single_condition_array, 0)
				&& phalcon_array_isset_fetch_long(&tmp_bind_params, single_condition_array, 1)
				&& Z_TYPE(condition_string) == IS_STRING
				&& Z_TYPE(tmp_bind_params) == IS_ARRAY
			) {	
				phalcon_array_update_zval(&merged_conditions, &condition_string, &condition_string, PH_COPY);
				phalcon_array_merge_recursive_n(&merged_bind_params, &tmp_bind_params);

				if (phalcon_array_isset_fetch_long(&tmp_bind_types, single_condition_array, 2) && Z_TYPE(tmp_bind_types) == IS_ARRAY) {
					phalcon_array_merge_recursive_n(&merged_bind_types, &tmp_bind_types);
				}
			} else if (Z_TYPE(single_condition_key) == IS_STRING) {
				PHALCON_CONCAT_VSVS(&condition_string, &single_condition_key, " = :", &single_condition_key, ":");

				phalcon_array_update_zval(&merged_conditions, &single_condition_key, &condition_string, PH_COPY);

				if (Z_TYPE_P(single_condition_array) == IS_ARRAY) {
					phalcon_array_merge_recursive_n(&merged_bind_params, single_condition_array);
				} else {
					phalcon_array_update_zval(&merged_bind_params, &single_condition_key, single_condition_array, PH_COPY);
				}
			}
		} ZEND_HASH_FOREACH_END();
Пример #17
0
/**
 * Stores cached content into the Memcached backend and stops the frontend
 *
 * @param int|string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, save){

	zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL;
	zval *last_key = NULL, *prefix, *frontend, *memcache = NULL, *cached_content = NULL;
	zval *prepared_content, *ttl = NULL, *flags, *success;
	zval *options, *special_key, *keys = NULL, *is_buffering;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zzzz", &key_name, &content, &lifetime, &stop_buffer) == FAILURE) {
		RETURN_MM_NULL();
	}

	if (!key_name) {
		PHALCON_INIT_VAR(key_name);
	}
	
	if (!content) {
		PHALCON_INIT_VAR(content);
	}
	
	if (!lifetime) {
		PHALCON_INIT_VAR(lifetime);
	}
	
	if (!stop_buffer) {
		PHALCON_INIT_VAR(stop_buffer);
		ZVAL_BOOL(stop_buffer, 1);
	}
	
	if (Z_TYPE_P(key_name) == IS_NULL) {
		PHALCON_OBS_VAR(last_key);
		phalcon_read_property(&last_key, this_ptr, SL("_lastKey"), PH_NOISY_CC);
	} else {
		PHALCON_OBS_VAR(prefix);
		phalcon_read_property(&prefix, this_ptr, SL("_prefix"), PH_NOISY_CC);
	
		PHALCON_INIT_NVAR(last_key);
		PHALCON_CONCAT_VV(last_key, prefix, key_name);
	}
	if (!zend_is_true(last_key)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The cache must be started first");
		return;
	}
	
	PHALCON_OBS_VAR(frontend);
	phalcon_read_property(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC);
	
	/** 
	 * Check if a connection is created or make a new one
	 */
	PHALCON_OBS_VAR(memcache);
	phalcon_read_property(&memcache, this_ptr, SL("_memcache"), PH_NOISY_CC);
	if (Z_TYPE_P(memcache) != IS_OBJECT) {
		PHALCON_CALL_METHOD_NORETURN(this_ptr, "_connect");
	
		PHALCON_OBS_NVAR(memcache);
		phalcon_read_property(&memcache, this_ptr, SL("_memcache"), PH_NOISY_CC);
	}
	
	if (Z_TYPE_P(content) == IS_NULL) {
		PHALCON_INIT_VAR(cached_content);
		PHALCON_CALL_METHOD(cached_content, frontend, "getcontent");
	} else {
		PHALCON_CPY_WRT(cached_content, content);
	}
	
	/** 
	 * Prepare the content in the frontend
	 */
	PHALCON_INIT_VAR(prepared_content);
	PHALCON_CALL_METHOD_PARAMS_1(prepared_content, frontend, "beforestore", cached_content);
	if (Z_TYPE_P(lifetime) == IS_NULL) {
		PHALCON_INIT_VAR(ttl);
		PHALCON_CALL_METHOD(ttl, frontend, "getlifetime");
	} else {
		PHALCON_CPY_WRT(ttl, lifetime);
	}
	
	PHALCON_INIT_VAR(flags);
	ZVAL_LONG(flags, 0);
	
	/** 
	 * We store without flags
	 */
	PHALCON_INIT_VAR(success);
	PHALCON_CALL_METHOD_PARAMS_4(success, memcache, "set", last_key, prepared_content, flags, ttl);
	if (!zend_is_true(success)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Failed storing data in memcached");
		return;
	}
	
	PHALCON_OBS_VAR(options);
	phalcon_read_property(&options, this_ptr, SL("_options"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(special_key);
	phalcon_array_fetch_string(&special_key, options, SL("statsKey"), PH_NOISY_CC);
	
	/** 
	 * Update the stats key
	 */
	PHALCON_INIT_VAR(keys);
	PHALCON_CALL_METHOD_PARAMS_1(keys, memcache, "get", special_key);
	if (Z_TYPE_P(keys) != IS_ARRAY) { 
		PHALCON_INIT_NVAR(keys);
		array_init(keys);
	}
	
	if (!phalcon_array_isset(keys, last_key)) {
		phalcon_array_update_zval(&keys, last_key, &ttl, PH_COPY | PH_SEPARATE TSRMLS_CC);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(memcache, "set", special_key, keys);
	}
	
	PHALCON_INIT_VAR(is_buffering);
	PHALCON_CALL_METHOD(is_buffering, frontend, "isbuffering");
	if (PHALCON_IS_TRUE(stop_buffer)) {
		PHALCON_CALL_METHOD_NORETURN(frontend, "stop");
	}
	
	if (PHALCON_IS_TRUE(is_buffering)) {
		zend_print_zval(cached_content, 0);
	}
	
	phalcon_update_property_bool(this_ptr, SL("_started"), 0 TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
Пример #18
0
/**
 * Phalcon\Config\Adapter\Ini constructor
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Ini, __construct){

	zval *file_path, *process_sections, *ini_config;
	zval *exception_message, *config, *directives = NULL;
	zval *section = NULL, *value = NULL, *key = NULL, *directive_parts = NULL, *left_part = NULL;
	zval *right_part = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &file_path);
	
	PHALCON_INIT_VAR(process_sections);
	ZVAL_BOOL(process_sections, 1);
	
	/** 
	 * Use the standard parse_ini_file
	 */
	PHALCON_INIT_VAR(ini_config);
	phalcon_call_func_p2(ini_config, "parse_ini_file", file_path, process_sections);
	
	/** 
	 * Check if the file had errors
	 */
	if (PHALCON_IS_FALSE(ini_config)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Configuration file ", file_path, " can't be loaded");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_config_exception_ce, exception_message);
		return;
	}
	
	PHALCON_INIT_VAR(config);
	array_init(config);
	
	phalcon_is_iterable(ini_config, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HKEY(section, ah0, hp0);
		PHALCON_GET_HVALUE(directives);
	
		if (unlikely(Z_TYPE_P(directives) != IS_ARRAY)) {
			Z_ADDREF_P(directives);
			if (phalcon_array_update_zval(&config, section, &directives, 0 TSRMLS_CC) != SUCCESS) {
				Z_DELREF_P(directives);
			}
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
	}
	
		phalcon_is_iterable(directives, &ah1, &hp1, 0, 0);
	
		if (zend_hash_num_elements(ah1) == 0) {
			phalcon_array_update_zval(&config, section, &directives, 0 TSRMLS_CC);
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_HKEY(key, ah1, hp1);
			PHALCON_GET_HVALUE(value);
	
			if (phalcon_memnstr_str(key, SL(".") TSRMLS_CC)) {
				PHALCON_INIT_NVAR(directive_parts);
				phalcon_fast_explode_str(directive_parts, SL("."), key TSRMLS_CC);
	
				PHALCON_OBS_NVAR(left_part);
				phalcon_array_fetch_long(&left_part, directive_parts, 0, PH_NOISY_CC);
	
				PHALCON_OBS_NVAR(right_part);
				phalcon_array_fetch_long(&right_part, directive_parts, 1, PH_NOISY_CC);
				phalcon_array_update_zval_zval_zval_multi_3(&config, section, left_part, right_part, &value, 0 TSRMLS_CC);
			} else {
				phalcon_array_update_multi_2(&config, section, key, &value, 0 TSRMLS_CC);
			}
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	/** 
	 * Calls the Phalcon\Config constructor
	 */
	PHALCON_CALL_PARENT_PARAMS_1_NORETURN(this_ptr, "Phalcon\\Config\\Adapter\\Ini", "__construct", config);
	
	PHALCON_MM_RESTORE();
}
Пример #19
0
/**
 * Setup a relation 1-n between two models
 *
 * @param Phalcon\Mvc\Model $model
 * @param mixed $fields
 * @param string $referenceModel
 * @param mixed $referencedFields
 * @param array $options
 */
PHP_METHOD(Phalcon_Mvc_Model_Manager, addHasMany){

	zval *model = NULL, *fields = NULL, *reference_model = NULL, *referenced_fields = NULL;
	zval *options = NULL, *entity_name = NULL, *has_many = NULL, *number_fields = NULL;
	zval *number_referenced = NULL, *diferent_fields = NULL;
	zval *relation = NULL;
	zval *a0 = NULL;
	zval *r0 = NULL;
	zval *t0 = NULL;
	int eval_int;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzz|z", &model, &fields, &reference_model, &referenced_fields, &options) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!options) {
		PHALCON_INIT_VAR(options);
		array_init(options);
	}
	
	PHALCON_INIT_VAR(entity_name);
	phalcon_get_class(entity_name, model TSRMLS_CC);
	
	PHALCON_INIT_VAR(has_many);
	phalcon_read_property(&has_many, this_ptr, SL("_hasMany"), PH_NOISY_CC);
	eval_int = phalcon_array_isset(has_many, entity_name);
	if (!eval_int) {
		PHALCON_ALLOC_ZVAL_MM(a0);
		array_init(a0);
		phalcon_array_update_zval(&has_many, entity_name, &a0, PH_COPY | PH_SEPARATE TSRMLS_CC);
	}
	
	PHALCON_ALLOC_ZVAL_MM(r0);
	phalcon_array_fetch(&r0, has_many, entity_name, PH_NOISY_CC);
	eval_int = phalcon_array_isset(r0, reference_model);
	if (!eval_int) {
		if (Z_TYPE_P(referenced_fields) == IS_ARRAY) { 
			PHALCON_INIT_VAR(number_fields);
			phalcon_fast_count(number_fields, fields TSRMLS_CC);
			
			PHALCON_INIT_VAR(number_referenced);
			phalcon_fast_count(number_referenced, referenced_fields TSRMLS_CC);
			
			PHALCON_INIT_VAR(diferent_fields);
			is_not_equal_function(diferent_fields, number_fields, number_referenced TSRMLS_CC);
			if (zend_is_true(diferent_fields)) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Number of referenced fields are not the same");
				return;
			}
		}
		
		PHALCON_INIT_VAR(relation);
		array_init(relation);
		phalcon_array_update_string(&relation, SL("fi"), &fields, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_string(&relation, SL("rt"), &reference_model, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_string(&relation, SL("rf"), &referenced_fields, PH_COPY | PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_string(&relation, SL("op"), &options, PH_COPY | PH_SEPARATE TSRMLS_CC);
		
		PHALCON_ALLOC_ZVAL_MM(t0);
		phalcon_read_property(&t0, this_ptr, SL("_hasMany"), PH_NOISY_CC);
		phalcon_array_update_multi_2(&t0, entity_name, reference_model, &relation, 0 TSRMLS_CC);
		phalcon_update_property_zval(this_ptr, SL("_hasMany"), t0 TSRMLS_CC);
		PHALCON_MM_RESTORE();
		RETURN_TRUE;
	}
	
	PHALCON_MM_RESTORE();
	RETURN_FALSE;
}
Пример #20
0
/**
 * Stores cached content into the XCache backend and stops the frontend
 *
 * @param string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_Xcache, save){

	zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL;
	zval *last_key = NULL, *prefix, *frontend, *cached_content = NULL;
	zval *prepared_content, *ttl = NULL, *success, *is_buffering;
	zval *options, *special_key, *keys = NULL, *zero;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 4, &key_name, &content, &lifetime, &stop_buffer);
	
	if (!key_name) {
		PHALCON_INIT_VAR(key_name);
	}
	
	if (!content) {
		PHALCON_INIT_VAR(content);
	}
	
	if (!lifetime) {
		PHALCON_INIT_VAR(lifetime);
	} else {
		PHALCON_SEPARATE_PARAM(lifetime);
	}
	
	if (!stop_buffer) {
		PHALCON_INIT_VAR(stop_buffer);
		ZVAL_BOOL(stop_buffer, 1);
	}
	
	if (Z_TYPE_P(key_name) == IS_NULL) {
		PHALCON_OBS_VAR(last_key);
		phalcon_read_property_this(&last_key, this_ptr, SL("_lastKey"), PH_NOISY_CC);
	} else {
		PHALCON_OBS_VAR(prefix);
		phalcon_read_property_this(&prefix, this_ptr, SL("_prefix"), PH_NOISY_CC);
	
		PHALCON_INIT_NVAR(last_key);
		PHALCON_CONCAT_SVV(last_key, "_PHCX", prefix, key_name);
	}
	if (!zend_is_true(last_key)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The cache must be started first");
		return;
	}
	
	PHALCON_OBS_VAR(frontend);
	phalcon_read_property_this(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC);
	if (Z_TYPE_P(content) == IS_NULL) {
		PHALCON_INIT_VAR(cached_content);
		phalcon_call_method(cached_content, frontend, "getcontent");
	} else {
		PHALCON_CPY_WRT(cached_content, content);
	}
	
	PHALCON_INIT_VAR(prepared_content);
	phalcon_call_method_p1(prepared_content, frontend, "beforestore", cached_content);
	
	/** 
	 * Take the lifetime from the frontend or read it from the set in start()
	 */
	if (Z_TYPE_P(lifetime) == IS_NULL) {
	
		PHALCON_OBS_NVAR(lifetime);
		phalcon_read_property_this(&lifetime, this_ptr, SL("_lastLifetime"), PH_NOISY_CC);
		if (Z_TYPE_P(lifetime) == IS_NULL) {
			PHALCON_INIT_VAR(ttl);
			phalcon_call_method(ttl, frontend, "getlifetime");
		} else {
			PHALCON_CPY_WRT(ttl, lifetime);
		}
	} else {
		PHALCON_CPY_WRT(ttl, lifetime);
	}
	
	/** 
	 * Call xcache_set in the PHP userland since most of the time it isn't available at
	 * compile time
	 */
	PHALCON_INIT_VAR(success);
	phalcon_call_func_p3(success, "xcache_set", last_key, prepared_content, ttl);
	
	PHALCON_INIT_VAR(is_buffering);
	phalcon_call_method(is_buffering, frontend, "isbuffering");
	if (PHALCON_IS_TRUE(stop_buffer)) {
		phalcon_call_method_noret(frontend, "stop");
	}
	
	if (PHALCON_IS_TRUE(is_buffering)) {
		zend_print_zval(cached_content, 0);
	}
	
	phalcon_update_property_bool(this_ptr, SL("_started"), 0 TSRMLS_CC);
	
	/** 
	 * xcache_set() could fail because of Out of Memory condition. I don't think it is
	 * appropriate to throw an exception here (like
	 * Phalcon\Cache\Backend\Memcache::save() does): first, to be consistent with
	 * Phalcon\Cache\Backend\Apc::save(), second, because xCache is usually given much
	 * less RAM than memcached
	 */
	if (zend_is_true(success)) {
	
		PHALCON_OBS_VAR(options);
		phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY_CC);
	
		PHALCON_OBS_VAR(special_key);
		phalcon_array_fetch_string(&special_key, options, SL("statsKey"), PH_NOISY_CC);
	
		/** 
		 * xcache_list() is available only to the administrator (unless XCache was
		 * patched). We have to update the list of the stored keys.
		 */
		PHALCON_INIT_VAR(keys);
		phalcon_call_func_p1(keys, "xcache_get", special_key);
		if (Z_TYPE_P(keys) != IS_ARRAY) { 
			PHALCON_INIT_NVAR(keys);
			array_init(keys);
		}
	
		if (!zend_is_true(keys)) {
			phalcon_array_update_zval(&keys, last_key, &ttl, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
			PHALCON_INIT_VAR(zero);
			ZVAL_LONG(zero, 0);
			phalcon_call_func_p3_noret("xcache_set", special_key, keys, zero);
		}
	}
	
	PHALCON_MM_RESTORE();
}
Пример #21
0
/**
 * Handles routing information received from the rewrite engine
 *
 * @param string $uri
 */
PHP_METHOD(Phalcon_Mvc_Router, handle){

	zval *uri = NULL, *real_uri = NULL, *request = NULL, *route_found = NULL, *parts = NULL;
	zval *params = NULL, *matches = NULL, *routes = NULL, *reversed_routes = NULL;
	zval *route = NULL, *methods = NULL, *dependency_injector = NULL;
	zval *match_method = NULL, *pattern = NULL, *paths = NULL, *position = NULL;
	zval *part = NULL, *match_position = NULL, *module = NULL, *default_module = NULL;
	zval *controller = NULL, *default_controller = NULL, *action = NULL;
	zval *default_action = NULL, *params_str = NULL, *str_params = NULL;
	zval *params_merge = NULL, *default_params = 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", &uri) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!uri) {
		PHALCON_ALLOC_ZVAL_MM(uri);
		ZVAL_NULL(uri);
	}
	
	if (!zend_is_true(uri)) {
		PHALCON_INIT_VAR(real_uri);
		PHALCON_CALL_METHOD(real_uri, this_ptr, "_getrewriteuri", PH_NO_CHECK);
	} else {
		PHALCON_CPY_WRT(real_uri, uri);
	}
	
	PHALCON_INIT_VAR(request);
	ZVAL_NULL(request);
	
	PHALCON_INIT_VAR(route_found);
	ZVAL_BOOL(route_found, 0);
	
	PHALCON_INIT_VAR(parts);
	array_init(parts);
	
	PHALCON_INIT_VAR(params);
	array_init(params);
	
	PHALCON_INIT_VAR(matches);
	array_init(matches);
	phalcon_update_property_bool(this_ptr, SL("_wasMatched"), 0 TSRMLS_CC);
	
	PHALCON_INIT_VAR(routes);
	phalcon_read_property(&routes, this_ptr, SL("_routes"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(reversed_routes);
	PHALCON_CALL_FUNC_PARAMS_1(reversed_routes, "array_reverse", routes);
	if (!phalcon_valid_foreach(reversed_routes TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(reversed_routes);
	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(route);
		ZVAL_ZVAL(route, *hd, 1, 0);
		PHALCON_INIT_VAR(methods);
		PHALCON_CALL_METHOD(methods, route, "gethttpmethods", PH_NO_CHECK);
		if (Z_TYPE_P(methods) != IS_NULL) {
			if (Z_TYPE_P(request) == IS_NULL) {
				PHALCON_INIT_VAR(dependency_injector);
				phalcon_read_property(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
				if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
					PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_dispatcher_exception_ce, "A dependency injection container is required to access the 'request' service");
					return;
				}
				
				PHALCON_INIT_VAR(c0);
				ZVAL_STRING(c0, "request", 1);
				
				PHALCON_INIT_VAR(request);
				PHALCON_CALL_METHOD_PARAMS_1(request, dependency_injector, "getshared", c0, PH_NO_CHECK);
			}
			
			PHALCON_INIT_VAR(match_method);
			PHALCON_CALL_METHOD_PARAMS_1(match_method, request, "ismethod", methods, PH_NO_CHECK);
			if (!zend_is_true(match_method)) {
				zend_hash_move_forward_ex(ah0, &hp0);
				goto fes_c9ff_0;
			}
		}
		
		PHALCON_INIT_VAR(pattern);
		PHALCON_CALL_METHOD(pattern, route, "getcompiledpattern", PH_NO_CHECK);
		Z_SET_ISREF_P(matches);
		
		PHALCON_INIT_VAR(route_found);
		PHALCON_CALL_FUNC_PARAMS_3(route_found, "preg_match", pattern, real_uri, matches);
		Z_UNSET_ISREF_P(matches);
		if (zend_is_true(route_found)) {
			PHALCON_INIT_VAR(paths);
			PHALCON_CALL_METHOD(paths, route, "getpaths", PH_NO_CHECK);
			PHALCON_CPY_WRT(parts, paths);
			if (!phalcon_valid_foreach(paths TSRMLS_CC)) {
				return;
			}
			
			ah1 = Z_ARRVAL_P(paths);
			zend_hash_internal_pointer_reset_ex(ah1, &hp1);
			fes_c9ff_1:
				if(zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) != SUCCESS){
					goto fee_c9ff_1;
				}
				
				PHALCON_INIT_VAR(part);
				PHALCON_GET_FOREACH_KEY(part, ah1, hp1);
				PHALCON_INIT_VAR(position);
				ZVAL_ZVAL(position, *hd, 1, 0);
				eval_int = phalcon_array_isset(matches, position);
				if (eval_int) {
					PHALCON_INIT_VAR(match_position);
					phalcon_array_fetch(&match_position, matches, position, PH_NOISY_CC);
					phalcon_array_update_zval(&parts, part, &match_position, PH_COPY | PH_SEPARATE TSRMLS_CC);
				}
				zend_hash_move_forward_ex(ah1, &hp1);
				goto fes_c9ff_1;
			fee_c9ff_1:
			
			phalcon_update_property_zval(this_ptr, SL("_matches"), matches TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_matchedRoute"), route TSRMLS_CC);
			goto fee_c9ff_0;
		}
		zend_hash_move_forward_ex(ah0, &hp0);
		goto fes_c9ff_0;
	fee_c9ff_0:
	
	if (zend_is_true(route_found)) {
		eval_int = phalcon_array_isset_string(parts, SL("module")+1);
		if (eval_int) {
			PHALCON_INIT_VAR(module);
			phalcon_array_fetch_string(&module, parts, SL("module"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_module"), module TSRMLS_CC);
			PHALCON_SEPARATE(parts);
			phalcon_array_unset_string(parts, SL("module")+1);
		} else {
			PHALCON_INIT_VAR(default_module);
			phalcon_read_property(&default_module, this_ptr, SL("_defaultModule"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_module"), default_module TSRMLS_CC);
		}
		eval_int = phalcon_array_isset_string(parts, SL("controller")+1);
		if (eval_int) {
			PHALCON_INIT_VAR(controller);
			phalcon_array_fetch_string(&controller, parts, SL("controller"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_controller"), controller TSRMLS_CC);
			PHALCON_SEPARATE(parts);
			phalcon_array_unset_string(parts, SL("controller")+1);
		} else {
			PHALCON_INIT_VAR(default_controller);
			phalcon_read_property(&default_controller, this_ptr, SL("_defaultController"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_controller"), default_controller TSRMLS_CC);
		}
		
		eval_int = phalcon_array_isset_string(parts, SL("action")+1);
		if (eval_int) {
			PHALCON_INIT_VAR(action);
			phalcon_array_fetch_string(&action, parts, SL("action"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_action"), action TSRMLS_CC);
			PHALCON_SEPARATE(parts);
			phalcon_array_unset_string(parts, SL("action")+1);
		} else {
			PHALCON_INIT_VAR(default_action);
			phalcon_read_property(&default_action, this_ptr, SL("_defaultAction"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_action"), default_action TSRMLS_CC);
		}
		
		eval_int = phalcon_array_isset_string(parts, SL("params")+1);
		if (eval_int) {
			PHALCON_INIT_VAR(params_str);
			phalcon_array_fetch_string(&params_str, parts, SL("params"), PH_NOISY_CC);
			
			PHALCON_INIT_VAR(c1);
			ZVAL_LONG(c1, 1);
			
			PHALCON_INIT_VAR(str_params);
			PHALCON_CALL_FUNC_PARAMS_2(str_params, "substr", params_str, c1);
			if (zend_is_true(str_params)) {
				PHALCON_INIT_VAR(c2);
				ZVAL_STRING(c2, "/", 1);
				PHALCON_INIT_VAR(params);
				phalcon_fast_explode(params, c2, str_params TSRMLS_CC);
			}
			
			PHALCON_SEPARATE(parts);
			phalcon_array_unset_string(parts, SL("params")+1);
		}
		
		PHALCON_INIT_VAR(params_merge);
		PHALCON_CALL_FUNC_PARAMS_2(params_merge, "array_merge", params, parts);
		phalcon_update_property_zval(this_ptr, SL("_params"), params_merge TSRMLS_CC);
		phalcon_update_property_bool(this_ptr, SL("_wasMatched"), 1 TSRMLS_CC);
	} else {
		PHALCON_INIT_VAR(default_module);
		phalcon_read_property(&default_module, this_ptr, SL("_defaultModule"), PH_NOISY_CC);
		phalcon_update_property_zval(this_ptr, SL("_module"), default_module TSRMLS_CC);
		
		PHALCON_INIT_VAR(default_controller);
		phalcon_read_property(&default_controller, this_ptr, SL("_defaultController"), PH_NOISY_CC);
		phalcon_update_property_zval(this_ptr, SL("_controller"), default_controller TSRMLS_CC);
		
		PHALCON_INIT_VAR(default_action);
		phalcon_read_property(&default_action, this_ptr, SL("_defaultAction"), PH_NOISY_CC);
		phalcon_update_property_zval(this_ptr, SL("_action"), default_action TSRMLS_CC);
		
		PHALCON_INIT_VAR(default_params);
		phalcon_read_property(&default_params, this_ptr, SL("_defaultParams"), PH_NOISY_CC);
		phalcon_update_property_zval(this_ptr, SL("_params"), default_params TSRMLS_CC);
		phalcon_update_property_bool(this_ptr, SL("_wasMatched"), 0 TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #22
0
/**
 * Attach a listener to the events manager
 *
 * @param string $eventType
 * @param object|callable $handler
 * @param int $priority
 */
PHP_METHOD(Phalcon_Events_Manager, attach){

	zval *event_type, *handler, *priority = NULL, *events = NULL;
	zval *enable_priorities, *priority_queue = NULL;
	zval *mode;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 1, &event_type, &handler, &priority);
	
	if (!priority) {
		PHALCON_INIT_VAR(priority);
		ZVAL_LONG(priority, 100);
	}
	
	if (unlikely(Z_TYPE_P(event_type) != IS_STRING)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_events_exception_ce, "Event type must be a string");
		return;
	}
	if (unlikely(Z_TYPE_P(handler) != IS_OBJECT)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_events_exception_ce, "Event handler must be an Object");
		return;
	}
	
	PHALCON_OBS_VAR(events);
	phalcon_read_property_this(&events, this_ptr, SL("_events"), PH_NOISY_CC);
	if (Z_TYPE_P(events) != IS_ARRAY) { 
		PHALCON_INIT_NVAR(events);
		array_init(events);
	}
	
	if (!phalcon_array_isset(events, event_type)) {
	
		PHALCON_OBS_VAR(enable_priorities);
		phalcon_read_property_this(&enable_priorities, this_ptr, SL("_enablePriorities"), PH_NOISY_CC);
		if (zend_is_true(enable_priorities)) {
			/** 
			 * Create a SplPriorityQueue to store the events with priorities
			 */
			PHALCON_INIT_VAR(priority_queue);
			object_init_ex(priority_queue, spl_ce_SplPriorityQueue);
			if (phalcon_has_constructor(priority_queue TSRMLS_CC)) {
				phalcon_call_method_noret(priority_queue, "__construct");
			}
	
			/** 
			 * Extract only the Data
			 */
			PHALCON_INIT_VAR(mode);
			ZVAL_LONG(mode, 1);
	
			/** 
			 * Set extraction flags
			 */
			phalcon_call_method_p1_noret(priority_queue, "setextractflags", mode);
	
			/** 
			 * Append the events to the queue
			 */
			phalcon_array_update_zval(&events, event_type, &priority_queue, PH_COPY | PH_SEPARATE);
			phalcon_update_property_this(this_ptr, SL("_events"), events TSRMLS_CC);
		} else {
			PHALCON_INIT_NVAR(priority_queue);
			array_init(priority_queue);
		}
	} else {
		/** 
		 * Get the current SplPriorityQueue
		 */
		PHALCON_OBS_NVAR(priority_queue);
		phalcon_array_fetch(&priority_queue, events, event_type, PH_NOISY);
	}
	
	/** 
	 * Insert the handler in the queue
	 */
	if (unlikely(Z_TYPE_P(priority_queue) == IS_OBJECT)) {
		phalcon_call_method_p2_noret(priority_queue, "insert", handler, priority);
	} else {
		phalcon_array_append(&priority_queue, handler, PH_SEPARATE);
	
		/** 
		 * Append the events to the queue
		 */
		phalcon_array_update_zval(&events, event_type, &priority_queue, PH_COPY | PH_SEPARATE);
		phalcon_update_property_this(this_ptr, SL("_events"), events TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #23
0
/**
 * Load config file
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Ini, read){

	zval *file_path, *absolute_path = NULL, *scanner_mode = NULL, config_dir_path = {}, base_path = {}, ini_config = {}, config = {}, *directives;
	zend_string *str_key;
	ulong idx;

	phalcon_fetch_params(0, 1, 2, &file_path, &absolute_path, &scanner_mode);
	PHALCON_ENSURE_IS_STRING(file_path);

	if (!absolute_path) {
		absolute_path = &PHALCON_GLOBAL(z_false);
	}

	if (zend_is_true(absolute_path)) {
		PHALCON_CPY_WRT_CTOR(&config_dir_path, file_path);
	} else {
		phalcon_return_static_property_ce(&base_path, phalcon_config_adapter_ce, SL("_basePath"));

		PHALCON_CONCAT_VV(&config_dir_path, &base_path, file_path);
	}

	/** 
	 * Use the standard parse_ini_file
	 */
	if (scanner_mode && Z_TYPE_P(scanner_mode) == IS_LONG) {
		PHALCON_CALL_FUNCTIONW(&ini_config, "parse_ini_file", &config_dir_path, &PHALCON_GLOBAL(z_true), scanner_mode);
	} else {
		PHALCON_CALL_FUNCTIONW(&ini_config, "parse_ini_file", &config_dir_path, &PHALCON_GLOBAL(z_true));
	}

	/** 
	 * Check if the file had errors
	 */
	if (Z_TYPE(ini_config) != IS_ARRAY) {
		zend_throw_exception_ex(phalcon_config_exception_ce, 0, "Configuration file '%s' cannot be read", Z_STRVAL(config_dir_path));
		return;
	}

	array_init(&config);

	ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(ini_config), idx, str_key, directives) {
		zval section = {}, *value;
		if (str_key) {
			ZVAL_STR(&section, str_key);
		} else {
			ZVAL_LONG(&section, idx);
		}

		if (unlikely(Z_TYPE_P(directives) != IS_ARRAY) || zend_hash_num_elements(Z_ARRVAL_P(directives)) == 0) {
			phalcon_array_update_zval(&config, &section, directives, PH_COPY);
		} else {
			ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(directives), idx, str_key, value) {
				zval key = {}, directive_parts = {};
				if (str_key) {
					ZVAL_STR(&key, str_key);
				} else {
					ZVAL_LONG(&key, idx);
				}

				if (str_key && memchr(Z_STRVAL(key), '.', Z_STRLEN(key))) {
					phalcon_fast_explode_str(&directive_parts, SL("."), &key);
					phalcon_config_adapter_ini_update_zval_directive(&config, &section, &directive_parts, value);
				} else {
					phalcon_array_update_multi_2(&config, &section, &key, value, PH_COPY);
				}
			} ZEND_HASH_FOREACH_END();
		}
Пример #24
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();
}
Пример #25
0
/**
 * Loads registered template engines, if none is registered it will use Phalcon\Mvc\View\Engine\Php
 *
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_View_Simple, _loadTemplateEngines){

	zval *engines = NULL, *dependency_injector, *registered_engines;
	zval *php_engine, *arguments, *engine_service = NULL;
	zval *extension = NULL, *engine_object = NULL, *exception_message = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(engines);
	phalcon_read_property_this(&engines, this_ptr, SL("_engines"), PH_NOISY_CC);
	
	/** 
	 * If the engines aren't initialized 'engines' is false
	 */
	if (PHALCON_IS_FALSE(engines)) {
	
		PHALCON_OBS_VAR(dependency_injector);
		phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
	
		PHALCON_INIT_NVAR(engines);
		array_init(engines);
	
		PHALCON_OBS_VAR(registered_engines);
		phalcon_read_property_this(&registered_engines, this_ptr, SL("_registeredEngines"), PH_NOISY_CC);
		if (Z_TYPE_P(registered_engines) != IS_ARRAY) { 
			/** 
			 * We use Phalcon\Mvc\View\Engine\Php as default
			 */
			PHALCON_INIT_VAR(php_engine);
			object_init_ex(php_engine, phalcon_mvc_view_engine_php_ce);
			phalcon_call_method_p2_noret(php_engine, "__construct", this_ptr, dependency_injector);
	
			/** 
			 * Use .phtml as extension for the PHP engine
			 */
			phalcon_array_update_string(&engines, SL(".phtml"), &php_engine, PH_COPY | PH_SEPARATE);
		} else {
			if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_view_exception_ce, "A dependency injector container is required to obtain the application services");
				return;
			}
	
			/** 
			 * Arguments for instantiated engines
			 */
			PHALCON_INIT_VAR(arguments);
			array_init_size(arguments, 2);
			phalcon_array_append(&arguments, this_ptr, PH_SEPARATE);
			phalcon_array_append(&arguments, dependency_injector, PH_SEPARATE);
	
			phalcon_is_iterable(registered_engines, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HKEY(extension, ah0, hp0);
				PHALCON_GET_HVALUE(engine_service);
	
				if (Z_TYPE_P(engine_service) == IS_OBJECT) {
	
					/** 
					 * Engine can be a closure
					 */
					if (phalcon_is_instance_of(engine_service, SL("Closure") TSRMLS_CC)) {
						PHALCON_INIT_NVAR(engine_object);
						PHALCON_CALL_USER_FUNC_ARRAY(engine_object, engine_service, arguments);
					} else {
						PHALCON_CPY_WRT(engine_object, engine_service);
					}
				} else {
					/** 
					 * Engine can be a string representing a service in the DI
					 */
					if (Z_TYPE_P(engine_service) == IS_STRING) {
						PHALCON_INIT_NVAR(engine_object);
						phalcon_call_method_p2(engine_object, dependency_injector, "getshared", engine_service, arguments);
					} else {
						PHALCON_INIT_NVAR(exception_message);
						PHALCON_CONCAT_SV(exception_message, "Invalid template engine registration for extension: ", extension);
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_view_exception_ce, exception_message);
						return;
					}
				}
				phalcon_array_update_zval(&engines, extension, &engine_object, PH_COPY | PH_SEPARATE);
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
		}
	
		phalcon_update_property_this(this_ptr, SL("_engines"), engines TSRMLS_CC);
	} else {
		PHALCON_OBS_NVAR(engines);
		phalcon_read_property_this(&engines, this_ptr, SL("_engines"), PH_NOISY_CC);
	}
	
	RETURN_CCTOR(engines);
}
Пример #26
0
/**
 * Handles routing information received from the rewrite engine
 *
 * @param string $uri
 */
PHP_METHOD(Phalcon_Mvc_Router, handle){

	zval *uri = NULL, *real_uri = NULL, *route_found = NULL, *parts = NULL, *params = NULL;
	zval *matches = NULL, *route = NULL, *position = NULL, *part = NULL, *str_params = NULL;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL;
	zval *r7 = NULL, *r8 = NULL, *r9 = NULL;
	zval *c0 = NULL, *c1 = NULL;
	zval *a0 = 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", &uri) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!uri) {
		PHALCON_ALLOC_ZVAL_MM(uri);
		ZVAL_NULL(uri);
	}
	
	if (!zend_is_true(uri)) {
		PHALCON_INIT_VAR(real_uri);
		PHALCON_CALL_METHOD(real_uri, this_ptr, "_getrewriteuri", PH_NO_CHECK);
	} else {
		PHALCON_CPY_WRT(real_uri, uri);
	}
	
	PHALCON_INIT_VAR(route_found);
	ZVAL_BOOL(route_found, 0);
	
	PHALCON_INIT_VAR(parts);
	array_init(parts);
	
	PHALCON_INIT_VAR(params);
	array_init(params);
	
	PHALCON_INIT_VAR(matches);
	array_init(matches);
	phalcon_update_property_bool(this_ptr, SL("_wasMatched"), 0 TSRMLS_CC);
	
	PHALCON_ALLOC_ZVAL_MM(t0);
	phalcon_read_property(&t0, this_ptr, SL("_routes"), PH_NOISY_CC);
	
	PHALCON_ALLOC_ZVAL_MM(r0);
	PHALCON_CALL_FUNC_PARAMS_1(r0, "array_reverse", t0);
	if (!phalcon_valid_foreach(r0 TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(r0);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	fes_c9ff_1:
		if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
			goto fee_c9ff_1;
		}
		
		PHALCON_INIT_VAR(route);
		ZVAL_ZVAL(route, *hd, 1, 0);
		PHALCON_INIT_VAR(parts);
		phalcon_array_fetch_string(&parts, route, SL("paths"), PH_NOISY_CC);
		
		PHALCON_INIT_VAR(r1);
		phalcon_array_fetch_string(&r1, route, SL("pattern"), PH_NOISY_CC);
		Z_SET_ISREF_P(matches);
		
		PHALCON_INIT_VAR(r2);
		PHALCON_CALL_FUNC_PARAMS_3(r2, "preg_match", r1, real_uri, matches);
		Z_UNSET_ISREF_P(matches);
		if (zend_is_true(r2)) {
			PHALCON_INIT_VAR(r3);
			phalcon_array_fetch_string(&r3, route, SL("paths"), PH_NOISY_CC);
			if (!phalcon_valid_foreach(r3 TSRMLS_CC)) {
				return;
			}
			
			ah1 = Z_ARRVAL_P(r3);
			zend_hash_internal_pointer_reset_ex(ah1, &hp1);
			fes_c9ff_2:
				if(zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) != SUCCESS){
					goto fee_c9ff_2;
				}
				
				PHALCON_INIT_VAR(part);
				PHALCON_GET_FOREACH_KEY(part, ah1, hp1);
				PHALCON_INIT_VAR(position);
				ZVAL_ZVAL(position, *hd, 1, 0);
				eval_int = phalcon_array_isset(matches, position);
				if (eval_int) {
					PHALCON_INIT_VAR(r4);
					phalcon_array_fetch(&r4, matches, position, PH_NOISY_CC);
					phalcon_array_update_zval(&parts, part, &r4, PH_COPY | PH_SEPARATE TSRMLS_CC);
				}
				zend_hash_move_forward_ex(ah1, &hp1);
				goto fes_c9ff_2;
			fee_c9ff_2:
			if(0){}
			
			phalcon_update_property_zval(this_ptr, SL("_matches"), matches TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_currentRoute"), route TSRMLS_CC);
			
			PHALCON_INIT_VAR(route_found);
			ZVAL_BOOL(route_found, 1);
			goto fee_c9ff_1;
		}
		zend_hash_move_forward_ex(ah0, &hp0);
		goto fes_c9ff_1;
	fee_c9ff_1:
	if(0){}
	
	if (zend_is_true(route_found)) {
		eval_int = phalcon_array_isset_string(parts, SL("module")+1);
		if (eval_int) {
			PHALCON_ALLOC_ZVAL_MM(r5);
			phalcon_array_fetch_string(&r5, parts, SL("module"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_module"), r5 TSRMLS_CC);
			PHALCON_SEPARATE(parts);
			phalcon_array_unset_string(parts, SL("module")+1);
		} else {
			PHALCON_ALLOC_ZVAL_MM(t1);
			phalcon_read_property(&t1, this_ptr, SL("_defaultModule"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_module"), t1 TSRMLS_CC);
		}
		eval_int = phalcon_array_isset_string(parts, SL("controller")+1);
		if (eval_int) {
			PHALCON_ALLOC_ZVAL_MM(r6);
			phalcon_array_fetch_string(&r6, parts, SL("controller"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_controller"), r6 TSRMLS_CC);
			PHALCON_SEPARATE(parts);
			phalcon_array_unset_string(parts, SL("controller")+1);
		} else {
			phalcon_update_property_null(this_ptr, SL("_controller") TSRMLS_CC);
		}
		
		eval_int = phalcon_array_isset_string(parts, SL("action")+1);
		if (eval_int) {
			PHALCON_ALLOC_ZVAL_MM(r7);
			phalcon_array_fetch_string(&r7, parts, SL("action"), PH_NOISY_CC);
			phalcon_update_property_zval(this_ptr, SL("_action"), r7 TSRMLS_CC);
			PHALCON_SEPARATE(parts);
			phalcon_array_unset_string(parts, SL("action")+1);
		} else {
			phalcon_update_property_null(this_ptr, SL("_action") TSRMLS_CC);
		}
		
		eval_int = phalcon_array_isset_string(parts, SL("params")+1);
		if (eval_int) {
			PHALCON_ALLOC_ZVAL_MM(r8);
			phalcon_array_fetch_string(&r8, parts, SL("params"), PH_NOISY_CC);
			PHALCON_INIT_VAR(c0);
			ZVAL_LONG(c0, 1);
			PHALCON_INIT_VAR(str_params);
			PHALCON_CALL_FUNC_PARAMS_2(str_params, "substr", r8, c0);
			if (zend_is_true(str_params)) {
				PHALCON_INIT_VAR(c1);
				ZVAL_STRING(c1, "/", 1);
				PHALCON_INIT_VAR(params);
				phalcon_fast_explode(params, c1, str_params TSRMLS_CC);
			}
			
			PHALCON_SEPARATE(parts);
			phalcon_array_unset_string(parts, SL("params")+1);
		}
		
		PHALCON_ALLOC_ZVAL_MM(r9);
		PHALCON_CALL_FUNC_PARAMS_2(r9, "array_merge", params, parts);
		phalcon_update_property_zval(this_ptr, SL("_params"), r9 TSRMLS_CC);
		phalcon_update_property_bool(this_ptr, SL("_wasMatched"), 1 TSRMLS_CC);
	} else {
		PHALCON_ALLOC_ZVAL_MM(t2);
		phalcon_read_property(&t2, this_ptr, SL("_defaultModule"), PH_NOISY_CC);
		phalcon_update_property_zval(this_ptr, SL("_module"), t2 TSRMLS_CC);
		phalcon_update_property_null(this_ptr, SL("_controller") TSRMLS_CC);
		phalcon_update_property_null(this_ptr, SL("_action") TSRMLS_CC);
		
		PHALCON_ALLOC_ZVAL_MM(a0);
		array_init(a0);
		phalcon_update_property_zval(this_ptr, SL("_params"), a0 TSRMLS_CC);
		phalcon_update_property_bool(this_ptr, SL("_wasMatched"), 0 TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #27
0
/**
 * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST
 *
 * @param Phalcon\DiInterface $dependencyInjector
 * @param string $modelName
 * @param array $data
 * @return static
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){

	zval *dependency_injector, *model_name, *data;
	zval *conditions, *service, *meta_data, *model;
	zval *data_types, *bind, *value = NULL, *field = NULL, *type = NULL, *condition = NULL;
	zval *value_pattern = NULL, *criteria, *join_conditions;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data);
	
	if (Z_TYPE_P(data) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array");
		return;
	}
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services");
		return;
	}
	
	PHALCON_INIT_VAR(conditions);
	array_init(conditions);
	if (phalcon_fast_count_ev(data TSRMLS_CC)) {
	
		PHALCON_INIT_VAR(service);
		ZVAL_STRING(service, "modelsMetadata", 1);
	
		PHALCON_INIT_VAR(meta_data);
		phalcon_call_method_p1(meta_data, dependency_injector, "getshared", service);
		ce0 = phalcon_fetch_class(model_name TSRMLS_CC);
	
		PHALCON_INIT_VAR(model);
		object_init_ex(model, ce0);
		if (phalcon_has_constructor(model TSRMLS_CC)) {
			phalcon_call_method_noret(model, "__construct");
		}
	
		PHALCON_INIT_VAR(data_types);
		phalcon_call_method_p1(data_types, meta_data, "getdatatypes", model);
	
		PHALCON_INIT_VAR(bind);
		array_init(bind);
	
		/** 
		 * We look for attributes in the array passed as data
		 */
		phalcon_is_iterable(data, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HKEY(field, ah0, hp0);
			PHALCON_GET_HVALUE(value);
	
			if (phalcon_array_isset(data_types, field)) {
				if (Z_TYPE_P(value) != IS_NULL) {
					if (!PHALCON_IS_STRING(value, "")) {
	
						PHALCON_OBS_NVAR(type);
						phalcon_array_fetch(&type, data_types, field, PH_NOISY_CC);
						if (PHALCON_IS_LONG(type, 2)) {
							/** 
							 * For varchar types we use LIKE operator
							 */
							PHALCON_INIT_NVAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":");
	
							PHALCON_INIT_NVAR(value_pattern);
							PHALCON_CONCAT_SVS(value_pattern, "%", value, "%");
							phalcon_array_update_zval(&bind, field, &value_pattern, PH_COPY | PH_SEPARATE TSRMLS_CC);
						} else {
							/** 
							 * For the rest of data types we use a plain = operator
							 */
							PHALCON_INIT_NVAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":");
							phalcon_array_update_zval(&bind, field, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
						}
	
						phalcon_array_append(&conditions, condition, PH_SEPARATE TSRMLS_CC);
					}
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
	}
	
	/** 
	 * Create an object instance and pass the paramaters to it
	 */
	PHALCON_INIT_VAR(criteria);
	object_init_ex(criteria, phalcon_mvc_model_criteria_ce);
	if (phalcon_fast_count_ev(conditions TSRMLS_CC)) {
		PHALCON_INIT_VAR(join_conditions);
		phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC);
		phalcon_call_method_p1_noret(criteria, "where", join_conditions);
		phalcon_call_method_p1_noret(criteria, "bind", bind);
	}
	
	RETURN_CTOR(criteria);
}
Пример #28
0
/**
 * Initialize the metadata for certain table
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param string $key
 * @param string $table
 * @param string $schema
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, _initialize){

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

	PHALCON_MM_GROW();

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

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

					/** 
					 * Get the meta-data extraction strategy
					 */
					PHALCON_CALL_METHOD(&strategy, this_ptr, "getstrategy");
	
					/** 
					 * Get the meta-data
					 */
					PHALCON_CALL_METHOD(&model_metadata, strategy, "getmetadata", model, dependency_injector);
				}
	
				/** 
				 * Store the meta-data locally
				 */
				phalcon_update_property_array(this_ptr, SL("_metaData"), key, model_metadata TSRMLS_CC);
	
				/** 
				 * Store the meta-data in the adapter
				 */
				PHALCON_CALL_METHOD(NULL, this_ptr, "write", prefix_key, model_metadata);
			}
		}
	}
	
	/** 
	 * Check for a column map, store in _columnMap in order and reversed order
	 */
	if (!PHALCON_GLOBAL(orm).column_renaming) {
		RETURN_MM_NULL();
	}
	
	PHALCON_INIT_VAR(key_name);
	phalcon_fast_strtolower(key_name, class_name);
	
	PHALCON_OBS_VAR(column_map);
	phalcon_read_property_this(&column_map, this_ptr, SL("_columnMap"), PH_NOISY TSRMLS_CC);
	if (phalcon_array_isset(column_map, key_name)) {
		RETURN_MM_NULL();
	}
	
	if (Z_TYPE_P(column_map) != IS_ARRAY) { 
		PHALCON_INIT_NVAR(column_map);
		array_init(column_map);
	}
	
	/** 
	 * Create the map key name
	 */
	PHALCON_INIT_NVAR(prefix_key);
	PHALCON_CONCAT_SV(prefix_key, "map-", key_name);
	
	/** 
	 * Check if the meta-data is already in the adapter
	 */
	PHALCON_CALL_METHOD(&data, this_ptr, "read", prefix_key);
	if (Z_TYPE_P(data) != IS_NULL) {
		phalcon_array_update_zval(&column_map, key_name, data, PH_COPY | PH_SEPARATE);
		phalcon_update_property_this(this_ptr, SL("_columnMap"), column_map TSRMLS_CC);
		RETURN_MM_NULL();
	}
	
	/** 
	 * Get the meta-data extraction strategy
	 */
	if (Z_TYPE_P(strategy) != IS_OBJECT) {
		PHALCON_CALL_METHOD(&strategy, this_ptr, "getstrategy");
	}
	
	/** 
	 * Get the meta-data
	 */
	PHALCON_CALL_METHOD(&model_column_map, strategy, "getcolumnmaps", model, dependency_injector);
	
	/** 
	 * Update the column map locally
	 */
	phalcon_update_property_array(this_ptr, SL("_columnMap"), key_name, model_column_map TSRMLS_CC);
	
	/** 
	 * Write the data to the adapter
	 */
	PHALCON_CALL_METHOD(NULL, this_ptr, "write", prefix_key, model_column_map);
	
	PHALCON_MM_RESTORE();
}
Пример #29
0
/**
 * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST
 *
 * @param Phalcon\DI $dependencyInjector
 * @param string $modelName
 * @param array $data
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){

	zval *dependency_injector = NULL, *model_name = NULL, *data = NULL;
	zval *conditions = NULL, *number_data = NULL, *service = NULL, *meta_data = NULL;
	zval *model = NULL, *data_types = NULL, *bind = NULL, *value = NULL, *field = NULL, *type = NULL;
	zval *condition = NULL, *value_pattern = NULL, *criteria = NULL, *number_conditions = NULL;
	zval *join_conditions = NULL;
	zval *c0 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &dependency_injector, &model_name, &data) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (Z_TYPE_P(data) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array");
		return;
	}
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services");
		return;
	}
	
	PHALCON_INIT_VAR(conditions);
	array_init(conditions);
	
	PHALCON_INIT_VAR(number_data);
	phalcon_fast_count(number_data, data TSRMLS_CC);
	if (!phalcon_compare_strict_long(number_data, 0 TSRMLS_CC)) {
		PHALCON_INIT_VAR(service);
		ZVAL_STRING(service, "modelsMetadata", 1);
		
		PHALCON_INIT_VAR(meta_data);
		PHALCON_CALL_METHOD_PARAMS_1(meta_data, dependency_injector, "getshared", service, PH_NO_CHECK);
		ce0 = phalcon_fetch_class(model_name TSRMLS_CC);
		
		PHALCON_INIT_VAR(model);
		object_init_ex(model, ce0);
		PHALCON_CALL_METHOD_NORETURN(model, "__construct", PH_CHECK);
		
		PHALCON_INIT_VAR(data_types);
		PHALCON_CALL_METHOD_PARAMS_1(data_types, meta_data, "getdatatypes", model, PH_NO_CHECK);
		
		PHALCON_INIT_VAR(bind);
		array_init(bind);
		if (!phalcon_valid_foreach(data TSRMLS_CC)) {
			return;
		}
		
		ah0 = Z_ARRVAL_P(data);
		zend_hash_internal_pointer_reset_ex(ah0, &hp0);
		fes_1d26_0:
			if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
				goto fee_1d26_0;
			}
			
			PHALCON_INIT_VAR(field);
			PHALCON_GET_FOREACH_KEY(field, ah0, hp0);
			PHALCON_INIT_VAR(value);
			ZVAL_ZVAL(value, *hd, 1, 0);
			eval_int = phalcon_array_isset(data_types, field);
			if (eval_int) {
				if (Z_TYPE_P(value) != IS_NULL) {
					if (!PHALCON_COMPARE_STRING(value, "")) {
						PHALCON_INIT_VAR(type);
						phalcon_array_fetch(&type, data_types, field, PH_NOISY_CC);
						if (phalcon_compare_strict_long(type, 2 TSRMLS_CC)) {
							PHALCON_INIT_VAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":");
							
							PHALCON_INIT_VAR(value_pattern);
							PHALCON_CONCAT_SVS(value_pattern, "%", value, "%");
							phalcon_array_update_zval(&bind, field, &value_pattern, PH_COPY | PH_SEPARATE TSRMLS_CC);
						} else {
							PHALCON_INIT_VAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":");
							phalcon_array_update_zval(&bind, field, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
						}
						
						phalcon_array_append(&conditions, condition, PH_SEPARATE TSRMLS_CC);
					}
				}
			}
			zend_hash_move_forward_ex(ah0, &hp0);
			goto fes_1d26_0;
		fee_1d26_0:
		if(0){}
		
	}
	
	PHALCON_INIT_VAR(criteria);
	object_init_ex(criteria, phalcon_mvc_model_criteria_ce);
	PHALCON_CALL_METHOD_NORETURN(criteria, "__construct", PH_CHECK);
	
	PHALCON_INIT_VAR(number_conditions);
	phalcon_fast_count(number_conditions, conditions TSRMLS_CC);
	if (!phalcon_compare_strict_long(number_conditions, 0 TSRMLS_CC)) {
		PHALCON_INIT_VAR(c0);
		ZVAL_STRING(c0, " AND ", 1);
		PHALCON_INIT_VAR(join_conditions);
		phalcon_fast_join(join_conditions, c0, conditions TSRMLS_CC);
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(criteria, "where", join_conditions, PH_NO_CHECK);
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(criteria, "bind", bind, PH_NO_CHECK);
	}
	
	
	RETURN_CTOR(criteria);
}
Пример #30
0
/**
 * The meta-data is obtained by reading the column descriptions from the database information schema
 *
 * @param Phalcon\Mvc\ModelInterface $model
 * @param Phalcon\DiInterface $dependencyInjector
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Annotations, getMetaData){

	zval *model, *dependency_injector, *service;
	zval *annotations = NULL, *class_name, *reflection = NULL;
	zval *exception_message = NULL, *properties_annotations = NULL;
	zval *attributes, *primary_keys, *non_primary_keys;
	zval *numeric_typed, *not_null, *field_types, *field_sizes, *field_bytes, *field_scales;
	zval *field_bind_types, *automatic_create_attributes, *automatic_update_attributes;
	zval *identity_field = NULL, *column_annot_name;
	zval *primary_annot_name, *id_annot_name;
	zval *column_map_name, *column_type_name, *column_size_name, *column_bytes_name, *column_scale_name, *column_nullable_name;
	zval *prop_annotations = NULL, *property = NULL, *has_annotation = NULL;
	zval *column_annotation = NULL, *real_property = NULL, *feature = NULL;
	zval *field_default_values, *column_default_value = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

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

	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The dependency injector is invalid");
		return;
	}

	PHALCON_INIT_VAR(service);
	ZVAL_STRING(service, "annotations", 1);

	PHALCON_CALL_METHOD(&annotations, dependency_injector, "get", service);

	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, model, 0 TSRMLS_CC);

	PHALCON_CALL_METHOD(&reflection, annotations, "get", class_name);
	if (Z_TYPE_P(reflection) != IS_OBJECT) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "No annotations were found in class ", class_name);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
		return;
	}

	/** 
	 * Get the properties defined in 
	 */
	PHALCON_CALL_METHOD(&properties_annotations, reflection, "getpropertiesannotations");

	if (!zend_is_true(properties_annotations) || !phalcon_fast_count_ev(properties_annotations TSRMLS_CC)) {
		PHALCON_INIT_NVAR(exception_message);
		PHALCON_CONCAT_SV(exception_message, "No properties with annotations were found in class ", class_name);
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
		return;
	}

	/** 
	 * Initialize meta-data
	 */
	PHALCON_INIT_VAR(attributes);
	array_init(attributes);

	PHALCON_INIT_VAR(primary_keys);
	array_init(primary_keys);

	PHALCON_INIT_VAR(non_primary_keys);
	array_init(non_primary_keys);

	PHALCON_INIT_VAR(numeric_typed);
	array_init(numeric_typed);

	PHALCON_INIT_VAR(not_null);
	array_init(not_null);

	PHALCON_INIT_VAR(field_types);
	array_init(field_types);

	PHALCON_INIT_VAR(field_sizes);
	array_init(field_sizes);

	PHALCON_INIT_VAR(field_bytes);
	array_init(field_bytes);

	PHALCON_INIT_VAR(field_scales);
	array_init(field_scales);

	PHALCON_INIT_VAR(field_bind_types);
	array_init(field_bind_types);

	PHALCON_INIT_VAR(automatic_create_attributes);
	array_init(automatic_create_attributes);

	PHALCON_INIT_VAR(automatic_update_attributes);
	array_init(automatic_update_attributes);

	PHALCON_INIT_VAR(identity_field);
	ZVAL_FALSE(identity_field);

	PHALCON_INIT_VAR(field_default_values);
	array_init(field_default_values);

	PHALCON_INIT_VAR(column_annot_name);
	ZVAL_STRING(column_annot_name, "Column", 1);

	PHALCON_INIT_VAR(primary_annot_name);
	ZVAL_STRING(primary_annot_name, "Primary", 1);

	PHALCON_INIT_VAR(id_annot_name);
	ZVAL_STRING(id_annot_name, "Identity", 1);

	PHALCON_INIT_VAR(column_map_name);
	ZVAL_STRING(column_map_name, "column", 1);

	PHALCON_INIT_VAR(column_type_name);
	ZVAL_STRING(column_type_name, "type", 1);

	PHALCON_INIT_VAR(column_size_name);
	ZVAL_STRING(column_size_name, "size", 1);

	PHALCON_INIT_VAR(column_bytes_name);
	ZVAL_STRING(column_bytes_name, "bytes", 1);

	PHALCON_INIT_VAR(column_scale_name);
	ZVAL_STRING(column_scale_name, "scale", 1);

	PHALCON_INIT_VAR(column_default_value);
	ZVAL_STRING(column_default_value, "default", 1);

	PHALCON_INIT_VAR(column_nullable_name);
	ZVAL_STRING(column_nullable_name, "nullable", 1);

	phalcon_is_iterable(properties_annotations, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HKEY(property, ah0, hp0);
		PHALCON_GET_HVALUE(prop_annotations);

		/** 
		 * All columns marked with the 'Column' annotation are considered columns
		 */
		PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", column_annot_name);
		if (!zend_is_true(has_annotation)) {
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}

		/** 
		 * Fetch the 'column' annotation
		 */
		PHALCON_CALL_METHOD(&column_annotation, prop_annotations, "get", column_annot_name);

		/** 
		 * Check column map
		 */
		PHALCON_CALL_METHOD(&real_property, column_annotation, "getargument", column_map_name);

		if (PHALCON_IS_EMPTY(real_property)) {
			PHALCON_CPY_WRT(real_property, property);
		}

		/** 
		 * Check if annotation has the 'type' named parameter
		 */
		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_type_name);

		if (PHALCON_IS_STRING(feature, "integer")) {
			phalcon_array_update_zval_long(&field_types, real_property, 0, PH_COPY);
			phalcon_array_update_zval_long(&field_bind_types, real_property, 1, PH_COPY);
			phalcon_array_update_zval_bool(&numeric_typed, real_property, 1, PH_COPY);
		} else if (PHALCON_IS_STRING(feature, "decimal")) {
			phalcon_array_update_zval_long(&field_types, real_property, 3, PH_COPY);
			phalcon_array_update_zval_long(&field_bind_types, real_property, 32, PH_COPY);
			phalcon_array_update_zval_bool(&numeric_typed, real_property, 1, PH_COPY);
		} else if (PHALCON_IS_STRING(feature, "boolean")) {
			phalcon_array_update_zval_long(&field_types, real_property, 8, PH_COPY);
			phalcon_array_update_zval_long(&field_bind_types, real_property, 5, PH_COPY);
		} else {
			if (PHALCON_IS_STRING(feature, "date")) {
				phalcon_array_update_zval_long(&field_types, real_property, 1, PH_COPY);
			} else {
				/**
				 * By default all columns are varchar/string
				 */
				phalcon_array_update_zval_long(&field_types, real_property, 2, PH_COPY);
			}
			phalcon_array_update_zval_long(&field_bind_types, real_property, 2, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_size_name);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_sizes, real_property, feature, PH_COPY);
			phalcon_array_update_zval(&field_bytes, real_property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_bytes_name);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_bytes, real_property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_scale_name);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_scales, real_property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_default_values, real_property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_default_values, property, feature, PH_COPY);
		}

		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value);
		if (!PHALCON_IS_EMPTY(feature)) {
			phalcon_array_update_zval(&field_default_values, property, feature, PH_COPY);
		}

		/** 
		 * All columns marked with the 'Primary' annotation are considered primary keys
		 */
		PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", primary_annot_name);
		if (zend_is_true(has_annotation)) {
			phalcon_array_append(&primary_keys, real_property, PH_COPY);
		} else {
			phalcon_array_append(&non_primary_keys, real_property, PH_COPY);
		}

		/** 
		 * All columns marked with the 'Primary' annotation are considered primary keys
		 */
		PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", id_annot_name);
		if (zend_is_true(has_annotation)) {
			PHALCON_CPY_WRT(identity_field, real_property);
		}

		/** 
		 * Check if the column 
		 */
		PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_nullable_name);
		if (!zend_is_true(feature)) {
			phalcon_array_append(&not_null, real_property, PH_COPY);
		}

		phalcon_array_append(&attributes, real_property, PH_COPY);
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	/** 
	 * Create an array using the MODELS_* constants as indexes
	 */
	array_init_size(return_value, 14);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_ATTRIBUTES,  attributes, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_PRIMARY_KEY,  primary_keys, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_NON_PRIMARY_KEY,  non_primary_keys, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_NOT_NULL,  not_null, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES,  field_types, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES_NUMERIC,  numeric_typed, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_IDENTITY_COLUMN,  identity_field, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES_BIND,  field_bind_types, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_AUTOMATIC_DEFAULT_INSERT, automatic_create_attributes, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_AUTOMATIC_DEFAULT_UPDATE, automatic_update_attributes, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_DEFAULT_VALUE, field_default_values, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_SZIE, field_sizes, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_SCALE, field_scales, PH_COPY);
	phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_BYTE, field_bytes, PH_COPY);
	
	PHALCON_MM_RESTORE();
}