示例#1
0
/**
 * Get last row in the resultset
 *
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, getLast){

	zval *one, *count, *pre_count, *valid, *current;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(one);
	ZVAL_LONG(one, 1);
	
	PHALCON_INIT_VAR(count);
	phalcon_call_method(count, this_ptr, "count");
	
	PHALCON_INIT_VAR(pre_count);
	sub_function(pre_count, count, one TSRMLS_CC);
	phalcon_call_method_p1_noret(this_ptr, "seek", pre_count);
	
	PHALCON_INIT_VAR(valid);
	phalcon_call_method(valid, this_ptr, "valid");
	if (PHALCON_IS_NOT_FALSE(valid)) {
		PHALCON_INIT_VAR(current);
		phalcon_call_method(current, this_ptr, "current");
		RETURN_CCTOR(current);
	}
	
	RETURN_MM_FALSE;
}
示例#2
0
/**
 * Returns a complete resultset as an array, if the resultset has a big number of rows
 * it could consume more memory than currently it does.
 *
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, toArray){

	zval *records, *valid = NULL, *current = NULL;
	zval *r0 = NULL;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(records);
	array_init(records);
	phalcon_call_method_noret(this_ptr, "rewind");
	
	while (1) {
	
		PHALCON_INIT_NVAR(r0);
		phalcon_call_method(r0, this_ptr, "valid");
		PHALCON_CPY_WRT(valid, r0);
		if (PHALCON_IS_NOT_FALSE(valid)) {
		} else {
			break;
		}
	
		PHALCON_INIT_NVAR(current);
		phalcon_call_method(current, this_ptr, "current");
		phalcon_array_append(&records, current, PH_SEPARATE);
		phalcon_call_method_noret(this_ptr, "next");
	}
	
	RETURN_CTOR(records);
}
示例#3
0
/**
 * Get first row in the resultset
 *
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, getFirst){

	zval *pointer, *current = NULL, *valid;

	PHALCON_MM_GROW();

	/** 
	 * Check if the last record returned is the current requested
	 */
	PHALCON_OBS_VAR(pointer);
	phalcon_read_property_this(&pointer, this_ptr, SL("_pointer"), PH_NOISY_CC);
	if (PHALCON_IS_LONG(pointer, 0)) {
		PHALCON_INIT_VAR(current);
		phalcon_call_method(current, this_ptr, "current");
		RETURN_CCTOR(current);
	}
	
	/** 
	 * Otherwise re-execute the statement
	 */
	phalcon_call_method_noret(this_ptr, "rewind");
	
	PHALCON_INIT_VAR(valid);
	phalcon_call_method(valid, this_ptr, "valid");
	if (PHALCON_IS_NOT_FALSE(valid)) {
		PHALCON_INIT_NVAR(current);
		phalcon_call_method(current, this_ptr, "current");
		RETURN_CCTOR(current);
	}
	
	RETURN_MM_FALSE;
}
示例#4
0
文件: micro.c 项目: RSivakov/cphalcon
/**
 * Maps a route to a handler that only matches if the HTTP method is HEAD
 *
 * @param string $routePattern
 * @param callable $handler
 * @return Phalcon\Mvc\Router\RouteInterface
 */
