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\Loader;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-01-26 23:14:31 +00:00
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
2011-01-07 14:44:29 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Alias;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2010-10-02 15:47:35 +01:00
|
|
|
use Symfony\Component\DependencyInjection\InterfaceInjector;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* YamlFileLoader loads YAML files service definitions.
|
|
|
|
*
|
|
|
|
* The YAML format does not support anonymous services (cf. the XML loader).
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
|
|
|
class YamlFileLoader extends FileLoader
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-10-26 15:01:39 +01:00
|
|
|
* Loads a Yaml file.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-07-16 08:12:58 +01:00
|
|
|
* @param mixed $resource The resource
|
2011-02-10 15:15:51 +00:00
|
|
|
* @param string $type The resource type
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-02-10 15:15:51 +00:00
|
|
|
public function load($file, $type = null)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-02-09 23:44:02 +00:00
|
|
|
$path = $this->locator->locate($file);
|
2010-01-17 10:21:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$content = $this->loadFile($path);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->container->addResource(new FileResource($path));
|
2010-01-17 10:21:31 +00:00
|
|
|
|
2010-12-12 07:39:37 +00:00
|
|
|
// empty file
|
|
|
|
if (null === $content) {
|
2010-07-16 08:12:58 +01:00
|
|
|
return;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// imports
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->parseImports($content, $file);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// parameters
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($content['parameters'])) {
|
2010-05-08 14:32:30 +01:00
|
|
|
foreach ($content['parameters'] as $key => $value) {
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->container->setParameter($key, $this->resolveServices($value));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-12-13 00:20:21 +00:00
|
|
|
// extensions
|
|
|
|
$this->loadFromExtensions($content);
|
|
|
|
|
2010-10-02 15:47:35 +01:00
|
|
|
// interface injectors
|
|
|
|
$this->parseInterfaceInjectors($content, $file);
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// services
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->parseDefinitions($content, $file);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-18 10:26:47 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this class supports the given resource.
|
|
|
|
*
|
2011-02-10 15:15:51 +00:00
|
|
|
* @param mixed $resource A resource
|
|
|
|
* @param string $type The resource type
|
2010-07-18 10:26:47 +01:00
|
|
|
*
|
|
|
|
* @return Boolean true if this class supports the given resource, false otherwise
|
|
|
|
*/
|
2011-02-10 15:15:51 +00:00
|
|
|
public function supports($resource, $type = null)
|
2010-07-18 10:26:47 +01:00
|
|
|
{
|
|
|
|
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Parses all imports
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param array $content
|
|
|
|
* @param string $file
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function parseImports($content, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!isset($content['imports'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($content['imports'] as $import) {
|
2011-03-23 18:34:00 +00:00
|
|
|
$this->setCurrentDir(dirname($file));
|
2010-08-24 15:36:29 +01:00
|
|
|
$this->import($import['resource'], isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Parses interface injectors.
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param array $content
|
|
|
|
* @param string $file
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function parseInterfaceInjectors($content, $file)
|
2010-10-02 15:47:35 +01:00
|
|
|
{
|
|
|
|
if (!isset($content['interfaces'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($content['interfaces'] as $class => $interface) {
|
|
|
|
$this->parseInterfaceInjector($class, $interface, $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Parses an interface injector.
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param array $interface
|
|
|
|
* @param string $file
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function parseInterfaceInjector($class, $interface, $file)
|
2010-10-02 15:47:35 +01:00
|
|
|
{
|
|
|
|
$injector = new InterfaceInjector($class);
|
|
|
|
if (isset($interface['calls'])) {
|
|
|
|
foreach ($interface['calls'] as $call) {
|
|
|
|
$injector->addMethodCall($call[0], $this->resolveServices($call[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->container->addInterfaceInjector($injector);
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Parses definitions
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param array $content
|
|
|
|
* @param string $file
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function parseDefinitions($content, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!isset($content['services'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($content['services'] as $id => $service) {
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->parseDefinition($id, $service, $file);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Parses a definition.
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param string $id
|
|
|
|
* @param array $service
|
|
|
|
* @param string $file
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function parseDefinition($id, $service, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_string($service) && 0 === strpos($service, '@')) {
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->container->setAlias($id, substr($service, 1));
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-01-07 14:44:29 +00:00
|
|
|
return;
|
|
|
|
} else if (isset($service['alias'])) {
|
|
|
|
$public = !array_key_exists('public', $service) || (Boolean) $service['public'];
|
|
|
|
$this->container->setAlias($id, new Alias($service['alias'], $public));
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-01-26 23:14:31 +00:00
|
|
|
if (isset($service['parent'])) {
|
|
|
|
$definition = new DefinitionDecorator($service['parent']);
|
|
|
|
} else {
|
|
|
|
$definition = new Definition();
|
|
|
|
}
|
2010-07-04 17:56:48 +01:00
|
|
|
|
|
|
|
if (isset($service['class'])) {
|
|
|
|
$definition->setClass($service['class']);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
if (isset($service['scope'])) {
|
|
|
|
$definition->setScope($service['scope']);
|
2011-01-23 11:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($service['synthetic'])) {
|
|
|
|
$definition->setSynthetic($service['synthetic']);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-12-29 19:12:24 +00:00
|
|
|
if (isset($service['public'])) {
|
|
|
|
$definition->setPublic($service['public']);
|
|
|
|
}
|
|
|
|
|
2011-01-26 23:14:31 +00:00
|
|
|
if (isset($service['abstract'])) {
|
|
|
|
$definition->setAbstract($service['abstract']);
|
|
|
|
}
|
|
|
|
|
2011-02-02 17:22:27 +00:00
|
|
|
if (isset($service['factory_class'])) {
|
|
|
|
$definition->setFactoryClass($service['factory_class']);
|
|
|
|
}
|
|
|
|
|
2010-07-05 08:47:14 +01:00
|
|
|
if (isset($service['factory_method'])) {
|
|
|
|
$definition->setFactoryMethod($service['factory_method']);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-07-04 17:56:48 +01:00
|
|
|
if (isset($service['factory_service'])) {
|
|
|
|
$definition->setFactoryService($service['factory_service']);
|
2010-07-03 23:35:39 +01:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($service['file'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->setFile($service['file']);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($service['arguments'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->setArguments($this->resolveServices($service['arguments']));
|
|
|
|
}
|
2010-02-09 08:45:23 +00:00
|
|
|
|
2011-03-04 14:26:00 +00:00
|
|
|
if (isset($service['properties'])) {
|
|
|
|
$definition->setProperties($this->resolveServices($service['properties']));
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($service['configurator'])) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (is_string($service['configurator'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->setConfigurator($service['configurator']);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
|
|
|
|
}
|
|
|
|
}
|
2010-02-09 08:45:23 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($service['calls'])) {
|
2010-05-08 14:32:30 +01:00
|
|
|
foreach ($service['calls'] as $call) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->addMethodCall($call[0], $this->resolveServices($call[1]));
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
if (isset($service['tags'])) {
|
2011-02-17 14:31:50 +00:00
|
|
|
if (!is_array($service['tags'])) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file));
|
|
|
|
}
|
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
foreach ($service['tags'] as $tag) {
|
2011-02-17 14:31:50 +00:00
|
|
|
if (!isset($tag['name'])) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key must be an array for service "%s" in %s.', $id, $file));
|
|
|
|
}
|
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
$name = $tag['name'];
|
|
|
|
unset($tag['name']);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
$definition->addTag($name, $tag);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->container->setDefinition($id, $definition);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Loads a YAML file.
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param string $file
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return array The file content
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function loadFile($file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->validate(Yaml::load($file), $file);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2011-02-13 18:06:41 +00:00
|
|
|
* Validates a YAML file.
|
|
|
|
*
|
|
|
|
* @param mixed $content
|
|
|
|
* @param string $file
|
|
|
|
* @return array
|
|
|
|
*
|
2010-05-06 12:25:53 +01:00
|
|
|
* @throws \InvalidArgumentException When service file is not valid
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function validate($content, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null === $content) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $content;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_array($content)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-02-05 18:42:07 +00:00
|
|
|
foreach (array_keys($content) as $namespace) {
|
|
|
|
if (in_array($namespace, array('imports', 'parameters', 'services', 'interfaces'))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-02-05 18:42:07 +00:00
|
|
|
if (!$this->container->hasExtension($namespace)) {
|
2011-02-15 04:58:18 +00:00
|
|
|
throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $namespace, $file));
|
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 $content;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Resolves services.
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param string $value
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private 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-06 12:25:53 +01:00
|
|
|
$value = array_map(array($this, 'resolveServices'), $value);
|
2011-01-17 22:28:59 +00:00
|
|
|
} else if (is_string($value) && 0 === strpos($value, '@')) {
|
|
|
|
if (0 === strpos($value, '@?')) {
|
|
|
|
$value = substr($value, 2);
|
|
|
|
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
|
|
|
|
} else {
|
|
|
|
$value = substr($value, 1);
|
|
|
|
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('=' === substr($value, -1)) {
|
|
|
|
$value = substr($value, 0, -1);
|
|
|
|
$strict = false;
|
|
|
|
} else {
|
|
|
|
$strict = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = new Reference($value, $invalidBehavior, $strict);
|
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 $value;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Loads from Extensions
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param array $content
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function loadFromExtensions($content)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-02-05 18:42:07 +00:00
|
|
|
foreach ($content as $namespace => $values) {
|
|
|
|
if (in_array($namespace, array('imports', 'parameters', 'services', 'interfaces'))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_array($values)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$values = array();
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-02-05 18:42:07 +00:00
|
|
|
$this->container->loadFromExtension($namespace, $values);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
}
|