Пример #1
0
static int spl_heap_object_count_elements(zval *object, zend_long *count) /* {{{ */
{
	spl_heap_object *intern = Z_SPLHEAP_P(object);

	if (intern->fptr_count) {
		zval rv;
		zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
		if (!Z_ISUNDEF(rv)) {
			*count = zval_get_long(&rv);
			zval_ptr_dtor(&rv);
			return SUCCESS;
		}
		*count = 0;
		return FAILURE;
	}

	*count = spl_ptr_heap_count(intern->heap);

	return SUCCESS;
}
Пример #2
0
/**
 * Makes fast count on implicit array types without creating a return zval value
 */
int zephir_fast_count_int(zval *value)
{
	long count = 0;

	if (Z_TYPE_P(value) == IS_ARRAY) {
		return zend_hash_num_elements(Z_ARRVAL_P(value));
	}

	if (Z_TYPE_P(value) == IS_OBJECT) {

		#ifdef HAVE_SPL
		zval retval;
		#endif

		if (Z_OBJ_HT_P(value)->count_elements) {
			Z_OBJ_HT(*value)->count_elements(value, &count TSRMLS_CC);
			return (int) count;
		}

		#ifdef HAVE_SPL
		if (instanceof_function(Z_OBJCE_P(value), spl_ce_Countable)) {
			zend_call_method_with_0_params(value, NULL, NULL, "count", &retval);
			if (Z_TYPE(retval) != IS_UNDEF) {
				convert_to_long_ex(&retval);
				count = Z_LVAL(retval);
				zval_ptr_dtor(&retval);
				return (int) count;
			}
			return 0;
		}
		#endif

		return 0;
	}

	if (Z_TYPE_P(value) == IS_NULL) {
		return 0;
	}

	return 1;
}
Пример #3
0
/* {{{ proto bool ProtocolBuffersEnum::isValid(long $value)
*/
PHP_METHOD(protocolbuffers_enum, isValid)
{
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 3)
	zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "ProtocolBuffersEnum::isValid can't work under PHP 5.3. please consider upgrading your PHP");
	return;
#else
	long value;
	zval *result;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
		"l", &value) == FAILURE) {
		return;
	}

	if (zend_call_method_with_0_params(NULL, EG(called_scope), NULL, "getenumdescriptor", &result)) {
		zval *values, **entry;
		HashPosition pos;

		if (!instanceof_function_ex(Z_OBJCE_P(result), php_protocol_buffers_enum_descriptor_class_entry, 0 TSRMLS_CC)) {
			zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "ProtocolBuffersEnum::getEnumDescriptor returns unexpected value.");
			zval_ptr_dtor(&result);
			return;
		}

		php_protocolbuffers_read_protected_property(result, ZEND_STRS("values"), &values TSRMLS_CC);
		zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos);
		while (zend_hash_get_current_data_ex(Z_ARRVAL_P(values), (void **)&entry, &pos) == SUCCESS) {
			if (Z_LVAL_PP(entry) == value) {
				RETVAL_TRUE;
				break;
			}
			zend_hash_move_forward_ex(Z_ARRVAL_P(values), &pos);
		}
		zval_ptr_dtor(&result);
		RETVAL_FALSE;
	} else {
			zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "cannot call ProtocolBuffersEnum::getEnumDescriptor.");
			return;
	}
