Exemple #1
0
/**
 * Listen for uncaught exceptions
 *
 * @return Phalcon\Debug
 */
PHP_METHOD(Phalcon_Debug, listenExceptions){

	zval *handler;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(handler);
	array_init_size(handler, 2);
	phalcon_array_append(&handler, this_ptr, PH_COPY);
	phalcon_array_append_string(&handler, SL("onUncaughtException"), PH_COPY);
	PHALCON_CALL_FUNCTION(NULL, "set_exception_handler", handler);
	RETURN_THIS();
}
Exemple #2
0
int phalcon_proc_parse_cmdline(phalcon_process *proc, unsigned int pid) {
	char path[256];
	int fd;
	char buf[1024];
	unsigned int buflen, i, argc;
	char *c;

    sprintf(path, "/proc/%i/cmdline", pid);
    fd = open(path, O_RDONLY);
	buflen = read(fd, buf, sizeof(buf));

	if(buflen == 0) {
			return 0;
	}
	proc->has_commandline = 1;

	/* cmdline is not null terminated if there are no command line arguments. */
	if (buf[buflen-1] != '\0') {
		buf[buflen++] = '\0';
	}

	/* figure out argc */
	argc=0;
	for (i=0; i < buflen; i++) {
		if (buf[i] == '\0'){
			argc++;
		}
		c++;
	}

	/* One for each pointer plus null */
	array_init_size(&(proc->args), argc);

	/* And the char arrays */
	c = buf;
	for (i=0; i < argc; i++) {
		phalcon_array_append_string(&(proc->args), c, strlen(c)+1, 0);
		while (*c != '\0'){
				c++;
		}
		c++;
	}
    close(fd);
	return 0;
}
Exemple #3
0
/**
 * Handles routing information received from command-line arguments
 *
 * @param array $arguments
 */
PHP_METHOD(Phalcon_CLI_Router, handle){

	zval *arguments = NULL, longopts = {}, options = {}, module_name = {}, namespace_name = {}, task_name = {}, action_name = {};

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

	if (!arguments || Z_TYPE_P(arguments) != IS_ARRAY) {
		if (unlikely(!strcmp(sapi_module.name, "cli"))) {
			array_init(&longopts);
			phalcon_array_append_string(&longopts, SL("module::"), 0);
			phalcon_array_append_string(&longopts, SL("namespace::"), 0);
			phalcon_array_append_string(&longopts, SL("task::"), 0);
			phalcon_array_append_string(&longopts, SL("action::"), 0);
			phalcon_array_append_string(&longopts, SL("params::"), 0);
			PHALCON_CALL_FUNCTIONW(&options, "getopt", &PHALCON_GLOBAL(z_null), &longopts);
		} else {
			array_init(&options);
		}
	} else {
		PHALCON_CPY_WRT_CTOR(&options, arguments);
	}

	/**
	 * Check for a module
	 */
	if (phalcon_array_isset_fetch_str(&module_name, &options, SL("module"))) {
		phalcon_array_unset_str(arguments, SL("module"), PH_COPY);
	} else {
		ZVAL_NULL(&module_name);
	}
	phalcon_update_property_zval(getThis(), SL("_module"), &module_name);

	/**
	 * Check for a namespace
	 */
	if (phalcon_array_isset_fetch_str(&namespace_name, &options, SL("namespace"))) {
		phalcon_array_unset_str(arguments, SL("namespace"), PH_COPY);
	} else {
		ZVAL_NULL(&namespace_name);
	}
	phalcon_update_property_zval(getThis(), SL("_namespace"), &namespace_name);

	/**
	 * Check for a task
	 */
	if (phalcon_array_isset_fetch_str(&task_name, &options, SL("task"))) {
		phalcon_array_unset_str(arguments, SL("task"), PH_COPY);
	} else {
		ZVAL_NULL(&task_name);
	}
	phalcon_update_property_zval(getThis(), SL("_task"), &task_name);

	/**
	 * Check for an action
	 */
	if (phalcon_array_isset_fetch_str(&action_name, &options, SL("action"))) {
		phalcon_array_unset_str(arguments, SL("action"), PH_COPY);
	} else {
		ZVAL_NULL(&action_name);
	}
	phalcon_update_property_zval(getThis(), SL("_action"), &action_name);

	phalcon_update_property_zval(getThis(), SL("_params"), &options);
}
Exemple #4
0
/**
 * Inserts data into a table using custom RBDM SQL syntax
 *
 * @param string $table
 * @param array $values
 * @param array $fields
 * @return boolean
 */
