YamlFileLoader.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. foreach ($service['tags'] as $tag) {
  205. $name = $tag['name'];
  206. unset($tag['name']);
  207. $definition->addTag($name, $tag);
  208. }
  209. }
  210. $this->container->setDefinition($id, $definition);
  211. }
  212. /**
  213. * Loads a YAML file.
  214. *
  215. * @param string $file
  216. * @return array The file content
  217. */
  218. protected function loadFile($file)
  219. {
  220. return $this->validate(Yaml::load($file), $file);
  221. }
  222. /**
  223. * Validates a YAML file.
  224. *
  225. * @param mixed $content
  226. * @param string $file
  227. * @return array
  228. *
  229. * @throws \InvalidArgumentException When service file is not valid
  230. */
  231. protected function validate($content, $file)
  232. {
  233. if (null === $content) {
  234. return $content;
  235. }
  236. if (!is_array($content)) {
  237. throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
  238. }
  239. foreach (array_keys($content) as $namespace) {
  240. if (in_array($namespace, array('imports', 'parameters', 'services', 'interfaces'))) {
  241. continue;
  242. }
  243. if (!$this->container->hasExtension($namespace)) {
  244. throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $key, $file));
  245. }
  246. }
  247. return $content;
  248. }
  249. /**
  250. * Resolves services.
  251. *
  252. * @param string $value
  253. * @return void
  254. */
  255. protected function resolveServices($value)
  256. {
  257. if (is_array($value)) {
  258. $value = array_map(array($this, 'resolveServices'), $value);
  259. } else if (is_string($value) && 0 === strpos($value, '@')) {
  260. if (0 === strpos($value, '@?')) {
  261. $value = substr($value, 2);
  262. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  263. } else {
  264. $value = substr($value, 1);
  265. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  266. }
  267. if ('=' === substr($value, -1)) {
  268. $value = substr($value, 0, -1);
  269. $strict = false;
  270. } else {
  271. $strict = true;
  272. }
  273. $value = new Reference($value, $invalidBehavior, $strict);
  274. }
  275. return $value;
  276. }
  277. /**
  278. * Loads from Extensions
  279. *
  280. * @param array $content
  281. * @return void
  282. */
  283. protected function loadFromExtensions($content)
  284. {
  285. foreach ($content as $namespace => $values) {
  286. if (in_array($namespace, array('imports', 'parameters', 'services', 'interfaces'))) {
  287. continue;
  288. }
  289. if (!is_array($values)) {
  290. $values = array();
  291. }
  292. $this->container->loadFromExtension($namespace, $values);
  293. }
  294. }
  295. }