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/Component/DependencyInjection/Loader/YamlFileLoader.php

319 lines
9.0 KiB
PHP
Raw Normal View History

2010-01-04 14:26:20 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
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;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Resource\FileResource;
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).
*
* @author Fabien Potencier <fabien@symfony.com>
2010-01-04 14:26:20 +00:00
*/
class YamlFileLoader extends FileLoader
{
/**
2010-10-26 15:01:39 +01:00
* Loads a Yaml file.
*
2011-04-23 16:05:44 +01:00
* @param mixed $file The resource
* @param string $type The resource type
*/
public function load($file, $type = null)
{
$path = $this->locator->locate($file);
$content = $this->loadFile($path);
2010-01-04 14:26:20 +00:00
$this->container->addResource(new FileResource($path));
// empty file
if (null === $content) {
return;
}
2010-01-04 14:26:20 +00:00
// imports
$this->parseImports($content, $file);
2010-01-04 14:26:20 +00:00
// 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
// extensions
$this->loadFromExtensions($content);
// 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
* @param string $type The resource type
*
* @return Boolean true if this class supports the given resource, false otherwise
*/
public function supports($resource, $type = null)
{
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
}
2011-02-13 18:06:41 +00:00
/**
* Parses all imports
*
* @param array $content
* @param string $file
2011-02-13 18:06:41 +00:00
* @return void
*/
private 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->setCurrentDir(dirname($file));
$this->import($import['resource'], null, isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false, $file);
}
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Parses definitions
*
* @param array $content
* @param string $file
2011-02-13 18:06:41 +00:00
* @return void
*/
private 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
}
2011-02-13 18:06:41 +00:00
/**
* Parses a definition.
*
* @param string $id
* @param array $service
* @param string $file
2011-02-13 18:06:41 +00:00
* @return void
*/
private 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
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));
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();
}
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']);
}
if (isset($service['synthetic'])) {
$definition->setSynthetic($service['synthetic']);
}
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']);
}
if (isset($service['factory_class'])) {
$definition->setFactoryClass($service['factory_class']);
}
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['properties'])) {
$definition->setProperties($this->resolveServices($service['properties']));
}
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
2010-08-05 06:34:53 +01:00
if (isset($service['tags'])) {
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) {
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);
}
}
$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.
*
* @param string $file
2011-02-13 18:06:41 +00:00
* @return array The file content
*/
private 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
}
/**
2011-02-13 18:06:41 +00:00
* Validates a YAML file.
*
* @param mixed $content
* @param string $file
* @return array
*
* @throws \InvalidArgumentException When service file is not valid
*/
private 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 $namespace) {
if (in_array($namespace, array('imports', 'parameters', 'services'))) {
continue;
}
if (!$this->container->hasExtension($namespace)) {
throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $namespace, $file));
}
}
2010-01-04 14:26:20 +00:00
return $content;
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Resolves services.
*
* @param string $value
2011-02-13 18:06:41 +00:00
* @return void
*/
private function resolveServices($value)
2010-01-04 14:26:20 +00:00
{
if (is_array($value)) {
$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-01-04 14:26:20 +00:00
return $value;
}
2010-01-04 14:26:20 +00:00
2011-02-13 18:06:41 +00:00
/**
* Loads from Extensions
*
* @param array $content
2011-02-13 18:06:41 +00:00
* @return void
*/
private function loadFromExtensions($content)
2010-01-04 14:26:20 +00:00
{
foreach ($content as $namespace => $values) {
if (in_array($namespace, array('imports', 'parameters', 'services'))) {
continue;
}
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($namespace, $values);
}
2010-01-04 14:26:20 +00:00
}
}