Example #1
0
/**
 * Initializes the internal handler, calling functions on it
 *
 * @param string $method
 * @param array $arguments
 * @return mixed
 */
PHP_METHOD(Phalcon_Mvc_Micro_LazyLoader, __call){

	zval *method, *arguments, *handler = NULL, *definition;
	zval *call_handler;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &method, &arguments);

	handler = phalcon_read_property(getThis(), SL("_handler"), PH_NOISY);
	if (Z_TYPE_P(handler) != IS_OBJECT) {
		definition = phalcon_read_property(getThis(), SL("_definition"), PH_NOISY);
		ce0 = phalcon_fetch_class(definition);

		PHALCON_INIT_NVAR(handler);
		object_init_ex(handler, ce0);
		if (phalcon_has_constructor(handler)) {
			PHALCON_CALL_METHOD(NULL, handler, "__construct");
		}
		phalcon_update_property_this(getThis(), SL("_handler"), handler);
	}

	PHALCON_INIT_VAR(call_handler);
	array_init_size(call_handler, 2);
	phalcon_array_append(call_handler, handler, PH_COPY);
	phalcon_array_append(call_handler, method, PH_COPY);

	/** 
	 * Call the handler
	 */
	PHALCON_CALL_USER_FUNC_ARRAY(&return_value, call_handler, arguments);
	RETURN_MM();
}
Example #2
0
/**
 * Initializes the internal handler, calling functions on it
 *
 * @param string $method
 * @param array $arguments
 * @return mixed
 */
