2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\DependencyInjection;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\Resource\ResourceInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\Resource\FileResource;
|
2010-07-15 14:11:33 +01:00
|
|
|
|
2010-01-04 14:26:20 +00:00
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-07-15 14:11:33 +01:00
|
|
|
* ContainerBuilder is a DI container that provides an API to easily describe services.
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
2010-08-05 06:34:53 +01:00
|
|
|
class ContainerBuilder extends Container implements TaggedContainerInterface
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-07-16 08:12:58 +01:00
|
|
|
static protected $extensions = array();
|
|
|
|
|
|
|
|
protected $definitions = array();
|
|
|
|
protected $aliases = array();
|
|
|
|
protected $loading = array();
|
|
|
|
protected $resources = array();
|
|
|
|
protected $extensionContainers = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers an extension.
|
|
|
|
*
|
|
|
|
* @param ExtensionInterface $extension An extension instance
|
|
|
|
*/
|
|
|
|
static public function registerExtension(ExtensionInterface $extension)
|
|
|
|
{
|
|
|
|
static::$extensions[$extension->getAlias()] = static::$extensions[$extension->getNamespace()] = $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an extension by alias or namespace.
|
|
|
|
*
|
|
|
|
* @param string $name An alias or a namespace
|
|
|
|
*
|
|
|
|
* @return ExtensionInterface An extension instance
|
|
|
|
*/
|
|
|
|
static public function getExtension($name)
|
|
|
|
{
|
|
|
|
if (!isset(static::$extensions[$name])) {
|
|
|
|
throw new \LogicException(sprintf('Container extension "%s" is not registered', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return static::$extensions[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function hasExtension($name)
|
|
|
|
{
|
|
|
|
return isset(static::$extensions[$name]);
|
|
|
|
}
|
2010-07-15 14:11:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of resources loaded to build this configuration.
|
|
|
|
*
|
|
|
|
* @return ResourceInterface[] An array of resources
|
|
|
|
*/
|
|
|
|
public function getResources()
|
|
|
|
{
|
|
|
|
return array_unique($this->resources);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a resource for this configuration.
|
|
|
|
*
|
|
|
|
* @param ResourceInterface $resource A resource instance
|
|
|
|
*
|
|
|
|
* @return ContainerBuilder The current instance
|
|
|
|
*/
|
|
|
|
public function addResource(ResourceInterface $resource)
|
|
|
|
{
|
|
|
|
$this->resources[] = $resource;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the object class hierarchy as resources.
|
|
|
|
*
|
|
|
|
* @param object $object An object instance
|
|
|
|
*/
|
|
|
|
public function addObjectResource($object)
|
|
|
|
{
|
|
|
|
$parent = new \ReflectionObject($object);
|
|
|
|
$this->addResource(new FileResource($parent->getFileName()));
|
|
|
|
while ($parent = $parent->getParentClass()) {
|
|
|
|
$this->addResource(new FileResource($parent->getFileName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the configuration for an extension.
|
|
|
|
*
|
2010-07-20 15:40:57 +01:00
|
|
|
* @param string $extension The extension alias or namespace
|
|
|
|
* @param string $tag The extension tag to load (without the namespace - namespace.tag)
|
|
|
|
* @param array $values An array of values that customizes the extension
|
2010-07-15 14:11:33 +01:00
|
|
|
*
|
|
|
|
* @return ContainerBuilder The current instance
|
|
|
|
*/
|
2010-07-20 15:40:57 +01:00
|
|
|
public function loadFromExtension($extension, $tag, array $values = array())
|
2010-07-15 14:11:33 +01:00
|
|
|
{
|
2010-07-16 08:15:22 +01:00
|
|
|
if (true === $this->isFrozen()) {
|
|
|
|
throw new \LogicException('Cannot load from an extension on a frozen container.');
|
|
|
|
}
|
|
|
|
|
2010-07-20 15:40:57 +01:00
|
|
|
$extension = $this->getExtension($extension);
|
2010-07-15 14:11:33 +01:00
|
|
|
$namespace = $extension->getAlias();
|
|
|
|
|
|
|
|
$this->addObjectResource($extension);
|
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
if (!isset($this->extensionContainers[$namespace])) {
|
|
|
|
$this->extensionContainers[$namespace] = new self($this->parameterBag);
|
2010-07-15 14:11:33 +01:00
|
|
|
|
|
|
|
$r = new \ReflectionObject($extension);
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->extensionContainers[$namespace]->addResource(new FileResource($r->getFileName()));
|
2010-07-15 14:11:33 +01:00
|
|
|
}
|
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
$extension->load($tag, $values, $this->extensionContainers[$namespace]);
|
2010-07-15 14:11:33 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a service.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
* @param object $service The service instance
|
|
|
|
*/
|
2010-06-27 17:28:29 +01:00
|
|
|
public function set($id, $service)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
unset($this->definitions[$id]);
|
|
|
|
unset($this->aliases[$id]);
|
|
|
|
|
2010-06-27 17:28:29 +01:00
|
|
|
parent::set($id, $service);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given service is defined.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
*
|
|
|
|
* @return Boolean true if the service is defined, false otherwise
|
|
|
|
*/
|
2010-06-27 17:28:29 +01:00
|
|
|
public function has($id)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-06-27 17:28:29 +01:00
|
|
|
return isset($this->definitions[$id]) || isset($this->aliases[$id]) || parent::has($id);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a service.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
* @param int $invalidBehavior The behavior when the service does not exist
|
|
|
|
*
|
|
|
|
* @return object The associated service
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the service is not defined
|
|
|
|
* @throws \LogicException if the service has a circular reference to itself
|
|
|
|
*
|
|
|
|
* @see Reference
|
|
|
|
*/
|
2010-06-27 17:28:29 +01:00
|
|
|
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-06-27 17:28:29 +01:00
|
|
|
return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (isset($this->loading[$id])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \LogicException(sprintf('The service "%s" has a circular reference to itself.', $id));
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$this->hasDefinition($id) && isset($this->aliases[$id])) {
|
2010-06-27 17:28:29 +01:00
|
|
|
return $this->get($this->aliases[$id]);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition = $this->getDefinition($id);
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
2010-06-27 17:28:29 +01:00
|
|
|
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return null;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
throw $e;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->loading[$id] = true;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$service = $this->createService($definition, $id);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
unset($this->loading[$id]);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $service;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
2010-07-15 14:11:33 +01:00
|
|
|
* Merges a ContainerBuilder with the current ContainerBuilder configuration.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* Service definitions overrides the current defined ones.
|
|
|
|
*
|
|
|
|
* But for parameters, they are overridden by the current ones. It allows
|
|
|
|
* the parameters passed to the container constructor to have precedence
|
|
|
|
* over the loaded ones.
|
|
|
|
*
|
2010-07-15 14:11:33 +01:00
|
|
|
* $container = new ContainerBuilder(array('foo' => 'bar'));
|
2010-05-06 12:25:53 +01:00
|
|
|
* $loader = new LoaderXXX($container);
|
|
|
|
* $loader->load('resource_name');
|
|
|
|
* $container->register('foo', new stdClass());
|
|
|
|
*
|
|
|
|
* In the above example, even if the loaded resource defines a foo
|
2010-07-15 14:11:33 +01:00
|
|
|
* parameter, the value will still be 'bar' as defined in the ContainerBuilder
|
2010-05-06 12:25:53 +01:00
|
|
|
* constructor.
|
|
|
|
*/
|
2010-07-15 14:11:33 +01:00
|
|
|
public function merge(ContainerBuilder $container)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-07-16 08:15:22 +01:00
|
|
|
if (true === $this->isFrozen()) {
|
|
|
|
throw new \LogicException('Cannot merge on a frozen container.');
|
|
|
|
}
|
|
|
|
|
2010-07-15 14:11:33 +01:00
|
|
|
$this->addDefinitions($container->getDefinitions());
|
|
|
|
$this->addAliases($container->getAliases());
|
|
|
|
$this->parameterBag->add($container->getParameterBag()->all());
|
|
|
|
|
|
|
|
foreach ($container->getResources() as $resource) {
|
|
|
|
$this->addResource($resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($container->getExtensionContainers() as $name => $container) {
|
2010-07-16 08:12:58 +01:00
|
|
|
if (isset($this->extensionContainers[$name])) {
|
|
|
|
$this->extensionContainers[$name]->merge($container);
|
2010-07-15 14:11:33 +01:00
|
|
|
} else {
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->extensionContainers[$name] = $container;
|
2010-07-15 14:11:33 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-07-15 14:11:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the containers for the registered extensions.
|
|
|
|
*
|
2010-07-16 08:12:58 +01:00
|
|
|
* @return ExtensionInterface[] An array of extension containers
|
2010-07-15 14:11:33 +01:00
|
|
|
*/
|
|
|
|
public function getExtensionContainers()
|
|
|
|
{
|
2010-07-16 08:12:58 +01:00
|
|
|
return $this->extensionContainers;
|
2010-07-15 14:11:33 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-07-15 14:11:33 +01:00
|
|
|
/**
|
2010-07-16 08:15:22 +01:00
|
|
|
* Freezes the container.
|
|
|
|
*
|
|
|
|
* This method does four things:
|
|
|
|
*
|
|
|
|
* * The extension configurations are merged;
|
|
|
|
* * Parameter values are resolved;
|
|
|
|
* * The parameter bag is freezed;
|
|
|
|
* * Extension loading is disabled.
|
2010-07-15 14:11:33 +01:00
|
|
|
*/
|
2010-07-16 08:15:22 +01:00
|
|
|
public function freeze()
|
2010-07-15 14:11:33 +01:00
|
|
|
{
|
|
|
|
$parameters = $this->parameterBag->all();
|
|
|
|
$definitions = $this->definitions;
|
|
|
|
$aliases = $this->aliases;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
foreach ($this->extensionContainers as $container) {
|
2010-07-15 14:11:33 +01:00
|
|
|
$this->merge($container);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->extensionContainers = array();
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-07-15 14:11:33 +01:00
|
|
|
$this->addDefinitions($definitions);
|
|
|
|
$this->addAliases($aliases);
|
|
|
|
$this->parameterBag->add($parameters);
|
|
|
|
|
2010-07-16 08:15:22 +01:00
|
|
|
parent::freeze();
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets all service ids.
|
|
|
|
*
|
|
|
|
* @return array An array of all defined service ids
|
|
|
|
*/
|
|
|
|
public function getServiceIds()
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return array_unique(array_merge(array_keys($this->getDefinitions()), array_keys($this->aliases), parent::getServiceIds()));
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the service aliases.
|
|
|
|
*
|
|
|
|
* @param array $aliases An array of aliases
|
|
|
|
*/
|
|
|
|
public function addAliases(array $aliases)
|
2010-02-02 10:45:21 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($aliases as $alias => $id) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->setAlias($alias, $id);
|
|
|
|
}
|
2010-02-02 10:45:21 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the service aliases.
|
|
|
|
*
|
|
|
|
* @param array $definitions An array of service definitions
|
|
|
|
*/
|
|
|
|
public function setAliases(array $aliases)
|
2010-01-21 20:28:21 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->aliases = array();
|
|
|
|
$this->addAliases($aliases);
|
2010-01-21 20:28:21 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sets an alias for an existing service.
|
|
|
|
*
|
|
|
|
* @param string $alias The alias to create
|
|
|
|
* @param string $id The service to alias
|
|
|
|
*/
|
|
|
|
public function setAlias($alias, $id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
unset($this->definitions[$alias]);
|
|
|
|
|
|
|
|
$this->aliases[$alias] = $id;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if an alias exists under the given identifier.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
*
|
|
|
|
* @return Boolean true if the alias exists, false otherwise
|
|
|
|
*/
|
|
|
|
public function hasAlias($id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return array_key_exists($id, $this->aliases);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets all defined aliases.
|
|
|
|
*
|
|
|
|
* @return array An array of aliases
|
|
|
|
*/
|
|
|
|
public function getAliases()
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->aliases;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets an alias.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
*
|
|
|
|
* @return string The aliased service identifier
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the alias does not exist
|
|
|
|
*/
|
|
|
|
public function getAlias($id)
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$this->hasAlias($id)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->aliases[$id];
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Registers a service definition.
|
|
|
|
*
|
|
|
|
* This methods allows for simple registration of service definition
|
|
|
|
* with a fluid interface.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
* @param string $class The service class
|
|
|
|
*
|
|
|
|
* @return Definition A Definition instance
|
|
|
|
*/
|
2010-07-04 17:56:48 +01:00
|
|
|
public function register($id, $class = null)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->setDefinition($id, new Definition($class));
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the service definitions.
|
|
|
|
*
|
|
|
|
* @param Definition[] $definitions An array of service definitions
|
|
|
|
*/
|
|
|
|
public function addDefinitions(array $definitions)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($definitions as $id => $definition) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->setDefinition($id, $definition);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sets the service definitions.
|
|
|
|
*
|
|
|
|
* @param array $definitions An array of service definitions
|
|
|
|
*/
|
|
|
|
public function setDefinitions(array $definitions)
|
2010-03-01 17:37:22 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->definitions = array();
|
|
|
|
$this->addDefinitions($definitions);
|
2010-03-01 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets all service definitions.
|
|
|
|
*
|
|
|
|
* @return array An array of Definition instances
|
|
|
|
*/
|
|
|
|
public function getDefinitions()
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->definitions;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sets a service definition.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
* @param Definition $definition A Definition instance
|
|
|
|
*/
|
|
|
|
public function setDefinition($id, Definition $definition)
|
|
|
|
{
|
|
|
|
unset($this->aliases[$id]);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->definitions[$id] = $definition;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns true if a service definition exists under the given identifier.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
*
|
|
|
|
* @return Boolean true if the service definition exists, false otherwise
|
|
|
|
*/
|
|
|
|
public function hasDefinition($id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return array_key_exists($id, $this->definitions);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets a service definition.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
*
|
|
|
|
* @return Definition A Definition instance
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the service definition does not exist
|
|
|
|
*/
|
|
|
|
public function getDefinition($id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$this->hasDefinition($id)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->definitions[$id];
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-07-15 14:11:33 +01:00
|
|
|
/**
|
|
|
|
* Gets a service definition by id or alias.
|
|
|
|
*
|
|
|
|
* The method "unaliases" recursively to return a Definition instance.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier or alias
|
|
|
|
*
|
|
|
|
* @return Definition A Definition instance
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the service definition does not exist
|
|
|
|
*/
|
|
|
|
public function findDefinition($id)
|
|
|
|
{
|
|
|
|
if ($this->hasAlias($id)) {
|
|
|
|
return $this->findDefinition($this->getAlias($id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getDefinition($id);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Creates a service for a service definition.
|
|
|
|
*
|
|
|
|
* @param Definition $definition A service definition instance
|
|
|
|
* @param string $id The service identifier
|
|
|
|
*
|
|
|
|
* @return object The service described by the service definition
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When configure callable is not callable
|
|
|
|
*/
|
|
|
|
protected function createService(Definition $definition, $id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $definition->getFile()) {
|
2010-07-15 19:15:49 +01:00
|
|
|
require_once $this->getParameterBag()->resolveValue($definition->getFile());
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 19:15:49 +01:00
|
|
|
$arguments = $this->resolveServices($this->getParameterBag()->resolveValue($definition->getArguments()));
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-07-05 08:47:14 +01:00
|
|
|
if (null !== $definition->getFactoryMethod()) {
|
2010-07-03 23:35:39 +01:00
|
|
|
if (null !== $definition->getFactoryService()) {
|
2010-07-15 19:15:49 +01:00
|
|
|
$factory = $this->get($this->getParameterBag()->resolveValue($definition->getFactoryService()));
|
2010-07-03 23:35:39 +01:00
|
|
|
} else {
|
2010-07-15 19:15:49 +01:00
|
|
|
$factory = $this->getParameterBag()->resolveValue($definition->getClass());
|
2010-07-03 23:35:39 +01:00
|
|
|
}
|
2010-07-04 17:56:48 +01:00
|
|
|
|
|
|
|
$service = call_user_func_array(array($factory, $definition->getFactoryMethod()), $arguments);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-07-15 19:15:49 +01:00
|
|
|
$r = new \ReflectionClass($this->getParameterBag()->resolveValue($definition->getClass()));
|
2010-07-04 17:56:48 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($definition->isShared()) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->services[$id] = $service;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($definition->getMethodCalls() as $call) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$services = self::getServiceConditionals($call[1]);
|
|
|
|
|
|
|
|
$ok = true;
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($services as $s) {
|
2010-06-27 17:28:29 +01:00
|
|
|
if (!$this->has($s)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($ok) {
|
2010-07-15 19:15:49 +01:00
|
|
|
call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($callable = $definition->getConfigurator()) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (is_array($callable) && is_object($callable[0]) && $callable[0] instanceof Reference) {
|
2010-06-27 17:28:29 +01:00
|
|
|
$callable[0] = $this->get((string) $callable[0]);
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif (is_array($callable)) {
|
2010-07-15 19:15:49 +01:00
|
|
|
$callable[0] = $this->getParameterBag()->resolveValue($callable[0]);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_callable($callable)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service)));
|
|
|
|
}
|
|
|
|
|
|
|
|
call_user_func($callable, $service);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $service;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Replaces service references by the real service instance.
|
|
|
|
*
|
|
|
|
* @param mixed $value A value
|
|
|
|
*
|
|
|
|
* @return mixed The same value with all service references replaced by the real service instances
|
|
|
|
*/
|
|
|
|
public function resolveServices($value)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_array($value)) {
|
2010-05-08 14:32:30 +01:00
|
|
|
foreach ($value as &$v) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$v = $this->resolveServices($v);
|
|
|
|
}
|
2010-05-07 15:09:11 +01:00
|
|
|
} else if (is_object($value) && $value instanceof Reference) {
|
2010-06-27 17:28:29 +01:00
|
|
|
$value = $this->get((string) $value, $value->getInvalidBehavior());
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-08-05 06:34:53 +01:00
|
|
|
* Returns service ids for a given tag.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-05 06:34:53 +01:00
|
|
|
* @param string $name The tag name
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-05 06:34:53 +01:00
|
|
|
* @return array An array of tags
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-08-05 06:34:53 +01:00
|
|
|
public function findTaggedServiceIds($name)
|
2010-02-09 08:45:23 +00:00
|
|
|
{
|
2010-08-05 06:34:53 +01:00
|
|
|
$tags = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->getDefinitions() as $id => $definition) {
|
2010-08-05 06:34:53 +01:00
|
|
|
if ($definition->getTag($name)) {
|
|
|
|
$tags[$id] = $definition->getTag($name);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
return $tags;
|
2010-02-09 08:45:23 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
static public function getServiceConditionals($value)
|
|
|
|
{
|
|
|
|
$services = array();
|
2010-02-09 08:45:23 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_array($value)) {
|
2010-05-08 14:32:30 +01:00
|
|
|
foreach ($value as $v) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$services = array_unique(array_merge($services, self::getServiceConditionals($v)));
|
|
|
|
}
|
2010-06-27 17:28:29 +01:00
|
|
|
} elseif (is_object($value) && $value instanceof Reference && $value->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$services[] = (string) $value;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $services;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
}
|