YamlFileLoader.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 must be an array 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::load($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. throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $namespace, $file));
  217. }
  218. }
  219. return $content;
  220. }
  221. /**
  222. * Resolves services.
  223. *
  224. * @param string $value
  225. * @return Reference
  226. */
  227. private function resolveServices($value)
  228. {
  229. if (is_array($value)) {
  230. $value = array_map(array($this, 'resolveServices'), $value);
  231. } else if (is_string($value) && 0 === strpos($value, '@')) {
  232. if (0 === strpos($value, '@?')) {
  233. $value = substr($value, 2);
  234. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  235. } else {
  236. $value = substr($value, 1);
  237. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  238. }
  239. if ('=' === substr($value, -1)) {
  240. $value = substr($value, 0, -1);
  241. $strict = false;
  242. } else {
  243. $strict = true;
  244. }
  245. $value = new Reference($value, $invalidBehavior, $strict);
  246. }
  247. return $value;
  248. }
  249. /**
  250. * Loads from Extensions
  251. *
  252. * @param array $content
  253. * @return void
  254. */
  255. private function loadFromExtensions($content)
  256. {
  257. foreach ($content as $namespace => $values) {
  258. if (in_array($namespace, array('imports', 'parameters', 'services'))) {
  259. continue;
  260. }
  261. if (!is_array($values)) {
  262. $values = array();
  263. }
  264. $this->container->loadFromExtension($namespace, $values);
  265. }
  266. }
  267. }