2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\DependencyInjection\Loader;
|
|
|
|
|
2010-06-27 17:28:29 +01:00
|
|
|
use Symfony\Components\DependencyInjection\ContainerInterface;
|
2010-01-04 14:26:20 +00:00
|
|
|
use Symfony\Components\DependencyInjection\Definition;
|
|
|
|
use Symfony\Components\DependencyInjection\Reference;
|
2010-07-15 14:11:33 +01:00
|
|
|
use Symfony\Components\DependencyInjection\ContainerBuilder;
|
2010-06-27 17:28:29 +01:00
|
|
|
use Symfony\Components\DependencyInjection\Resource\FileResource;
|
2010-02-11 13:40:29 +00:00
|
|
|
use Symfony\Components\Yaml\Yaml;
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* YamlFileLoader loads YAML files service definitions.
|
|
|
|
*
|
|
|
|
* The YAML format does not support anonymous services (cf. the XML loader).
|
|
|
|
*
|
2010-04-07 01:51:29 +01:00
|
|
|
* @package Symfony
|
|
|
|
* @subpackage Components_DependencyInjection
|
2010-01-04 14:26:20 +00:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class YamlFileLoader extends FileLoader
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Loads an array of Yaml files.
|
|
|
|
*
|
2010-07-16 08:12:58 +01:00
|
|
|
* @param mixed $resource The resource
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-07-16 08:12:58 +01:00
|
|
|
public function load($file)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$path = $this->findFile($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-05-07 15:09:11 +01:00
|
|
|
if (!$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-06-22 16:27:49 +01:00
|
|
|
// extensions
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->loadFromExtensions($content);
|
2010-06-22 16:27:49 +01: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-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.
|
|
|
|
*
|
|
|
|
* @param mixed $resource A resource
|
|
|
|
*
|
|
|
|
* @return Boolean true if this class supports the given resource, false otherwise
|
|
|
|
*/
|
|
|
|
public function supports($resource)
|
|
|
|
{
|
|
|
|
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
|
|
|
|
}
|
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
protected 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) {
|
2010-07-18 10:26:47 +01:00
|
|
|
$this->currentDir = dirname($file);
|
|
|
|
$this->import($import['resource']);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
protected 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
|
|
|
}
|
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
protected 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
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-07-04 17:56:48 +01:00
|
|
|
$definition = new Definition();
|
|
|
|
|
|
|
|
if (isset($service['class'])) {
|
|
|
|
$definition->setClass($service['class']);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($service['shared'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->setShared($service['shared']);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
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
|
|
|
|
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-05-07 15:09:11 +01:00
|
|
|
if (isset($service['annotations'])) {
|
2010-05-08 14:32:30 +01:00
|
|
|
foreach ($service['annotations'] as $annotation) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$name = $annotation['name'];
|
|
|
|
unset($annotation['name']);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->addAnnotation($name, $annotation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->container->setDefinition($id, $definition);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected 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
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When service file is not valid
|
|
|
|
*/
|
|
|
|
protected 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
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach (array_keys($content) as $key) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (in_array($key, array('imports', 'parameters', 'services'))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// can it be handled by an extension?
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false !== strpos($key, '.')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
list($namespace, $tag) = explode('.', $key);
|
2010-07-16 08:12:58 +01:00
|
|
|
if (!$this->container->hasExtension($namespace)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $key, $file));
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException(sprintf('The "%s" tag is not valid (in %s).', $key, $file));
|
|
|
|
}
|
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
|
|
|
|
|
|
|
protected 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);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else if (is_string($value) && 0 === strpos($value, '@@')) {
|
2010-06-27 17:28:29 +01:00
|
|
|
$value = new Reference(substr($value, 2), ContainerInterface::IGNORE_ON_INVALID_REFERENCE);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else if (is_string($value) && 0 === strpos($value, '@')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$value = new Reference(substr($value, 1));
|
|
|
|
}
|
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
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
protected function loadFromExtensions($content)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($content as $key => $values) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (in_array($key, array('imports', 'parameters', 'services'))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
list($namespace, $tag) = explode('.', $key);
|
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
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->container->loadFromExtension($this->container->getExtension($namespace), $tag, $values);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
}
|