PHP_METHOD(Phalcon_Mvc_Micro, head){

	zval *route_pattern, *handler, *router, *route;
	zval *route_id;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &route_pattern, &handler);
	
	/** 
	 * We create a router even if there is no one in the DI
	 */
	PHALCON_INIT_VAR(router);
	phalcon_call_method(router, this_ptr, "getrouter");
	
	/** 
	 * Routes are added to the router restricting to HEAD
	 */
	PHALCON_INIT_VAR(route);
	phalcon_call_method_p1(route, router, "addhead", route_pattern);
	
	/** 
	 * Using the id produced by the router we store the handler
	 */
	PHALCON_INIT_VAR(route_id);
	phalcon_call_method(route_id, route, "getrouteid");
	phalcon_update_property_array(this_ptr, SL("_handlers"), route_id, handler TSRMLS_CC);
	
	/** 
	 * The route is returned, the developer can add more things on it
	 */
	RETURN_CCTOR(route);
}
示例#5
0
/**
 * Counts how many rows are in the resultset
 *
 * @return int
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, count){

	zval *count = NULL, *type, *result = NULL, *number_rows, *rows = NULL;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(count);
	phalcon_read_property_this(&count, this_ptr, SL("_count"), PH_NOISY_CC);
	
	/** 
	 * We only calculate the row number is it wasn't calculated before
	 */
	if (Z_TYPE_P(count) == IS_NULL) {
	
		PHALCON_INIT_NVAR(count);
		ZVAL_LONG(count, 0);
	
		PHALCON_OBS_VAR(type);
		phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
		if (zend_is_true(type)) {
	
			/** 
			 * Here, the resultset act as a result that is fetched 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(number_rows);
				phalcon_call_method(number_rows, result, "numrows");
	
				PHALCON_INIT_NVAR(count);
				ZVAL_LONG(count, phalcon_get_intval(number_rows));
			}
		} else {
			/** 
			 * Here, the resultset act as an array
			 */
			PHALCON_OBS_VAR(rows);
			phalcon_read_property_this(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
			if (Z_TYPE_P(rows) == IS_NULL) {
	
				PHALCON_OBS_NVAR(result);
				phalcon_read_property_this(&result, this_ptr, SL("_result"), PH_NOISY_CC);
				if (Z_TYPE_P(result) == IS_OBJECT) {
					PHALCON_INIT_NVAR(rows);
					phalcon_call_method(rows, result, "fetchall");
					phalcon_update_property_this(this_ptr, SL("_rows"), rows TSRMLS_CC);
				}
			}
	
			PHALCON_INIT_NVAR(count);
			phalcon_fast_count(count, rows TSRMLS_CC);
		}
	
		phalcon_update_property_this(this_ptr, SL("_count"), count TSRMLS_CC);
	}
	
	RETURN_CCTOR(count);
}
示例#6
0
/**
 * Reads 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
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_Model_MetaData, readMetaDataIndex) {

    zval *model, *index, *table, *schema, *class_name;
    zval *key, *meta_data = NULL, *meta_data_index, *attributes;

    PHALCON_MM_GROW();

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

    if (Z_TYPE_P(model) != IS_OBJECT) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A model instance is required to retrieve the meta-data");
        return;
    }
    if (Z_TYPE_P(index) != IS_LONG) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Index must be a valid integer constant");
        return;
    }

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

    PHALCON_INIT_VAR(schema);
    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_CC);
    if (!phalcon_array_isset(meta_data, key)) {
        phalcon_call_method_p4_noret(this_ptr, "_initialize", model, key, table, schema);

        PHALCON_OBS_NVAR(meta_data);
        phalcon_read_property_this(&meta_data, this_ptr, SL("_metaData"), PH_NOISY_CC);
    }

    PHALCON_OBS_VAR(meta_data_index);
    phalcon_array_fetch(&meta_data_index, meta_data, key, PH_NOISY);

    PHALCON_OBS_VAR(attributes);
    phalcon_array_fetch(&attributes, meta_data_index, index, PH_NOISY);

    RETURN_CCTOR(attributes);
}
示例#7
0
/**
 * Gets row in a specific position of the resultset
 *
 * @param int $index
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, offsetGet){

	zval *index, *count, *exists, *pointer, *current = NULL, *valid;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &index);
	
	PHALCON_INIT_VAR(count);
	phalcon_call_method(count, this_ptr, "count");
	
	PHALCON_INIT_VAR(exists);
	is_smaller_function(exists, index, count TSRMLS_CC);
	if (PHALCON_IS_TRUE(exists)) {
	
		/** 
		 * Check if the last record returned is the current requested
		 */
		PHALCON_OBS_VAR(pointer);
		phalcon_read_property_this(&pointer, this_ptr, SL("_pointer"), PH_NOISY_CC);
		if (PHALCON_IS_EQUAL(pointer, index)) {
			PHALCON_INIT_VAR(current);
			phalcon_call_method(current, this_ptr, "current");
			RETURN_CCTOR(current);
		}
	
		/** 
		 * Move the cursor to the specific position
		 */
		phalcon_call_method_p1_noret(this_ptr, "seek", index);
	
		/** 
		 * Check if the last record returned is the requested
		 */
		PHALCON_INIT_VAR(valid);
		phalcon_call_method(valid, this_ptr, "valid");
		if (PHALCON_IS_NOT_FALSE(valid)) {
			PHALCON_INIT_NVAR(current);
			phalcon_call_method(current, this_ptr, "current");
			RETURN_CCTOR(current);
		}
	
		RETURN_MM_FALSE;
	}
	
	PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The index does not exist in the cursor");
	return;
}
示例#8
0
/**
 * Rollbacks active transactions within the manager
 * Collect will remove transaction from the manager
 *
 * @param boolean $collect
 */
