4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Loader;
14 use Symfony\Component\DependencyInjection\DefinitionDecorator;
15 use Symfony\Component\DependencyInjection\Alias;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\DependencyInjection\Definition;
18 use Symfony\Component\DependencyInjection\Reference;
19 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
20 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
21 use Symfony\Component\Config\Resource\FileResource;
22 use Symfony\Component\Yaml\Exception\ParseException;
23 use Symfony\Component\Yaml\Parser as YamlParser;
24 use Symfony\Component\ExpressionLanguage\Expression;
27 * YamlFileLoader loads YAML files service definitions.
29 * The YAML format does not support anonymous services (cf. the XML loader).
31 * @author Fabien Potencier <fabien@symfony.com>
33 class YamlFileLoader extends FileLoader
40 public function load($resource, $type = null)
42 $path = $this->locator->locate($resource);
44 $content = $this->loadFile($path);
46 $this->container->addResource(new FileResource($path));
49 if (null === $content) {
54 $this->parseImports($content, $path);
57 if (isset($content['parameters'])) {
58 if (!is_array($content['parameters'])) {
59 throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $resource));
62 foreach ($content['parameters'] as $key => $value) {
63 $this->container->setParameter($key, $this->resolveServices($value));
68 $this->loadFromExtensions($content);
71 $this->parseDefinitions($content, $resource);
77 public function supports($resource, $type = null)
79 return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true);
85 * @param array $content
88 private function parseImports(array $content, $file)
90 if (!isset($content['imports'])) {
94 if (!is_array($content['imports'])) {
95 throw new InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file));
98 $defaultDirectory = dirname($file);
99 foreach ($content['imports'] as $import) {
100 if (!is_array($import)) {
101 throw new InvalidArgumentException(sprintf('The values in the "imports" key should be arrays in %s. Check your YAML syntax.', $file));
104 $this->setCurrentDir($defaultDirectory);
105 $this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
110 * Parses definitions.
112 * @param array $content
113 * @param string $file
115 private function parseDefinitions(array $content, $file)
117 if (!isset($content['services'])) {
121 if (!is_array($content['services'])) {
122 throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
125 foreach ($content['services'] as $id => $service) {
126 $this->parseDefinition($id, $service, $file);
131 * Parses a definition.
134 * @param array|string $service
135 * @param string $file
137 * @throws InvalidArgumentException When tags are invalid
139 private function parseDefinition($id, $service, $file)
141 if (is_string($service) && 0 === strpos($service, '@')) {
142 $this->container->setAlias($id, substr($service, 1));
147 if (!is_array($service)) {
148 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));
151 if (isset($service['alias'])) {
152 $public = !array_key_exists('public', $service) || (bool) $service['public'];
153 $this->container->setAlias($id, new Alias($service['alias'], $public));
158 if (isset($service['parent'])) {
159 $definition = new DefinitionDecorator($service['parent']);
161 $definition = new Definition();
164 if (isset($service['class'])) {
165 $definition->setClass($service['class']);
168 if (isset($service['shared'])) {
169 $definition->setShared($service['shared']);
172 if (isset($service['scope'])) {
173 if ('request' !== $id) {
174 @trigger_error(sprintf('The "scope" key of service "%s" in file "%s" is deprecated since version 2.8 and will be removed in 3.0.', $id, $file), E_USER_DEPRECATED);
176 $definition->setScope($service['scope'], false);
179 if (isset($service['synthetic'])) {
180 $definition->setSynthetic($service['synthetic']);
183 if (isset($service['synchronized'])) {
184 @trigger_error(sprintf('The "synchronized" key of service "%s" in file "%s" is deprecated since version 2.7 and will be removed in 3.0.', $id, $file), E_USER_DEPRECATED);
185 $definition->setSynchronized($service['synchronized'], 'request' !== $id);
188 if (isset($service['lazy'])) {
189 $definition->setLazy($service['lazy']);
192 if (isset($service['public'])) {
193 $definition->setPublic($service['public']);
196 if (isset($service['abstract'])) {
197 $definition->setAbstract($service['abstract']);
200 if (array_key_exists('deprecated', $service)) {
201 $definition->setDeprecated(true, $service['deprecated']);
204 if (isset($service['factory'])) {
205 if (is_string($service['factory'])) {
206 if (strpos($service['factory'], ':') !== false && strpos($service['factory'], '::') === false) {
207 $parts = explode(':', $service['factory']);
208 $definition->setFactory(array($this->resolveServices('@'.$parts[0]), $parts[1]));
210 $definition->setFactory($service['factory']);
213 $definition->setFactory(array($this->resolveServices($service['factory'][0]), $service['factory'][1]));
217 if (isset($service['factory_class'])) {
218 @trigger_error(sprintf('The "factory_class" key of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use "factory" instead.', $id, $file), E_USER_DEPRECATED);
219 $definition->setFactoryClass($service['factory_class']);
222 if (isset($service['factory_method'])) {
223 @trigger_error(sprintf('The "factory_method" key of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use "factory" instead.', $id, $file), E_USER_DEPRECATED);
224 $definition->setFactoryMethod($service['factory_method']);
227 if (isset($service['factory_service'])) {
228 @trigger_error(sprintf('The "factory_service" key of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use "factory" instead.', $id, $file), E_USER_DEPRECATED);
229 $definition->setFactoryService($service['factory_service']);
232 if (isset($service['file'])) {
233 $definition->setFile($service['file']);
236 if (isset($service['arguments'])) {
237 $definition->setArguments($this->resolveServices($service['arguments']));
240 if (isset($service['properties'])) {
241 $definition->setProperties($this->resolveServices($service['properties']));
244 if (isset($service['configurator'])) {
245 if (is_string($service['configurator'])) {
246 $definition->setConfigurator($service['configurator']);
248 $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
252 if (isset($service['calls'])) {
253 if (!is_array($service['calls'])) {
254 throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
257 foreach ($service['calls'] as $call) {
258 if (isset($call['method'])) {
259 $method = $call['method'];
260 $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array();
263 $args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
266 $definition->addMethodCall($method, $args);
270 if (isset($service['tags'])) {
271 if (!is_array($service['tags'])) {
272 throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
275 foreach ($service['tags'] as $tag) {
276 if (!is_array($tag)) {
277 throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
280 if (!isset($tag['name'])) {
281 throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
284 if (!is_string($tag['name']) || '' === $tag['name']) {
285 throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
288 $name = $tag['name'];
291 foreach ($tag as $attribute => $value) {
292 if (!is_scalar($value) && null !== $value) {
293 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));
297 $definition->addTag($name, $tag);
301 if (isset($service['decorates'])) {
302 if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) {
303 throw new InvalidArgumentException(sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($service['decorates'], 1)));
306 $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
307 $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0;
308 $definition->setDecoratedService($service['decorates'], $renameId, $priority);
311 if (isset($service['autowire'])) {
312 $definition->setAutowired($service['autowire']);
315 if (isset($service['autowiring_types'])) {
316 if (is_string($service['autowiring_types'])) {
317 $definition->addAutowiringType($service['autowiring_types']);
319 if (!is_array($service['autowiring_types'])) {
320 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));
323 foreach ($service['autowiring_types'] as $autowiringType) {
324 if (!is_string($autowiringType)) {
325 throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
328 $definition->addAutowiringType($autowiringType);
333 $this->container->setDefinition($id, $definition);
339 * @param string $file
341 * @return array The file content
343 * @throws InvalidArgumentException when the given file is not a local file or when it does not exist
345 protected function loadFile($file)
347 if (!class_exists('Symfony\Component\Yaml\Parser')) {
348 throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
351 if (!stream_is_local($file)) {
352 throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
355 if (!file_exists($file)) {
356 throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
359 if (null === $this->yamlParser) {
360 $this->yamlParser = new YamlParser();
364 $configuration = $this->yamlParser->parse(file_get_contents($file));
365 } catch (ParseException $e) {
366 throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
369 return $this->validate($configuration, $file);
373 * Validates a YAML file.
375 * @param mixed $content
376 * @param string $file
380 * @throws InvalidArgumentException When service file is not valid
382 private function validate($content, $file)
384 if (null === $content) {
388 if (!is_array($content)) {
389 throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
392 foreach ($content as $namespace => $data) {
393 if (in_array($namespace, array('imports', 'parameters', 'services'))) {
397 if (!$this->container->hasExtension($namespace)) {
398 $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
399 throw new InvalidArgumentException(sprintf(
400 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
404 $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
415 * @param string|array $value
417 * @return array|string|Reference
419 private function resolveServices($value)
421 if (is_array($value)) {
422 $value = array_map(array($this, 'resolveServices'), $value);
423 } elseif (is_string($value) && 0 === strpos($value, '@=')) {
424 return new Expression(substr($value, 2));
425 } elseif (is_string($value) && 0 === strpos($value, '@')) {
426 if (0 === strpos($value, '@@')) {
427 $value = substr($value, 1);
428 $invalidBehavior = null;
429 } elseif (0 === strpos($value, '@?')) {
430 $value = substr($value, 2);
431 $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
433 $value = substr($value, 1);
434 $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
437 if ('=' === substr($value, -1)) {
438 $value = substr($value, 0, -1);
444 if (null !== $invalidBehavior) {
445 $value = new Reference($value, $invalidBehavior, $strict);
453 * Loads from Extensions.
455 * @param array $content
457 private function loadFromExtensions(array $content)
459 foreach ($content as $namespace => $values) {
460 if (in_array($namespace, array('imports', 'parameters', 'services'))) {
464 if (!is_array($values)) {
468 $this->container->loadFromExtension($namespace, $values);