/** * @return object */ public function __call($name, $args) { if (substr($name, 0, 6) === 'create') { return call_user_func_array(array( $this->container, NDIContainer::getMethodName($this->namespace . substr($name, 6), FALSE) ), $args); } throw new NotSupportedException; }
public function processServices() { $this->parseServices($this->container, $this->config); foreach ($this->extensions as $name => $extension) { $this->container->addDefinition($name) ->setClass('NDINestedAccessor', array('@container', $name)) ->setAutowired(FALSE); if (isset($this->config[$name])) { $this->parseServices($this->container, $this->config[$name], $name); } } foreach ($this->container->getDefinitions() as $name => $def) { $factory = $name . 'Factory'; if (!$def->shared && !$def->internal && !$this->container->hasDefinition($factory)) { $this->container->addDefinition($factory) ->setClass('NCallback', array('@container', NDIContainer::getMethodName($name, FALSE))) ->setAutowired(FALSE) ->tags = $def->tags; } } }
/** * Formats PHP code for class instantiating, function calling or property setting in PHP. * @return string * @internal */ public function formatStatement(NDIStatement $statement, $self = NULL) { $entity = $this->normalizeEntity($statement->entity); $arguments = $statement->arguments; if (is_string($entity) && NStrings::contains($entity, '?')) { // PHP literal return $this->formatPhp($entity, $arguments, $self); } elseif ($service = $this->getServiceName($entity)) { // factory calling or service retrieving if ($this->definitions[$service]->shared) { if ($arguments) { throw new NServiceCreationException("Unable to call service '$entity'."); } return $this->formatPhp('$this->getService(?)', array($service)); } $params = array(); foreach ($this->definitions[$service]->parameters as $k => $v) { $params[] = preg_replace('#\w+\z#', '\$$0', (is_int($k) ? $v : $k)) . (is_int($k) ? '' : ' = ' . NPhpHelpers::dump($v)); } $rm = new NFunctionReflection(create_function(implode(', ', $params), '')); $arguments = NDIHelpers::autowireArguments($rm, $arguments, $this); return $this->formatPhp('$this->?(?*)', array(NDIContainer::getMethodName($service, FALSE), $arguments), $self); } elseif ($entity === 'not') { // operator return $this->formatPhp('!?', array($arguments[0])); } elseif (is_string($entity)) { // class name if ($constructor = NClassReflection::from($entity)->getConstructor()) { $this->addDependency($constructor->getFileName()); $arguments = NDIHelpers::autowireArguments($constructor, $arguments, $this); } elseif ($arguments) { throw new NServiceCreationException("Unable to pass arguments, class $entity has no constructor."); } return $this->formatPhp("new $entity" . ($arguments ? '(?*)' : ''), array($arguments), $self); } elseif (!NValidators::isList($entity) || count($entity) !== 2) { throw new InvalidStateException("Expected class, method or property, " . NPhpHelpers::dump($entity) . " given."); } elseif ($entity[0] === '') { // globalFunc return $this->formatPhp("$entity[1](?*)", array($arguments), $self); } elseif (NStrings::contains($entity[1], '$')) { // property setter NValidators::assert($arguments, 'list:1', "setup arguments for '" . NCallback::create($entity) . "'"); if ($this->getServiceName($entity[0], $self)) { return $this->formatPhp('?->? = ?', array($entity[0], substr($entity[1], 1), $arguments[0]), $self); } else { return $this->formatPhp($entity[0] . '::$? = ?', array(substr($entity[1], 1), $arguments[0]), $self); } } elseif ($service = $this->getServiceName($entity[0], $self)) { // service method if ($this->definitions[$service]->class) { $arguments = $this->autowireArguments($this->definitions[$service]->class, $entity[1], $arguments); } return $this->formatPhp('?->?(?*)', array($entity[0], $entity[1], $arguments), $self); } else { // static method $arguments = $this->autowireArguments($entity[0], $entity[1], $arguments); return $this->formatPhp("$entity[0]::$entity[1](?*)", array($arguments), $self); } }
/** * Does the service exist? * @param string service name * @return bool */ public function hasService($name) { return isset($this->registry[$name]) || isset($this->factories[$name]) || method_exists($this, $method = NDIContainer::getMethodName($name)) && $this->getReflection()->getMethod($method)->getName() === $method; }