PHP_METHOD(Phalcon_Mvc_Model_Transaction_Manager, rollback){

	zval *collect = NULL, *transactions, *transaction = NULL, *connection = NULL;
	zval *is_under_transaction = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &collect);
	
	if (!collect) {
		PHALCON_INIT_VAR(collect);
		ZVAL_BOOL(collect, 1);
	}
	
	PHALCON_OBS_VAR(transactions);
	phalcon_read_property_this(&transactions, this_ptr, SL("_transactions"), PH_NOISY_CC);
	if (Z_TYPE_P(transactions) == IS_ARRAY) { 
	
		phalcon_is_iterable(transactions, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HVALUE(transaction);
	
			PHALCON_INIT_NVAR(connection);
			phalcon_call_method(connection, transaction, "getconnection");
	
			PHALCON_INIT_NVAR(is_under_transaction);
			phalcon_call_method(is_under_transaction, connection, "isundertransaction");
			if (zend_is_true(is_under_transaction)) {
				phalcon_call_method_noret(connection, "rollback");
				phalcon_call_method_noret(connection, "close");
			}
	
			if (zend_is_true(collect)) {
				phalcon_call_method_p1_noret(this_ptr, "_collecttransaction", transaction);
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
	}
	
	PHALCON_MM_RESTORE();
}
示例#9
0
/**
 * Writes the log to the stream itself
 *
 * @param string $message
 * @param int $type
 * @param int $time
 */
PHP_METHOD(Phalcon_Logger_Adapter_Syslog, logInternal){

	zval *message, *type, *time, *formatter, *applied_format;
	zval *syslog_type, *syslog_message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &message, &type, &time);
	
	PHALCON_INIT_VAR(formatter);
	phalcon_call_method(formatter, this_ptr, "getformatter");
	
	PHALCON_INIT_VAR(applied_format);
	phalcon_call_method_p3(applied_format, formatter, "format", message, type, time);
	if (Z_TYPE_P(applied_format) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "The formatted message is not valid");
		return;
	}
	
	PHALCON_OBS_VAR(syslog_type);
	phalcon_array_fetch_long(&syslog_type, applied_format, 0, PH_NOISY);
	
	PHALCON_OBS_VAR(syslog_message);
	phalcon_array_fetch_long(&syslog_message, applied_format, 1, PH_NOISY);
	phalcon_call_func_p2_noret("syslog", syslog_type, syslog_message);
	
	PHALCON_MM_RESTORE();
}
示例#10
0
/**
 * Returns a new Phalcon\Mvc\Model\Transaction or an already created once
 * This method registers a shutdown function to rollback active connections
 *
 * @param boolean $autoBegin
 * @return Phalcon\Mvc\Model\TransactionInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Transaction_Manager, get){

	zval *auto_begin = NULL, *initialized, *rollback_pendent = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 1, &auto_begin);
	
	if (!auto_begin) {
		PHALCON_INIT_VAR(auto_begin);
		ZVAL_BOOL(auto_begin, 1);
	}
	
	PHALCON_OBS_VAR(initialized);
	phalcon_read_property_this(&initialized, this_ptr, SL("_initialized"), PH_NOISY_CC);
	if (zend_is_true(initialized)) {
	
		PHALCON_OBS_VAR(rollback_pendent);
		phalcon_read_property_this(&rollback_pendent, this_ptr, SL("_rollbackPendent"), PH_NOISY_CC);
		if (zend_is_true(rollback_pendent)) {
			PHALCON_INIT_NVAR(rollback_pendent);
			array_init_size(rollback_pendent, 2);
			phalcon_array_append(&rollback_pendent, this_ptr, PH_SEPARATE);
			add_next_index_stringl(rollback_pendent, SL("rollbackPendent"), 1);
			phalcon_call_func_p1_noret("register_shutdown_function", rollback_pendent);
		}
	
		phalcon_update_property_bool(this_ptr, SL("_initialized"), 1 TSRMLS_CC);
	}
	
	phalcon_call_method(return_value, this_ptr, "getorcreatetransaction");
	RETURN_MM();
}
示例#11
0
/**
 * Creates a password hash using bcrypt with a pseudo random salt
 *
 * @param string $password
 * @param int $workFactor
 * @return string
 */