PHP_METHOD(Phalcon_Db, insert){

	zval *table = NULL, *values = NULL, *fields = NULL, *number_values = NULL, *exception_message = NULL;
	zval *exception = NULL, *placeholders = NULL, *value = NULL, *position = NULL;
	zval *str_value = NULL, *comma = NULL, *joined_values = NULL, *joined_fields = NULL;
	zval *insert_sql = NULL, *success = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &table, &values, &fields) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_SEPARATE_PARAM(values);
	
	if (!fields) {
		PHALCON_ALLOC_ZVAL_MM(fields);
		ZVAL_NULL(fields);
	}
	
	if (Z_TYPE_P(values) == IS_ARRAY) { 
		PHALCON_INIT_VAR(number_values);
		phalcon_fast_count(number_values, values TSRMLS_CC);
		if (phalcon_compare_strict_long(number_values, 0 TSRMLS_CC)) {
			PHALCON_INIT_VAR(exception_message);
			PHALCON_CONCAT_SVS(exception_message, "Unable to insert into ", table, " without data");
			
			PHALCON_INIT_VAR(exception);
			object_init_ex(exception, phalcon_db_exception_ce);
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(exception, "__construct", exception_message, PH_CHECK);
			phalcon_throw_exception(exception TSRMLS_CC);
			return;
		}
		
		PHALCON_INIT_VAR(placeholders);
		array_init(placeholders);
		
		if (!phalcon_valid_foreach(values TSRMLS_CC)) {
			return;
		}
		
		ALLOC_HASHTABLE(ah0);
		zend_hash_init(ah0, 0, NULL, NULL, 0);
		zend_hash_copy(ah0, Z_ARRVAL_P(values), NULL, NULL, sizeof(zval*));
		zend_hash_internal_pointer_reset_ex(ah0, &hp0);
		
		ph_cycle_start_0:
		
			if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
				goto ph_cycle_end_0;
			}
			
			PHALCON_INIT_VAR(position);
			PHALCON_GET_FOREACH_KEY(position, ah0, hp0);
			PHALCON_GET_FOREACH_VALUE(value);
			
			if (Z_TYPE_P(value) == IS_OBJECT) {
				PHALCON_INIT_VAR(str_value);
				PHALCON_CALL_FUNC_PARAMS_1(str_value, "strval", value);
				phalcon_array_append(&placeholders, str_value, PH_SEPARATE TSRMLS_CC);
				PHALCON_SEPARATE_PARAM(values);
				phalcon_array_unset(values, position);
			} else {
				if (Z_TYPE_P(value) == IS_NULL) {
					phalcon_array_append_string(&placeholders, SL("null"), PH_SEPARATE TSRMLS_CC);
					PHALCON_SEPARATE_PARAM(values);
					phalcon_array_unset(values, position);
				} else {
					phalcon_array_append_string(&placeholders, SL("?"), PH_SEPARATE TSRMLS_CC);
				}
			}
			
			zend_hash_move_forward_ex(ah0, &hp0);
			goto ph_cycle_start_0;
		
		ph_cycle_end_0:
		zend_hash_destroy(ah0);
		efree(ah0);
		
		PHALCON_INIT_VAR(comma);
		ZVAL_STRING(comma, ", ", 1);
		
		PHALCON_INIT_VAR(joined_values);
		phalcon_fast_join(joined_values, comma, placeholders TSRMLS_CC);
		if (Z_TYPE_P(fields) == IS_ARRAY) { 
			PHALCON_INIT_VAR(joined_fields);
			phalcon_fast_join(joined_fields, comma, fields TSRMLS_CC);
			
			PHALCON_INIT_VAR(insert_sql);
			PHALCON_CONCAT_SVSVSVS(insert_sql, "INSERT INTO ", table, " (", joined_fields, ") VALUES (", joined_values, ")");
		} else {
			PHALCON_INIT_VAR(insert_sql);
			PHALCON_CONCAT_SVSVS(insert_sql, "INSERT INTO ", table, " VALUES (", joined_values, ")");
		}
		
		PHALCON_INIT_VAR(success);
		PHALCON_CALL_METHOD_PARAMS_2(success, this_ptr, "execute", insert_sql, values, PH_NO_CHECK);
		
		RETURN_CCTOR(success);
	}
	PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The second parameter for insert isn't an Array");
	return;
}
Exemple #5
0
/**
 * Inserts data into a table using custom RBDM SQL syntax
 *
 * @param string $table
 * @param array $values
 * @param array $fields
 * @return boolean
 */
