2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\DependencyInjection;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Compiler\Compiler;
|
2010-12-14 23:25:33 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
2011-01-16 07:53:28 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
|
2011-12-04 23:51:22 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
|
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
|
|
|
use Symfony\Component\DependencyInjection\Exception\LogicException;
|
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
|
|
|
use Symfony\Component\Config\Resource\ResourceInterface;
|
2010-07-15 14:11:33 +01:00
|
|
|
|
2010-01-04 14:26:20 +00:00
|
|
|
/**
|
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
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
2010-08-05 06:34:53 +01:00
|
|
|
class ContainerBuilder extends Container implements TaggedContainerInterface
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2012-11-01 15:08:59 +00:00
|
|
|
/**
|
|
|
|
* @var ExtensionInterface[]
|
|
|
|
*/
|
|
|
|
private $extensions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ExtensionInterface[]
|
|
|
|
*/
|
|
|
|
private $extensionsByNs = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Definition[]
|
|
|
|
*/
|
|
|
|
private $definitions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Alias[]
|
|
|
|
*/
|
|
|
|
private $aliases = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ResourceInterface[]
|
|
|
|
*/
|
|
|
|
private $resources = array();
|
|
|
|
|
2011-03-11 13:50:46 +00:00
|
|
|
private $extensionConfigs = array();
|
2012-11-01 15:08:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Compiler
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private $compiler;
|
2010-12-14 23:25:33 +00:00
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
/**
|
|
|
|
* Registers an extension.
|
|
|
|
*
|
|
|
|
* @param ExtensionInterface $extension An extension instance
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-07-16 08:12:58 +01:00
|
|
|
*/
|
2011-02-15 06:52:11 +00:00
|
|
|
public function registerExtension(ExtensionInterface $extension)
|
2010-07-16 08:12:58 +01:00
|
|
|
{
|
2011-02-15 06:52:11 +00:00
|
|
|
$this->extensions[$extension->getAlias()] = $extension;
|
2011-02-15 03:14:08 +00:00
|
|
|
|
|
|
|
if (false !== $extension->getNamespace()) {
|
2011-02-15 06:52:11 +00:00
|
|
|
$this->extensionsByNs[$extension->getNamespace()] = $extension;
|
2011-02-15 03:14:08 +00:00
|
|
|
}
|
2010-07-16 08:12:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an extension by alias or namespace.
|
|
|
|
*
|
|
|
|
* @param string $name An alias or a namespace
|
|
|
|
*
|
|
|
|
* @return ExtensionInterface An extension instance
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
2012-12-16 12:02:54 +00:00
|
|
|
* @throws LogicException if the extension is not registered
|
2012-09-07 23:56:45 +01:00
|
|
|
*
|
2011-07-20 09:50:27 +01:00
|
|
|
* @api
|
2010-07-16 08:12:58 +01:00
|
|
|
*/
|
2011-02-15 06:52:11 +00:00
|
|
|
public function getExtension($name)
|
2010-07-16 08:12:58 +01:00
|
|
|
{
|
2011-02-15 06:52:11 +00:00
|
|
|
if (isset($this->extensions[$name])) {
|
|
|
|
return $this->extensions[$name];
|
2011-02-15 04:58:18 +00:00
|
|
|
}
|
2011-02-05 18:42:07 +00:00
|
|
|
|
2011-02-15 06:52:11 +00:00
|
|
|
if (isset($this->extensionsByNs[$name])) {
|
|
|
|
return $this->extensionsByNs[$name];
|
2010-07-16 08:12:58 +01:00
|
|
|
}
|
|
|
|
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new LogicException(sprintf('Container extension "%s" is not registered', $name));
|
2010-07-16 08:12:58 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 18:42:07 +00:00
|
|
|
/**
|
2011-02-15 04:58:18 +00:00
|
|
|
* Returns all registered extensions.
|
2011-02-05 18:42:07 +00:00
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @return ExtensionInterface[] An array of ExtensionInterface
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-02-05 18:42:07 +00:00
|
|
|
*/
|
2011-02-15 06:52:11 +00:00
|
|
|
public function getExtensions()
|
2011-02-05 18:42:07 +00:00
|
|
|
{
|
2011-02-15 06:52:11 +00:00
|
|
|
return $this->extensions;
|
2011-02-05 18:42:07 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Checks if we have an extension.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the extension
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-04-27 06:25:26 +01:00
|
|
|
* @return Boolean If the extension exists
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-02-15 06:52:11 +00:00
|
|
|
public function hasExtension($name)
|
2010-07-16 08:12:58 +01:00
|
|
|
{
|
2011-02-15 06:52:11 +00:00
|
|
|
return isset($this->extensions[$name]) || isset($this->extensionsByNs[$name]);
|
2011-02-15 04:58:18 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 14:11:33 +01:00
|
|
|
/**
|
|
|
|
* Returns an array of resources loaded to build this configuration.
|
|
|
|
*
|
|
|
|
* @return ResourceInterface[] An array of resources
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-07-15 14:11:33 +01:00
|
|
|
*/
|
|
|
|
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
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-07-15 14:11:33 +01:00
|
|
|
*/
|
|
|
|
public function addResource(ResourceInterface $resource)
|
|
|
|
{
|
|
|
|
$this->resources[] = $resource;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:08:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the resources for this configuration.
|
2012-12-11 10:49:22 +00:00
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @param ResourceInterface[] $resources An array of resources
|
2012-12-11 10:49:22 +00:00
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @return ContainerBuilder The current instance
|
2012-12-11 10:49:22 +00:00
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @api
|
|
|
|
*/
|
2011-09-16 12:43:09 +01:00
|
|
|
public function setResources(array $resources)
|
|
|
|
{
|
|
|
|
$this->resources = $resources;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-07-15 14:11:33 +01:00
|
|
|
/**
|
|
|
|
* Adds the object class hierarchy as resources.
|
|
|
|
*
|
|
|
|
* @param object $object An object instance
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
2012-11-16 13:04:00 +00:00
|
|
|
* @return ContainerBuilder The current instance
|
|
|
|
*
|
2011-07-20 09:50:27 +01:00
|
|
|
* @api
|
2010-07-15 14:11:33 +01:00
|
|
|
*/
|
|
|
|
public function addObjectResource($object)
|
|
|
|
{
|
|
|
|
$parent = new \ReflectionObject($object);
|
2010-12-23 08:46:49 +00:00
|
|
|
do {
|
2010-07-15 14:11:33 +01:00
|
|
|
$this->addResource(new FileResource($parent->getFileName()));
|
2010-12-23 08:46:49 +00:00
|
|
|
} while ($parent = $parent->getParentClass());
|
2012-11-16 13:04:00 +00:00
|
|
|
|
|
|
|
return $this;
|
2010-07-15 14:11:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the configuration for an extension.
|
|
|
|
*
|
2010-07-20 15:40:57 +01:00
|
|
|
* @param string $extension The extension alias or namespace
|
|
|
|
* @param array $values An array of values that customizes the extension
|
2010-07-15 14:11:33 +01:00
|
|
|
*
|
|
|
|
* @return ContainerBuilder The current instance
|
2011-12-05 00:09:11 +00:00
|
|
|
* @throws BadMethodCallException When this ContainerBuilder is frozen
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
2012-09-07 23:56:45 +01:00
|
|
|
* @throws \LogicException if the container is frozen
|
|
|
|
*
|
2011-07-20 09:50:27 +01:00
|
|
|
* @api
|
2010-07-15 14:11:33 +01:00
|
|
|
*/
|
2011-02-05 18:42:07 +00:00
|
|
|
public function loadFromExtension($extension, array $values = array())
|
2010-07-15 14:11:33 +01:00
|
|
|
{
|
2011-12-05 00:09:11 +00:00
|
|
|
if ($this->isFrozen()) {
|
|
|
|
throw new BadMethodCallException('Cannot load from an extension on a frozen container.');
|
2010-07-16 08:15:22 +01:00
|
|
|
}
|
|
|
|
|
2010-08-24 15:25:08 +01:00
|
|
|
$namespace = $this->getExtension($extension)->getAlias();
|
2010-07-15 14:11:33 +01:00
|
|
|
|
2011-07-09 17:03:05 +01:00
|
|
|
$this->extensionConfigs[$namespace][] = $values;
|
2010-07-15 14:11:33 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-12-14 23:25:33 +00:00
|
|
|
/**
|
2011-03-03 12:53:29 +00:00
|
|
|
* Adds a compiler pass.
|
2010-12-14 23:25:33 +00:00
|
|
|
*
|
2011-03-03 12:53:29 +00:00
|
|
|
* @param CompilerPassInterface $pass A compiler pass
|
|
|
|
* @param string $type The type of compiler pass
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
2012-11-16 13:04:00 +00:00
|
|
|
* @return ContainerBuilder The current instance
|
|
|
|
*
|
2011-07-20 09:50:27 +01:00
|
|
|
* @api
|
2010-12-14 23:25:33 +00:00
|
|
|
*/
|
2011-01-16 07:53:28 +00:00
|
|
|
public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)
|
2010-12-14 23:25:33 +00:00
|
|
|
{
|
2011-03-08 13:34:07 +00:00
|
|
|
if (null === $this->compiler) {
|
|
|
|
$this->compiler = new Compiler();
|
|
|
|
}
|
|
|
|
|
2011-01-16 07:53:28 +00:00
|
|
|
$this->compiler->addPass($pass, $type);
|
2011-01-16 07:38:41 +00:00
|
|
|
|
|
|
|
$this->addObjectResource($pass);
|
2012-11-16 13:04:00 +00:00
|
|
|
|
|
|
|
return $this;
|
2010-12-14 23:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-03 12:53:29 +00:00
|
|
|
* Returns the compiler pass config which can then be modified.
|
2010-12-14 23:25:33 +00:00
|
|
|
*
|
2011-03-03 12:53:29 +00:00
|
|
|
* @return PassConfig The compiler pass config
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-12-14 23:25:33 +00:00
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function getCompilerPassConfig()
|
2010-12-14 23:25:33 +00:00
|
|
|
{
|
2011-03-08 13:34:07 +00:00
|
|
|
if (null === $this->compiler) {
|
|
|
|
$this->compiler = new Compiler();
|
|
|
|
}
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
return $this->compiler->getPassConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-03 12:53:29 +00:00
|
|
|
* Returns the compiler.
|
2011-01-09 09:53:46 +00:00
|
|
|
*
|
2011-03-03 12:53:29 +00:00
|
|
|
* @return Compiler The compiler
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-09 09:53:46 +00:00
|
|
|
*/
|
|
|
|
public function getCompiler()
|
|
|
|
{
|
2011-03-08 13:34:07 +00:00
|
|
|
if (null === $this->compiler) {
|
|
|
|
$this->compiler = new Compiler();
|
|
|
|
}
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
return $this->compiler;
|
2010-12-14 23:25:33 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Returns all Scopes.
|
|
|
|
*
|
|
|
|
* @return array An array of scopes
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-17 22:28:59 +00:00
|
|
|
public function getScopes()
|
|
|
|
{
|
|
|
|
return $this->scopes;
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
2011-05-29 15:19:02 +01:00
|
|
|
* Returns all Scope children.
|
2011-02-13 18:06:41 +00:00
|
|
|
*
|
|
|
|
* @return array An array of scope children.
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-17 22:28:59 +00:00
|
|
|
public function getScopeChildren()
|
|
|
|
{
|
|
|
|
return $this->scopeChildren;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sets a service.
|
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
|
|
|
* @param object $service The service instance
|
2011-02-13 18:06:41 +00:00
|
|
|
* @param string $scope The scope
|
2010-10-02 15:47:35 +01:00
|
|
|
*
|
2011-12-04 23:51:22 +00:00
|
|
|
* @throws BadMethodCallException When this ContainerBuilder is frozen
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-01-17 22:28:59 +00:00
|
|
|
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-10-02 15:47:35 +01:00
|
|
|
if ($this->isFrozen()) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new BadMethodCallException('Setting service on a frozen container is not allowed');
|
2010-10-02 15:47:35 +01:00
|
|
|
}
|
|
|
|
|
2011-01-03 08:07:06 +00:00
|
|
|
$id = strtolower($id);
|
|
|
|
|
2010-12-23 08:46:49 +00:00
|
|
|
unset($this->definitions[$id], $this->aliases[$id]);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
parent::set($id, $service, $scope);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-09-02 11:20:20 +01:00
|
|
|
/**
|
2011-04-19 22:40:18 +01:00
|
|
|
* Removes a service definition.
|
2010-09-02 11:20:20 +01:00
|
|
|
*
|
|
|
|
* @param string $id The service identifier
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-02 11:20:20 +01:00
|
|
|
*/
|
2011-04-19 22:40:18 +01:00
|
|
|
public function removeDefinition($id)
|
2010-09-02 11:20:20 +01:00
|
|
|
{
|
2011-01-03 08:07:06 +00:00
|
|
|
unset($this->definitions[strtolower($id)]);
|
2010-09-02 11:20:20 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the given service is defined.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $id The service identifier
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return Boolean true if the service is defined, false otherwise
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-06-27 17:28:29 +01:00
|
|
|
public function has($id)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-01-03 08:07:06 +00:00
|
|
|
$id = strtolower($id);
|
|
|
|
|
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.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $id The service identifier
|
|
|
|
* @param integer $invalidBehavior The behavior when the service does not exist
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return object The associated service
|
|
|
|
*
|
2011-12-04 23:51:22 +00:00
|
|
|
* @throws InvalidArgumentException if the service is not defined
|
|
|
|
* @throws LogicException if the service has a circular reference to itself
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @see Reference
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
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
|
|
|
{
|
2011-01-03 08:07:06 +00:00
|
|
|
$id = strtolower($id);
|
|
|
|
|
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);
|
2011-12-04 23:51:22 +00:00
|
|
|
} catch (InvalidArgumentException $e) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (isset($this->loading[$id])) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2011-12-04 23:51:22 +00: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.
|
2011-02-13 18:06:41 +00:00
|
|
|
*
|
|
|
|
* @param ContainerBuilder $container The ContainerBuilder instance to merge.
|
2011-12-05 00:09:11 +00:00
|
|
|
*
|
2011-12-13 15:12:53 +00:00
|
|
|
*
|
2011-12-05 00:09:11 +00:00
|
|
|
* @throws BadMethodCallException When this ContainerBuilder is frozen
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-07-15 14:11:33 +01:00
|
|
|
public function merge(ContainerBuilder $container)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-12-05 00:09:11 +00:00
|
|
|
if ($this->isFrozen()) {
|
|
|
|
throw new BadMethodCallException('Cannot merge on a frozen container.');
|
2010-07-16 08:15:22 +01:00
|
|
|
}
|
|
|
|
|
2010-07-15 14:11:33 +01:00
|
|
|
$this->addDefinitions($container->getDefinitions());
|
|
|
|
$this->addAliases($container->getAliases());
|
2011-03-11 13:50:46 +00:00
|
|
|
$this->getParameterBag()->add($container->getParameterBag()->all());
|
2010-07-15 14:11:33 +01:00
|
|
|
|
|
|
|
foreach ($container->getResources() as $resource) {
|
|
|
|
$this->addResource($resource);
|
|
|
|
}
|
|
|
|
|
2011-02-15 06:52:11 +00:00
|
|
|
foreach ($this->extensions as $name => $extension) {
|
2011-02-15 04:58:18 +00:00
|
|
|
if (!isset($this->extensionConfigs[$name])) {
|
|
|
|
$this->extensionConfigs[$name] = array();
|
2010-07-15 14:11:33 +01:00
|
|
|
}
|
2011-02-15 04:58:18 +00:00
|
|
|
|
|
|
|
$this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $container->getExtensionConfig($name));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-07-15 14:11:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-15 04:58:18 +00:00
|
|
|
* Returns the configuration array for the given extension.
|
2010-07-15 14:11:33 +01:00
|
|
|
*
|
2011-02-15 04:58:18 +00:00
|
|
|
* @param string $name The name of the extension
|
2011-02-05 18:42:07 +00:00
|
|
|
*
|
2011-02-15 04:58:18 +00:00
|
|
|
* @return array An array of configuration
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-02-05 18:42:07 +00:00
|
|
|
*/
|
|
|
|
public function getExtensionConfig($name)
|
|
|
|
{
|
2011-02-15 04:58:18 +00:00
|
|
|
if (!isset($this->extensionConfigs[$name])) {
|
2011-03-03 12:53:29 +00:00
|
|
|
$this->extensionConfigs[$name] = array();
|
2011-02-05 18:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->extensionConfigs[$name];
|
|
|
|
}
|
|
|
|
|
2012-09-20 22:20:53 +01:00
|
|
|
/**
|
|
|
|
* Prepends a config array to the configs of the given extension.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the extension
|
|
|
|
* @param array $config The config to set
|
|
|
|
*/
|
|
|
|
public function prependExtensionConfig($name, array $config)
|
|
|
|
{
|
|
|
|
if (!isset($this->extensionConfigs[$name])) {
|
|
|
|
$this->extensionConfigs[$name] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
array_unshift($this->extensionConfigs[$name], $config);
|
|
|
|
}
|
|
|
|
|
2010-07-15 14:11:33 +01:00
|
|
|
/**
|
2011-01-16 07:12:36 +00:00
|
|
|
* Compiles the container.
|
2010-07-16 08:15:22 +01:00
|
|
|
*
|
2011-01-16 07:12:36 +00:00
|
|
|
* This method passes the container to compiler
|
|
|
|
* passes whose job is to manipulate and optimize
|
|
|
|
* the container.
|
|
|
|
*
|
|
|
|
* The main compiler passes roughly do four things:
|
2010-07-16 08:15:22 +01:00
|
|
|
*
|
|
|
|
* * The extension configurations are merged;
|
|
|
|
* * Parameter values are resolved;
|
2011-01-16 07:12:36 +00:00
|
|
|
* * The parameter bag is frozen;
|
2010-07-16 08:15:22 +01:00
|
|
|
* * Extension loading is disabled.
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-07-15 14:11:33 +01:00
|
|
|
*/
|
2011-01-16 07:12:36 +00:00
|
|
|
public function compile()
|
2010-07-15 14:11:33 +01:00
|
|
|
{
|
2011-03-08 13:34:07 +00:00
|
|
|
if (null === $this->compiler) {
|
|
|
|
$this->compiler = new Compiler();
|
|
|
|
}
|
|
|
|
|
2011-03-03 12:53:29 +00:00
|
|
|
foreach ($this->compiler->getPassConfig()->getPasses() as $pass) {
|
|
|
|
$this->addObjectResource($pass);
|
2011-01-27 19:42:41 +00:00
|
|
|
}
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
$this->compiler->compile($this);
|
2010-10-02 15:47:35 +01:00
|
|
|
|
2011-02-15 04:58:18 +00:00
|
|
|
$this->extensionConfigs = array();
|
2011-01-16 09:17:38 +00:00
|
|
|
|
2011-01-16 07:12:36 +00:00
|
|
|
parent::compile();
|
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
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @param array $aliases An array of aliases
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2012-09-07 23:56:45 +01:00
|
|
|
* @param string $alias The alias to create
|
|
|
|
* @param string|Alias $id The service to alias
|
|
|
|
*
|
2012-12-16 12:02:54 +00:00
|
|
|
* @throws InvalidArgumentException if the id is not a string or an Alias
|
|
|
|
* @throws InvalidArgumentException if the alias is for itself
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setAlias($alias, $id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-01-03 08:07:06 +00:00
|
|
|
$alias = strtolower($alias);
|
2011-01-07 14:44:29 +00:00
|
|
|
|
|
|
|
if (is_string($id)) {
|
|
|
|
$id = new Alias($id);
|
2011-12-18 13:42:59 +00:00
|
|
|
} elseif (!$id instanceof Alias) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException('$id must be a string, or an Alias object.');
|
2011-01-07 14:44:29 +00:00
|
|
|
}
|
2011-01-03 08:07:06 +00:00
|
|
|
|
2011-02-12 14:43:38 +00:00
|
|
|
if ($alias === strtolower($id)) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException('An alias can not reference itself, got a circular reference on "'.$alias.'".');
|
2011-02-12 14:43:38 +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
|
|
|
|
2010-09-02 11:20:20 +01:00
|
|
|
/**
|
|
|
|
* Removes an alias.
|
|
|
|
*
|
|
|
|
* @param string $alias The alias to remove
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-02 11:20:20 +01:00
|
|
|
*/
|
|
|
|
public function removeAlias($alias)
|
|
|
|
{
|
2011-01-03 08:07:06 +00:00
|
|
|
unset($this->aliases[strtolower($alias)]);
|
2010-09-02 11:20:20 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns true if an alias exists under the given identifier.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $id The service identifier
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return Boolean true if the alias exists, false otherwise
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function hasAlias($id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-01-07 14:44:29 +00:00
|
|
|
return isset($this->aliases[strtolower($id)]);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets all defined aliases.
|
|
|
|
*
|
2012-09-07 23:56:45 +01:00
|
|
|
* @return Alias[] An array of aliases
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $id The service identifier
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2012-08-03 10:22:09 +01:00
|
|
|
* @return Alias An Alias instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-12-04 23:51:22 +00:00
|
|
|
* @throws InvalidArgumentException if the alias does not exist
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getAlias($id)
|
|
|
|
{
|
2011-01-03 08:07:06 +00:00
|
|
|
$id = strtolower($id);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$this->hasAlias($id)) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
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.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $id The service identifier
|
|
|
|
* @param string $class The service class
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return Definition A Definition instance
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-07-04 17:56:48 +01:00
|
|
|
public function register($id, $class = null)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-01-03 08:07:06 +00:00
|
|
|
return $this->setDefinition(strtolower($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
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @param Definition[] $definitions An array of service definitions
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2012-09-07 23:56:45 +01:00
|
|
|
* @return Definition[] An array of Definition instances
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $id The service identifier
|
|
|
|
* @param Definition $definition A Definition instance
|
2010-10-02 15:47:35 +01:00
|
|
|
*
|
2012-09-07 23:56:45 +01:00
|
|
|
* @return Definition the service definition
|
|
|
|
*
|
2011-12-04 23:57:59 +00:00
|
|
|
* @throws BadMethodCallException When this ContainerBuilder is frozen
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function setDefinition($id, Definition $definition)
|
|
|
|
{
|
2010-10-02 15:47:35 +01:00
|
|
|
if ($this->isFrozen()) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
|
2010-10-02 15:47:35 +01:00
|
|
|
}
|
|
|
|
|
2011-01-03 08:07:06 +00:00
|
|
|
$id = strtolower($id);
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
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.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $id The service identifier
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return Boolean true if the service definition exists, false otherwise
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function hasDefinition($id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-01-03 08:07:06 +00:00
|
|
|
return array_key_exists(strtolower($id), $this->definitions);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets a service definition.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $id The service identifier
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return Definition A Definition instance
|
|
|
|
*
|
2011-12-04 23:51:22 +00:00
|
|
|
* @throws InvalidArgumentException if the service definition does not exist
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getDefinition($id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-01-03 08:07:06 +00:00
|
|
|
$id = strtolower($id);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$this->hasDefinition($id)) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
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.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $id The service identifier or alias
|
2010-07-15 14:11:33 +01:00
|
|
|
*
|
|
|
|
* @return Definition A Definition instance
|
|
|
|
*
|
2011-12-04 23:51:22 +00:00
|
|
|
* @throws InvalidArgumentException if the service definition does not exist
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-07-15 14:11:33 +01:00
|
|
|
*/
|
|
|
|
public function findDefinition($id)
|
|
|
|
{
|
2011-07-11 16:55:12 +01:00
|
|
|
while ($this->hasAlias($id)) {
|
|
|
|
$id = (string) $this->getAlias($id);
|
2010-07-15 14:11:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getDefinition($id);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Creates a service for a service definition.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param Definition $definition A service definition instance
|
|
|
|
* @param string $id The service identifier
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2012-09-07 23:56:45 +01:00
|
|
|
* @return object The service described by the service definition
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2012-09-17 21:41:30 +01:00
|
|
|
* @throws RuntimeException When the scope is inactive
|
|
|
|
* @throws RuntimeException When the factory definition is incomplete
|
2011-12-04 23:51:22 +00:00
|
|
|
* @throws InvalidArgumentException When configure callable is not callable
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function createService(Definition $definition, $id)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2012-06-26 18:33:57 +01:00
|
|
|
$parameterBag = $this->getParameterBag();
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $definition->getFile()) {
|
2012-06-26 18:33:57 +01:00
|
|
|
require_once $parameterBag->resolveValue($definition->getFile());
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2012-06-26 18:33:57 +01:00
|
|
|
$arguments = $this->resolveServices(
|
|
|
|
$parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments()))
|
|
|
|
);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-07-05 08:47:14 +01:00
|
|
|
if (null !== $definition->getFactoryMethod()) {
|
2011-02-02 17:22:27 +00:00
|
|
|
if (null !== $definition->getFactoryClass()) {
|
2012-06-26 18:33:57 +01:00
|
|
|
$factory = $parameterBag->resolveValue($definition->getFactoryClass());
|
2011-02-02 17:22:27 +00:00
|
|
|
} elseif (null !== $definition->getFactoryService()) {
|
2012-06-26 18:33:57 +01:00
|
|
|
$factory = $this->get($parameterBag->resolveValue($definition->getFactoryService()));
|
2010-07-03 23:35:39 +01:00
|
|
|
} else {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new RuntimeException('Cannot create service from factory method without a factory service or factory class.');
|
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 {
|
2012-06-26 18:33:57 +01:00
|
|
|
$r = new \ReflectionClass($parameterBag->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
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
if (self::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
|
|
|
|
if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new RuntimeException('You tried to create a service of an inactive scope.');
|
2011-01-17 22:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->services[$lowerId = strtolower($id)] = $service;
|
|
|
|
|
|
|
|
if (self::SCOPE_CONTAINER !== $scope) {
|
|
|
|
$this->scopedServices[$scope][$lowerId] = $service;
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
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) {
|
2012-06-26 18:33:57 +01:00
|
|
|
call_user_func_array(array($service, $call[0]), $this->resolveServices($parameterBag->resolveValue($call[1])));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 18:33:57 +01:00
|
|
|
$properties = $this->resolveServices($parameterBag->resolveValue($definition->getProperties()));
|
2011-05-03 22:46:02 +01:00
|
|
|
foreach ($properties as $name => $value) {
|
2011-06-27 07:28:54 +01:00
|
|
|
$service->$name = $value;
|
2011-05-03 22:46:02 +01:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($callable = $definition->getConfigurator()) {
|
2012-11-18 20:27:35 +00:00
|
|
|
if (is_array($callable)) {
|
|
|
|
$callable[0] = $callable[0] instanceof Reference ? $this->get((string) $callable[0]) : $parameterBag->resolveValue($callable[0]);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_callable($callable)) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service)));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param mixed $value A value
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
}
|
2012-11-18 20:27:35 +00:00
|
|
|
} elseif ($value instanceof Reference) {
|
2010-06-27 17:28:29 +01:00
|
|
|
$value = $this->get((string) $value, $value->getInvalidBehavior());
|
2012-11-18 20:27:35 +00:00
|
|
|
} elseif ($value instanceof Definition) {
|
2011-05-23 21:00:38 +01:00
|
|
|
$value = $this->createService($value, null);
|
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
|
2011-07-20 09:50:27 +01:00
|
|
|
*
|
|
|
|
* @api
|
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
|
|
|
}
|
|
|
|
|
2012-09-16 16:15:14 +01:00
|
|
|
/**
|
|
|
|
* Returns all tags the defined services use.
|
|
|
|
*
|
|
|
|
* @return array An array of tags
|
|
|
|
*/
|
|
|
|
public function findTags()
|
|
|
|
{
|
|
|
|
$tags = array();
|
|
|
|
foreach ($this->getDefinitions() as $id => $definition) {
|
|
|
|
$tags = array_merge(array_keys($definition->getTags()), $tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($tags);
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the Service Conditionals.
|
|
|
|
*
|
|
|
|
* @param mixed $value An array of conditionals to return.
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return array An array of Service conditionals
|
|
|
|
*/
|
2012-07-09 13:50:58 +01:00
|
|
|
public static function getServiceConditionals($value)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$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)));
|
|
|
|
}
|
2012-11-18 20:27:35 +00:00
|
|
|
} elseif ($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
|
|
|
}
|
|
|
|
}
|