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

230 lines
6.7 KiB
PHP
Raw Normal View History

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;
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
/*
* 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
*/
public function load($file)
{
$path = $this->findFile($file);
$content = $this->loadFile($path);
2010-01-04 14:26:20 +00:00
$this->container->addResource(new FileResource($path));
if (!$content) {
return;
}
2010-01-04 14:26:20 +00:00
// imports
$this->parseImports($content, $file);
2010-01-04 14:26:20 +00:00
// extensions
$this->loadFromExtensions($content);
// parameters
if (isset($content['parameters'])) {
foreach ($content['parameters'] as $key => $value) {
$this->container->setParameter($key, $this->resolveServices($value));
}
}
2010-01-04 14:26:20 +00:00
// services
$this->parseDefinitions($content, $file);
2010-01-04 14:26:20 +00: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);
}
protected function parseImports($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->currentDir = dirname($file);
$this->import($import['resource']);
}
2010-01-04 14:26:20 +00:00
}
protected function parseDefinitions($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($id, $service, $file);
}
2010-01-04 14:26:20 +00:00
}
protected function parseDefinition($id, $service, $file)
2010-01-04 14:26:20 +00:00
{
if (is_string($service) && 0 === strpos($service, '@')) {
$this->container->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();
if (isset($service['class'])) {
$definition->setClass($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['factory_method'])) {
$definition->setFactoryMethod($service['factory_method']);
}
2010-01-04 14:26:20 +00:00
if (isset($service['factory_service'])) {
$definition->setFactoryService($service['factory_service']);
}
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);
}
}
$this->container->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 (!$this->container->hasExtension($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, '@@')) {
2010-06-27 17:28:29 +01:00
$value = new Reference(substr($value, 2), ContainerInterface::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($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
$this->container->loadFromExtension($this->container->getExtension($namespace), $tag, $values);
}
2010-01-04 14:26:20 +00:00
}
}