PHP_METHOD(Phalcon_Db, insert){

	zval *table = NULL, *values = NULL, *fields = NULL, *placeholders = NULL, *value = NULL;
	zval *n = NULL, *comma = NULL, *joined_values = NULL, *insert_sql = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL;
	zval *i0 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &table, &values, &fields) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	PHALCON_SEPARATE_PARAM(values);
	
	if (!fields) {
		PHALCON_ALLOC_ZVAL_MM(fields);
		ZVAL_NULL(fields);
	}
	
	if (Z_TYPE_P(values) == IS_ARRAY) { 
		PHALCON_ALLOC_ZVAL_MM(r0);
		phalcon_fast_count(r0, values TSRMLS_CC);
		if (!zend_is_true(r0)) {
			PHALCON_ALLOC_ZVAL_MM(i0);
			object_init_ex(i0, phalcon_db_exception_ce);
			PHALCON_ALLOC_ZVAL_MM(r1);
			PHALCON_CONCAT_SVS(r1, "Unable to insert into ", table, " without data");
			PHALCON_CALL_METHOD_PARAMS_1_NORETURN(i0, "__construct", r1, PH_CHECK);
			phalcon_throw_exception(i0 TSRMLS_CC);
			return;
		}
		
		PHALCON_INIT_VAR(placeholders);
		array_init(placeholders);
		if (!phalcon_valid_foreach(values TSRMLS_CC)) {
			return;
		}
		
		ALLOC_HASHTABLE(ah0);
		zend_hash_init(ah0, 0, NULL, NULL, 0);
		zend_hash_copy(ah0, Z_ARRVAL_P(values), NULL, NULL, sizeof(zval*));
		zend_hash_internal_pointer_reset_ex(ah0, &hp0);
		fes_e7f0_1:
			if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
				goto fee_e7f0_1;
			}
			
			PHALCON_INIT_VAR(n);
			PHALCON_GET_FOREACH_KEY(n, ah0, hp0);
			PHALCON_INIT_VAR(value);
			ZVAL_ZVAL(value, *hd, 1, 0);
			if (Z_TYPE_P(value) == IS_OBJECT) {
				PHALCON_INIT_VAR(r2);
				PHALCON_CALL_FUNC_PARAMS_1(r2, "strval", value);
				phalcon_array_append(&placeholders, r2, PH_SEPARATE TSRMLS_CC);
				PHALCON_SEPARATE_PARAM(values);
				phalcon_array_unset(values, n);
			} else {
				phalcon_array_append_string(&placeholders, SL("?"), PH_SEPARATE TSRMLS_CC);
			}
			zend_hash_move_forward_ex(ah0, &hp0);
			goto fes_e7f0_1;
		fee_e7f0_1:
		zend_hash_destroy(ah0);
		efree(ah0);
		
		PHALCON_INIT_VAR(comma);
		ZVAL_STRING(comma, ", ", 1);
		
		PHALCON_INIT_VAR(joined_values);
		phalcon_fast_join(joined_values, comma, placeholders TSRMLS_CC);
		if (Z_TYPE_P(fields) == IS_ARRAY) { 
			PHALCON_ALLOC_ZVAL_MM(r3);
			phalcon_fast_join(r3, comma, fields TSRMLS_CC);
			PHALCON_INIT_VAR(insert_sql);
			PHALCON_CONCAT_SVSVSVS(insert_sql, "INSERT INTO ", table, " (", r3, ") VALUES (", joined_values, ")");
		} else {
			PHALCON_INIT_VAR(insert_sql);
			PHALCON_CONCAT_SVSVS(insert_sql, "INSERT INTO ", table, " VALUES (", joined_values, ")");
		}
		
		PHALCON_ALLOC_ZVAL_MM(r4);
		PHALCON_CALL_METHOD_PARAMS_2(r4, this_ptr, "execute", insert_sql, values, PH_NO_CHECK);
		RETURN_CTOR(r4);
	}
	PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The second parameter for insert isn't an Array");
	return;
}
Exemple #6
0
/**
 * Constructor for Phalcon\Session\Adapter\Memcache
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Session_Adapter_Memcache, __construct){

	zval *options, host = {}, port = {}, lifetime = {}, persistent = {}, prefix = {};
	zval frontend_option = {}, backend_option = {}, frontend_data = {}, memcache = {};
	zval callable_open = {}, callable_close = {}, callable_read = {}, callable_write = {}, callable_destroy = {}, callable_gc = {};

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

	if (Z_TYPE_P(options) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STRW(phalcon_session_exception_ce, "The options must be an array");
		return;
	}

	if (!phalcon_array_isset_fetch_str(&host, options, SL("host"))) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_session_exception_ce, "No session host given in options");
		return;
	}

	if (!phalcon_array_isset_fetch_str(&port, options, SL("port"))) {
		ZVAL_LONG(&port, 11211);
	}

	if (!phalcon_array_isset_fetch_str(&lifetime, options, SL("lifetime"))) {
		ZVAL_LONG(&lifetime, 8600);
	} else {
		phalcon_update_property_this(getThis(), SL("_lifetime"), &lifetime);
	}

	if (!phalcon_array_isset_fetch_str(&persistent, options, SL("persistent"))) {
		ZVAL_FALSE(&persistent);
	}

	if (!phalcon_array_isset_fetch_str(&prefix, options, SL("prefix"))) {
		ZVAL_EMPTY_STRING(&prefix);
	}

	/* create memcache instance */
	array_init_size(&frontend_option, 1);

	phalcon_array_update_str(&frontend_option, SL("lifetime"), &lifetime, PH_COPY);

	object_init_ex(&frontend_data, phalcon_cache_frontend_data_ce);

	PHALCON_CALL_METHODW(NULL, &frontend_data, "__construct", &frontend_option);

	array_init_size(&backend_option, 3);

	phalcon_array_update_str(&backend_option, SL("host"), &host, PH_COPY);
	phalcon_array_update_str(&backend_option, SL("port"), &port, PH_COPY);
	phalcon_array_update_str(&backend_option, SL("persistent"), &persistent, PH_COPY);
	phalcon_array_update_str(&backend_option, SL("prefix"), &prefix, PH_COPY);

	object_init_ex(&memcache, phalcon_cache_backend_memcache_ce);

	PHALCON_CALL_METHODW(NULL, &memcache, "__construct", &frontend_data, &backend_option);

	phalcon_update_property_this(getThis(), SL("_memcache"), &memcache);

	/* open callback */
	array_init_size(&callable_open, 2);
	phalcon_array_append(&callable_open, getThis(), 0);
	phalcon_array_append_string(&callable_open, SL("open"), 0);

	/* close callback */
	array_init_size(&callable_close, 2);
	phalcon_array_append(&callable_close, getThis(), 0);
	phalcon_array_append_string(&callable_close, SL("close"), 0);

	/* read callback */
	array_init_size(&callable_read, 2);
	phalcon_array_append(&callable_read, getThis(), 0);
	phalcon_array_append_string(&callable_read, SL("read"), 0);

	/* write callback */
	array_init_size(&callable_write, 2);
	phalcon_array_append(&callable_write, getThis(), 0);
	phalcon_array_append_string(&callable_write, SL("write"), 0);

	/* destroy callback */
	array_init_size(&callable_destroy, 2);
	phalcon_array_append(&callable_destroy, getThis(), 0);
	phalcon_array_append_string(&callable_destroy, SL("destroy"), 0);

	/* gc callback */
	array_init_size(&callable_gc, 2);
	phalcon_array_append(&callable_gc, getThis(), 0);
	phalcon_array_append_string(&callable_gc, SL("gc"), 0);

	PHALCON_CALL_FUNCTIONW(NULL, "session_set_save_handler", &callable_open, &callable_close, &callable_read, &callable_write, &callable_destroy, &callable_gc);
	PHALCON_CALL_PARENTW(NULL, phalcon_session_adapter_memcache_ce, getThis(), "__construct", options);
}
Exemple #7
0
/**
 * Inserts data into a table using custom RBDM SQL syntax
 *
 * <code>
 * //Inserting a new robot
 * $success = $connection->insert(
 *     "robots",
 *     array("Astro Boy", 1952),
 *     array("name", "year")
 * );
 *
 * //Next SQL sentence is sent to the database system
 * INSERT INTO `robots` (`name`, `year`) VALUES ("Astro boy", 1952);
 * </code>
 *
 * @param 	string $table
 * @param 	array $values
 * @param 	array $fields
 * @param 	array $dataTypes
 * @return 	boolean
 */
