2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
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;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
2011-12-04 23:51:22 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
2013-05-05 10:27:54 +01:00
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser;
|
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).
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
|
|
|
class YamlFileLoader extends FileLoader
|
|
|
|
{
|
2013-05-05 10:27:54 +01:00
|
|
|
private $yamlParser;
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-10-26 15:01:39 +01:00
|
|
|
* Loads a Yaml file.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param mixed $file The resource
|
|
|
|
* @param string $type The resource type
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-02-10 15:15:51 +00:00
|
|
|
public function load($file, $type = null)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-02-09 23:44:02 +00:00
|
|
|
$path = $this->locator->locate($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-07-16 08:12:58 +01:00
|
|
|
$this->container->addResource(new FileResource($path));
|
2010-01-17 10:21:31 +00:00
|
|
|
|
2010-12-12 07:39:37 +00:00
|
|
|
// empty file
|
|
|
|
if (null === $content) {
|
2010-07-16 08:12:58 +01:00
|
|
|
return;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// imports
|
2014-03-27 06:16:27 +00:00
|
|
|
$this->parseImports($content, $path);
|
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-08 14:32:30 +01:00
|
|
|
foreach ($content['parameters'] as $key => $value) {
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->container->setParameter($key, $this->resolveServices($value));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-12-13 00:20:21 +00:00
|
|
|
// extensions
|
|
|
|
$this->loadFromExtensions($content);
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// services
|
2010-07-16 08:12:58 +01:00
|
|
|
$this->parseDefinitions($content, $file);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-07-18 10:26:47 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this class supports the given resource.
|
|
|
|
*
|
2011-02-10 15:15:51 +00:00
|
|
|
* @param mixed $resource A resource
|
|
|
|
* @param string $type The resource type
|
2010-07-18 10:26:47 +01:00
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool true if this class supports the given resource, false otherwise
|
2010-07-18 10:26:47 +01:00
|
|
|
*/
|
2011-02-10 15:15:51 +00:00
|
|
|
public function supports($resource, $type = null)
|
2010-07-18 10:26:47 +01:00
|
|
|
{
|
|
|
|
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Parses all imports
|
|
|
|
*
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param array $content
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param string $file
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function parseImports($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) {
|
2011-03-23 18:34:00 +00:00
|
|
|
$this->setCurrentDir(dirname($file));
|
2014-04-12 18:44:00 +01:00
|
|
|
$this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Parses definitions
|
|
|
|
*
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param array $content
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param string $file
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function parseDefinitions($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-07-16 08:12:58 +01:00
|
|
|
$this->parseDefinition($id, $service, $file);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Parses a definition.
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param string $id
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param array $service
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param string $file
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2012-09-07 23:56:45 +01:00
|
|
|
* @throws InvalidArgumentException When tags are invalid
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function parseDefinition($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-07-16 08:12:58 +01:00
|
|
|
$this->container->setAlias($id, substr($service, 1));
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-01-07 14:44:29 +00:00
|
|
|
return;
|
2011-12-18 13:42:59 +00:00
|
|
|
} elseif (isset($service['alias'])) {
|
2014-04-12 18:44:00 +01:00
|
|
|
$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));
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
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();
|
|
|
|
}
|
2010-07-04 17:56:48 +01:00
|
|
|
|
|
|
|
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']);
|
2011-01-23 11:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($service['synthetic'])) {
|
|
|
|
$definition->setSynthetic($service['synthetic']);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2013-02-06 20:42:01 +00:00
|
|
|
if (isset($service['synchronized'])) {
|
|
|
|
$definition->setSynchronized($service['synchronized']);
|
|
|
|
}
|
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
|
2011-02-02 17:22:27 +00:00
|
|
|
if (isset($service['factory_class'])) {
|
|
|
|
$definition->setFactoryClass($service['factory_class']);
|
|
|
|
}
|
|
|
|
|
2010-07-05 08:47:14 +01:00
|
|
|
if (isset($service['factory_method'])) {
|
|
|
|
$definition->setFactoryMethod($service['factory_method']);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-07-04 17:56:48 +01:00
|
|
|
if (isset($service['factory_service'])) {
|
|
|
|
$definition->setFactoryService($service['factory_service']);
|
2010-07-03 23:35:39 +01: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
|
|
|
|
2011-03-04 14:26:00 +00:00
|
|
|
if (isset($service['properties'])) {
|
|
|
|
$definition->setProperties($this->resolveServices($service['properties']));
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($service['configurator'])) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (is_string($service['configurator'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$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-08 14:32:30 +01:00
|
|
|
foreach ($service['calls'] as $call) {
|
2012-04-06 13:21:18 +01:00
|
|
|
$args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
|
|
|
|
$definition->addMethodCall($call[0], $args);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
if (isset($service['tags'])) {
|
2011-02-17 14:31:50 +00:00
|
|
|
if (!is_array($service['tags'])) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file));
|
2011-02-17 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
foreach ($service['tags'] as $tag) {
|
2011-02-17 14:31:50 +00:00
|
|
|
if (!isset($tag['name'])) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
|
2011-02-17 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
$name = $tag['name'];
|
|
|
|
unset($tag['name']);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2012-04-13 15:48:30 +01:00
|
|
|
foreach ($tag as $attribute => $value) {
|
|
|
|
if (!is_scalar($value)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s" in %s.', $id, $name, $file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 06:34:53 +01:00
|
|
|
$definition->addTag($name, $tag);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-16 08:12:58 +01:00
|
|
|
$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.
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param string $file
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return array The file content
|
|
|
|
*/
|
2012-11-11 11:59:36 +00:00
|
|
|
protected function loadFile($file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2013-05-05 10:27:54 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->validate($this->yamlParser->parse(file_get_contents($file)), $file);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01: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
|
|
|
|
*
|
2011-12-04 23:51:22 +00:00
|
|
|
* @throws InvalidArgumentException When service file is not valid
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private 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)) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2011-02-05 18:42:07 +00:00
|
|
|
foreach (array_keys($content) as $namespace) {
|
2011-04-23 10:42:31 +01:00
|
|
|
if (in_array($namespace, array('imports', 'parameters', 'services'))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-02-05 18:42:07 +00:00
|
|
|
if (!$this->container->hasExtension($namespace)) {
|
2011-07-06 14:26:21 +01:00
|
|
|
$extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
2011-07-06 14:26:21 +01:00
|
|
|
'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-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Resolves services.
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param string $value
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-06-04 16:30:56 +01:00
|
|
|
* @return Reference
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private 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);
|
2011-12-18 13:42:59 +00:00
|
|
|
} elseif (is_string($value) && 0 === strpos($value, '@')) {
|
2013-03-08 15:38:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-08 15:38:09 +00:00
|
|
|
if (null !== $invalidBehavior) {
|
|
|
|
$value = new Reference($value, $invalidBehavior, $strict);
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
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
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Loads from Extensions
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param array $content
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function loadFromExtensions($content)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-02-05 18:42:07 +00:00
|
|
|
foreach ($content as $namespace => $values) {
|
2011-04-23 10:42:31 +01:00
|
|
|
if (in_array($namespace, array('imports', 'parameters', 'services'))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
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
|
|
|
|
2011-02-05 18:42:07 +00:00
|
|
|
$this->container->loadFromExtension($namespace, $values);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
}
|