PHP_METHOD(Phalcon_Mvc_Micro_LazyLoader, __call){

	zval *method, *arguments, handler = {}, definition = {}, call_handler = {};
	zend_class_entry *ce0;

	phalcon_fetch_params(0, 2, 0, &method, &arguments);

	phalcon_return_property(&handler, getThis(), SL("_handler"));
	if (Z_TYPE(handler) != IS_OBJECT) {
		phalcon_read_property(&definition, getThis(), SL("_definition"), PH_NOISY);
		ce0 = phalcon_fetch_class(&definition, ZEND_FETCH_CLASS_DEFAULT);

		PHALCON_OBJECT_INIT(&handler, ce0);
		if (phalcon_has_constructor(&handler)) {
			PHALCON_CALL_METHODW(NULL, &handler, "__construct");
		}
		phalcon_update_property_zval(getThis(), SL("_handler"), &handler);
	}

	array_init_size(&call_handler, 2);
	phalcon_array_append(&call_handler, &handler, PH_COPY);
	phalcon_array_append(&call_handler, method, PH_COPY);

	/** 
	 * Call the handler
	 */
	PHALCON_CALL_USER_FUNC_ARRAYW(return_value, &call_handler, arguments);
}
Example #3
0
/**
 * Sets a Expires header to use HTTP cache
 *
 *<code>
 *	$this->response->setExpires(new DateTime());
 *</code>
 *
 * @param DateTime $datetime
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Http_Response, setExpires){

	zval *datetime, *headers, *date, *utc_zone, *timezone;
	zval *format, *utc_format, *utc_date, *expires_header;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &datetime);
	
	if (Z_TYPE_P(datetime) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_http_response_exception_ce, "datetime parameter must be an instance of DateTime");
		return;
	}
	
	PHALCON_INIT_VAR(headers);
	phalcon_call_method(headers, this_ptr, "getheaders");
	
	PHALCON_INIT_VAR(date);
	if (phalcon_clone(date, datetime TSRMLS_CC) == FAILURE) {
		return;
	}
	
	/** 
	 * All the expiration times are sent in UTC
	 */
	PHALCON_INIT_VAR(utc_zone);
	ZVAL_STRING(utc_zone, "UTC", 1);
	ce0 = zend_fetch_class(SL("DateTimeZone"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
	
	PHALCON_INIT_VAR(timezone);
	object_init_ex(timezone, ce0);
	if (phalcon_has_constructor(timezone TSRMLS_CC)) {
		phalcon_call_method_p1_noret(timezone, "__construct", utc_zone);
	}
	
	/** 
	 * Change the timezone to utc
	 */
	phalcon_call_method_p1_noret(date, "settimezone", timezone);
	
	PHALCON_INIT_VAR(format);
	ZVAL_STRING(format, "D, d M Y H:i:s", 1);
	
	PHALCON_INIT_VAR(utc_format);
	phalcon_call_method_p1(utc_format, date, "format", format);
	
	PHALCON_INIT_VAR(utc_date);
	PHALCON_CONCAT_VS(utc_date, utc_format, " GMT");
	
	/** 
	 * The 'Expires' header set this info
	 */
	PHALCON_INIT_VAR(expires_header);
	ZVAL_STRING(expires_header, "Expires", 1);
	phalcon_call_method_p2_noret(this_ptr, "setheader", expires_header, utc_date);
	
	RETURN_THIS();
}
Example #4
0
/**
 * Create internal connection to memcached
 */
PHP_METHOD(Phalcon_Cache_Backend_Libmemcached, _connect){

	zval *options, *memcache, *servers, *client = NULL, *res = NULL;
	zend_string *str_key;
	ulong idx;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	options = phalcon_read_property(getThis(), SL("_options"), PH_NOISY);
	ce0 = zend_fetch_class(SSL("Memcached"), ZEND_FETCH_CLASS_AUTO);

	PHALCON_INIT_VAR(memcache);
	object_init_ex(memcache, ce0);
	if (phalcon_has_constructor(memcache)) {
		PHALCON_CALL_METHOD(NULL, memcache, "__construct");
	}

	if (!phalcon_array_isset_str_fetch(&servers, options, SL("servers")) || Z_TYPE_P(servers) != IS_ARRAY) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Servers must be an array");
		return;
	}

	phalcon_array_isset_str_fetch(&client, options, SL("client"));

	PHALCON_RETURN_CALL_METHOD(memcache, "addservers", servers);

	if (!zend_is_true(return_value)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cannot connect to Memcached server");
		return;
	}

	if (client && Z_TYPE_P(client) == IS_ARRAY) {
		zval *value;

		ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(client), idx, str_key, value) {
			zval option;

			if (str_key) {
				if ((res = zend_get_constant(str_key)) != NULL) {
					PHALCON_CALL_METHOD(NULL, memcache, "setoption", res, value);
				}
			} else {
				ZVAL_LONG(&option, idx);
				PHALCON_CALL_METHOD(NULL, memcache, "setoption", &option, value);
			}
		} ZEND_HASH_FOREACH_END();
Example #5
0
/**
 * Create internal connection to memcached
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, _connect){

	zval *options, *memcache, *host, *port, *persistent;
	zval *success = NULL;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(options);
	phalcon_read_property(&options, this_ptr, SL("_options"), PH_NOISY_CC);
	ce0 = zend_fetch_class(SL("Memcache"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
	
	PHALCON_INIT_VAR(memcache);
	object_init_ex(memcache, ce0);
	if (phalcon_has_constructor(memcache TSRMLS_CC)) {
		PHALCON_CALL_METHOD_NORETURN(memcache, "__construct");
	}
	
	PHALCON_OBS_VAR(host);
	phalcon_array_fetch_string(&host, options, SL("host"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(port);
	phalcon_array_fetch_string(&port, options, SL("port"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(persistent);
	phalcon_array_fetch_string(&persistent, options, SL("persistent"), PH_NOISY_CC);
	if (zend_is_true(persistent)) {
		PHALCON_INIT_VAR(success);
		PHALCON_CALL_METHOD_PARAMS_2(success, memcache, "pconnect", host, port);
	} else {
		PHALCON_INIT_NVAR(success);
		PHALCON_CALL_METHOD_PARAMS_2(success, memcache, "connect", host, port);
	}
	
	if (!zend_is_true(success)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cannot connect to Memcached server");
		return;
	}
	
	phalcon_update_property_zval(this_ptr, SL("_memcache"), memcache TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
Example #6
0
/**
 * Create internal connection to memcached
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, _connect)
{
	zval *options, *memcache, *host, *port, *persistent;
	zval *success = NULL;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);
	ce0 = zend_fetch_class(SL("Memcache"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

	PHALCON_INIT_VAR(memcache);
	object_init_ex(memcache, ce0);
	if (phalcon_has_constructor(memcache TSRMLS_CC)) {
		PHALCON_CALL_METHOD(NULL, memcache, "__construct");
	}
	
	if (
		   !phalcon_array_isset_string_fetch(&host, options, SS("host"))
		|| !phalcon_array_isset_string_fetch(&port, options, SS("port"))
		|| !phalcon_array_isset_string_fetch(&persistent, options, SS("persistent"))
	) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	if (zend_is_true(persistent)) {
		PHALCON_CALL_METHOD(&success, memcache, "pconnect", host, port);
	} else {
		PHALCON_CALL_METHOD(&success, memcache, "connect", host, port);
	}
	
	if (!zend_is_true(success)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cannot connect to Memcached server");
		return;
	}

	phalcon_update_property_this(this_ptr, SL("_memcache"), memcache TSRMLS_CC);
	RETURN_CTOR(memcache);
}
Example #7
0
/**
 * Initializes the internal handler, calling functions on it
 *
 * @param string $method
 * @param array $arguments
 * @return mixed
 */
PHP_METHOD(Phalcon_Mvc_Micro_LazyLoader, __call) {

    zval *method, *arguments, *handler = NULL, *definition;
    zval *call_handler, *result;
    zend_class_entry *ce0;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 2, 0, &method, &arguments);

    PHALCON_OBS_VAR(handler);
    phalcon_read_property_this(&handler, this_ptr, SL("_handler"), PH_NOISY_CC);
    if (Z_TYPE_P(handler) != IS_OBJECT) {
        PHALCON_OBS_VAR(definition);
        phalcon_read_property_this(&definition, this_ptr, SL("_definition"), PH_NOISY_CC);
        ce0 = phalcon_fetch_class(definition TSRMLS_CC);

        PHALCON_INIT_NVAR(handler);
        object_init_ex(handler, ce0);
        if (phalcon_has_constructor(handler TSRMLS_CC)) {
            phalcon_call_method_noret(handler, "__construct");
        }
        phalcon_update_property_this(this_ptr, SL("_handler"), handler TSRMLS_CC);
    }

    PHALCON_INIT_VAR(call_handler);
    array_init_size(call_handler, 2);
    phalcon_array_append(&call_handler, handler, PH_SEPARATE TSRMLS_CC);
    phalcon_array_append(&call_handler, method, PH_SEPARATE TSRMLS_CC);

    /**
     * Call the handler
     */
    PHALCON_INIT_VAR(result);
    PHALCON_CALL_USER_FUNC_ARRAY(result, call_handler, arguments);

    RETURN_CCTOR(result);
}
Example #8
0
/**
 * Create internal connection to memcached
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, _connect)
{
	zval options = {}, memcache = {}, host = {}, port = {}, persistent = {}, success = {};
	zend_class_entry *ce0;

	phalcon_return_property(&options, getThis(), SL("_options"));
	ce0 = phalcon_fetch_str_class(SL("Memcache"), ZEND_FETCH_CLASS_AUTO);

	object_init_ex(&memcache, ce0);
	if (phalcon_has_constructor(&memcache)) {
		PHALCON_CALL_METHODW(NULL, &memcache, "__construct");
	}

	if (
		   !phalcon_array_isset_fetch_str(&host, &options, SL("host"))
		|| !phalcon_array_isset_fetch_str(&port, &options, SL("port"))
		|| !phalcon_array_isset_fetch_str(&persistent, &options, SL("persistent"))
	) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	if (zend_is_true(&persistent)) {
		PHALCON_CALL_METHODW(&success, &memcache, "pconnect", &host, &port);
	} else {
		PHALCON_CALL_METHODW(&success, &memcache, "connect", &host, &port);
	}

	if (!zend_is_true(&success)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cannot connect to Memcached server");
		return;
	}

	phalcon_update_property_zval(getThis(), SL("_memcache"), &memcache);
	RETURN_CTORW(&memcache);
}
Example #9
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);
}
Example #10
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 Phalcon\Mvc\Model\Criteria
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){

	zval *dependency_injector, *model_name, *data;
	zval *conditions, *service, *meta_data = NULL, *model;
	zval *data_types = NULL, *bind, *value = NULL, *field = NULL, *type, *condition = NULL;
	zval *value_pattern = NULL, *join_conditions;
	zval *column_map = NULL;
	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;
	}
	
	object_init_ex(return_value, phalcon_mvc_model_criteria_ce);

	if (zend_hash_num_elements(Z_ARRVAL_P(data))) {
	
		PHALCON_INIT_VAR(service);
		PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_modelsMetadata);
	
		PHALCON_CALL_METHOD(&meta_data, dependency_injector, "getshared", service);
		PHALCON_VERIFY_INTERFACE(meta_data, phalcon_mvc_model_metadatainterface_ce);
		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(NULL, model, "__construct");
		}

		PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1);

		if (PHALCON_GLOBAL(orm).column_renaming) {
			PHALCON_CALL_METHOD(&column_map, meta_data, "getreversecolumnmap", model);
			if (Z_TYPE_P(column_map) != IS_ARRAY) {
				PHALCON_INIT_NVAR(column_map);
			}
		}
		else {
			column_map = PHALCON_GLOBAL(z_null);
		}

		PHALCON_CALL_METHOD(&data_types, meta_data, "getdatatypes", model);
	
		PHALCON_INIT_VAR(bind);
		array_init(bind);
	
		PHALCON_INIT_VAR(conditions);
		array_init(conditions);

		/** 
		 * 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) {
			zval *real_field;
	
			PHALCON_GET_HKEY(field, ah0, hp0);
			PHALCON_GET_HVALUE(value);

			if (Z_TYPE_P(column_map) != IS_ARRAY || !phalcon_array_isset_fetch(&real_field, column_map, field)) {
				real_field = field;
			}
	
			if (phalcon_array_isset_fetch(&type, data_types, real_field)) {
				if (Z_TYPE_P(value) != IS_NULL && !PHALCON_IS_STRING(value, "")) {
					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);
					} 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);
					}

					phalcon_array_append(&conditions, condition, 0);
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		if (zend_hash_num_elements(Z_ARRVAL_P(conditions))) {
			PHALCON_INIT_VAR(join_conditions);
			phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC);
			PHALCON_CALL_METHOD(NULL, return_value, "where", join_conditions, bind);
		}
	}
	
	PHALCON_CALL_METHOD(NULL, return_value, "setmodelname", model_name);
	RETURN_MM();
}
Example #11
0
/**
 * Shows a backtrace item
 *
 * @param int $n
 * @param array $trace
 */
