PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/cryofrost/portal
PHP | 339 lines | 197 code | 52 blank | 90 comment | 41 complexity | 3bf88425768e369ab6867bfbaf0b56c5 MD5 | raw file
Possible License(s): Apache-2.0, JSON, LGPL-2.1, LGPL-2.0, LGPL-3.0, BSD-3-Clause, BSD-2-Clause
  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\Exception\InvalidArgumentException;
  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. *
  74. * @return void
  75. */
  76. private function parseImports($content, $file)
  77. {
  78. if (!isset($content['imports'])) {
  79. return;
  80. }
  81. foreach ($content['imports'] as $import) {
  82. $this->setCurrentDir(dirname($file));
  83. $this->import($import['resource'], null, isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false, $file);
  84. }
  85. }
  86. /**
  87. * Parses definitions
  88. *
  89. * @param array $content
  90. * @param string $file
  91. *
  92. * @return void
  93. */
  94. private function parseDefinitions($content, $file)
  95. {
  96. if (!isset($content['services'])) {
  97. return;
  98. }
  99. foreach ($content['services'] as $id => $service) {
  100. $this->parseDefinition($id, $service, $file);
  101. }
  102. }
  103. /**
  104. * Parses a definition.
  105. *
  106. * @param string $id
  107. * @param array $service
  108. * @param string $file
  109. *
  110. * @return void
  111. */
  112. private function parseDefinition($id, $service, $file)
  113. {
  114. if (is_string($service) && 0 === strpos($service, '@')) {
  115. $this->container->setAlias($id, substr($service, 1));
  116. return;
  117. } elseif (isset($service['alias'])) {
  118. $public = !array_key_exists('public', $service) || (Boolean) $service['public'];
  119. $this->container->setAlias($id, new Alias($service['alias'], $public));
  120. return;
  121. }
  122. if (isset($service['parent'])) {
  123. $definition = new DefinitionDecorator($service['parent']);
  124. } else {
  125. $definition = new Definition();
  126. }
  127. if (isset($service['class'])) {
  128. $definition->setClass($service['class']);
  129. }
  130. if (isset($service['scope'])) {
  131. $definition->setScope($service['scope']);
  132. }
  133. if (isset($service['synthetic'])) {
  134. $definition->setSynthetic($service['synthetic']);
  135. }
  136. if (isset($service['public'])) {
  137. $definition->setPublic($service['public']);
  138. }
  139. if (isset($service['abstract'])) {
  140. $definition->setAbstract($service['abstract']);
  141. }
  142. if (isset($service['factory_class'])) {
  143. $definition->setFactoryClass($service['factory_class']);
  144. }
  145. if (isset($service['factory_method'])) {
  146. $definition->setFactoryMethod($service['factory_method']);
  147. }
  148. if (isset($service['factory_service'])) {
  149. $definition->setFactoryService($service['factory_service']);
  150. }
  151. if (isset($service['file'])) {
  152. $definition->setFile($service['file']);
  153. }
  154. if (isset($service['arguments'])) {
  155. $definition->setArguments($this->resolveServices($service['arguments']));
  156. }
  157. if (isset($service['properties'])) {
  158. $definition->setProperties($this->resolveServices($service['properties']));
  159. }
  160. if (isset($service['configurator'])) {
  161. if (is_string($service['configurator'])) {
  162. $definition->setConfigurator($service['configurator']);
  163. } else {
  164. $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
  165. }
  166. }
  167. if (isset($service['calls'])) {
  168. foreach ($service['calls'] as $call) {
  169. $args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
  170. $definition->addMethodCall($call[0], $args);
  171. }
  172. }
  173. if (isset($service['tags'])) {
  174. if (!is_array($service['tags'])) {
  175. throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file));
  176. }
  177. foreach ($service['tags'] as $tag) {
  178. if (!isset($tag['name'])) {
  179. throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
  180. }
  181. $name = $tag['name'];
  182. unset($tag['name']);
  183. foreach ($tag as $attribute => $value) {
  184. if (!is_scalar($value)) {
  185. throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s" in %s.', $id, $name, $file));
  186. }
  187. }
  188. $definition->addTag($name, $tag);
  189. }
  190. }
  191. $this->container->setDefinition($id, $definition);
  192. }
  193. /**
  194. * Loads a YAML file.
  195. *
  196. * @param string $file
  197. *
  198. * @return array The file content
  199. */
  200. private function loadFile($file)
  201. {
  202. return $this->validate(Yaml::parse($file), $file);
  203. }
  204. /**
  205. * Validates a YAML file.
  206. *
  207. * @param mixed $content
  208. * @param string $file
  209. *
  210. * @return array
  211. *
  212. * @throws \InvalidArgumentException When service file is not valid
  213. */
  214. private function validate($content, $file)
  215. {
  216. if (null === $content) {
  217. return $content;
  218. }
  219. if (!is_array($content)) {
  220. throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
  221. }
  222. foreach (array_keys($content) as $namespace) {
  223. if (in_array($namespace, array('imports', 'parameters', 'services'))) {
  224. continue;
  225. }
  226. if (!$this->container->hasExtension($namespace)) {
  227. $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
  228. throw new \InvalidArgumentException(sprintf(
  229. 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
  230. $namespace,
  231. $file,
  232. $namespace,
  233. $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
  234. ));
  235. }
  236. }
  237. return $content;
  238. }
  239. /**
  240. * Resolves services.
  241. *
  242. * @param string $value
  243. *
  244. * @return Reference
  245. */
  246. private function resolveServices($value)
  247. {
  248. if (is_array($value)) {
  249. $value = array_map(array($this, 'resolveServices'), $value);
  250. } elseif (is_string($value) && 0 === strpos($value, '@')) {
  251. if (0 === strpos($value, '@?')) {
  252. $value = substr($value, 2);
  253. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  254. } else {
  255. $value = substr($value, 1);
  256. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  257. }
  258. if ('=' === substr($value, -1)) {
  259. $value = substr($value, 0, -1);
  260. $strict = false;
  261. } else {
  262. $strict = true;
  263. }
  264. $value = new Reference($value, $invalidBehavior, $strict);
  265. }
  266. return $value;
  267. }
  268. /**
  269. * Loads from Extensions
  270. *
  271. * @param array $content
  272. *
  273. * @return void
  274. */
  275. private function loadFromExtensions($content)
  276. {
  277. foreach ($content as $namespace => $values) {
  278. if (in_array($namespace, array('imports', 'parameters', 'services'))) {
  279. continue;
  280. }
  281. if (!is_array($values)) {
  282. $values = array();
  283. }
  284. $this->container->loadFromExtension($namespace, $values);
  285. }
  286. }
  287. }