YamlFileLoader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. /**
  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. private $yamlParser;
  29. /**
  30. * Loads a Yaml file.
  31. *
  32. * @param mixed $file The resource
  33. * @param string $type The resource type
  34. */
  35. public function load($file, $type = null)
  36. {
  37. $path = $this->locator->locate($file);
  38. $content = $this->loadFile($path);
  39. $this->container->addResource(new FileResource($path));
  40. // empty file
  41. if (null === $content) {
  42. return;
  43. }
  44. // imports
  45. $this->parseImports($content, $file);
  46. // parameters
  47. if (isset($content['parameters'])) {
  48. foreach ($content['parameters'] as $key => $value) {
  49. $this->container->setParameter($key, $this->resolveServices($value));
  50. }
  51. }
  52. // extensions
  53. $this->loadFromExtensions($content);
  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. * @param string $type The resource type
  62. *
  63. * @return Boolean true if this class supports the given resource, false otherwise
  64. */
  65. public function supports($resource, $type = null)
  66. {
  67. return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
  68. }
  69. /**
  70. * Parses all imports
  71. *
  72. * @param array $content
  73. * @param string $file
  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. */
  91. private function parseDefinitions($content, $file)
  92. {
  93. if (!isset($content['services'])) {
  94. return;
  95. }
  96. foreach ($content['services'] as $id => $service) {
  97. $this->parseDefinition($id, $service, $file);
  98. }
  99. }
  100. /**
  101. * Parses a definition.
  102. *
  103. * @param string $id
  104. * @param array $service
  105. * @param string $file
  106. *
  107. * @throws InvalidArgumentException When tags are invalid
  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. } elseif (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['synchronized'])) {
  134. $definition->setSynchronized($service['synchronized']);
  135. }
  136. if (isset($service['lazy'])) {
  137. $definition->setLazy($service['lazy']);
  138. }
  139. if (isset($service['public'])) {
  140. $definition->setPublic($service['public']);
  141. }
  142. if (isset($service['abstract'])) {
  143. $definition->setAbstract($service['abstract']);
  144. }
  145. if (isset($service['factory_class'])) {
  146. $definition->setFactoryClass($service['factory_class']);
  147. }
  148. if (isset($service['factory_method'])) {
  149. $definition->setFactoryMethod($service['factory_method']);
  150. }
  151. if (isset($service['factory_service'])) {
  152. $definition->setFactoryService($service['factory_service']);
  153. }
  154. if (isset($service['file'])) {
  155. $definition->setFile($service['file']);
  156. }
  157. if (isset($service['arguments'])) {
  158. $definition->setArguments($this->resolveServices($service['arguments']));
  159. }
  160. if (isset($service['properties'])) {
  161. $definition->setProperties($this->resolveServices($service['properties']));
  162. }
  163. if (isset($service['configurator'])) {
  164. if (is_string($service['configurator'])) {
  165. $definition->setConfigurator($service['configurator']);
  166. } else {
  167. $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
  168. }
  169. }
  170. if (isset($service['calls'])) {
  171. foreach ($service['calls'] as $call) {
  172. $args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
  173. $definition->addMethodCall($call[0], $args);
  174. }
  175. }
  176. if (isset($service['tags'])) {
  177. if (!is_array($service['tags'])) {
  178. throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file));
  179. }
  180. foreach ($service['tags'] as $tag) {
  181. if (!isset($tag['name'])) {
  182. throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
  183. }
  184. $name = $tag['name'];
  185. unset($tag['name']);
  186. foreach ($tag as $attribute => $value) {
  187. if (!is_scalar($value)) {
  188. throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s" in %s.', $id, $name, $file));
  189. }
  190. }
  191. $definition->addTag($name, $tag);
  192. }
  193. }
  194. $this->container->setDefinition($id, $definition);
  195. }
  196. /**
  197. * Loads a YAML file.
  198. *
  199. * @param string $file
  200. *
  201. * @return array The file content
  202. */
  203. protected function loadFile($file)
  204. {
  205. if (!stream_is_local($file)) {
  206. throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
  207. }
  208. if (!file_exists($file)) {
  209. throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
  210. }
  211. if (null === $this->yamlParser) {
  212. $this->yamlParser = new YamlParser();
  213. }
  214. return $this->validate($this->yamlParser->parse(file_get_contents($file)), $file);
  215. }
  216. /**
  217. * Validates a YAML file.
  218. *
  219. * @param mixed $content
  220. * @param string $file
  221. *
  222. * @return array
  223. *
  224. * @throws InvalidArgumentException When service file is not valid
  225. */
  226. private function validate($content, $file)
  227. {
  228. if (null === $content) {
  229. return $content;
  230. }
  231. if (!is_array($content)) {
  232. throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
  233. }
  234. foreach (array_keys($content) as $namespace) {
  235. if (in_array($namespace, array('imports', 'parameters', 'services'))) {
  236. continue;
  237. }
  238. if (!$this->container->hasExtension($namespace)) {
  239. $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
  240. throw new InvalidArgumentException(sprintf(
  241. 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
  242. $namespace,
  243. $file,
  244. $namespace,
  245. $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
  246. ));
  247. }
  248. }
  249. return $content;
  250. }
  251. /**
  252. * Resolves services.
  253. *
  254. * @param string $value
  255. *
  256. * @return Reference
  257. */
  258. private function resolveServices($value)
  259. {
  260. if (is_array($value)) {
  261. $value = array_map(array($this, 'resolveServices'), $value);
  262. } elseif (is_string($value) && 0 === strpos($value, '@')) {
  263. if (0 === strpos($value, '@@')) {
  264. $value = substr($value, 1);
  265. $invalidBehavior = null;
  266. } elseif (0 === strpos($value, '@?')) {
  267. $value = substr($value, 2);
  268. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  269. } else {
  270. $value = substr($value, 1);
  271. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  272. }
  273. if ('=' === substr($value, -1)) {
  274. $value = substr($value, 0, -1);
  275. $strict = false;
  276. } else {
  277. $strict = true;
  278. }
  279. if (null !== $invalidBehavior) {
  280. $value = new Reference($value, $invalidBehavior, $strict);
  281. }
  282. }
  283. return $value;
  284. }
  285. /**
  286. * Loads from Extensions
  287. *
  288. * @param array $content
  289. */
  290. private function loadFromExtensions($content)
  291. {
  292. foreach ($content as $namespace => $values) {
  293. if (in_array($namespace, array('imports', 'parameters', 'services'))) {
  294. continue;
  295. }
  296. if (!is_array($values)) {
  297. $values = array();
  298. }
  299. $this->container->loadFromExtension($namespace, $values);
  300. }
  301. }
  302. }