PHP_METHOD(Phalcon_Security, hash){

	zval *password, *work_factor = NULL, *format, *factor;
	zval *salt_bytes, *salt;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &password, &work_factor);
	
	if (!work_factor) {
		PHALCON_INIT_VAR(work_factor);
	} else {
		PHALCON_SEPARATE_PARAM(work_factor);
	}
	
	if (Z_TYPE_P(work_factor) == IS_NULL) {
		PHALCON_OBS_NVAR(work_factor);
		phalcon_read_property_this(&work_factor, this_ptr, SL("_workFactor"), PH_NOISY_CC);
	}
	
	PHALCON_INIT_VAR(format);
	ZVAL_STRING(format, "%02s", 1);
	
	PHALCON_INIT_VAR(factor);
	phalcon_call_func_p2(factor, "sprintf", format, work_factor);
	
	PHALCON_INIT_VAR(salt_bytes);
	phalcon_call_method(salt_bytes, this_ptr, "getsaltbytes");
	
	PHALCON_INIT_VAR(salt);
	PHALCON_CONCAT_SVSV(salt, "$2a$", factor, "$", salt_bytes);
	phalcon_call_func_p2(return_value, "crypt", password, salt);
	RETURN_MM();
}
/**
 * Phalcon\Mvc\Model\ValidationFailed constructor
 *
 * @param Phalcon\Mvc\Model $model
 * @param Phalcon\Mvc\Model\Message[] $validationMessages
 */
