/** * Magic method to get or set services using setters/getters * * @param string $method * @param array $arguments * @return mixed */ PHP_METHOD(Phalcon_DI, __call){ zval *method, *arguments = NULL, possible_service = {}, name = {}, definition = {}; phalcon_fetch_params(0, 1, 1, &method, &arguments); PHALCON_ENSURE_IS_STRING(method); if (!arguments) { arguments = &PHALCON_GLOBAL(z_null); } phalcon_substr(&possible_service, method, 3, 0); phalcon_lcfirst(&name, &possible_service); /** * If the magic method starts with "get" we try to get a service with that name */ if (phalcon_start_with_str(method, SL("get"))) { if (phalcon_isset_property_array(getThis(), SL("_services"), &name)) { PHALCON_RETURN_CALL_SELFW("get", &name, arguments); return; } } /** * If the magic method starts with "set" we try to set a service using that name */ if (phalcon_start_with_str(method, SL("set"))) { if (phalcon_array_isset_fetch_long(&definition, arguments, 0)) { PHALCON_CALL_SELFW(NULL, "set", &name, &definition); return; } } /** * The method doesn't start with set/get throw an exception */ zend_throw_exception_ex(phalcon_di_exception_ce, 0, "Call to undefined method or service '%s'", Z_STRVAL_P(method)); }
/** * Magic method to get or set services using setters/getters * * @param string $method * @param array $arguments * @return mixed */ PHP_METHOD(Phalcon_DI, __call){ zval *method, *arguments = NULL, *services, *service_name = NULL; zval *possible_service = NULL, *handler, *exception_message; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &method, &arguments); if (!arguments) { PHALCON_INIT_VAR(arguments); } /** * If the magic method starts with 'get' we try to get a service with that name */ if (phalcon_start_with_str(method, SL("get"))) { PHALCON_OBS_VAR(services); phalcon_read_property_this(&services, this_ptr, SL("_services"), PH_NOISY_CC); PHALCON_INIT_VAR(service_name); phalcon_substr(service_name, method, 3, 0); PHALCON_INIT_VAR(possible_service); phalcon_lcfirst(possible_service, service_name); if (phalcon_array_isset(services, possible_service)) { if (phalcon_fast_count_ev(arguments TSRMLS_CC)) { phalcon_call_method_p2(return_value, this_ptr, "get", possible_service, arguments); RETURN_MM(); } phalcon_call_method_p1(return_value, this_ptr, "get", possible_service); RETURN_MM(); } } /** * If the magic method starts with 'set' we try to set a service using that name */ if (phalcon_start_with_str(method, SL("set"))) { if (phalcon_array_isset_long(arguments, 0)) { PHALCON_INIT_NVAR(service_name); phalcon_substr(service_name, method, 3, 0); PHALCON_INIT_NVAR(possible_service); phalcon_lcfirst(possible_service, service_name); PHALCON_OBS_VAR(handler); phalcon_array_fetch_long(&handler, arguments, 0, PH_NOISY); phalcon_call_method_p2_noret(this_ptr, "set", possible_service, handler); RETURN_MM_NULL(); } } /** * The method doesn't start with set/get throw an exception */ PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Call to undefined method or service '", method, "'"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_di_exception_ce, exception_message); return; }