/** * Resolves a service, the resolved service is stored in the DI, subsequent requests for this service will return the same instance * * @param string $name * @param array $parameters * @return mixed */ PHP_METHOD(Phalcon_DI, getShared){ zval *name, *parameters = NULL, *noerror = NULL, instance = {}; phalcon_fetch_params(0, 1, 2, &name, ¶meters, &noerror); PHALCON_ENSURE_IS_STRING(name); if (!parameters) { parameters = &PHALCON_GLOBAL(z_null); } if (!noerror) { noerror = &PHALCON_GLOBAL(z_null); } if (phalcon_isset_property_array(getThis(), SL("_sharedInstances"), name)) { phalcon_return_property_array(&instance, getThis(), SL("_sharedInstances"), name); phalcon_update_property_bool(getThis(), SL("_freshInstance"), 0); } else { PHALCON_CALL_SELFW(&instance, "get", name, parameters, noerror); if (zend_is_true(&instance)) { phalcon_update_property_bool(getThis(), SL("_freshInstance"), 1); phalcon_update_property_array(getThis(), SL("_sharedInstances"), name, &instance); } } RETURN_CTORW(&instance); }
/** * @brief void Phalcon\Registry::offsetExists(mixed $offset) */ PHP_METHOD(Phalcon_Registry, offsetExists){ zval *property; phalcon_fetch_params(0, 1, 0, &property); if (phalcon_isset_property_array(getThis(), SL("_data"), property)) { RETURN_TRUE; } RETURN_FALSE; }
/** * @brief mixed& Phalcon\Registry::offsetGet(mixed $offset) */ PHP_METHOD(Phalcon_Registry, offsetGet){ zval *property, *callback; phalcon_fetch_params(0, 1, 0, &property); if (phalcon_isset_property_array(getThis(), SL("_data"), property)) { callback = phalcon_read_property_array(getThis(), SL("_data"), property); RETURN_ZVAL(callback, 1, 0); } RETURN_NULL(); }
/** * Check whether the DI contains a service by a name * * @param string $name * @return boolean */ PHP_METHOD(Phalcon_DI, has){ zval *name; phalcon_fetch_params(0, 1, 0, &name); PHALCON_ENSURE_IS_STRING(name); if (phalcon_isset_property_array(getThis(), SL("_services"), name)) { RETURN_TRUE; } RETURN_FALSE; }
/** * @brief void Phalcon\Registry::__call(string $name, array $arguments) */ PHP_METHOD(Phalcon_Registry, __call){ zval *name, *arguments, *callback; phalcon_fetch_params(0, 2, 0, &name, &arguments); PHALCON_ENSURE_IS_STRING(name); if (phalcon_isset_property_array(getThis(), SL("_data"), name)) { callback = phalcon_read_property_array(getThis(), SL("_data"), name); PHALCON_RETURN_CALL_ZVAL_FUNCTIONW(callback, arguments); } else { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Call to undefined method Phalcon\\Registry::%s", Z_STRVAL_P(name)); } }
/** * Returns a Phalcon\DI\Service instance * * @param string $name * @return Phalcon\DI\ServiceInterface */ PHP_METHOD(Phalcon_DI, getService){ zval *name, *service; phalcon_fetch_params(0, 1, 0, &name); PHALCON_ENSURE_IS_STRING(name); if (phalcon_isset_property_array(getThis(), SL("_services"), name)) { service = phalcon_read_property_array(getThis(), SL("_services"), name); RETURN_ZVAL(service, 1, 0); } zend_throw_exception_ex(phalcon_di_exception_ce, 0, "Service '%s' was not found in the dependency injection container", Z_STRVAL_P(name)); }
/** * Attempts to register a service in the services container * Only is successful if a service hasn't been registered previously * with the same name * * @param string $name * @param mixed $definition * @param boolean $shared * @return Phalcon\DI\ServiceInterface */ PHP_METHOD(Phalcon_DI, attempt){ zval *name, *definition, *shared = NULL; phalcon_fetch_params(0, 2, 1, &name, &definition, &shared); PHALCON_ENSURE_IS_STRING(name); if (!phalcon_isset_property_array(getThis(), SL("_services"), name)) { if (!shared) { shared = &PHALCON_GLOBAL(z_false); } object_init_ex(return_value, phalcon_di_service_ce); PHALCON_CALL_METHODW(NULL, return_value, "__construct", name, definition, shared); phalcon_update_property_array(getThis(), SL("_services"), name, return_value); } }
/** * 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)); }