PHP_METHOD(Phalcon_Mvc_Model_ValidationFailed, __construct){

	zval *model, *validation_messages, *message;
	zval *message_str = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &model, &validation_messages);
	
	if (phalcon_fast_count_ev(validation_messages TSRMLS_CC)) {
		/** 
		 * Get the first message in the array
		 */
		PHALCON_OBS_VAR(message);
		phalcon_array_fetch_long(&message, validation_messages, 0, PH_NOISY);
	
		/** 
		 * Get the message to use it in the exception
		 */
		PHALCON_INIT_VAR(message_str);
		phalcon_call_method(message_str, message, "getmessage");
	} else {
		PHALCON_INIT_NVAR(message_str);
		ZVAL_STRING(message_str, "Validation failed", 1);
	}
	phalcon_update_property_this(this_ptr, SL("_model"), model TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_messages"), validation_messages TSRMLS_CC);
	PHALCON_CALL_PARENT_PARAMS_1_NORETURN(this_ptr, "Phalcon\\Mvc\\Model\\ValidationFailed", "__construct", message_str);
	
	PHALCON_MM_RESTORE();
}
示例#13
0
/**
 * Sets the response content-type mime, optionally the charset
 *
 *<code>
 *	$response->setContentType('application/pdf');
 *	$response->setContentType('text/plain', 'UTF-8');
 *</code>
 *
 * @param string $contentType
 * @param string $charset
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Http_Response, setContentType){

	zval *content_type, *charset = NULL, *headers, *name, *header_value;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &content_type, &charset);
	
	if (!charset) {
		PHALCON_INIT_VAR(charset);
	}
	
	PHALCON_INIT_VAR(headers);
	phalcon_call_method(headers, this_ptr, "getheaders");
	
	PHALCON_INIT_VAR(name);
	ZVAL_STRING(name, "Content-Type", 1);
	if (Z_TYPE_P(charset) == IS_NULL) {
		phalcon_call_method_p2_noret(headers, "set", name, content_type);
	} else {
		PHALCON_INIT_VAR(header_value);
		PHALCON_CONCAT_VSV(header_value, content_type, "; charset=", charset);
		phalcon_call_method_p2_noret(headers, "set", name, header_value);
	}
	
	RETURN_THIS();
}
示例#14
0
/**
 * Sets the HTTP response code
 *
 *<code>
 *	$response->setStatusCode(404, "Not Found");
 *</code>
 *
 * @param int $code
 * @param string $message
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Http_Response, setStatusCode){

	zval *code, *message, *headers, *header_value, *status_value;
	zval *status_header;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &code, &message);
	
	PHALCON_INIT_VAR(headers);
	phalcon_call_method(headers, this_ptr, "getheaders");
	
	/** 
	 * We use HTTP/1.1 instead of HTTP/1.0
	 */
	PHALCON_INIT_VAR(header_value);
	PHALCON_CONCAT_SVSV(header_value, "HTTP/1.1 ", code, " ", message);
	phalcon_call_method_p1_noret(headers, "setraw", header_value);
	
	/** 
	 * We also define a 'Status' header with the HTTP status
	 */
	PHALCON_INIT_VAR(status_value);
	PHALCON_CONCAT_VSV(status_value, code, " ", message);
	
	PHALCON_INIT_VAR(status_header);
	ZVAL_STRING(status_header, "Status", 1);
	phalcon_call_method_p2_noret(headers, "set", status_header, status_value);
	phalcon_update_property_this(this_ptr, SL("_headers"), headers TSRMLS_CC);
	RETURN_THIS();
}
示例#15
0
文件: di.c 项目: 11mariom/cphalcon
/**
 * Returns a service definition without resolving
 *
 * @param string $name
 * @return mixed
 */
PHP_METHOD(Phalcon_DI, getRaw){

	zval *name, *services, *service, *exception_message;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &name);
	
	if (Z_TYPE_P(name) != IS_STRING) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_di_exception_ce, "The service name must be a string");
		return;
	}
	
	PHALCON_OBS_VAR(services);
	phalcon_read_property_this(&services, this_ptr, SL("_services"), PH_NOISY_CC);
	if (phalcon_array_isset(services, name)) {
		PHALCON_OBS_VAR(service);
		phalcon_array_fetch(&service, services, name, PH_NOISY);
		phalcon_call_method(return_value, service, "getdefinition");
		RETURN_MM();
	}
	
	PHALCON_INIT_VAR(exception_message);
	PHALCON_CONCAT_SVS(exception_message, "Service '", name, "' wasn't found in the dependency injection container");
	PHALCON_THROW_EXCEPTION_ZVAL(phalcon_di_exception_ce, exception_message);
	return;
}
示例#16
0
文件: file.c 项目: DonOzone/cphalcon
/**
 * Writes the log to the file itself
 *
 * @param string $message
 * @param int $type
 * @param int $time
 */
PHP_METHOD(Phalcon_Logger_Adapter_File, logInternal){

	zval *message, *type, *time, *file_handler, *formatter;
	zval *applied_format;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &message, &type, &time);
	
	PHALCON_OBS_VAR(file_handler);
	phalcon_read_property_this(&file_handler, this_ptr, SL("_fileHandler"), PH_NOISY_CC);
	if (!zend_is_true(file_handler)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_logger_exception_ce, "Cannot send message to the log because it is invalid");
		return;
	}
	
	PHALCON_INIT_VAR(formatter);
	phalcon_call_method(formatter, this_ptr, "getformatter");
	
	PHALCON_INIT_VAR(applied_format);
	phalcon_call_method_p3(applied_format, formatter, "format", message, type, time);
	phalcon_call_func_p2_noret("fwrite", file_handler, applied_format);
	
	PHALCON_MM_RESTORE();
}
示例#17
0
/**
 * Checks for annotations in the controller docblock
 *
 * @param string $handler
 * @param Phalcon\Annotations\AdapterInterface
 */
