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

441 lines
15 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\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\ExpressionLanguage\Expression;
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
{
private $yamlParser;
/**
* {@inheritdoc}
*/
public function load($resource, $type = null)
{
$path = $this->locator->locate($resource);
$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, $path);
2010-01-04 14:26:20 +00:00
// parameters
if (isset($content['parameters'])) {
if (!is_array($content['parameters'])) {
throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $resource));
}
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, $resource);
2010-01-04 14:26:20 +00:00
}
/**
* {@inheritdoc}
*/
public function supports($resource, $type = null)
{
return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true);
}
2011-02-13 18:06:41 +00:00
/**
2014-12-21 17:00:50 +00:00
* Parses all imports.
2011-02-13 18:06:41 +00:00
*
2012-05-18 18:41:48 +01:00
* @param array $content
* @param string $file
2011-02-13 18:06:41 +00:00
*/
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
if (!is_array($content['imports'])) {
throw new InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file));
}
$defaultDirectory = dirname($file);
foreach ($content['imports'] as $import) {
if (!is_array($import)) {
throw new InvalidArgumentException(sprintf('The values in the "imports" key should be arrays in %s. Check your YAML syntax.', $file));
}
$this->setCurrentDir($defaultDirectory);
$this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
}
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
2014-12-21 17:00:50 +00:00
* Parses definitions.
2011-02-13 18:06:41 +00:00
*
2012-05-18 18:41:48 +01:00
* @param array $content
* @param string $file
2011-02-13 18:06:41 +00:00
*/
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
if (!is_array($content['services'])) {
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
}
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
2012-05-18 18:41:48 +01:00
* @param array $service
* @param string $file
2011-12-13 07:50:54 +00:00
*
* @throws InvalidArgumentException When tags are invalid
2011-02-13 18:06:41 +00:00
*/
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;
}
if (!is_array($service)) {
throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', gettype($service), $id, $file));
}
if (isset($service['alias'])) {
$public = !array_key_exists('public', $service) || (bool) $service['public'];
2011-01-07 14:44:29 +00:00
$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
if (isset($service['shared'])) {
$definition->setShared($service['shared']);
}
if (isset($service['synthetic'])) {
$definition->setSynthetic($service['synthetic']);
}
2010-01-04 14:26:20 +00:00
2013-03-29 23:21:12 +00:00
if (isset($service['lazy'])) {
$definition->setLazy($service['lazy']);
}
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 (array_key_exists('deprecated', $service)) {
$definition->setDeprecated(true, $service['deprecated']);
}
if (isset($service['factory'])) {
if (is_string($service['factory'])) {
if (strpos($service['factory'], ':') !== false && strpos($service['factory'], '::') === false) {
$parts = explode(':', $service['factory']);
$definition->setFactory(array($this->resolveServices('@'.$parts[0]), $parts[1]));
} else {
$definition->setFactory($service['factory']);
}
} else {
$definition->setFactory(array($this->resolveServices($service['factory'][0]), $service['factory'][1]));
}
}
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'])) {
if (!is_array($service['calls'])) {
throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
}
foreach ($service['calls'] as $call) {
if (isset($call['method'])) {
$method = $call['method'];
$args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array();
} else {
$method = $call[0];
$args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
}
$definition->addMethodCall($method, $args);
}
}
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. Check your YAML syntax.', $id, $file));
}
2010-08-05 06:34:53 +01:00
foreach ($service['tags'] as $tag) {
if (!is_array($tag)) {
throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
}
if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
}
if (!is_string($tag['name']) || '' === $tag['name']) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
}
2010-08-05 06:34:53 +01:00
$name = $tag['name'];
unset($tag['name']);
2010-01-04 14:26:20 +00:00
foreach ($tag as $attribute => $value) {
2013-09-15 19:46:31 +01:00
if (!is_scalar($value) && null !== $value) {
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file));
}
}
2010-08-05 06:34:53 +01:00
$definition->addTag($name, $tag);
}
}
if (isset($service['decorates'])) {
$renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
$priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0;
$definition->setDecoratedService($service['decorates'], $renameId, $priority);
}
if (isset($service['autowire'])) {
$definition->setAutowired($service['autowire']);
}
if (isset($service['autowiring_types'])) {
if (is_string($service['autowiring_types'])) {
$definition->addAutowiringType($service['autowiring_types']);
} else {
if (!is_array($service['autowiring_types'])) {
throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
}
foreach ($service['autowiring_types'] as $autowiringType) {
if (!is_string($autowiringType)) {
throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
}
$definition->addAutowiringType($autowiringType);
}
}
}
$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-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return array The file content
2014-07-26 13:09:47 +01:00
*
* @throws InvalidArgumentException when the given file is not a local file or when it does not exist
2011-02-13 18:06:41 +00:00
*/
protected function loadFile($file)
2010-01-04 14:26:20 +00:00
{
if (!class_exists('Symfony\Component\Yaml\Parser')) {
throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
}
if (!stream_is_local($file)) {
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
}
if (!file_exists($file)) {
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
}
if (null === $this->yamlParser) {
$this->yamlParser = new YamlParser();
}
try {
$configuration = $this->yamlParser->parse(file_get_contents($file));
} catch (ParseException $e) {
2015-09-10 08:57:42 +01:00
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
}
return $this->validate($configuration, $file);
2010-01-04 14:26:20 +00:00
}
/**
2011-02-13 18:06:41 +00:00
* Validates a YAML file.
*
2012-05-18 18:41:48 +01:00
* @param mixed $content
2011-02-13 18:06:41 +00:00
* @param string $file
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @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. It should contain an array. Check your YAML syntax.', $file));
}
2010-01-04 14:26:20 +00:00
foreach ($content as $namespace => $data) {
if (in_array($namespace, array('imports', 'parameters', 'services'))) {
continue;
}
if (!$this->container->hasExtension($namespace)) {
$extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
throw new InvalidArgumentException(sprintf(
'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
$namespace,
$file,
$namespace,
$extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
));
}
}
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|array $value
2011-12-13 07:50:54 +00:00
*
* @return array|string|Reference
2011-02-13 18:06:41 +00:00
*/
private function resolveServices($value)
2010-01-04 14:26:20 +00:00
{
if (is_array($value)) {
$value = array_map(array($this, 'resolveServices'), $value);
} elseif (is_string($value) && 0 === strpos($value, '@=')) {
return new Expression(substr($value, 2));
2011-12-18 13:42:59 +00:00
} elseif (is_string($value) && 0 === strpos($value, '@')) {
if (0 === strpos($value, '@@')) {
$value = substr($value, 1);
$invalidBehavior = null;
} elseif (0 === strpos($value, '@?')) {
2011-01-17 22:28:59 +00:00
$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;
}
if (null !== $invalidBehavior) {
$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
/**
2014-12-21 17:00:50 +00:00
* Loads from Extensions.
2011-02-13 18:06:41 +00:00
*
* @param array $content
2011-02-13 18:06:41 +00:00
*/
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
}
}