2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\DependencyInjection\Loader;
|
|
|
|
|
|
|
|
use Symfony\Components\DependencyInjection\Container;
|
|
|
|
use Symfony\Components\DependencyInjection\Definition;
|
|
|
|
use Symfony\Components\DependencyInjection\Reference;
|
|
|
|
use Symfony\Components\DependencyInjection\BuilderConfiguration;
|
2010-01-17 10:21:31 +00:00
|
|
|
use Symfony\Components\DependencyInjection\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.
|
|
|
|
*
|
|
|
|
* @param string $file A YAML file path
|
|
|
|
*
|
|
|
|
* @return BuilderConfiguration A BuilderConfiguration instance
|
|
|
|
*/
|
|
|
|
public function load($file)
|
|
|
|
{
|
|
|
|
$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-05-06 12:25:53 +01:00
|
|
|
$configuration = new BuilderConfiguration();
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$configuration->addResource(new FileResource($path));
|
2010-01-17 10:21:31 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$content) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $configuration;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// imports
|
|
|
|
$this->parseImports($configuration, $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-06 12:25:53 +01:00
|
|
|
foreach ($content['parameters'] as $key => $value)
|
|
|
|
{
|
|
|
|
$configuration->setParameter(strtolower($key), $this->resolveServices($value));
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// services
|
|
|
|
$this->parseDefinitions($configuration, $content, $file);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// extensions
|
|
|
|
$this->loadFromExtensions($configuration, $content);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $configuration;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function parseImports(BuilderConfiguration $configuration, $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-05-06 12:25:53 +01:00
|
|
|
$configuration->merge($this->parseImport($import, $file));
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function parseImport($import, $file)
|
|
|
|
{
|
|
|
|
$class = null;
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($import['class']) && $import['class'] !== get_class($this)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$class = $import['class'];
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
// try to detect loader with the extension
|
2010-05-07 15:09:11 +01:00
|
|
|
switch (pathinfo($import['resource'], PATHINFO_EXTENSION)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
case 'xml':
|
|
|
|
$class = 'Symfony\\Components\\DependencyInjection\\Loader\\XmlFileLoader';
|
|
|
|
break;
|
|
|
|
case 'ini':
|
|
|
|
$class = 'Symfony\\Components\\DependencyInjection\\Loader\\IniFileLoader';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$loader = null === $class ? $this : new $class($this->paths);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$importedFile = $this->getAbsolutePath($import['resource'], dirname($file));
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $loader->load($importedFile);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function parseDefinitions(BuilderConfiguration $configuration, $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-05-06 12:25:53 +01:00
|
|
|
$this->parseDefinition($configuration, $id, $service, $file);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function parseDefinition(BuilderConfiguration $configuration, $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-05-06 12:25:53 +01:00
|
|
|
$configuration->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-05-06 12:25:53 +01:00
|
|
|
$definition = new Definition($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-05-07 15:09:11 +01:00
|
|
|
if (isset($service['constructor'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->setConstructor($service['constructor']);
|
|
|
|
}
|
2010-01-04 14:26:20 +00: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-06 12:25:53 +01:00
|
|
|
if (is_string($service['configurator']))
|
|
|
|
{
|
|
|
|
$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-06 12:25:53 +01:00
|
|
|
foreach ($service['calls'] as $call)
|
|
|
|
{
|
|
|
|
$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-06 12:25:53 +01:00
|
|
|
foreach ($service['annotations'] as $annotation)
|
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$configuration->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-06 12:25:53 +01:00
|
|
|
if (in_array($key, array('imports', 'parameters', 'services')))
|
|
|
|
{
|
|
|
|
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-05-07 15:09:11 +01:00
|
|
|
if (!static::getExtension($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-05-06 12:25:53 +01:00
|
|
|
$value = new Reference(substr($value, 2), Container::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-05-06 12:25:53 +01:00
|
|
|
protected function loadFromExtensions(BuilderConfiguration $configuration, $content)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($content as $key => $values) {
|
2010-05-06 12:25:53 +01:00
|
|
|
if (in_array($key, array('imports', 'parameters', 'services')))
|
|
|
|
{
|
|
|
|
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-05-06 12:25:53 +01:00
|
|
|
$config = static::getExtension($namespace)->load($tag, $values);
|
2010-02-12 16:44:52 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$r = new \ReflectionObject($this->getExtension($namespace));
|
|
|
|
$config->addResource(new FileResource($r->getFileName()));
|
2010-02-12 16:44:52 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$configuration->merge($config);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
}
|