PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/outlawscumbag/symfony
PHP | 325 lines | 191 code | 51 blank | 83 comment | 44 complexity | 1eeba764d2ad5a59f2f01b8b10ca4a9d MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\DefinitionDecorator;
  12. use Symfony\Component\DependencyInjection\Alias;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\Config\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@symfony.com>
  25. */
  26. class YamlFileLoader extends FileLoader
  27. {
  28. /**
  29. * Loads a Yaml file.
  30. *
  31. * @param mixed $file The resource
  32. * @param string $type The resource type
  33. */
  34. public function load($file, $type = null)
  35. {
  36. $path = $this->locator->locate($file);
  37. $content = $this->loadFile($path);
  38. $this->container->addResource(new FileResource($path));
  39. // empty file
  40. if (null === $content) {
  41. return;
  42. }
  43. // imports
  44. $this->parseImports($content, $file);
  45. // parameters
  46. if (isset($content['parameters'])) {
  47. foreach ($content['parameters'] as $key => $value) {
  48. $this->container->setParameter($key, $this->resolveServices($value));
  49. }
  50. }
  51. // extensions
  52. $this->loadFromExtensions($content);
  53. // services
  54. $this->parseDefinitions($content, $file);
  55. }
  56. /**
  57. * Returns true if this class supports the given resource.
  58. *
  59. * @param mixed $resource A resource
  60. * @param string $type The resource type
  61. *
  62. * @return Boolean true if this class supports the given resource, false otherwise
  63. */
  64. public function supports($resource, $type = null)
  65. {
  66. return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
  67. }
  68. /**
  69. * Parses all imports
  70. *
  71. * @param array $content
  72. * @param string $file
  73. * @return void
  74. */
  75. private function parseImports($content, $file)
  76. {
  77. if (!isset($content['imports'])) {
  78. return;
  79. }
  80. foreach ($content['imports'] as $import) {
  81. $this->setCurrentDir(dirname($file));
  82. $this->import($import['resource'], null, isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false, $file);
  83. }
  84. }
  85. /**
  86. * Parses definitions
  87. *
  88. * @param array $content
  89. * @param string $file
  90. * @return void
  91. */
  92. private function parseDefinitions($content, $file)
  93. {
  94. if (!isset($content['services'])) {
  95. return;
  96. }
  97. foreach ($content['services'] as $id => $service) {
  98. $this->parseDefinition($id, $service, $file);
  99. }
  100. }
  101. /**
  102. * Parses a definition.
  103. *
  104. * @param string $id
  105. * @param array $service
  106. * @param string $file
  107. * @return void
  108. */
  109. private function parseDefinition($id, $service, $file)
  110. {
  111. if (is_string($service) && 0 === strpos($service, '@')) {
  112. $this->container->setAlias($id, substr($service, 1));
  113. return;
  114. } else if (isset($service['alias'])) {
  115. $public = !array_key_exists('public', $service) || (Boolean) $service['public'];
  116. $this->container->setAlias($id, new Alias($service['alias'], $public));
  117. return;
  118. }
  119. if (isset($service['parent'])) {
  120. $definition = new DefinitionDecorator($service['parent']);
  121. } else {
  122. $definition = new Definition();
  123. }
  124. if (isset($service['class'])) {
  125. $definition->setClass($service['class']);
  126. }
  127. if (isset($service['scope'])) {
  128. $definition->setScope($service['scope']);
  129. }
  130. if (isset($service['synthetic'])) {
  131. $definition->setSynthetic($service['synthetic']);
  132. }
  133. if (isset($service['public'])) {
  134. $definition->setPublic($service['public']);
  135. }
  136. if (isset($service['abstract'])) {
  137. $definition->setAbstract($service['abstract']);
  138. }
  139. if (isset($service['factory_class'])) {
  140. $definition->setFactoryClass($service['factory_class']);
  141. }
  142. if (isset($service['factory_method'])) {
  143. $definition->setFactoryMethod($service['factory_method']);
  144. }
  145. if (isset($service['factory_service'])) {
  146. $definition->setFactoryService($service['factory_service']);
  147. }
  148. if (isset($service['file'])) {
  149. $definition->setFile($service['file']);
  150. }
  151. if (isset($service['arguments'])) {
  152. $definition->setArguments($this->resolveServices($service['arguments']));
  153. }
  154. if (isset($service['properties'])) {
  155. $definition->setProperties($this->resolveServices($service['properties']));
  156. }
  157. if (isset($service['configurator'])) {
  158. if (is_string($service['configurator'])) {
  159. $definition->setConfigurator($service['configurator']);
  160. } else {
  161. $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
  162. }
  163. }
  164. if (isset($service['calls'])) {
  165. foreach ($service['calls'] as $call) {
  166. $definition->addMethodCall($call[0], $this->resolveServices($call[1]));
  167. }
  168. }
  169. if (isset($service['tags'])) {
  170. if (!is_array($service['tags'])) {
  171. throw new \InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file));
  172. }
  173. foreach ($service['tags'] as $tag) {
  174. if (!isset($tag['name'])) {
  175. throw new \InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
  176. }
  177. $name = $tag['name'];
  178. unset($tag['name']);
  179. $definition->addTag($name, $tag);
  180. }
  181. }
  182. $this->container->setDefinition($id, $definition);
  183. }
  184. /**
  185. * Loads a YAML file.
  186. *
  187. * @param string $file
  188. * @return array The file content
  189. */
  190. private function loadFile($file)
  191. {
  192. return $this->validate(Yaml::parse($file), $file);
  193. }
  194. /**
  195. * Validates a YAML file.
  196. *
  197. * @param mixed $content
  198. * @param string $file
  199. * @return array
  200. *
  201. * @throws \InvalidArgumentException When service file is not valid
  202. */
  203. private function validate($content, $file)
  204. {
  205. if (null === $content) {
  206. return $content;
  207. }
  208. if (!is_array($content)) {
  209. throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
  210. }
  211. foreach (array_keys($content) as $namespace) {
  212. if (in_array($namespace, array('imports', 'parameters', 'services'))) {
  213. continue;
  214. }
  215. if (!$this->container->hasExtension($namespace)) {
  216. $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
  217. throw new \InvalidArgumentException(sprintf(
  218. 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
  219. $namespace,
  220. $file,
  221. $namespace,
  222. $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
  223. ));
  224. }
  225. }
  226. return $content;
  227. }
  228. /**
  229. * Resolves services.
  230. *
  231. * @param string $value
  232. * @return Reference
  233. */
  234. private function resolveServices($value)
  235. {
  236. if (is_array($value)) {
  237. $value = array_map(array($this, 'resolveServices'), $value);
  238. } else if (is_string($value) && 0 === strpos($value, '@')) {
  239. if (0 === strpos($value, '@?')) {
  240. $value = substr($value, 2);
  241. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  242. } else {
  243. $value = substr($value, 1);
  244. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  245. }
  246. if ('=' === substr($value, -1)) {
  247. $value = substr($value, 0, -1);
  248. $strict = false;
  249. } else {
  250. $strict = true;
  251. }
  252. $value = new Reference($value, $invalidBehavior, $strict);
  253. }
  254. return $value;
  255. }
  256. /**
  257. * Loads from Extensions
  258. *
  259. * @param array $content
  260. * @return void
  261. */
  262. private function loadFromExtensions($content)
  263. {
  264. foreach ($content as $namespace => $values) {
  265. if (in_array($namespace, array('imports', 'parameters', 'services'))) {
  266. continue;
  267. }
  268. if (!is_array($values)) {
  269. $values = array();
  270. }
  271. $this->container->loadFromExtension($namespace, $values);
  272. }
  273. }
  274. }