PHP_METHOD(Phalcon_Db, insert){

	zval *table, *values, *fields = NULL, *data_types = NULL, *exception_message;
	zval *placeholders, *insert_values, *bind_data_types = NULL;
	zval *value = NULL, *position = NULL, *str_value = NULL, *bind_type = NULL;
	zval *joined_values, *joined_fields, *insert_sql = NULL;
	zval *success;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|zz", &table, &values, &fields, &data_types) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!fields) {
		PHALCON_INIT_NVAR(fields);
	}
	
	if (!data_types) {
		PHALCON_INIT_NVAR(data_types);
	}
	
	if (Z_TYPE_P(values) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The second parameter for insert isn't an Array");
		return;
	}
	if (!phalcon_fast_count_ev(values TSRMLS_CC)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Unable to insert into ", table, " without data");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_db_exception_ce, exception_message);
		return;
	}
	
	PHALCON_INIT_VAR(placeholders);
	array_init(placeholders);
	
	PHALCON_INIT_VAR(insert_values);
	array_init(insert_values);
	if (Z_TYPE_P(data_types) == IS_ARRAY) { 
		PHALCON_INIT_VAR(bind_data_types);
		array_init(bind_data_types);
	} else {
		PHALCON_CPY_WRT(bind_data_types, data_types);
	}
	
	
	if (!phalcon_valid_foreach(values TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(values);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	
	ph_cycle_start_0:
	
		if (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS) {
			goto ph_cycle_end_0;
		}
		
		PHALCON_GET_FOREACH_KEY(position, ah0, hp0);
		PHALCON_GET_FOREACH_VALUE(value);
		
		if (Z_TYPE_P(value) == IS_OBJECT) {
			PHALCON_INIT_NVAR(str_value);
			PHALCON_CALL_FUNC_PARAMS_1(str_value, "strval", value);
			phalcon_array_append(&placeholders, str_value, PH_SEPARATE TSRMLS_CC);
		} else {
			if (Z_TYPE_P(value) == IS_NULL) {
				phalcon_array_append_string(&placeholders, SL("null"), PH_SEPARATE TSRMLS_CC);
			} else {
				phalcon_array_append_string(&placeholders, SL("?"), PH_SEPARATE TSRMLS_CC);
				phalcon_array_append(&insert_values, value, PH_SEPARATE TSRMLS_CC);
				if (Z_TYPE_P(data_types) == IS_ARRAY) { 
					eval_int = phalcon_array_isset(data_types, position);
					if (!eval_int) {
						PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Incomplete number of bind types");
						return;
					}
					
					PHALCON_INIT_NVAR(bind_type);
					phalcon_array_fetch(&bind_type, data_types, position, PH_NOISY_CC);
					phalcon_array_append(&bind_data_types, bind_type, PH_SEPARATE TSRMLS_CC);
				}
			}
		}
		
		zend_hash_move_forward_ex(ah0, &hp0);
		goto ph_cycle_start_0;
		
	ph_cycle_end_0:
	
	PHALCON_INIT_VAR(joined_values);
	phalcon_fast_join_str(joined_values, SL(", "), placeholders TSRMLS_CC);
	if (Z_TYPE_P(fields) == IS_ARRAY) { 
		PHALCON_INIT_VAR(joined_fields);
		phalcon_fast_join_str(joined_fields, SL(", "), fields TSRMLS_CC);
		
		PHALCON_INIT_VAR(insert_sql);
		PHALCON_CONCAT_SVSVSVS(insert_sql, "INSERT INTO ", table, " (", joined_fields, ") VALUES (", joined_values, ")");
	} else {
		PHALCON_INIT_NVAR(insert_sql);
		PHALCON_CONCAT_SVSVS(insert_sql, "INSERT INTO ", table, " VALUES (", joined_values, ")");
	}
	
	PHALCON_INIT_VAR(success);
	PHALCON_CALL_METHOD_PARAMS_3(success, this_ptr, "execute", insert_sql, insert_values, bind_data_types, PH_NO_CHECK);
	
	RETURN_CCTOR(success);
}
Exemple #8
0
/**
 * Constructor for Phalcon\Session\Adapter\Memcache
 *
 * @param array $options
 */
PHP_METHOD(Phalcon_Session_Adapter_Memcache, __construct){

	zval *options;
	zval *host, *port, *lifetime, *persistent, *prefix;
	zval *frontend_data, *memcache, *option;
	zval *callable_open , *callable_close , *callable_read , *callable_write , *callable_destroy , *callable_gc;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &options);
	
	if (Z_TYPE_P(options) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_session_exception_ce, "The options must be an array");
		return;
	}

	if (!phalcon_array_isset_string_fetch(&host, options, SS("host"))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_session_exception_ce, "No session host given in options");
		return;
	}

	if (!phalcon_array_isset_string_fetch(&port, options, SS("port"))) {
		PHALCON_INIT_VAR(port);
		ZVAL_LONG(port, 11211);
	}

	if (!phalcon_array_isset_string_fetch(&lifetime, options, SS("lifetime"))) {
		PHALCON_INIT_VAR(lifetime);
		ZVAL_LONG(lifetime, 8600);
	}

	phalcon_update_property_this(this_ptr, SL("_lifetime"), lifetime TSRMLS_CC);

	if (!phalcon_array_isset_string_fetch(&persistent, options, SS("persistent"))) {
		PHALCON_INIT_VAR(persistent);
		ZVAL_FALSE(persistent);
	}

	if (!phalcon_array_isset_string_fetch(&prefix, options, SS("prefix"))) {
		PHALCON_INIT_VAR(prefix);
		ZVAL_EMPTY_STRING(prefix);
	}

	/* create memcache instance */
	PHALCON_INIT_VAR(option);
	array_init_size(option, 1);

	phalcon_array_update_string(&option, SL("lifetime"), lifetime, PH_COPY);

	PHALCON_INIT_VAR(frontend_data);
	object_init_ex(frontend_data, phalcon_cache_frontend_data_ce);

	PHALCON_CALL_METHOD(NULL, frontend_data, "__construct", option);

	PHALCON_INIT_NVAR(option);
	array_init_size(option, 3);

	phalcon_array_update_string(&option, SL("host"), host, PH_COPY);
	phalcon_array_update_string(&option, SL("port"), port, PH_COPY);
	phalcon_array_update_string(&option, SL("persistent"), persistent, PH_COPY);
	phalcon_array_update_string(&option, SL("prefix"), prefix, PH_COPY);

	PHALCON_INIT_VAR(memcache);
	object_init_ex(memcache, phalcon_cache_backend_memcache_ce);

	PHALCON_CALL_METHOD(NULL, memcache, "__construct", frontend_data, option);

	phalcon_update_property_this(this_ptr, SL("_memcache"), memcache TSRMLS_CC);

	/* open callback */
	PHALCON_INIT_VAR(callable_open);
	array_init_size(callable_open, 2);
	phalcon_array_append(&callable_open, this_ptr, PH_COPY);
	phalcon_array_append_string(&callable_open, SL("open"), 0);

	/* close callback */
	PHALCON_INIT_VAR(callable_close);
	array_init_size(callable_close, 2);
	phalcon_array_append(&callable_close, this_ptr, PH_COPY);
	phalcon_array_append_string(&callable_close, SL("close"), 0);

	/* read callback */
	PHALCON_INIT_VAR(callable_read);
	array_init_size(callable_read, 2);
	phalcon_array_append(&callable_read, this_ptr, PH_COPY);
	phalcon_array_append_string(&callable_read, SL("read"), 0);

	/* write callback */
	PHALCON_INIT_VAR(callable_write);
	array_init_size(callable_write, 2);
	phalcon_array_append(&callable_write, this_ptr, PH_COPY);
	phalcon_array_append_string(&callable_write, SL("write"), 0);

	/* destroy callback */
	PHALCON_INIT_VAR(callable_destroy);
	array_init_size(callable_destroy, 2);
	phalcon_array_append(&callable_destroy, this_ptr, PH_COPY);
	phalcon_array_append_string(&callable_destroy, SL("destroy"), 0);

	/* gc callback */
	PHALCON_INIT_VAR(callable_gc);
	array_init_size(callable_gc, 2);
	phalcon_array_append(&callable_gc, this_ptr, PH_COPY);
	phalcon_array_append_string(&callable_gc, SL("gc"), 0);

	PHALCON_CALL_FUNCTION(NULL, "session_set_save_handler", callable_open, callable_close, callable_read, callable_write, callable_destroy, callable_gc);

	PHALCON_CALL_PARENT(NULL, phalcon_session_adapter_memcache_ce, this_ptr, "__construct", options);
	
	PHALCON_MM_RESTORE();
}