#endif
}
Пример #4
0
static int spl_fixedarray_object_count_elements(zval *object, zend_long *count) /* {{{ */
{
	spl_fixedarray_object *intern;

	intern = Z_SPLFIXEDARRAY_P(object);
	if (intern->fptr_count) {
		zval rv;
		zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
		if (!Z_ISUNDEF(rv)) {
			*count = zval_get_long(&rv);
			zval_ptr_dtor(&rv);
			return SUCCESS;
		}
	} else if (intern->array) {
		*count = intern->array->size;
		return SUCCESS;
	}

	*count = 0;
	return SUCCESS;
}
static PHP_METHOD(php_midgard_reflection_class, get_user_value)
{
	char *field_name = NULL;
	int field_name_len = 0;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &field_name, &field_name_len) == FAILURE)
		return;

	zval *this = getThis();

	zval *class_name = NULL;
	zend_call_method_with_0_params(&this, zend_reflection_class_class, NULL, "getname", &class_name);

	const gchar *value = midgard_reflector_object_get_schema_value(Z_STRVAL_P(class_name), field_name);
	zval_ptr_dtor(&class_name);

	if (value)
		RETURN_STRING(value, 1);

	RETURN_NULL();
}
Пример #6
0
static int spl_heap_object_count_elements(zval *object, zend_long *count) /* {{{ */
{
	spl_heap_object *intern = Z_SPLHEAP_P(object);

	if (intern->fptr_count) {
		zval rv;
		zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
		if (!Z_ISUNDEF(rv)) {
			zval_ptr_dtor(&intern->retval);
			ZVAL_ZVAL(&intern->retval, &rv, 0, 0);
			convert_to_long(&intern->retval);
			*count = (zend_long) Z_LVAL(intern->retval);
			return SUCCESS;
		}
		*count = 0;
		return FAILURE;
	}
	
	*count = spl_ptr_heap_count(intern->heap);

	return SUCCESS;
} 
PHP_METHOD(midgard_connection, get_instance)
{
	zval *instance;

	if (zend_parse_parameters_none() == FAILURE) {
		return;
	}

	instance = zend_read_static_property(php_midgard_connection_class, "instance", sizeof("instance")-1, 1 TSRMLS_CC);

	if (instance == NULL || ZVAL_IS_NULL(instance)) {
		/* instance is not found. we need to create it */
		zval_ptr_dtor(&instance);
		MAKE_STD_ZVAL(instance);

		object_init_ex(instance, php_midgard_connection_class);
		zend_call_method_with_0_params(&instance, php_midgard_connection_class, &php_midgard_connection_class->constructor, "__construct", NULL);

		MGDG(connection_established) = TRUE;
	}

	zval_add_ref(&instance);
	RETURN_ZVAL(instance, 1, 1);
}
Пример #8
0
U_CFUNC PHP_FUNCTION(intlcal_from_date_time)
{
	zval			*zv_arg,
					zv_tmp,
					*zv_datetime  		= NULL,
					zv_timestamp;
	php_date_obj	*datetime;
	char			*locale_str			= NULL;
	int				locale_str_len;
	TimeZone		*timeZone;
	UErrorCode		status				= U_ZERO_ERROR;
	Calendar        *cal;
	intl_error_reset(NULL TSRMLS_CC);

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s!",
			&zv_arg, &locale_str, &locale_str_len) == FAILURE) {
		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
			"intlcal_from_date_time: bad arguments", 0 TSRMLS_CC);
		RETURN_NULL();
	}

	if (!(Z_TYPE_P(zv_arg) == IS_OBJECT && instanceof_function(
			Z_OBJCE_P(zv_arg), php_date_get_date_ce() TSRMLS_CC))) {
		object_init_ex(&zv_tmp, php_date_get_date_ce());
		zend_call_method_with_1_params(&zv_tmp, NULL, NULL, "__construct", NULL, zv_arg);
		if (EG(exception)) {
			zend_object_store_ctor_failed(Z_OBJ(zv_tmp) TSRMLS_CC);
			goto error;
		}
		zv_datetime = &zv_tmp;
	} else {
		zv_datetime = zv_arg;
	}

	datetime = Z_PHPDATE_P(zv_datetime);
	if (!datetime->time) {
		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
			"intlcal_from_date_time: DateTime object is unconstructed",
			0 TSRMLS_CC);
		goto error;
	}

	zend_call_method_with_0_params(zv_datetime, php_date_get_date_ce(), NULL, "gettimestamp", &zv_timestamp);
	if (Z_TYPE(zv_timestamp) != IS_LONG) {
		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
			"intlcal_from_date_time: bad DateTime; call to "
			"DateTime::getTimestamp() failed", 0 TSRMLS_CC);
		zval_ptr_dtor(&zv_timestamp);
		goto error;
	}

	if (!datetime->time->is_localtime) {
		timeZone = TimeZone::getGMT()->clone();
	} else {
		timeZone = timezone_convert_datetimezone(datetime->time->zone_type,
			datetime, 1, NULL, "intlcal_from_date_time" TSRMLS_CC);
		if (timeZone == NULL) {
			goto error;
		}
	}

	if (!locale_str) {
		locale_str = const_cast<char*>(intl_locale_get_default(TSRMLS_C));
	}

	cal = Calendar::createInstance(timeZone,
		Locale::createFromName(locale_str), status);
	if (cal == NULL) {
		delete timeZone;
		intl_error_set(NULL, status, "intlcal_from_date_time: "
				"error creating ICU Calendar object", 0 TSRMLS_CC);
		goto error;
	}
	cal->setTime(((UDate)Z_LVAL(zv_timestamp)) * 1000., status);
    if (U_FAILURE(status)) {
		/* time zone was adopted by cal; should not be deleted here */
		delete cal;
		intl_error_set(NULL, status, "intlcal_from_date_time: "
				"error creating ICU Calendar::setTime()", 0 TSRMLS_CC);
        goto error;
    }

	calendar_object_create(return_value, cal TSRMLS_CC);

