PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/Snizer/PI-DEV-TUNISIAMALL3A6-WEB
PHP | 392 lines | 247 code | 67 blank | 78 comment | 58 complexity | fa1b8024ce6d9867fbf7f5135a0dbce6 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\Exception\InvalidArgumentException;
  17. use Symfony\Component\Config\Resource\FileResource;
  18. use Symfony\Component\Yaml\Parser as YamlParser;
  19. use Symfony\Component\ExpressionLanguage\Expression;
  20. /**
  21. * YamlFileLoader loads YAML files service definitions.
  22. *
  23. * The YAML format does not support anonymous services (cf. the XML loader).
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. */
  27. class YamlFileLoader extends FileLoader
  28. {
  29. private $yamlParser;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function load($resource, $type = null)
  34. {
  35. $path = $this->locator->locate($resource);
  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, $path);
  44. // parameters
  45. if (isset($content['parameters'])) {
  46. if (!is_array($content['parameters'])) {
  47. throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $resource));
  48. }
  49. foreach ($content['parameters'] as $key => $value) {
  50. $this->container->setParameter($key, $this->resolveServices($value));
  51. }
  52. }
  53. // extensions
  54. $this->loadFromExtensions($content);
  55. // services
  56. $this->parseDefinitions($content, $resource);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function supports($resource, $type = null)
  62. {
  63. return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
  64. }
  65. /**
  66. * Parses all imports.
  67. *
  68. * @param array $content
  69. * @param string $file
  70. */
  71. private function parseImports($content, $file)
  72. {
  73. if (!isset($content['imports'])) {
  74. return;
  75. }
  76. if (!is_array($content['imports'])) {
  77. throw new InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file));
  78. }
  79. foreach ($content['imports'] as $import) {
  80. if (!is_array($import)) {
  81. throw new InvalidArgumentException(sprintf('The values in the "imports" key should be arrays in %s. Check your YAML syntax.', $file));
  82. }
  83. $this->setCurrentDir(dirname($file));
  84. $this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
  85. }
  86. }
  87. /**
  88. * Parses definitions.
  89. *
  90. * @param array $content
  91. * @param string $file
  92. */
  93. private function parseDefinitions($content, $file)
  94. {
  95. if (!isset($content['services'])) {
  96. return;
  97. }
  98. if (!is_array($content['services'])) {
  99. throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
  100. }
  101. foreach ($content['services'] as $id => $service) {
  102. $this->parseDefinition($id, $service, $file);
  103. }
  104. }
  105. /**
  106. * Parses a definition.
  107. *
  108. * @param string $id
  109. * @param array $service
  110. * @param string $file
  111. *
  112. * @throws InvalidArgumentException When tags are invalid
  113. */
  114. private function parseDefinition($id, $service, $file)
  115. {
  116. if (is_string($service) && 0 === strpos($service, '@')) {
  117. $this->container->setAlias($id, substr($service, 1));
  118. return;
  119. }
  120. if (!is_array($service)) {
  121. 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));
  122. }
  123. if (isset($service['alias'])) {
  124. $public = !array_key_exists('public', $service) || (bool) $service['public'];
  125. $this->container->setAlias($id, new Alias($service['alias'], $public));
  126. return;
  127. }
  128. if (isset($service['parent'])) {
  129. $definition = new DefinitionDecorator($service['parent']);
  130. } else {
  131. $definition = new Definition();
  132. }
  133. if (isset($service['class'])) {
  134. $definition->setClass($service['class']);
  135. }
  136. if (isset($service['scope'])) {
  137. $definition->setScope($service['scope']);
  138. }
  139. if (isset($service['synthetic'])) {
  140. $definition->setSynthetic($service['synthetic']);
  141. }
  142. if (isset($service['synchronized'])) {
  143. $definition->setSynchronized($service['synchronized']);
  144. }
  145. if (isset($service['lazy'])) {
  146. $definition->setLazy($service['lazy']);
  147. }
  148. if (isset($service['public'])) {
  149. $definition->setPublic($service['public']);
  150. }
  151. if (isset($service['abstract'])) {
  152. $definition->setAbstract($service['abstract']);
  153. }
  154. if (isset($service['factory_class'])) {
  155. $definition->setFactoryClass($service['factory_class']);
  156. }
  157. if (isset($service['factory_method'])) {
  158. $definition->setFactoryMethod($service['factory_method']);
  159. }
  160. if (isset($service['factory_service'])) {
  161. $definition->setFactoryService($service['factory_service']);
  162. }
  163. if (isset($service['file'])) {
  164. $definition->setFile($service['file']);
  165. }
  166. if (isset($service['arguments'])) {
  167. $definition->setArguments($this->resolveServices($service['arguments']));
  168. }
  169. if (isset($service['properties'])) {
  170. $definition->setProperties($this->resolveServices($service['properties']));
  171. }
  172. if (isset($service['configurator'])) {
  173. if (is_string($service['configurator'])) {
  174. $definition->setConfigurator($service['configurator']);
  175. } else {
  176. $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
  177. }
  178. }
  179. if (isset($service['calls'])) {
  180. if (!is_array($service['calls'])) {
  181. throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
  182. }
  183. foreach ($service['calls'] as $call) {
  184. $args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
  185. $definition->addMethodCall($call[0], $args);
  186. }
  187. }
  188. if (isset($service['tags'])) {
  189. if (!is_array($service['tags'])) {
  190. throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
  191. }
  192. foreach ($service['tags'] as $tag) {
  193. if (!is_array($tag)) {
  194. throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
  195. }
  196. if (!isset($tag['name'])) {
  197. throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
  198. }
  199. $name = $tag['name'];
  200. unset($tag['name']);
  201. foreach ($tag as $attribute => $value) {
  202. if (!is_scalar($value) && null !== $value) {
  203. 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));
  204. }
  205. }
  206. $definition->addTag($name, $tag);
  207. }
  208. }
  209. if (isset($service['decorates'])) {
  210. $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
  211. $definition->setDecoratedService($service['decorates'], $renameId);
  212. }
  213. $this->container->setDefinition($id, $definition);
  214. }
  215. /**
  216. * Loads a YAML file.
  217. *
  218. * @param string $file
  219. *
  220. * @return array The file content
  221. *
  222. * @throws InvalidArgumentException when the given file is not a local file or when it does not exist
  223. */
  224. protected function loadFile($file)
  225. {
  226. if (!stream_is_local($file)) {
  227. throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
  228. }
  229. if (!file_exists($file)) {
  230. throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
  231. }
  232. if (null === $this->yamlParser) {
  233. $this->yamlParser = new YamlParser();
  234. }
  235. return $this->validate($this->yamlParser->parse(file_get_contents($file)), $file);
  236. }
  237. /**
  238. * Validates a YAML file.
  239. *
  240. * @param mixed $content
  241. * @param string $file
  242. *
  243. * @return array
  244. *
  245. * @throws InvalidArgumentException When service file is not valid
  246. */
  247. private function validate($content, $file)
  248. {
  249. if (null === $content) {
  250. return $content;
  251. }
  252. if (!is_array($content)) {
  253. throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
  254. }
  255. foreach (array_keys($content) as $namespace) {
  256. if (in_array($namespace, array('imports', 'parameters', 'services'))) {
  257. continue;
  258. }
  259. if (!$this->container->hasExtension($namespace)) {
  260. $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
  261. throw new InvalidArgumentException(sprintf(
  262. 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
  263. $namespace,
  264. $file,
  265. $namespace,
  266. $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
  267. ));
  268. }
  269. }
  270. return $content;
  271. }
  272. /**
  273. * Resolves services.
  274. *
  275. * @param string|array $value
  276. *
  277. * @return array|string|Reference
  278. */
  279. private function resolveServices($value)
  280. {
  281. if (is_array($value)) {
  282. $value = array_map(array($this, 'resolveServices'), $value);
  283. } elseif (is_string($value) && 0 === strpos($value, '@=')) {
  284. return new Expression(substr($value, 2));
  285. } elseif (is_string($value) && 0 === strpos($value, '@')) {
  286. if (0 === strpos($value, '@@')) {
  287. $value = substr($value, 1);
  288. $invalidBehavior = null;
  289. } elseif (0 === strpos($value, '@?')) {
  290. $value = substr($value, 2);
  291. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  292. } else {
  293. $value = substr($value, 1);
  294. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  295. }
  296. if ('=' === substr($value, -1)) {
  297. $value = substr($value, 0, -1);
  298. $strict = false;
  299. } else {
  300. $strict = true;
  301. }
  302. if (null !== $invalidBehavior) {
  303. $value = new Reference($value, $invalidBehavior, $strict);
  304. }
  305. }
  306. return $value;
  307. }
  308. /**
  309. * Loads from Extensions.
  310. *
  311. * @param array $content
  312. */
  313. private function loadFromExtensions($content)
  314. {
  315. foreach ($content as $namespace => $values) {
  316. if (in_array($namespace, array('imports', 'parameters', 'services'))) {
  317. continue;
  318. }
  319. if (!is_array($values)) {
  320. $values = array();
  321. }
  322. $this->container->loadFromExtension($namespace, $values);
  323. }
  324. }
  325. }