PHP_METHOD(Phalcon_Mvc_Router_Annotations, processControllerAnnotation){

	zval *handler, *annotation, *name, *position, *value;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &handler, &annotation);
	
	PHALCON_INIT_VAR(name);
	phalcon_call_method(name, annotation, "getname");
	
	/** 
	 * @RoutePrefix add a prefix for all the routes defined in the model
	 */
	if (PHALCON_IS_STRING(name, "RoutePrefix")) {
		PHALCON_INIT_VAR(position);
		ZVAL_LONG(position, 0);
	
		PHALCON_INIT_VAR(value);
		phalcon_call_method_p1(value, annotation, "getargument", position);
		phalcon_update_property_this(this_ptr, SL("_routePrefix"), value TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
示例#18
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();
}
示例#19
0
/**
 * Possible controller class name that will be located to dispatch the request
 *
 * @return string
 */
PHP_METHOD(Phalcon_Mvc_Dispatcher, getControllerClass){


	PHALCON_MM_GROW();

	phalcon_call_method(return_value, this_ptr, "gethandlername");
	RETURN_MM();
}
示例#20
0
/**
 * Checks whether the platform supports releasing savepoints.
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Db_Dialect, supportsReleaseSavepoints){


	PHALCON_MM_GROW();

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

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

    PHALCON_MM_GROW();

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

    PHALCON_INIT_VAR(ordered_column_map);

    PHALCON_INIT_VAR(reversed_column_map);

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

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

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

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

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

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

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

            zend_hash_move_forward_ex(ah0, &hp0);
        }

    }

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

    RETURN_CTOR(model_column_map);
}
示例#22
0
/**
 * Redirect by HTTP to another action or URL
 *
 *<code>
 *  //Using a string redirect (internal/external)
 *	$response->redirect("posts/index");
 *	$response->redirect("http://en.wikipedia.org", true);
 *	$response->redirect("http://www.example.com/new-location", true, 301);
 *
 *	//Making a redirection based on a named route
 *	$response->redirect(array(
 *		"for" => "index-lang",
 *		"lang" => "jp",
 *		"controller" => "index"
 *	));
 *</code>
 *
 * @param string $location
 * @param boolean $externalRedirect
 * @param int $statusCode
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Http_Response, redirect){

	zval *location = NULL, *external_redirect = NULL, *status_code = NULL;
	zval *header = NULL, *dependency_injector, *service;
	zval *url, *status_text, *header_name;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 3, &location, &external_redirect, &status_code);
	
	if (!location) {
		PHALCON_INIT_VAR(location);
	}
	
	if (!external_redirect) {
		PHALCON_INIT_VAR(external_redirect);
		ZVAL_BOOL(external_redirect, 0);
	}
	
	if (!status_code) {
		PHALCON_INIT_VAR(status_code);
		ZVAL_LONG(status_code, 302);
	}
	
	if (zend_is_true(external_redirect)) {
		PHALCON_CPY_WRT(header, location);
	} else {
		PHALCON_INIT_VAR(dependency_injector);
		phalcon_call_method(dependency_injector, this_ptr, "getdi");
	
		PHALCON_INIT_VAR(service);
		ZVAL_STRING(service, "url", 1);
	
		PHALCON_INIT_VAR(url);
		phalcon_call_method_p1(url, dependency_injector, "getshared", service);
	
		PHALCON_INIT_NVAR(header);
		phalcon_call_method_p1(header, url, "get", location);
	}
	
	/** 
	 * The HTTP status is 302 by default, a temporary redirection
	 */
	PHALCON_INIT_VAR(status_text);
	ZVAL_STRING(status_text, "Redirect", 1);
	phalcon_call_method_p2_noret(this_ptr, "setstatuscode", status_code, status_text);
	
	/** 
	 * Change the current location using 'Location'
	 */
	PHALCON_INIT_VAR(header_name);
	ZVAL_STRING(header_name, "Location", 1);
	phalcon_call_method_p2_noret(this_ptr, "setheader", header_name, header);
	
	RETURN_THIS();
}
示例#23
0
/**
 * Magic method __toString renders the widget without atttributes
 *
 * @return string
 */
