/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

https://github.com/sebio/symfony · PHP · 281 lines · 191 code · 53 blank · 37 comment · 40 complexity · 11145edc95e31258e5e13255466cd426 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Loader;
  11. use Symfony\Component\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\DependencyInjection\InterfaceInjector;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Resource\FileResource;
  18. use Symfony\Component\Yaml\Yaml;
  19. /**
  20. * YamlFileLoader loads YAML files service definitions.
  21. *
  22. * The YAML format does not support anonymous services (cf. the XML loader).
  23. *
  24. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  25. */
  26. class YamlFileLoader extends FileLoader
  27. {
  28. /**
  29. * Loads a Yaml file.
  30. *
  31. * @param mixed $resource The resource
  32. */
  33. public function load($file)
  34. {
  35. $path = $this->findFile($file);
  36. $content = $this->loadFile($path);
  37. $this->container->addResource(new FileResource($path));
  38. // empty file
  39. if (null === $content) {
  40. return;
  41. }
  42. // imports
  43. $this->parseImports($content, $file);
  44. // parameters
  45. if (isset($content['parameters'])) {
  46. foreach ($content['parameters'] as $key => $value) {
  47. $this->container->setParameter($key, $this->resolveServices($value));
  48. }
  49. }
  50. // extensions
  51. $this->loadFromExtensions($content);
  52. // interface injectors
  53. $this->parseInterfaceInjectors($content, $file);
  54. // services
  55. $this->parseDefinitions($content, $file);
  56. }
  57. /**
  58. * Returns true if this class supports the given resource.
  59. *
  60. * @param mixed $resource A resource
  61. *
  62. * @return Boolean true if this class supports the given resource, false otherwise
  63. */
  64. public function supports($resource)
  65. {
  66. return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
  67. }
  68. protected function parseImports($content, $file)
  69. {
  70. if (!isset($content['imports'])) {
  71. return;
  72. }
  73. foreach ($content['imports'] as $import) {
  74. $this->currentDir = dirname($file);
  75. $this->import($import['resource'], isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false);
  76. }
  77. }
  78. protected function parseInterfaceInjectors($content, $file)
  79. {
  80. if (!isset($content['interfaces'])) {
  81. return;
  82. }
  83. foreach ($content['interfaces'] as $class => $interface) {
  84. $this->parseInterfaceInjector($class, $interface, $file);
  85. }
  86. }
  87. protected function parseInterfaceInjector($class, $interface, $file)
  88. {
  89. $injector = new InterfaceInjector($class);
  90. if (isset($interface['calls'])) {
  91. foreach ($interface['calls'] as $call) {
  92. $injector->addMethodCall($call[0], $this->resolveServices($call[1]));
  93. }
  94. }
  95. $this->container->addInterfaceInjector($injector);
  96. }
  97. protected function parseDefinitions($content, $file)
  98. {
  99. if (!isset($content['services'])) {
  100. return;
  101. }
  102. foreach ($content['services'] as $id => $service) {
  103. $this->parseDefinition($id, $service, $file);
  104. }
  105. }
  106. protected function parseDefinition($id, $service, $file)
  107. {
  108. if (is_string($service) && 0 === strpos($service, '@')) {
  109. $this->container->setAlias($id, substr($service, 1));
  110. return;
  111. } else if (isset($service['alias'])) {
  112. $public = !array_key_exists('public', $service) || (Boolean) $service['public'];
  113. $this->container->setAlias($id, new Alias($service['alias'], $public));
  114. return;
  115. }
  116. $definition = new Definition();
  117. if (isset($service['class'])) {
  118. $definition->setClass($service['class']);
  119. }
  120. if (isset($service['scope'])) {
  121. $definition->setScope($service['scope']);
  122. }
  123. if (isset($service['synthetic'])) {
  124. $definition->setSynthetic($service['synthetic']);
  125. }
  126. if (isset($service['public'])) {
  127. $definition->setPublic($service['public']);
  128. }
  129. if (isset($service['factory_method'])) {
  130. $definition->setFactoryMethod($service['factory_method']);
  131. }
  132. if (isset($service['factory_service'])) {
  133. $definition->setFactoryService($service['factory_service']);
  134. }
  135. if (isset($service['file'])) {
  136. $definition->setFile($service['file']);
  137. }
  138. if (isset($service['arguments'])) {
  139. $definition->setArguments($this->resolveServices($service['arguments']));
  140. }
  141. if (isset($service['configurator'])) {
  142. if (is_string($service['configurator'])) {
  143. $definition->setConfigurator($service['configurator']);
  144. } else {
  145. $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
  146. }
  147. }
  148. if (isset($service['calls'])) {
  149. foreach ($service['calls'] as $call) {
  150. $definition->addMethodCall($call[0], $this->resolveServices($call[1]));
  151. }
  152. }
  153. if (isset($service['tags'])) {
  154. foreach ($service['tags'] as $tag) {
  155. $name = $tag['name'];
  156. unset($tag['name']);
  157. $definition->addTag($name, $tag);
  158. }
  159. }
  160. $this->container->setDefinition($id, $definition);
  161. }
  162. protected function loadFile($file)
  163. {
  164. return $this->validate(Yaml::load($file), $file);
  165. }
  166. /**
  167. * @throws \InvalidArgumentException When service file is not valid
  168. */
  169. protected function validate($content, $file)
  170. {
  171. if (null === $content) {
  172. return $content;
  173. }
  174. if (!is_array($content)) {
  175. throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
  176. }
  177. foreach (array_keys($content) as $key) {
  178. if (in_array($key, array('imports', 'parameters', 'services', 'interfaces'))) {
  179. continue;
  180. }
  181. // can it be handled by an extension?
  182. if (false !== strpos($key, '.')) {
  183. list($namespace, $tag) = explode('.', $key);
  184. if (!$this->container->hasExtension($namespace)) {
  185. throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $key, $file));
  186. }
  187. continue;
  188. }
  189. throw new \InvalidArgumentException(sprintf('The "%s" tag is not valid (in %s).', $key, $file));
  190. }
  191. return $content;
  192. }
  193. protected function resolveServices($value)
  194. {
  195. if (is_array($value)) {
  196. $value = array_map(array($this, 'resolveServices'), $value);
  197. } else if (is_string($value) && 0 === strpos($value, '@')) {
  198. if (0 === strpos($value, '@?')) {
  199. $value = substr($value, 2);
  200. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  201. } else {
  202. $value = substr($value, 1);
  203. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  204. }
  205. if ('=' === substr($value, -1)) {
  206. $value = substr($value, 0, -1);
  207. $strict = false;
  208. } else {
  209. $strict = true;
  210. }
  211. $value = new Reference($value, $invalidBehavior, $strict);
  212. }
  213. return $value;
  214. }
  215. protected function loadFromExtensions($content)
  216. {
  217. foreach ($content as $key => $values) {
  218. if (in_array($key, array('imports', 'parameters', 'services', 'interfaces'))) {
  219. continue;
  220. }
  221. list($namespace, $tag) = explode('.', $key);
  222. if (!is_array($values)) {
  223. $values = array();
  224. }
  225. $this->container->loadFromExtension($namespace, $tag, $values);
  226. }
  227. }
  228. }