/** * Adds a method to call after service initialization. * * @param string $method The method name to call * @param array $arguments An array of arguments to pass to the method call * * @return tubepress_api_ioc_DefinitionInterface The current instance * * @throws InvalidArgumentException on empty $method param * * @api * @since 3.1.0 */ public function addMethodCall($method, array $arguments = array()) { try { return parent::addMethodCall($method, $arguments); } catch (ehough_iconic_exception_InvalidArgumentException $e) { throw new InvalidArgumentException($e); } }
/** * Sets a configurator to call after the service is fully initialized. * * @param callable $callable A PHP callable * * @return ehough_iconic_Definition The current instance * * @api */ public function setConfigurator($callable) { $this->_delegate->setConfigurator($callable); return parent::setConfigurator($callable); }
/** * Adds a service * * @param string $id * @param ehough_iconic_Definition $definition * * @return string */ private function addService($id, $definition) { $code = " {$id}:\n"; if ($definition->getClass()) { $code .= sprintf(" class: %s\n", $definition->getClass()); } if (!$definition->isPublic()) { $code .= " public: false\n"; } $tagsCode = ''; foreach ($definition->getTags() as $name => $tags) { foreach ($tags as $attributes) { $att = array(); foreach ($attributes as $key => $value) { $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value)); } $att = $att ? ', ' . implode(' ', $att) : ''; $tagsCode .= sprintf(" - { name: %s%s }\n", $this->dumper->dump($name), $att); } } if ($tagsCode) { $code .= " tags:\n" . $tagsCode; } if ($definition->getFile()) { $code .= sprintf(" file: %s\n", $definition->getFile()); } if ($definition->isSynthetic()) { $code .= sprintf(" synthetic: true\n"); } if ($definition->isSynchronized()) { $code .= sprintf(" synchronized: true\n"); } if ($definition->getFactoryClass()) { $code .= sprintf(" factory_class: %s\n", $definition->getFactoryClass()); } if ($definition->isLazy()) { $code .= sprintf(" lazy: true\n"); } if ($definition->getFactoryMethod()) { $code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod()); } if ($definition->getFactoryService()) { $code .= sprintf(" factory_service: %s\n", $definition->getFactoryService()); } if ($definition->getArguments()) { $code .= sprintf(" arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0)); } if ($definition->getProperties()) { $code .= sprintf(" properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0)); } if ($definition->getMethodCalls()) { $code .= sprintf(" calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12)); } if (ehough_iconic_ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope())) { $code .= sprintf(" scope: %s\n", $scope); } if ($callable = $definition->getConfigurator()) { if (is_array($callable)) { if ($callable[0] instanceof ehough_iconic_Reference) { $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]); } else { $callable = array($callable[0], $callable[1]); } } $code .= sprintf(" configurator: %s\n", $this->dumper->dump($callable, 0)); } return $code; }
private function addNewInstance($id, ehough_iconic_Definition $definition, $return, $instantiation) { $class = $this->dumpValue($definition->getClass()); $arguments = array(); foreach ($definition->getArguments() as $value) { $arguments[] = $this->dumpValue($value); } if (null !== $definition->getFactoryMethod()) { if (null !== $definition->getFactoryClass()) { return sprintf(" {$return}{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass()), $definition->getFactoryMethod(), $arguments ? ', ' . implode(', ', $arguments) : ''); } if (null !== $definition->getFactoryService()) { return sprintf(" {$return}{$instantiation}%s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService()), $definition->getFactoryMethod(), implode(', ', $arguments)); } throw new ehough_iconic_exception_RuntimeException(sprintf('Factory method requires a factory service or factory class in service definition for %s', $id)); } if (false !== strpos($class, '$')) { return sprintf(" \$class = %s;\n\n {$return}{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments)); } return sprintf(" {$return}{$instantiation}new %s(%s);\n", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments)); }
/** * Adds a service. * * @param ehough_iconic_Definition $definition * @param string $id * @param \DOMElement $parent */ private function addService($definition, $id, DOMElement $parent) { $service = $this->document->createElement('service'); if (null !== $id) { $service->setAttribute('id', $id); } if ($definition->getClass()) { $service->setAttribute('class', $definition->getClass()); } if ($definition->getFactoryMethod()) { $service->setAttribute('factory-method', $definition->getFactoryMethod()); } if ($definition->getFactoryClass()) { $service->setAttribute('factory-class', $definition->getFactoryClass()); } if ($definition->getFactoryService()) { $service->setAttribute('factory-service', $definition->getFactoryService()); } if (ehough_iconic_ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope())) { $service->setAttribute('scope', $scope); } if (!$definition->isPublic()) { $service->setAttribute('public', 'false'); } if ($definition->isSynthetic()) { $service->setAttribute('synthetic', 'true'); } if ($definition->isSynchronized()) { $service->setAttribute('synchronized', 'true'); } if ($definition->isLazy()) { $service->setAttribute('lazy', 'true'); } if (null !== ($decorated = $definition->getDecoratedService())) { list($decorated, $renamedId) = $decorated; $service->setAttribute('decorates', $decorated); if (null !== $renamedId) { $service->setAttribute('decoration-inner-name', $renamedId); } } foreach ($definition->getTags() as $name => $tags) { foreach ($tags as $attributes) { $tag = $this->document->createElement('tag'); $tag->setAttribute('name', $name); foreach ($attributes as $key => $value) { $tag->setAttribute($key, $value); } $service->appendChild($tag); } } if ($definition->getFile()) { $file = $this->document->createElement('file'); $file->appendChild($this->document->createTextNode($definition->getFile())); $service->appendChild($file); } if ($parameters = $definition->getArguments()) { $this->convertParameters($parameters, 'argument', $service); } if ($parameters = $definition->getProperties()) { $this->convertParameters($parameters, 'property', $service, 'name'); } $this->addMethodCalls($definition->getMethodCalls(), $service); if ($callable = $definition->getConfigurator()) { $configurator = $this->document->createElement('configurator'); if (is_array($callable)) { $configurator->setAttribute($callable[0] instanceof ehough_iconic_Reference ? 'service' : 'class', $callable[0]); $configurator->setAttribute('method', $callable[1]); } else { $configurator->setAttribute('function', $callable); } $service->appendChild($configurator); } $parent->appendChild($service); }
/** * Resolves the definition * * @param string $id The definition identifier * @param ehough_iconic_DefinitionDecorator $definition * * @return ehough_iconic_Definition * * @throws RuntimeException When the definition is invalid */ private function resolveDefinition($id, ehough_iconic_DefinitionDecorator $definition) { if (!$this->container->hasDefinition($parent = $definition->getParent())) { throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $id)); } $parentDef = $this->container->getDefinition($parent); if ($parentDef instanceof ehough_iconic_DefinitionDecorator) { $parentDef = $this->resolveDefinition($parent, $parentDef); } $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $id, $parent)); $def = new ehough_iconic_Definition(); // merge in parent definition // purposely ignored attributes: scope, abstract, tags $def->setClass($parentDef->getClass()); $def->setArguments($parentDef->getArguments()); $def->setMethodCalls($parentDef->getMethodCalls()); $def->setProperties($parentDef->getProperties()); $def->setFactoryClass($parentDef->getFactoryClass()); $def->setFactoryMethod($parentDef->getFactoryMethod()); $def->setFactoryService($parentDef->getFactoryService()); $def->setConfigurator($parentDef->getConfigurator()); $def->setFile($parentDef->getFile()); $def->setPublic($parentDef->isPublic()); $def->setLazy($parentDef->isLazy()); // overwrite with values specified in the decorator $changes = $definition->getChanges(); if (isset($changes['class'])) { $def->setClass($definition->getClass()); } if (isset($changes['factory_class'])) { $def->setFactoryClass($definition->getFactoryClass()); } if (isset($changes['factory_method'])) { $def->setFactoryMethod($definition->getFactoryMethod()); } if (isset($changes['factory_service'])) { $def->setFactoryService($definition->getFactoryService()); } if (isset($changes['configurator'])) { $def->setConfigurator($definition->getConfigurator()); } if (isset($changes['file'])) { $def->setFile($definition->getFile()); } if (isset($changes['public'])) { $def->setPublic($definition->isPublic()); } if (isset($changes['lazy'])) { $def->setLazy($definition->isLazy()); } // merge arguments foreach ($definition->getArguments() as $k => $v) { if (is_numeric($k)) { $def->addArgument($v); continue; } if (0 !== strpos($k, 'index_')) { throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k)); } $index = (int) substr($k, strlen('index_')); $def->replaceArgument($index, $v); } // merge properties foreach ($definition->getProperties() as $k => $v) { $def->setProperty($k, $v); } // append method calls if (count($calls = $definition->getMethodCalls()) > 0) { $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls)); } // these attributes are always taken from the child $def->setAbstract($definition->isAbstract()); $def->setScope($definition->getScope()); $def->setTags($definition->getTags()); // set new definition on container $this->container->setDefinition($id, $def); return $def; }
/** * Parses an individual ehough_iconic_Definition * * @param string $id * @param SimpleXMLElement $service * @param string $file */ private function parseDefinition($id, $service, $file) { if ((string) $service['alias']) { $public = true; if (isset($service['public'])) { $public = $service->getAttributeAsPhp('public'); } $this->container->setAlias($id, new ehough_iconic_Alias((string) $service['alias'], $public)); return; } if (isset($service['parent'])) { $definition = new ehough_iconic_DefinitionDecorator((string) $service['parent']); } else { $definition = new ehough_iconic_Definition(); } foreach (array('class', 'scope', 'public', 'factory-class', 'factory-method', 'factory-service', 'synthetic', 'synchronized', 'lazy', 'abstract') as $key) { if (isset($service[$key])) { $method = 'set' . str_replace('-', '', $key); $definition->{$method}((string) $service->getAttributeAsPhp($key)); } } if ($service->file) { $definition->setFile((string) $service->file); } $definition->setArguments($service->getArgumentsAsPhp('argument')); $definition->setProperties($service->getArgumentsAsPhp('property')); if (isset($service->configurator)) { if (isset($service->configurator['function'])) { $definition->setConfigurator((string) $service->configurator['function']); } else { if (isset($service->configurator['service'])) { $class = new ehough_iconic_Reference((string) $service->configurator['service'], ehough_iconic_ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false); } else { $class = (string) $service->configurator['class']; } $definition->setConfigurator(array($class, (string) $service->configurator['method'])); } } foreach ($service->call as $call) { $definition->addMethodCall((string) $call['method'], $call->getArgumentsAsPhp('argument')); } foreach ($service->tag as $tag) { $parameters = array(); foreach ($tag->attributes() as $name => $value) { if ('name' === $name) { continue; } $parameters[$name] = ehough_iconic_SimpleXMLElement::phpize($value); } $definition->addTag((string) $tag['name'], $parameters); } $this->container->setDefinition($id, $definition); }
/** * {@inheritdoc} * * @api */ public function setLazy($boolean) { $this->changes['lazy'] = true; return parent::setLazy($boolean); }
/** * Parses a definition. * * @param string $id * @param array $service * @param string $file * * @throws ehough_iconic_exception_InvalidArgumentException When tags are invalid */ private function parseDefinition($id, $service, $file) { if (is_string($service) && 0 === strpos($service, '@')) { $this->container->setAlias($id, substr($service, 1)); return; } elseif (isset($service['alias'])) { $public = !array_key_exists('public', $service) || (bool) $service['public']; $this->container->setAlias($id, new ehough_iconic_Alias($service['alias'], $public)); return; } if (isset($service['parent'])) { $definition = new ehough_iconic_DefinitionDecorator($service['parent']); } else { $definition = new ehough_iconic_Definition(); } if (isset($service['class'])) { $definition->setClass($service['class']); } if (isset($service['scope'])) { $definition->setScope($service['scope']); } if (isset($service['synthetic'])) { $definition->setSynthetic($service['synthetic']); } if (isset($service['synchronized'])) { $definition->setSynchronized($service['synchronized']); } if (isset($service['lazy'])) { $definition->setLazy($service['lazy']); } if (isset($service['public'])) { $definition->setPublic($service['public']); } if (isset($service['abstract'])) { $definition->setAbstract($service['abstract']); } if (isset($service['factory_class'])) { $definition->setFactoryClass($service['factory_class']); } if (isset($service['factory_method'])) { $definition->setFactoryMethod($service['factory_method']); } if (isset($service['factory_service'])) { $definition->setFactoryService($service['factory_service']); } if (isset($service['file'])) { $definition->setFile($service['file']); } if (isset($service['arguments'])) { $definition->setArguments($this->resolveServices($service['arguments'])); } if (isset($service['properties'])) { $definition->setProperties($this->resolveServices($service['properties'])); } if (isset($service['configurator'])) { if (is_string($service['configurator'])) { $definition->setConfigurator($service['configurator']); } else { $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1])); } } if (isset($service['calls'])) { foreach ($service['calls'] as $call) { $args = isset($call[1]) ? $this->resolveServices($call[1]) : array(); $definition->addMethodCall($call[0], $args); } } if (isset($service['tags'])) { if (!is_array($service['tags'])) { throw new ehough_iconic_exception_InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file)); } foreach ($service['tags'] as $tag) { if (!isset($tag['name'])) { throw new ehough_iconic_exception_InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); } $name = $tag['name']; unset($tag['name']); foreach ($tag as $attribute => $value) { if (!is_scalar($value) && null !== $value) { throw new ehough_iconic_exception_InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s.', $id, $name, $attribute, $file)); } } $definition->addTag($name, $tag); } } if (isset($service['decorates'])) { $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null; $definition->setDecoratedService($service['decorates'], $renameId); } $this->container->setDefinition($id, $definition); }
/** * Validates the scope of a single ehough_iconic_Reference. * * @param ehough_iconic_Reference $reference * @param ehough_iconic_Definition $definition * * @throws ehough_iconic_exception_ScopeWideningInjectionException when the definition references a service of a narrower scope * @throws ehough_iconic_exception_ScopeCrossingInjectionException when the definition references a service of another scope hierarchy */ private function validateScope(ehough_iconic_Reference $reference, ehough_iconic_Definition $definition = null) { if (ehough_iconic_ContainerInterface::SCOPE_PROTOTYPE === $this->currentScope) { return; } if (!$reference->isStrict()) { return; } if (null === $definition) { return; } if ($this->currentScope === ($scope = $definition->getScope())) { return; } $id = (string) $reference; if (in_array($scope, $this->currentScopeChildren, true)) { throw new ehough_iconic_exception_ScopeWideningInjectionException($this->currentId, $this->currentScope, $id, $scope); } if (!in_array($scope, $this->currentScopeAncestors, true)) { throw new ehough_iconic_exception_ScopeCrossingInjectionException($this->currentId, $this->currentScope, $id, $scope); } }
/** * Parses an individual ehough_iconic_Definition * * @param string $id * @param DOMElement $service * @param string $file */ private function parseDefinition($id, DOMElement $service, $file) { if ($alias = $service->getAttribute('alias')) { $public = true; if ($publicAttr = $service->getAttribute('public')) { $public = self::_phpize($publicAttr); } $this->container->setAlias($id, new ehough_iconic_Alias($alias, $public)); return; } if ($parent = $service->getAttribute('parent')) { $definition = new ehough_iconic_DefinitionDecorator($parent); } else { $definition = new ehough_iconic_Definition(); } foreach (array('class', 'scope', 'public', 'factory-class', 'factory-method', 'factory-service', 'synthetic', 'synchronized', 'lazy', 'abstract') as $key) { if ($value = $service->getAttribute($key)) { $method = 'set' . str_replace('-', '', $key); $definition->{$method}(self::_phpize($value)); } } if ($files = $this->getChildren($service, 'file')) { $definition->setFile($files[0]->nodeValue); } $definition->setArguments($this->getArgumentsAsPhp($service, 'argument')); $definition->setProperties($this->getArgumentsAsPhp($service, 'property')); if ($configurators = $this->getChildren($service, 'configurator')) { $configurator = $configurators[0]; if ($function = $configurator->getAttribute('function')) { $definition->setConfigurator($function); } else { if ($childService = $configurator->getAttribute('service')) { $class = new ehough_iconic_Reference($childService, ehough_iconic_ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false); } else { $class = $configurator->getAttribute('class'); } $definition->setConfigurator(array($class, $configurator->getAttribute('method'))); } } foreach ($this->getChildren($service, 'call') as $call) { $definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument')); } foreach ($this->getChildren($service, 'tag') as $tag) { $parameters = array(); foreach ($tag->attributes as $name => $node) { if ('name' === $name) { continue; } if (false !== strpos($name, '-') && false === strpos($name, '_') && !array_key_exists($normalizedName = str_replace('-', '_', $name), $parameters)) { $parameters[$normalizedName] = self::_phpize($node->nodeValue); } // keep not normalized key for BC too $parameters[$name] = self::_phpize($node->nodeValue); } $definition->addTag($tag->getAttribute('name'), $parameters); } if ($value = $service->getAttribute('decorates')) { $renameId = $service->hasAttribute('decoration-inner-name') ? $service->getAttribute('decoration-inner-name') : null; $definition->setDecoratedService($value, $renameId); } $this->container->setDefinition($id, $definition); }
/** * Shares a given service in the container * * @param ehough_iconic_Definition $definition * @param mixed $service * @param string $id * * @throws ehough_iconic_exception_InactiveScopeException */ private function shareService(ehough_iconic_Definition $definition, $service, $id) { if (self::SCOPE_PROTOTYPE !== ($scope = $definition->getScope())) { if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) { throw new ehough_iconic_exception_InactiveScopeException($id, $scope); } $this->services[$lowerId = strtolower($id)] = $service; if (self::SCOPE_CONTAINER !== $scope) { $this->scopedServices[$scope][$lowerId] = $service; } } }
/** * Checks if the definition is inlineable. * * @param ehough_iconic_ContainerBuilder $container * @param string $id * @param ehough_iconic_Definition $definition * * @return bool If the definition is inlineable */ private function isInlineableDefinition(ehough_iconic_ContainerBuilder $container, $id, ehough_iconic_Definition $definition) { if (ehough_iconic_ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) { return true; } if ($definition->isPublic() || $definition->isLazy()) { return false; } if (!$this->graph->hasNode($id)) { return true; } if ($this->currentId == $id) { return false; } $ids = array(); foreach ($this->graph->getNode($id)->getInEdges() as $edge) { $ids[] = $edge->getSourceNode()->getId(); } if (count(array_unique($ids)) > 1) { return false; } return $container->getDefinition(reset($ids))->getScope() === $definition->getScope(); }
private function processDefinition(ehough_iconic_Definition $definition) { $this->processReferences($definition->getArguments()); $this->processReferences($definition->getMethodCalls()); $this->processReferences($definition->getProperties()); }