PHP_METHOD(Phalcon_Forms_Element, __toString){

	zval *content;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(content);
	phalcon_call_method(content, this_ptr, "render");
	RETURN_CCTOR(content);
}
示例#24
0
/**
 * Do a role inherit from another existing role
 *
 * @param string $roleName
 * @param string $roleToInherit
 */
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addInherit){

	zval *role_name, *role_to_inherit, *roles_names;
	zval *exception_message = NULL, *role_inherit_name = NULL;
	zval *roles_inherits, *empty_arr, *_roleInherits;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &role_name, &role_to_inherit);
	
	PHALCON_OBS_VAR(roles_names);
	phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
	if (!phalcon_array_isset(roles_names, role_name)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Role '", role_name, "' does not exist in the role list");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
		return;
	}
	
	if (Z_TYPE_P(role_to_inherit) == IS_OBJECT) {
		PHALCON_INIT_VAR(role_inherit_name);
		phalcon_call_method(role_inherit_name, role_to_inherit, "getname");
	} else {
		PHALCON_CPY_WRT(role_inherit_name, role_to_inherit);
	}
	
	/** 
	 * Check if the role to inherit is valid
	 */
	if (!phalcon_array_isset(roles_names, role_inherit_name)) {
		PHALCON_INIT_NVAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Role '", role_inherit_name, "' (to inherit) does not exist in the role list");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
		return;
	}
	
	if (PHALCON_IS_EQUAL(role_inherit_name, role_name)) {
		RETURN_MM_FALSE;
	}
	
	PHALCON_OBS_VAR(roles_inherits);
	phalcon_read_property_this(&roles_inherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
	if (!phalcon_array_isset(roles_inherits, role_name)) {
		PHALCON_INIT_VAR(empty_arr);
		array_init(empty_arr);
		phalcon_update_property_array(this_ptr, SL("_roleInherits"), role_name, empty_arr TSRMLS_CC);
	}
	
	PHALCON_OBS_VAR(_roleInherits);
	phalcon_read_property_this(&_roleInherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
	phalcon_array_update_append_multi_2(&_roleInherits, role_name, role_inherit_name, 0);
	phalcon_update_property_this(this_ptr, SL("_roleInherits"), _roleInherits TSRMLS_CC);
	RETURN_MM_TRUE;
}
示例#25
0
/**
 * Resets all the stablished headers
 *
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Http_Response, resetHeaders){

	zval *headers;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(headers);
	phalcon_call_method(headers, this_ptr, "getheaders");
	phalcon_call_method_noret(headers, "reset");
	RETURN_THIS();
}
示例#26
0
/**
 * Generates a link to the current version documentation
 *
 * @return string
 */
PHP_METHOD(Phalcon_Debug, getVersion){

	zval *version;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(version);
	phalcon_call_method(version, this_ptr, "getmajorversion");
	PHALCON_CONCAT_SVSVS(return_value, "<div class=\"version\">Phalcon Framework <a target=\"_new\" href=\"http://docs.phalconphp.com/en/", version, "/\">", version, "</a></div>");
	RETURN_MM();
}
示例#27
0
/**
 * Returns cached ouput on another view stage
 *
 * @return array
 */
PHP_METHOD(Phalcon_Mvc_View_Engine, getContent){

	zval *view;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(view);
	phalcon_read_property_this(&view, this_ptr, SL("_view"), PH_NOISY_CC);
	phalcon_call_method(return_value, view, "getcontent");
	RETURN_MM();
}
示例#28
0
/**
 * Rewinds resultset to its beginning
 *
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, rewind){

	zval *type, *result = NULL, *active_row, *zero, *rows = NULL;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(type);
	phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC);
	if (zend_is_true(type)) {
	
		/** 
		 * Here, the resultset act as a result that is fetched 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_OBS_VAR(active_row);
			phalcon_read_property_this(&active_row, this_ptr, SL("_activeRow"), PH_NOISY_CC);
			if (Z_TYPE_P(active_row) != IS_NULL) {
				PHALCON_INIT_VAR(zero);
				ZVAL_LONG(zero, 0);
				phalcon_call_method_p1_noret(result, "dataseek", zero);
			}
		}
	} else {
		/** 
		 * Here, the resultset act as an array
		 */
		PHALCON_OBS_VAR(rows);
		phalcon_read_property_this(&rows, this_ptr, SL("_rows"), PH_NOISY_CC);
		if (Z_TYPE_P(rows) == IS_NULL) {
	
			PHALCON_OBS_NVAR(result);
			phalcon_read_property_this(&result, this_ptr, SL("_result"), PH_NOISY_CC);
			if (Z_TYPE_P(result) == IS_OBJECT) {
				PHALCON_INIT_NVAR(rows);
				phalcon_call_method(rows, result, "fetchall");
				phalcon_update_property_this(this_ptr, SL("_rows"), rows TSRMLS_CC);
			}
		}
	
		if (Z_TYPE_P(rows) == IS_ARRAY) { 
			Z_SET_ISREF_P(rows);
			phalcon_call_func_p1_noret("reset", rows);
			Z_UNSET_ISREF_P(rows);
		}
	}
	
	phalcon_update_property_long(this_ptr, SL("_pointer"), 0 TSRMLS_CC);
	
	PHALCON_MM_RESTORE();
}
示例#29
0
/**
 * Adds a role to the ACL list. Second parameter allows inheriting access data from other existing role
 *
 * Example:
 * <code>
 * 	$acl->addRole(new Phalcon\Acl\Role('administrator'), 'consultant');
 * 	$acl->addRole('administrator', 'consultant');
 * </code>
 *
 * @param  Phalcon\Acl\RoleInterface $role
 * @param  array|string $accessInherits
 * @return boolean
 */
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addRole){

	zval *role, *access_inherits = NULL, *role_name = NULL, *object = NULL;
	zval *roles_names, *exists, *default_access;
	zval *key, *success;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &role, &access_inherits);
	
	if (!access_inherits) {
		PHALCON_INIT_VAR(access_inherits);
	}
	
	if (Z_TYPE_P(role) == IS_OBJECT) {
		PHALCON_INIT_VAR(role_name);
		phalcon_call_method(role_name, role, "getname");
		PHALCON_CPY_WRT(object, role);
	} else {
		PHALCON_CPY_WRT(role_name, role);
	
		PHALCON_INIT_NVAR(object);
		object_init_ex(object, phalcon_acl_role_ce);
		phalcon_call_method_p1_noret(object, "__construct", role);
	
	}
	
	PHALCON_OBS_VAR(roles_names);
	phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
	if (phalcon_array_isset(roles_names, role_name)) {
		RETURN_MM_FALSE;
	}
	
	PHALCON_INIT_VAR(exists);
	ZVAL_BOOL(exists, 1);
	phalcon_update_property_array_append(this_ptr, SL("_roles"), object TSRMLS_CC);
	phalcon_update_property_array(this_ptr, SL("_rolesNames"), role_name, exists TSRMLS_CC);
	
	PHALCON_OBS_VAR(default_access);
	phalcon_read_property_this(&default_access, this_ptr, SL("_defaultAccess"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(key);
	PHALCON_CONCAT_VS(key, role_name, "!*!*");
	phalcon_update_property_array(this_ptr, SL("_access"), key, default_access TSRMLS_CC);
	if (Z_TYPE_P(access_inherits) != IS_NULL) {
		PHALCON_INIT_VAR(success);
		phalcon_call_method_p2(success, this_ptr, "addinherit", role_name, access_inherits);
		RETURN_CCTOR(success);
	}
	
	RETURN_MM_TRUE;
}
示例#30
0
/**
 * Send a raw header to the response
 *
 *<code>
 *	$response->setRawHeader("HTTP/1.1 404 Not Found");
 *</code>
 *
 * @param string $header
 * @return Phalcon\Http\ResponseInterface
 */
PHP_METHOD(Phalcon_Http_Response, setRawHeader){

	zval *header, *headers;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &header);
	
	PHALCON_INIT_VAR(headers);
	phalcon_call_method(headers, this_ptr, "getheaders");
	phalcon_call_method_p1_noret(headers, "setraw", header);
	RETURN_THIS();
}