PHP_METHOD(Phalcon_Debug, showTraceItem){

	zval *n, *trace, *space, *two_spaces, *underscore;
	zval *minus, *html, *class_name, *pattern, *is_phalcon_class;
	zval *namespace_separator, *prepare_uri_class;
	zval *class_reflection, *is_internal = NULL, *lower_class_name;
	zval *prepare_internal_class, *type, *function_name = NULL;
	zval *function_reflection, *prepared_function_name;
	zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL;
	zval *span_argument = NULL, *joined_arguments, *one;
	zval *file, *line, *show_files, *lines, *number_lines;
	zval *show_file_fragment, *seven, *before_line;
	zval *first_line = NULL, *five, *after_line, *last_line = NULL;
	zval *comment_pattern, *utf8, *ent_compat, *tab;
	zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL;
	zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL;
	zval *escaped_line = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	zend_class_entry *ce0, *ce1;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &n, &trace);
	
	PHALCON_INIT_VAR(space);
	ZVAL_STRING(space, " ", 1);
	
	PHALCON_INIT_VAR(two_spaces);
	ZVAL_STRING(two_spaces, "  ", 1);
	
	PHALCON_INIT_VAR(underscore);
	ZVAL_STRING(underscore, "_", 1);
	
	PHALCON_INIT_VAR(minus);
	ZVAL_STRING(minus, "-", 1);
	
	/** 
	 * Every trace in the backtrace have a unique number
	 */
	PHALCON_INIT_VAR(html);
	PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>");
	if (phalcon_array_isset_string(trace, SS("class"))) {
	
		PHALCON_OBS_VAR(class_name);
		phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY);
	
		PHALCON_INIT_VAR(pattern);
		ZVAL_STRING(pattern, "/^Phalcon/", 1);
	
		PHALCON_INIT_VAR(is_phalcon_class);
	
		phalcon_preg_match(is_phalcon_class, pattern, class_name, NULL TSRMLS_CC);
	
		/** 
		 * We assume that classes starting by Phalcon are framework's classes
		 */
		if (zend_is_true(is_phalcon_class)) {
			PHALCON_INIT_VAR(namespace_separator);
			ZVAL_STRING(namespace_separator, "\\", 1);
	
			/** 
			 * Prepare the class name according to the Phalcon's conventions
			 */
			PHALCON_INIT_VAR(prepare_uri_class);
			phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name);
	
			/** 
			 * Generate a link to the official docs
			 */
			PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>");
		} else {
			ce0 = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
			PHALCON_INIT_VAR(class_reflection);
			object_init_ex(class_reflection, ce0);
			if (phalcon_has_constructor(class_reflection TSRMLS_CC)) {
				phalcon_call_method_p1_noret(class_reflection, "__construct", class_name);
			}
	
			/** 
			 * Check if classes are PHP's classes
			 */
			PHALCON_INIT_VAR(is_internal);
			phalcon_call_method(is_internal, class_reflection, "isinternal");
			if (zend_is_true(is_internal)) {
				PHALCON_INIT_VAR(lower_class_name);
				phalcon_fast_strtolower(lower_class_name, class_name);
	
				PHALCON_INIT_VAR(prepare_internal_class);
				phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name);
	
				/** 
				 * Generate a link to the official docs
				 */
				PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>");
			} else {
				PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>");
			}
		}
	
		/** 
		 * Object access operator: static/instance
		 */
		PHALCON_OBS_VAR(type);
		phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY);
		phalcon_concat_self(&html, type TSRMLS_CC);
	}
	
	/** 
	 * Normally the backtrace contains only classes
	 */
	if (phalcon_array_isset_string(trace, SS("class"))) {
		PHALCON_OBS_VAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
		PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
	} else {
		PHALCON_OBS_NVAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
	
		/** 
		 * Check if the function exists
		 */
		if (phalcon_function_exists(function_name TSRMLS_CC) == SUCCESS) {
			ce1 = zend_fetch_class(SL("ReflectionFunction"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
			PHALCON_INIT_VAR(function_reflection);
			object_init_ex(function_reflection, ce1);
			if (phalcon_has_constructor(function_reflection TSRMLS_CC)) {
				phalcon_call_method_p1_noret(function_reflection, "__construct", function_name);
			}
	
			PHALCON_INIT_NVAR(is_internal);
			phalcon_call_method(is_internal, function_reflection, "isinternal");
	
			/** 
			 * Internal functions links to the PHP documentation
			 */
			if (zend_is_true(is_internal)) {
				/** 
				 * Prepare function's name according to the conventions in the docs
				 */
				PHALCON_INIT_VAR(prepared_function_name);
				phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name);
				PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>");
			} else {
				PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
			}
		} else {
			PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
		}
	}
	
	/** 
	 * Check for arguments in the function
	 */
	if (phalcon_array_isset_string(trace, SS("args"))) {
	
		PHALCON_OBS_VAR(trace_args);
		phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY);
		if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) {
	
			PHALCON_INIT_VAR(arguments);
			array_init(arguments);
	
			phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HVALUE(argument);
	
				/** 
				 * Every argument is generated using _getVarDump
				 */
				PHALCON_INIT_NVAR(dumped_argument);
				phalcon_call_method_p1(dumped_argument, this_ptr, "_getvardump", argument);
	
				PHALCON_INIT_NVAR(span_argument);
				PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>");
	
				/** 
				 * Append the HTML generated to the argument's list
				 */
				phalcon_array_append(&arguments, span_argument, PH_SEPARATE);
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			/** 
			 * Join all the arguments
			 */
			PHALCON_INIT_VAR(joined_arguments);
			phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC);
			PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")");
		} else {
			phalcon_concat_self_str(&html, SL("()") TSRMLS_CC);
		}
	}
	
	/** 
	 * When 'file' is present, it usually means the function is provided by the user
	 */
	if (phalcon_array_isset_string(trace, SS("file"))) {
	
		PHALCON_INIT_VAR(one);
		ZVAL_LONG(one, 1);
	
		PHALCON_OBS_VAR(file);
		phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY);
	
		PHALCON_OBS_VAR(line);
		phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY);
	
		/** 
		 * Realpath to the file and its line using a special header
		 */
		PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", file, " (", line, ")</div>");
	
		PHALCON_OBS_VAR(show_files);
		phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY_CC);
	
		/** 
		 * The developer can change if the files must be opened or not
		 */
		if (zend_is_true(show_files)) {
	
			/** 
			 * Open the file to an array using 'file', this respects the openbase-dir directive
			 */
			PHALCON_INIT_VAR(lines);
			phalcon_call_func_p1(lines, "file", file);
	
			PHALCON_INIT_VAR(number_lines);
			phalcon_fast_count(number_lines, lines TSRMLS_CC);
	
			PHALCON_OBS_VAR(show_file_fragment);
			phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY_CC);
	
			/** 
			 * File fragments just show a piece of the file where the exception is located
			 */
			if (zend_is_true(show_file_fragment)) {
	
				/** 
				 * Take seven lines back to the current exception's line, @TODO add an option for
				 * this
				 */
				PHALCON_INIT_VAR(seven);
				ZVAL_LONG(seven, 7);
	
				PHALCON_INIT_VAR(before_line);
				sub_function(before_line, line, seven TSRMLS_CC);
	
				/** 
				 * Check for overflows
				 */
				if (PHALCON_LT_LONG(before_line, 1)) {
					PHALCON_CPY_WRT(first_line, one);
				} else {
					PHALCON_CPY_WRT(first_line, before_line);
				}
	
				/** 
				 * Take five lines after the current exception's line, @TODO add an option for this
				 */
				PHALCON_INIT_VAR(five);
				ZVAL_LONG(five, 5);
	
				PHALCON_INIT_VAR(after_line);
				phalcon_add_function(after_line, line, five TSRMLS_CC);
	
				/** 
				 * Check for overflows
				 */
				if (PHALCON_GT(after_line, number_lines)) {
					PHALCON_CPY_WRT(last_line, number_lines);
				} else {
					PHALCON_CPY_WRT(last_line, after_line);
				}
	
				PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>");
			} else {
				PHALCON_CPY_WRT(first_line, one);
				PHALCON_CPY_WRT(last_line, number_lines);
				PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>");
			}
	
			PHALCON_INIT_VAR(comment_pattern);
			ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1);
	
			/** 
			 * We assume the file is utf-8 encoded, @TODO add an option for this
			 */
			PHALCON_INIT_VAR(utf8);
			ZVAL_STRING(utf8, "UTF-8", 1);
	
			/** 
			 * Don't escape quotes
			 */
			PHALCON_INIT_VAR(ent_compat);
			ZVAL_LONG(ent_compat, 2);
	
			PHALCON_INIT_VAR(tab);
			ZVAL_STRING(tab, "\t", 1);
	
			PHALCON_INIT_VAR(comment);
			ZVAL_STRING(comment, "* /", 1);
			PHALCON_CPY_WRT(i, first_line);
	
			while (1) {
	
				if (PHALCON_LE(i, last_line)) {
				} else {
					break;
				}
	
				/** 
				 * Current line in the file
				 */
				PHALCON_INIT_NVAR(line_position);
				sub_function(line_position, i, one TSRMLS_CC);
	
				/** 
				 * Current line content in the piece of file
				 */
				PHALCON_OBS_NVAR(current_line);
				phalcon_array_fetch(&current_line, lines, line_position, PH_NOISY);
	
				/** 
				 * File fragments are cleaned, removing tabs and comments
				 */
				if (zend_is_true(show_file_fragment)) {
					if (PHALCON_IS_EQUAL(i, first_line)) {
	
						PHALCON_INIT_NVAR(trimmed);
						phalcon_fast_trim(trimmed, current_line, PHALCON_TRIM_RIGHT TSRMLS_CC);
	
						PHALCON_INIT_NVAR(is_comment);
	
						phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC);
	
						if (zend_is_true(is_comment)) {
							PHALCON_INIT_NVAR(spaced_current_line);
							phalcon_fast_str_replace(spaced_current_line, comment, space, current_line);
							PHALCON_CPY_WRT(current_line, spaced_current_line);
						}
					}
				}
	
				/** 
				 * Print a non break space if the current line is a line break, this allows to show
				 * the html zebra properly
				 */
				if (PHALCON_IS_STRING(current_line, "\n")) {
					phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
				} else {
					if (PHALCON_IS_STRING(current_line, "\r\n")) {
						phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(spaced_current_line);
						phalcon_fast_str_replace(spaced_current_line, tab, two_spaces, current_line);
	
						PHALCON_INIT_NVAR(escaped_line);
						phalcon_call_func_p3(escaped_line, "htmlentities", spaced_current_line, ent_compat, utf8);
						phalcon_concat_self(&html, escaped_line TSRMLS_CC);
					}
				}
	
				PHALCON_SEPARATE(i);
				phalcon_increment(i);
			}
			phalcon_concat_self_str(&html, SL("</pre>") TSRMLS_CC);
		}
	}
	
	phalcon_concat_self_str(&html, SL("</td></tr>") TSRMLS_CC);
	
	RETURN_CTOR(html);
}
Example #12
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);
}
Example #13
0
/**
 * Returns a PHQL statement built based on the builder parameters
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, getPhql){

	zval *dependency_injector = NULL, *models, *conditions = NULL;
	zval *one, *number_models, *invalid_condition;
	zval *model = NULL, *service_name, *meta_data, *model_instance;
	zval *no_primary = NULL, *primary_keys, *first_primary_key;
	zval *column_map = NULL, *attribute_field = NULL, *exception_message;
	zval *primary_key_condition, *phql, *columns;
	zval *selected_columns = NULL, *column = NULL, *alias = NULL, *aliased_column = NULL;
	zval *joined_columns = NULL, *selected_column = NULL, *selected_models;
	zval *selected_model = NULL, *joined_models, *joins;
	zval *join = NULL, *join_model = NULL, *join_conditions = NULL, *join_alias = NULL;
	zval *join_type = NULL, *group, *group_items, *group_item = NULL;
	zval *escaped_item = NULL, *joined_items = NULL, *having, *order;
	zval *order_items, *order_item = NULL, *limit, *number;
	zval *offset = NULL;
	HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5;
	HashPosition hp0, hp1, hp2, hp3, hp4, hp5;
	zval **hd;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(dependency_injector);
	phalcon_read_property(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_INIT_NVAR(dependency_injector);
		PHALCON_CALL_STATIC(dependency_injector, "phalcon\\di", "getdefault");
		phalcon_update_property_zval(this_ptr, SL("_dependencyInjector"), dependency_injector TSRMLS_CC);
	}
	
	PHALCON_OBS_VAR(models);
	phalcon_read_property(&models, this_ptr, SL("_models"), PH_NOISY_CC);
	if (Z_TYPE_P(models) == IS_ARRAY) { 
		if (!phalcon_fast_count_ev(models TSRMLS_CC)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "At least one model is required to build the query");
			return;
		}
	} else {
		if (!zend_is_true(models)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "At least one model is required to build the query");
			return;
		}
	}
	
	PHALCON_OBS_VAR(conditions);
	phalcon_read_property(&conditions, this_ptr, SL("_conditions"), PH_NOISY_CC);
	if (phalcon_is_numeric(conditions)) {
	
		/** 
		 * If the conditions is a single numeric field. We internally create a condition
		 * using the related primary key
		 */
		if (Z_TYPE_P(models) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(one);
			ZVAL_LONG(one, 1);
	
			PHALCON_INIT_VAR(number_models);
			phalcon_fast_count(number_models, models TSRMLS_CC);
	
			PHALCON_INIT_VAR(invalid_condition);
			is_smaller_function(invalid_condition, one, number_models TSRMLS_CC);
			if (PHALCON_IS_TRUE(invalid_condition)) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Cannot build the query. Invalid condition");
				return;
			}
	
			PHALCON_OBS_VAR(model);
			phalcon_array_fetch_long(&model, models, 0, PH_NOISY_CC);
		} else {
			PHALCON_CPY_WRT(model, models);
		}
	
		PHALCON_INIT_VAR(service_name);
		ZVAL_STRING(service_name, "modelsMetadata", 1);
	
		/** 
		 * Get the models metadata service to obtain the column names, column map and
		 * primary key
		 */
		PHALCON_INIT_VAR(meta_data);
		PHALCON_CALL_METHOD_PARAMS_1(meta_data, dependency_injector, "getshared", service_name);
		ce0 = phalcon_fetch_class(model TSRMLS_CC);
	
		PHALCON_INIT_VAR(model_instance);
		object_init_ex(model_instance, ce0);
		if (phalcon_has_constructor(model_instance TSRMLS_CC)) {
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(model_instance, "__construct", dependency_injector);
		}
	
		PHALCON_INIT_VAR(no_primary);
		ZVAL_BOOL(no_primary, 1);
	
		PHALCON_INIT_VAR(primary_keys);
		PHALCON_CALL_METHOD_PARAMS_1(primary_keys, meta_data, "getprimarykeyattributes", model_instance);
		if (phalcon_fast_count_ev(primary_keys TSRMLS_CC)) {
			if (phalcon_array_isset_long(primary_keys, 0)) {
	
				PHALCON_OBS_VAR(first_primary_key);
				phalcon_array_fetch_long(&first_primary_key, primary_keys, 0, PH_NOISY_CC);
	
				/** 
				 * The PHQL contains the renamed columns if available
				 */
				if (PHALCON_GLOBAL(orm).column_renaming) {
					PHALCON_INIT_VAR(column_map);
					PHALCON_CALL_METHOD_PARAMS_1(column_map, meta_data, "getcolumnmap", model_instance);
				} else {
					PHALCON_INIT_NVAR(column_map);
				}
	
				if (Z_TYPE_P(column_map) == IS_ARRAY) { 
					if (phalcon_array_isset(column_map, first_primary_key)) {
						PHALCON_OBS_VAR(attribute_field);
						phalcon_array_fetch(&attribute_field, column_map, first_primary_key, PH_NOISY_CC);
					} else {
						PHALCON_INIT_VAR(exception_message);
						PHALCON_CONCAT_SVS(exception_message, "Column '", first_primary_key, "\" isn't part of the column map");
						PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
						return;
					}
				} else {
					PHALCON_CPY_WRT(attribute_field, first_primary_key);
				}
	
				PHALCON_INIT_VAR(primary_key_condition);
				PHALCON_CONCAT_SVSVSV(primary_key_condition, "[", model, "].[", attribute_field, "] = ", conditions);
				PHALCON_CPY_WRT(conditions, primary_key_condition);
	
				ZVAL_BOOL(no_primary, 0);
			}
		}
	
		/** 
		 * A primary key is mandatory in these cases
		 */
		if (PHALCON_IS_TRUE(no_primary)) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Source related to this model does not have a primary key defined");
			return;
		}
	}
	
	PHALCON_INIT_VAR(phql);
	ZVAL_STRING(phql, "SELECT ", 1);
	
	PHALCON_OBS_VAR(columns);
	phalcon_read_property(&columns, this_ptr, SL("_columns"), PH_NOISY_CC);
	if (Z_TYPE_P(columns) != IS_NULL) {
		if (Z_TYPE_P(columns) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(selected_columns);
			array_init(selected_columns);
	
			if (!phalcon_is_iterable(columns, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(alias, ah0, hp0);
				PHALCON_GET_FOREACH_VALUE(column);
	
				if (Z_TYPE_P(alias) == IS_LONG) {
					phalcon_array_append(&selected_columns, column, PH_SEPARATE TSRMLS_CC);
				} else {
					PHALCON_INIT_NVAR(aliased_column);
					PHALCON_CONCAT_VSV(aliased_column, column, " AS ", alias);
					phalcon_array_append(&selected_columns, aliased_column, PH_SEPARATE TSRMLS_CC);
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			PHALCON_INIT_VAR(joined_columns);
			phalcon_fast_join_str(joined_columns, SL(", "), selected_columns TSRMLS_CC);
			phalcon_concat_self(&phql, joined_columns TSRMLS_CC);
		} else {
			phalcon_concat_self(&phql, columns TSRMLS_CC);
		}
	} else {
		if (Z_TYPE_P(models) == IS_ARRAY) { 
	
			PHALCON_INIT_NVAR(selected_columns);
			array_init(selected_columns);
	
			if (!phalcon_is_iterable(models, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(alias, ah1, hp1);
				PHALCON_GET_FOREACH_VALUE(model);
	
				if (Z_TYPE_P(alias) == IS_LONG) {
					PHALCON_INIT_NVAR(selected_column);
					PHALCON_CONCAT_SVS(selected_column, "[", model, "].*");
				} else {
					PHALCON_INIT_NVAR(selected_column);
					PHALCON_CONCAT_SVS(selected_column, "[", alias, "].*");
				}
				phalcon_array_append(&selected_columns, selected_column, PH_SEPARATE TSRMLS_CC);
	
				zend_hash_move_forward_ex(ah1, &hp1);
			}
	
			PHALCON_INIT_NVAR(joined_columns);
			phalcon_fast_join_str(joined_columns, SL(", "), selected_columns TSRMLS_CC);
			phalcon_concat_self(&phql, joined_columns TSRMLS_CC);
		} else {
			PHALCON_SCONCAT_SVS(phql, "[", models, "].*");
		}
	}
	
	/** 
	 * Join multiple models or use a single one if it is a string
	 */
	if (Z_TYPE_P(models) == IS_ARRAY) { 
	
		PHALCON_INIT_VAR(selected_models);
		array_init(selected_models);
	
		if (!phalcon_is_iterable(models, &ah2, &hp2, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
	
			PHALCON_GET_FOREACH_KEY(alias, ah2, hp2);
			PHALCON_GET_FOREACH_VALUE(model);
	
			if (Z_TYPE_P(alias) == IS_STRING) {
				PHALCON_INIT_NVAR(selected_model);
				PHALCON_CONCAT_SVSVS(selected_model, "[", model, "] AS [", alias, "]");
			} else {
				PHALCON_INIT_NVAR(selected_model);
				PHALCON_CONCAT_SVS(selected_model, "[", model, "]");
			}
			phalcon_array_append(&selected_models, selected_model, PH_SEPARATE TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah2, &hp2);
		}
	
		PHALCON_INIT_VAR(joined_models);
		phalcon_fast_join_str(joined_models, SL(", "), selected_models TSRMLS_CC);
		PHALCON_SCONCAT_SV(phql, " FROM ", joined_models);
	} else {
		PHALCON_SCONCAT_SVS(phql, " FROM [", models, "]");
	}
	
	/** 
	 * Check if joins were passed to the builders
	 */
	PHALCON_OBS_VAR(joins);
	phalcon_read_property(&joins, this_ptr, SL("_joins"), PH_NOISY_CC);
	if (Z_TYPE_P(joins) == IS_ARRAY) { 
	
		if (!phalcon_is_iterable(joins, &ah3, &hp3, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(join);
	
			/** 
			 * The joined table is in the first place of the array
			 */
			PHALCON_OBS_NVAR(join_model);
			phalcon_array_fetch_long(&join_model, join, 0, PH_NOISY_CC);
	
			/** 
			 * The join conditions are in the second place of the array
			 */
			PHALCON_OBS_NVAR(join_conditions);
			phalcon_array_fetch_long(&join_conditions, join, 1, PH_NOISY_CC);
	
			/** 
			 * The join alias is in the second place of the array
			 */
			PHALCON_OBS_NVAR(join_alias);
			phalcon_array_fetch_long(&join_alias, join, 2, PH_NOISY_CC);
	
			/** 
			 * Join type
			 */
			PHALCON_OBS_NVAR(join_type);
			phalcon_array_fetch_long(&join_type, join, 3, PH_NOISY_CC);
	
			/** 
			 * Create the join according to the type
			 */
			if (zend_is_true(join_type)) {
				PHALCON_SCONCAT_VSVS(phql, join_type, " JOIN [", join_model, "]");
			} else {
				PHALCON_SCONCAT_SVS(phql, " JOIN [", join_model, "]");
			}
	
			/** 
			 * Alias comes first
			 */
			if (zend_is_true(join_alias)) {
				PHALCON_SCONCAT_SVS(phql, " AS [", join_alias, "]");
			}
	
			/** 
			 * Conditions then
			 */
			if (zend_is_true(join_conditions)) {
				PHALCON_SCONCAT_SV(phql, " ON ", join_conditions);
			}
	
			zend_hash_move_forward_ex(ah3, &hp3);
		}
	
	}
	
	if (Z_TYPE_P(conditions) == IS_STRING) {
		PHALCON_SCONCAT_SV(phql, " WHERE ", conditions);
	}
	
	/** 
	 * Process group parameters
	 */
	PHALCON_OBS_VAR(group);
	phalcon_read_property(&group, this_ptr, SL("_group"), PH_NOISY_CC);
	if (Z_TYPE_P(group) != IS_NULL) {
		if (Z_TYPE_P(group) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(group_items);
			array_init(group_items);
	
			if (!phalcon_is_iterable(group, &ah4, &hp4, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) {
	
				PHALCON_GET_FOREACH_VALUE(group_item);
	
				if (phalcon_is_numeric(group_item)) {
					phalcon_array_append(&group_items, group_item, PH_SEPARATE TSRMLS_CC);
				} else {
					if (phalcon_memnstr_str(group_item, SL(".") TSRMLS_CC)) {
						phalcon_array_append(&group_items, group_item, PH_SEPARATE TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(escaped_item);
						PHALCON_CONCAT_SVS(escaped_item, "[", group_item, "]");
						phalcon_array_append(&group_items, escaped_item, PH_SEPARATE TSRMLS_CC);
					}
				}
	
				zend_hash_move_forward_ex(ah4, &hp4);
			}
	
			PHALCON_INIT_VAR(joined_items);
			phalcon_fast_join_str(joined_items, SL(", "), group_items TSRMLS_CC);
			PHALCON_SCONCAT_SV(phql, " GROUP BY ", joined_items);
		} else {
			if (phalcon_is_numeric(group)) {
				PHALCON_SCONCAT_SV(phql, " GROUP BY ", group);
			} else {
				if (phalcon_memnstr_str(group, SL(".") TSRMLS_CC)) {
					PHALCON_SCONCAT_SV(phql, " GROUP BY ", group);
				} else {
					PHALCON_SCONCAT_SVS(phql, " GROUP BY [", group, "]");
				}
			}
		}
	
		PHALCON_OBS_VAR(having);
		phalcon_read_property(&having, this_ptr, SL("_having"), PH_NOISY_CC);
		if (Z_TYPE_P(having) != IS_NULL) {
			PHALCON_SCONCAT_SV(phql, " HAVING ", having);
		}
	}
	
	/** 
	 * Process order clause
	 */
	PHALCON_OBS_VAR(order);
	phalcon_read_property(&order, this_ptr, SL("_order"), PH_NOISY_CC);
	if (Z_TYPE_P(order) != IS_NULL) {
		if (Z_TYPE_P(order) == IS_ARRAY) { 
	
			PHALCON_INIT_VAR(order_items);
			array_init(order_items);
	
			if (!phalcon_is_iterable(order, &ah5, &hp5, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) {
	
				PHALCON_GET_FOREACH_VALUE(order_item);
	
				if (phalcon_is_numeric(order_item)) {
					phalcon_array_append(&order_items, order_item, PH_SEPARATE TSRMLS_CC);
				} else {
					if (phalcon_memnstr_str(order_item, SL(".") TSRMLS_CC)) {
						phalcon_array_append(&order_items, order_item, PH_SEPARATE TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(escaped_item);
						PHALCON_CONCAT_SVS(escaped_item, "[", order_item, "]");
						phalcon_array_append(&order_items, escaped_item, PH_SEPARATE TSRMLS_CC);
					}
				}
	
				zend_hash_move_forward_ex(ah5, &hp5);
			}
	
			PHALCON_INIT_NVAR(joined_items);
			phalcon_fast_join_str(joined_items, SL(", "), order_items TSRMLS_CC);
			PHALCON_SCONCAT_SV(phql, " ORDER BY ", joined_items);
		} else {
			PHALCON_SCONCAT_SV(phql, " ORDER BY ", order);
		}
	}
	
	/** 
	 * Process limit parameters
	 */
	PHALCON_OBS_VAR(limit);
	phalcon_read_property(&limit, this_ptr, SL("_limit"), PH_NOISY_CC);
	if (Z_TYPE_P(limit) != IS_NULL) {
		if (Z_TYPE_P(limit) == IS_ARRAY) { 
	
			PHALCON_OBS_VAR(number);
			phalcon_array_fetch_string(&number, limit, SL("number"), PH_NOISY_CC);
			if (phalcon_array_isset_string(limit, SS("offset"))) {
	
				PHALCON_OBS_VAR(offset);
				phalcon_array_fetch_string(&offset, limit, SL("offset"), PH_NOISY_CC);
				if (phalcon_is_numeric(offset)) {
					PHALCON_SCONCAT_SVSV(phql, " LIMIT ", number, " OFFSET ", offset);
				} else {
					PHALCON_SCONCAT_SVS(phql, " LIMIT ", number, " OFFSET 0");
				}
			} else {
				PHALCON_SCONCAT_SV(phql, " LIMIT ", number);
			}
		} else {
			if (phalcon_is_numeric(limit)) {
				PHALCON_SCONCAT_SV(phql, " LIMIT ", limit);
	
				PHALCON_OBS_NVAR(offset);
				phalcon_read_property(&offset, this_ptr, SL("_offset"), PH_NOISY_CC);
				if (Z_TYPE_P(offset) != IS_NULL) {
					if (phalcon_is_numeric(offset)) {
						PHALCON_SCONCAT_SV(phql, " OFFSET ", offset);
					} else {
						phalcon_concat_self_str(&phql, SL(" OFFSET 0") TSRMLS_CC);
					}
				}
			}
		}
	}
	
	
	RETURN_CTOR(phql);
}
Example #14
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);
}
Example #15
0
/**
 * Returns a MongoDb collection based on the backend parameters
 *
 * @return MongoCollection
 */
PHP_METHOD(Phalcon_Cache_Backend_Mongo, _getCollection){

	zval *mongo_collection, *mongo_database = NULL;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	mongo_collection = phalcon_fetch_nproperty_this(this_ptr, SL("_collection"), PH_NOISY TSRMLS_CC);
	if (Z_TYPE_P(mongo_collection) != IS_OBJECT) {
		zval *options, *mongo;
		zval *server = NULL, *database = NULL, *collection = NULL;

		options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);
	
		/** 
		 * If mongo is defined a valid Mongo object must be passed
		 */
		if (phalcon_array_isset_string_fetch(&mongo, options, SS("mongo"))) {
			if (Z_TYPE_P(mongo) != IS_OBJECT) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The 'mongo' parameter must be a valid Mongo instance");
				return;
			}
		} else {
			/** 
			 * Server must be defined otherwise
			 */
			phalcon_array_isset_string_fetch(&server, options, SS("server"));
			if (!server || Z_TYPE_P(server) != IS_STRING) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB connection string");
				return;
			}
	
			ce0 = zend_fetch_class(SL("MongoClient"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
	
			PHALCON_INIT_VAR(mongo);
			object_init_ex(mongo, ce0);
			assert(phalcon_has_constructor(mongo TSRMLS_CC));
			PHALCON_CALL_METHOD(NULL, mongo, "__construct", server);
		}
	
		/** 
		 * Check if the database name is a string
		 */
		phalcon_array_isset_string_fetch(&database, options, SS("db"));
		if (!database || Z_TYPE_P(database) != IS_STRING) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB db");
			return;
		}
	
		/** 
		 * Retrieve the connection name
		 */
		phalcon_array_isset_string_fetch(&collection, options, SS("collection"));
		if (!collection || Z_TYPE_P(collection) != IS_STRING) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB collection");
			return;
		}
	
		/** 
		 * Make the connection and get the collection
		 */
		PHALCON_CALL_METHOD(&mongo_database, mongo, "selectdb", database);
		PHALCON_RETURN_CALL_METHOD(mongo_database, "selectcollection", collection);
	}
	else {
		RETVAL_ZVAL(mongo_collection, 1, 0);
	}
	
	PHALCON_MM_RESTORE();
}
Example #16
0
/**
 * Detach a listener from the events manager
 *
 * @param object|callable $handler
 */
PHP_METHOD(Phalcon_Events_Manager, detach){

	zval *type, *handler, events = {}, queue = {}, priority_queue = {}, *listener;
	zend_string *str_key;
	ulong idx;

	phalcon_fetch_params(0, 2, 0, &type, &handler);

	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;
	}

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

	if (!phalcon_array_isset_fetch(&queue, &events, type)) {
		RETURN_FALSE;
	}

	if (Z_TYPE(queue) == IS_OBJECT) {
		object_init_ex(&priority_queue, spl_ce_SplPriorityQueue);
		if (phalcon_has_constructor(&priority_queue)) {
			PHALCON_CALL_METHODW(NULL, &priority_queue, "__construct");
		}

		PHALCON_CALL_METHODW(NULL, &queue, "top");

		while (1) {
			zval r0 = {}, listener0 = {}, handler_embeded = {}, priority = {};
			PHALCON_CALL_METHODW(&r0, &queue, "valid");
			if (!zend_is_true(&r0)) {
				break;
			}

			PHALCON_CALL_METHODW(&listener0, &queue, "current");
			PHALCON_CALL_METHODW(&handler_embeded, &listener0, "getlistener");

			if (!phalcon_is_equal(&handler_embeded, handler)) {
				PHALCON_CALL_METHODW(&priority, &listener0, "getpriority");
				PHALCON_CALL_METHODW(NULL, &priority_queue, "insert", &listener0, &priority);
			}
			
			PHALCON_CALL_METHODW(NULL, &queue, "next");
		}
	} else {
		PHALCON_CPY_WRT_CTOR(&priority_queue, &queue);
		ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(queue), idx, str_key, listener) {
			zval key = {}, handler_embeded = {};
			if (str_key) {
				ZVAL_STR(&key, str_key);
			} else {
				ZVAL_LONG(&key, idx);
			}

			PHALCON_CALL_METHODW(&handler_embeded, listener, "getlistener");

			if (phalcon_is_equal_object(&handler_embeded, handler)) {
				phalcon_array_unset(&priority_queue, &key, PH_COPY);
			}

		} ZEND_HASH_FOREACH_END();
	}
Example #17
0
/**
 * Returns a MongoDb collection based on the backend parameters
 *
 * @return MongoCollection
 */
PHP_METHOD(Phalcon_Cache_Backend_Mongo, _getCollection){

	zval *mongo_collection = NULL, *options, *mongo = NULL, *server;
	zval *database, *collection, *mongo_database;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(mongo_collection);
	phalcon_read_property_this(&mongo_collection, this_ptr, SL("_collection"), PH_NOISY_CC);
	if (Z_TYPE_P(mongo_collection) != IS_OBJECT) {
	
		PHALCON_OBS_VAR(options);
		phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY_CC);
	
		/** 
		 * If mongo is defined a valid Mongo object must be passed
		 */
		if (phalcon_array_isset_string(options, SS("mongo"))) {
	
			PHALCON_OBS_VAR(mongo);
			phalcon_array_fetch_string(&mongo, options, SL("mongo"), PH_NOISY);
			if (Z_TYPE_P(mongo) != IS_OBJECT) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The 'mongo' parameter must be a valid Mongo instance");
				return;
			}
		} else {
			/** 
			 * Server must be defined otherwise
			 */
			PHALCON_OBS_VAR(server);
			phalcon_array_fetch_string(&server, options, SL("server"), PH_NOISY);
			if (Z_TYPE_P(server) != IS_STRING) {
				PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB connection string");
				return;
			}
	
			ce0 = zend_fetch_class(SL("Mongo"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
	
			PHALCON_INIT_NVAR(mongo);
			object_init_ex(mongo, ce0);
			if (phalcon_has_constructor(mongo TSRMLS_CC)) {
				phalcon_call_method_p1_noret(mongo, "__construct", server);
			}
		}
	
		/** 
		 * Check if the database name is a string
		 */
		PHALCON_OBS_VAR(database);
		phalcon_array_fetch_string(&database, options, SL("db"), PH_NOISY);
		if (Z_TYPE_P(database) != IS_STRING) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB db");
			return;
		}
	
		/** 
		 * Retrieve the connection name
		 */
		PHALCON_OBS_VAR(collection);
		phalcon_array_fetch_string(&collection, options, SL("collection"), PH_NOISY);
		if (Z_TYPE_P(collection) != IS_STRING) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "The backend requires a valid MongoDB collection");
			return;
		}
	
		/** 
		 * Make the connection and get the collection
		 */
		PHALCON_INIT_VAR(mongo_database);
		phalcon_call_method_p1(mongo_database, mongo, "selectdb", database);
	
		PHALCON_INIT_NVAR(mongo_collection);
		phalcon_call_method_p1(mongo_collection, mongo_database, "selectcollection", collection);
	}
	
	RETURN_CCTOR(mongo_collection);
}
Example #18
0
/**
 * Executes the validation
 *
 * @param string $file
 * @param int $minsize
 * @param int $maxsize
 * @param array $mimes
 * @param int $minwidth
 * @param int $maxwidth
 * @param int $minheight
 * @param int $maxheight
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_File, valid)
{
	zval *value, *minsize = NULL, *maxsize = NULL, *mimes = NULL, *minwidth = NULL, *maxwidth = NULL, *minheight = NULL, *maxheight = NULL;
	zval file = {}, size = {}, *constant, finfo = {}, pathname = {}, mime = {}, image = {}, imageinfo = {}, width = {}, height = {}, valid = {};
	zend_class_entry *imagick_ce;

	phalcon_fetch_params(0, 1, 7, &value, &minsize, &maxsize, &mimes, &minwidth, &maxwidth, &minheight, &maxheight);

	if (Z_TYPE_P(value) == IS_STRING) {
		object_init_ex(&file, spl_ce_SplFileInfo);
		if (phalcon_has_constructor(&file)) {
			PHALCON_CALL_METHODW(NULL, &file, "__construct", value);
		}
	} else if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function_ex(Z_OBJCE_P(value), spl_ce_SplFileInfo, 0)) {
		PHALCON_CPY_WRT(&file, value);
	}

	if (Z_TYPE(file) <= IS_NULL) {
		phalcon_update_property_str(getThis(), SL("_type"), SL("TypeUnknow"));
		RETURN_FALSE;
	}

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

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

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

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

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

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

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

	PHALCON_CALL_METHODW(&valid, &file, "isfile");

	if (!zend_is_true(&valid)) {
		phalcon_update_property_str(getThis(), SL("_type"), SL("FileValid"));
		RETURN_FALSE;
	}

	PHALCON_CALL_METHODW(&size, &file, "getsize");

	if (!PHALCON_IS_EMPTY(minsize)) {
		is_smaller_or_equal_function(&valid, minsize, &size);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooSmall"));
			RETURN_FALSE;
		}
	}

	if (!PHALCON_IS_EMPTY(maxsize)) {
		is_smaller_or_equal_function(&valid, &size, maxsize);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooLarge"));
			RETURN_FALSE;
		}
	}

	PHALCON_CALL_METHODW(&pathname, &file, "getpathname");

	if (Z_TYPE_P(mimes) == IS_ARRAY) {
		if ((constant = zend_get_constant_str(SL("FILEINFO_MIME_TYPE"))) == NULL) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_validation_exception_ce, "Undefined constant `FILEINFO_MIME_TYPE`");
			return;
		}

		PHALCON_CALL_FUNCTIONW(&finfo, "finfo_open", constant);

		if (Z_TYPE(finfo) != IS_RESOURCE) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_validation_exception_ce, "Opening fileinfo database failed");
			return;
		}		

		PHALCON_CALL_FUNCTIONW(&mime, "finfo_file", &finfo, &pathname);
		PHALCON_CALL_FUNCTIONW(NULL, "finfo_close", &finfo);
		
		if (!phalcon_fast_in_array(&mime, mimes)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("MimeValid"));
			RETURN_FALSE;
		}
	}

	if (phalcon_class_str_exists(SL("imagick"), 0) != NULL) {
		imagick_ce = phalcon_fetch_str_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO);

		object_init_ex(&image, imagick_ce);
		PHALCON_CALL_METHODW(NULL, &image, "__construct", &pathname);

		PHALCON_CALL_METHODW(&width, &image, "getImageWidth");
		PHALCON_CALL_METHODW(&height, &image, "getImageHeight");
	} else if (phalcon_function_exists_ex(SL("getimagesize")) != FAILURE) {
		PHALCON_CALL_FUNCTIONW(&imageinfo, "getimagesize", &pathname);
		if (!phalcon_array_isset_fetch_long(&width, &imageinfo, 0)) {
			ZVAL_LONG(&width, -1);
		}

		if (!phalcon_array_isset_fetch_long(&height, &imageinfo, 1)) {
			ZVAL_LONG(&height, -1);
		}
	} else {
		ZVAL_LONG(&width, -1);
		ZVAL_LONG(&height, -1);
	}

	if (!PHALCON_IS_EMPTY(minwidth)) {
		is_smaller_or_equal_function(&valid, minwidth, &width);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooNarrow"));
			RETURN_FALSE;
		}
	}

	if (!PHALCON_IS_EMPTY(maxwidth)) {
		is_smaller_or_equal_function(&valid, &width, maxwidth);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooWide"));
			RETURN_FALSE;
		}
	}

	if (!PHALCON_IS_EMPTY(minheight)) {
		is_smaller_or_equal_function(&valid, minheight, &height);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooShort"));
			RETURN_FALSE;
		}
	}

	if (!PHALCON_IS_EMPTY(maxheight)) {
		is_smaller_or_equal_function(&valid, &height, maxheight);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooLong"));
			RETURN_FALSE;
		}
	}

	RETURN_TRUE;
}
Example #19
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();
}