error:
	if (zv_datetime && zv_datetime != zv_arg) {
		zval_ptr_dtor(zv_datetime);
	}
}
Пример #9
0
/* {{{ zend_user_it_new_iterator */
ZEND_API void zend_user_it_new_iterator(zend_class_entry *ce, zval *object, zval *retval)
{
	zend_call_method_with_0_params(object, ce, &ce->iterator_funcs.zf_new_iterator, "getiterator", retval);
}
Пример #10
0
static int fix_datetime_timestamp(zval *datetime_obj, zval *time, zval *timezone_obj)
{
	zval orig_timestamp, fixed_timestamp;
	zval now;
	zval orig_zonename;

	/*
	 * if ($time === null || $time === false || $time === "") {
	 *     $time = "now";
	 * }
	 */
	if (time == NULL || Z_TYPE_P(time) == IS_NULL ||
		Z_TYPE_P(time) == IS_FALSE ||
		(Z_TYPE_P(time) == IS_STRING && Z_STRLEN_P(time) == 0)) {
		ZVAL_STRING(&now, "now");
		time = &now;
	}

	/*
	 * $orig_timestamp = $datetime_obj->getTimeStamp();
	 */
	zend_call_method_with_0_params(datetime_obj, Z_OBJCE_P(datetime_obj), NULL, "gettimestamp", &orig_timestamp);

	/*
	 * if ($timezone_obj) {
	 *     $zonename = $timezone_obj->getName()
	 *     $orig_zonename = date_default_timezone_get();
	 *     date_default_timezone_set($zonename);
	 * }
	 */
	if (timezone_obj) {
		zval zonename;
		zend_call_method_with_0_params(timezone_obj, Z_OBJCE_P(timezone_obj), NULL, "getname", &zonename);
		zend_call_method_with_0_params(NULL, NULL, NULL, "date_default_timezone_get", &orig_zonename);
		zend_call_method_with_1_params(NULL, NULL, NULL, "date_default_timezone_set", NULL, &zonename);
		zval_ptr_dtor(&zonename);
	}

	/*
	 * $fixed_timestamp = timecop_strtotime($time);
	 */
	zend_call_method_with_1_params(NULL, NULL, NULL, "timecop_strtotime", &fixed_timestamp, time);

	/*
	 * if ($timezone_obj && $orig_zonename) {
	 *     date_default_timezone_set($orig_zonename);
	 * }
	 */
	if (timezone_obj) {
		zend_call_method_with_1_params(NULL, NULL, NULL, "date_default_timezone_set", NULL, &orig_zonename);
	}

	/*
	 * if ($fixed_timestamp === false) {
	 *     // warn
	 * } elseif ($orig_timestamp != $fixed_timestamp) {
	 *     $datetime_obj->setTimeStamp($fixed_timestamp)
	 * }
	 */
	if (Z_TYPE(fixed_timestamp) == IS_FALSE) {
		php_error_docref(NULL, E_WARNING,
						 "Failed to parse time string '%s': giving up time traveling",
						 Z_STRVAL_P(time));
	} else if (Z_LVAL(orig_timestamp) != Z_LVAL(fixed_timestamp)) {
		// set time-travelled timestamp
		zend_call_method_with_1_params(datetime_obj, Z_OBJCE_P(datetime_obj), NULL, "settimestamp", NULL, &fixed_timestamp);
	}

	// restore original timezone
	/*
	 * if ($timezone_obj) {
	 *     date_default_timezone_set($orig_zonename);
	 * }
	 */
	if (timezone_obj) {
		zend_call_method_with_1_params(NULL, NULL, NULL, "date_default_timezone_set", NULL, &orig_zonename);
		zval_ptr_dtor(&orig_zonename);
	}
	if (time == &now) {
		zval_ptr_dtor(&now);
	}
	zval_ptr_dtor(&orig_timestamp);
	zval_ptr_dtor(&fixed_timestamp);
	return 0;
}
static PHP_METHOD(midgard_object_class, factory)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *class_name;
	int class_name_length;
	zval *zvalue = NULL;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &class_name, &class_name_length, &zvalue) == FAILURE)
		return;

	zend_class_entry **pce = NULL;
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3
	if (zend_lookup_class_ex(class_name, class_name_length, NULL, 1, &pce TSRMLS_CC) == FAILURE) {
#else
	if (zend_lookup_class_ex(class_name, class_name_length, 1, &pce TSRMLS_CC) == FAILURE) {
#endif
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can not find %s class", class_name);
		return;
	}

	zend_class_entry *ce = *pce;

	object_init_ex(return_value, ce); /* Initialize new object for which QB has been created for */

	/* Call class constructor on given instance */
	if (zvalue == NULL) {
		zend_call_method_with_0_params(&return_value, ce, &ce->constructor, "__construct", NULL);
	} else {
		zend_call_method_with_1_params(&return_value, ce, &ce->constructor, "__construct", NULL, zvalue);
	}
}

ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_object_class_factory, 0, 0, 1)
	ZEND_ARG_INFO(0, classname)
	ZEND_ARG_INFO(0, id)
