YamlFileLoader.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\InterfaceInjector;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. use Symfony\Component\Config\Resource\FileResource;
  19. use Symfony\Component\Yaml\Yaml;
  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.potencier@symfony-project.com>
  26. */
  27. class YamlFileLoader extends FileLoader
  28. {
  29. /**
  30. * Loads a Yaml file.
  31. *
  32. * @param mixed $resource 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. // interface injectors
  55. $this->parseInterfaceInjectors($content, $file);
  56. // services
  57. $this->parseDefinitions($content, $file);
  58. }
  59. /**
  60. * Returns true if this class supports the given resource.
  61. *
  62. * @param mixed $resource A resource
  63. * @param string $type The resource type
  64. *
  65. * @return Boolean true if this class supports the given resource, false otherwise
  66. */
  67. public function supports($resource, $type = null)
  68. {
  69. return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
  70. }
  71. /**
  72. * Parses all imports
  73. *
  74. * @param array $content
  75. * @param string $file
  76. * @return void
  77. */
  78. protected function parseImports($content, $file)
  79. {
  80. if (!isset($content['imports'])) {
  81. return;
  82. }
  83. foreach ($content['imports'] as $import) {
  84. $this->currentDir = dirname($file);
  85. $this->import($import['resource'], isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false);
  86. }
  87. }
  88. /**
  89. * Parses interface injectors.
  90. *
  91. * @param array $content
  92. * @param string $file
  93. * @return void
  94. */
  95. protected function parseInterfaceInjectors($content, $file)
  96. {
  97. if (!isset($content['interfaces'])) {
  98. return;
  99. }
  100. foreach ($content['interfaces'] as $class => $interface) {
  101. $this->parseInterfaceInjector($class, $interface, $file);
  102. }
  103. }
  104. /**
  105. * Parses an interface injector.
  106. *
  107. * @param string $class
  108. * @param array $interface
  109. * @param string $file
  110. * @return void
  111. */
  112. protected function parseInterfaceInjector($class, $interface, $file)
  113. {
  114. $injector = new InterfaceInjector($class);
  115. if (isset($interface['calls'])) {
  116. foreach ($interface['calls'] as $call) {
  117. $injector->addMethodCall($call[0], $this->resolveServices($call[1]));
  118. }
  119. }
  120. $this->container->addInterfaceInjector($injector);
  121. }
  122. /**
  123. * Parses definitions
  124. *
  125. * @param array $content
  126. * @param string $file
  127. * @return void
  128. */
  129. protected function parseDefinitions($content, $file)
  130. {
  131. if (!isset($content['services'])) {
  132. return;
  133. }
  134. foreach ($content['services'] as $id => $service) {
  135. $this->parseDefinition($id, $service, $file);
  136. }
  137. }
  138. /**
  139. * Parses a definition.
  140. *
  141. * @param string $id
  142. * @param array $service
  143. * @param string $file
  144. * @return void
  145. */
  146. protected function parseDefinition($id, $service, $file)
  147. {
  148. if (is_string($service) && 0 === strpos($service, '@')) {
  149. $this->container->setAlias($id, substr($service, 1));
  150. return;
  151. } else if (isset($service['alias'])) {
  152. $public = !array_key_exists('public', $service) || (Boolean) $service['public'];
  153. $this->container->setAlias($id, new Alias($service['alias'], $public));
  154. return;
  155. }
  156. if (isset($service['parent'])) {
  157. $definition = new DefinitionDecorator($service['parent']);
  158. } else {
  159. $definition = new Definition();
  160. }
  161. if (isset($service['class'])) {
  162. $definition->setClass($service['class']);
  163. }
  164. if (isset($service['scope'])) {
  165. $definition->setScope($service['scope']);
  166. }
  167. if (isset($service['synthetic'])) {
  168. $definition->setSynthetic($service['synthetic']);
  169. }
  170. if (isset($service['public'])) {
  171. $definition->setPublic($service['public']);
  172. }
  173. if (isset($service['abstract'])) {
  174. $definition->setAbstract($service['abstract']);
  175. }
  176. if (isset($service['factory_class'])) {
  177. $definition->setFactoryClass($service['factory_class']);
  178. }
  179. if (isset($service['factory_method'])) {
  180. $definition->setFactoryMethod($service['factory_method']);
  181. }
  182. if (isset($service['factory_service'])) {
  183. $definition->setFactoryService($service['factory_service']);
  184. }
  185. if (isset($service['file'])) {
  186. $definition->setFile($service['file']);
  187. }
  188. if (isset($service['arguments'])) {
  189. $definition->setArguments($this->resolveServices($service['arguments']));
  190. }
  191. if (isset($service['configurator'])) {
  192. if (is_string($service['configurator'])) {
  193. $definition->setConfigurator($service['configurator']);
  194. } else {
  195. $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
  196. }
  197. }
  198. if (isset($service['calls'])) {
  199. foreach ($service['calls'] as $call) {
  200. $definition->addMethodCall($call[0], $this->resolveServices($call[1]));
  201. }
  202. }
  203. if (isset($service['tags'])) {
  204. if (!is_array($service['tags'])) {
  205. throw new \InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file));
  206. }
  207. foreach ($service['tags'] as $tag) {
  208. if (!isset($tag['name'])) {
  209. throw new \InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key must be an array for service "%s" in %s.', $id, $file));
  210. }
  211. $name = $tag['name'];
  212. unset($tag['name']);
  213. $definition->addTag($name, $tag);
  214. }
  215. }
  216. $this->container->setDefinition($id, $definition);
  217. }
  218. /**
  219. * Loads a YAML file.
  220. *
  221. * @param string $file
  222. * @return array The file content
  223. */
  224. protected function loadFile($file)
  225. {
  226. return $this->validate(Yaml::load($file), $file);
  227. }
  228. /**
  229. * Validates a YAML file.
  230. *
  231. * @param mixed $content
  232. * @param string $file
  233. * @return array
  234. *
  235. * @throws \InvalidArgumentException When service file is not valid
  236. */
  237. protected function validate($content, $file)
  238. {
  239. if (null === $content) {
  240. return $content;
  241. }
  242. if (!is_array($content)) {
  243. throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
  244. }
  245. foreach (array_keys($content) as $namespace) {
  246. if (in_array($namespace, array('imports', 'parameters', 'services', 'interfaces'))) {
  247. continue;
  248. }
  249. if (!$this->container->hasExtension($namespace)) {
  250. throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $namespace, $file));
  251. }
  252. }
  253. return $content;
  254. }
  255. /**
  256. * Resolves services.
  257. *
  258. * @param string $value
  259. * @return void
  260. */
  261. protected function resolveServices($value)
  262. {
  263. if (is_array($value)) {
  264. $value = array_map(array($this, 'resolveServices'), $value);
  265. } else if (is_string($value) && 0 === strpos($value, '@')) {
  266. if (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. $value = new Reference($value, $invalidBehavior, $strict);
  280. }
  281. return $value;
  282. }
  283. /**
  284. * Loads from Extensions
  285. *
  286. * @param array $content
  287. * @return void
  288. */
  289. protected function loadFromExtensions($content)
  290. {
  291. foreach ($content as $namespace => $values) {
  292. if (in_array($namespace, array('imports', 'parameters', 'services', 'interfaces'))) {
  293. continue;
  294. }
  295. if (!is_array($values)) {
  296. $values = array();
  297. }
  298. $this->container->loadFromExtension($namespace, $values);
  299. }
  300. }
  301. }