This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Components/DependencyInjection/Loader/YamlFileLoader.php

247 lines
7.7 KiB
PHP
Raw Normal View History

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;
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
/*
* 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).
*
* @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
{
/**
* Loads an array of Yaml files.
*
* @param mixed $resource The resource
* @param Boolean $main Whether this is the main load() call
* @param BuilderConfiguration $configuration A BuilderConfiguration instance to use for the configuration
*
* @return BuilderConfiguration A BuilderConfiguration instance
*/
public function load($file, $main = true, BuilderConfiguration $configuration = null)
{
$path = $this->findFile($file);
$content = $this->loadFile($path);
2010-01-04 14:26:20 +00:00
if (null === $configuration) {
$configuration = new BuilderConfiguration();
}
2010-01-04 14:26:20 +00:00
$configuration->addResource(new FileResource($path));
if (!$content) {
return $configuration;
}
2010-01-04 14:26:20 +00:00
// imports
$this->parseImports($configuration, $content, $file);
2010-01-04 14:26:20 +00:00
// extensions
$this->loadFromExtensions($configuration, $content);
if ($main) {
$configuration->mergeExtensionsConfiguration();
}
// parameters
if (isset($content['parameters'])) {
foreach ($content['parameters'] as $key => $value) {
$configuration->setParameter(strtolower($key), $this->resolveServices($value));
}
}
2010-01-04 14:26:20 +00:00
// services
$this->parseDefinitions($configuration, $content, $file);
2010-01-04 14:26:20 +00:00
return $configuration;
2010-01-04 14:26:20 +00:00
}
protected function parseImports(BuilderConfiguration $configuration, $content, $file)
2010-01-04 14:26:20 +00:00
{
if (!isset($content['imports'])) {
return;
}
2010-01-04 14:26:20 +00:00
foreach ($content['imports'] as $import) {
$this->parseImport($configuration, $import, $file);
}
2010-01-04 14:26:20 +00:00
}
protected function parseImport(BuilderConfiguration $configuration, $import, $file)
{
$class = null;
if (isset($import['class']) && $import['class'] !== get_class($this)) {
$class = $import['class'];
} else {
// try to detect loader with the extension
switch (pathinfo($import['resource'], PATHINFO_EXTENSION)) {
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
$loader = null === $class ? $this : new $class($this->paths);
2010-01-04 14:26:20 +00:00
$importedFile = $this->getAbsolutePath($import['resource'], dirname($file));
2010-01-04 14:26:20 +00:00
return $loader->load($importedFile, false, $configuration);
2010-01-04 14:26:20 +00:00
}
protected function parseDefinitions(BuilderConfiguration $configuration, $content, $file)
2010-01-04 14:26:20 +00:00
{
if (!isset($content['services'])) {
return;
}
2010-01-04 14:26:20 +00:00
foreach ($content['services'] as $id => $service) {
$this->parseDefinition($configuration, $id, $service, $file);
}
2010-01-04 14:26:20 +00:00
}
protected function parseDefinition(BuilderConfiguration $configuration, $id, $service, $file)
2010-01-04 14:26:20 +00:00
{
if (is_string($service) && 0 === strpos($service, '@')) {
$configuration->setAlias($id, substr($service, 1));
2010-01-04 14:26:20 +00:00
return;
}
2010-01-04 14:26:20 +00:00
$definition = new Definition($service['class']);
2010-01-04 14:26:20 +00:00
if (isset($service['shared'])) {
$definition->setShared($service['shared']);
}
2010-01-04 14:26:20 +00:00
if (isset($service['constructor'])) {
$definition->setConstructor($service['constructor']);
}
2010-01-04 14:26:20 +00:00
if (isset($service['file'])) {
$definition->setFile($service['file']);
}
2010-01-04 14:26:20 +00:00
if (isset($service['arguments'])) {
$definition->setArguments($this->resolveServices($service['arguments']));
}
if (isset($service['configurator'])) {
if (is_string($service['configurator'])) {
$definition->setConfigurator($service['configurator']);
} else {
$definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
}
}
if (isset($service['calls'])) {
foreach ($service['calls'] as $call) {
$definition->addMethodCall($call[0], $this->resolveServices($call[1]));
}
}
2010-01-04 14:26:20 +00:00
if (isset($service['annotations'])) {
foreach ($service['annotations'] as $annotation) {
$name = $annotation['name'];
unset($annotation['name']);
2010-01-04 14:26:20 +00:00
$definition->addAnnotation($name, $annotation);
}
}
$configuration->setDefinition($id, $definition);
2010-01-04 14:26:20 +00:00
}
protected function loadFile($file)
2010-01-04 14:26:20 +00:00
{
return $this->validate(Yaml::load($file), $file);
2010-01-04 14:26:20 +00:00
}
/**
* @throws \InvalidArgumentException When service file is not valid
*/
protected function validate($content, $file)
2010-01-04 14:26:20 +00:00
{
if (null === $content) {
return $content;
2010-01-04 14:26:20 +00:00
}
if (!is_array($content)) {
throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
}
2010-01-04 14:26:20 +00:00
foreach (array_keys($content) as $key) {
if (in_array($key, array('imports', 'parameters', 'services'))) {
continue;
}
// can it be handled by an extension?
if (false !== strpos($key, '.')) {
list($namespace, $tag) = explode('.', $key);
if (!static::getExtension($namespace)) {
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
return $content;
2010-01-04 14:26:20 +00:00
}
protected function resolveServices($value)
2010-01-04 14:26:20 +00:00
{
if (is_array($value)) {
$value = array_map(array($this, 'resolveServices'), $value);
} else if (is_string($value) && 0 === strpos($value, '@@')) {
$value = new Reference(substr($value, 2), Container::IGNORE_ON_INVALID_REFERENCE);
} else if (is_string($value) && 0 === strpos($value, '@')) {
$value = new Reference(substr($value, 1));
}
2010-01-04 14:26:20 +00:00
return $value;
}
2010-01-04 14:26:20 +00:00
protected function loadFromExtensions(BuilderConfiguration $configuration, $content)
2010-01-04 14:26:20 +00:00
{
foreach ($content as $key => $values) {
if (in_array($key, array('imports', 'parameters', 'services'))) {
continue;
}
2010-01-04 14:26:20 +00:00
list($namespace, $tag) = explode('.', $key);
2010-01-04 14:26:20 +00:00
if (!is_array($values)) {
$values = array();
}
2010-01-04 14:26:20 +00:00
$configuration->loadFromExtension($this->getExtension($namespace), $tag, $values);
}
2010-01-04 14:26:20 +00:00
}
}