ZEND_END_ARG_INFO()

static PHP_METHOD(midgard_object_class, get_object_by_guid)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *guid;
	int guid_length;
	const gchar *type_name;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &guid, &guid_length) == FAILURE)
		return;

	MidgardObject *object = midgard_schema_object_factory_get_object_by_guid(mgd, guid);

	if (!object) {
		php_midgard_error_exception_throw(mgd TSRMLS_CC);
		return;
	}

	type_name = G_OBJECT_TYPE_NAME(G_OBJECT(object));
	zend_class_entry *ce = zend_fetch_class((gchar *)type_name, strlen(type_name), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

	if (ce == NULL) {
		php_error(E_WARNING, "Can not find %s class", type_name);
		return;
	}

	php_midgard_gobject_new_with_gobject(return_value, ce, G_OBJECT(object), TRUE TSRMLS_CC);
}
Пример #12
0
/* {{{ proto Router Router::addRoute(string method, string uri, Callable handler)
	   proto Router Router::addRoute(Route route)
	Throws RoutingException on failure */
static PHP_METHOD(Router, addRoute) {
	const char *request_method = NULL;
	size_t request_method_length = 0L;
	const char *request_uri = NULL;
	size_t request_uri_length = 0L;
	zval *callable = NULL;
	zend_class_entry *ce = NULL;
	router_t *router = getRouter();
	
	switch (ZEND_NUM_ARGS()) {
		case 3: if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "ssz", 
				&request_method, &request_method_length, 
				&request_uri, &request_uri_length, &callable) != SUCCESS) {
			return;
		} break;
		
		case 1: if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "O", 
				&callable, ce) != SUCCESS) {
			return;
		} break;
		
		default:
			zend_throw_exception(
				RoutingException, "Router::addRoute expects one or three arguments, please see documentation", 0 TSRMLS_CC);
			return;
	}

	{
		route_t route = {ROUTER_ROUTE_NORMAL};
		
		route.callable = callable;
		
		if (ZEND_NUM_ARGS() == 1) {
			zval *pmethod = NULL, 
				 *puri = NULL;
			
			zend_call_method_with_0_params(
				&callable, ce, NULL, "getMethod", &pmethod);
			
			if (Z_TYPE_P(pmethod) != IS_STRING) {
				zend_throw_exception(
					RoutingException, "getMethod returned a non-string value", 0 TSRMLS_CC);
				zval_ptr_dtor(&pmethod);
				return;
			}
			
			zend_call_method_with_0_params(
				&callable, ce, NULL, "getURI", &puri);
				
			if (Z_TYPE_P(puri) != IS_STRING) {
				zend_throw_exception(
					RoutingException, "getURI returned a non-string value", 0 TSRMLS_CC);
				zval_ptr_dtor(&pmethod);
				zval_ptr_dtor(&puri);
				return;
			}
			
			ZVAL_STRINGL(&route.method, Z_STRVAL_P(pmethod), Z_STRLEN_P(pmethod), 1);
			ZVAL_STRINGL(&route.uri, Z_STRVAL_P(puri), Z_STRLEN_P(puri), 1);
			Z_ADDREF_P(route.callable);
			zend_hash_next_index_insert(
				&router->routes, 
				(void**) &route, sizeof(route_t), NULL);
			zval_ptr_dtor(&pmethod);
			zval_ptr_dtor(&puri);
		} else {
			char *callable_name = NULL;
			
			if (!zend_is_callable(callable, 0, &callable_name TSRMLS_CC)) {
				zend_throw_exception(
					RoutingException, "handler is not callable", 0 TSRMLS_CC);
				if (callable_name)
					efree(callable_name);
				return;
			}
			
			ZVAL_STRINGL(&route.method, request_method, request_method_length, 1);
			ZVAL_STRINGL(&route.uri, request_uri, request_uri_length, 1);
			Z_ADDREF_P(route.callable);
			zend_hash_next_index_insert(
				&router->routes, 
				(void**) &route, sizeof(route_t), NULL);
			if (callable_name)
				efree(callable_name);
		}
	}

	RETURN_CHAIN();
} /* }}} */