PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/routing/Loader/YamlFileLoader.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 236 lines | 149 code | 31 blank | 56 comment | 28 complexity | 45a5446f48149c2d88b1a21d608cb54f 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\Routing\Loader;
  11. use Symfony\Component\Routing\RouteCollection;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. use Symfony\Component\Yaml\Exception\ParseException;
  15. use Symfony\Component\Yaml\Parser as YamlParser;
  16. use Symfony\Component\Config\Loader\FileLoader;
  17. /**
  18. * YamlFileLoader loads Yaml routing files.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Tobias Schultze <http://tobion.de>
  22. */
  23. class YamlFileLoader extends FileLoader
  24. {
  25. private static $availableKeys = array(
  26. 'resource', 'type', 'prefix', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition',
  27. );
  28. private $yamlParser;
  29. /**
  30. * Loads a Yaml file.
  31. *
  32. * @param string $file A Yaml file path
  33. * @param string|null $type The resource type
  34. *
  35. * @return RouteCollection A RouteCollection instance
  36. *
  37. * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
  38. */
  39. public function load($file, $type = null)
  40. {
  41. $path = $this->locator->locate($file);
  42. if (!stream_is_local($path)) {
  43. throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
  44. }
  45. if (!file_exists($path)) {
  46. throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
  47. }
  48. if (null === $this->yamlParser) {
  49. $this->yamlParser = new YamlParser();
  50. }
  51. try {
  52. $parsedConfig = $this->yamlParser->parse(file_get_contents($path));
  53. } catch (ParseException $e) {
  54. throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
  55. }
  56. $collection = new RouteCollection();
  57. $collection->addResource(new FileResource($path));
  58. // empty file
  59. if (null === $parsedConfig) {
  60. return $collection;
  61. }
  62. // not an array
  63. if (!is_array($parsedConfig)) {
  64. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
  65. }
  66. foreach ($parsedConfig as $name => $config) {
  67. if (isset($config['pattern'])) {
  68. if (isset($config['path'])) {
  69. throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
  70. }
  71. @trigger_error(sprintf('The "pattern" option in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.', $path), E_USER_DEPRECATED);
  72. $config['path'] = $config['pattern'];
  73. unset($config['pattern']);
  74. }
  75. $this->validate($config, $name, $path);
  76. if (isset($config['resource'])) {
  77. $this->parseImport($collection, $config, $path, $file);
  78. } else {
  79. $this->parseRoute($collection, $name, $config, $path);
  80. }
  81. }
  82. return $collection;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function supports($resource, $type = null)
  88. {
  89. return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
  90. }
  91. /**
  92. * Parses a route and adds it to the RouteCollection.
  93. *
  94. * @param RouteCollection $collection A RouteCollection instance
  95. * @param string $name Route name
  96. * @param array $config Route definition
  97. * @param string $path Full path of the YAML file being processed
  98. */
  99. protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
  100. {
  101. $defaults = isset($config['defaults']) ? $config['defaults'] : array();
  102. $requirements = isset($config['requirements']) ? $config['requirements'] : array();
  103. $options = isset($config['options']) ? $config['options'] : array();
  104. $host = isset($config['host']) ? $config['host'] : '';
  105. $schemes = isset($config['schemes']) ? $config['schemes'] : array();
  106. $methods = isset($config['methods']) ? $config['methods'] : array();
  107. $condition = isset($config['condition']) ? $config['condition'] : null;
  108. if (isset($requirements['_method'])) {
  109. if (0 === count($methods)) {
  110. $methods = explode('|', $requirements['_method']);
  111. }
  112. unset($requirements['_method']);
  113. @trigger_error(sprintf('The "_method" requirement of route "%s" in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "methods" option instead.', $name, $path), E_USER_DEPRECATED);
  114. }
  115. if (isset($requirements['_scheme'])) {
  116. if (0 === count($schemes)) {
  117. $schemes = explode('|', $requirements['_scheme']);
  118. }
  119. unset($requirements['_scheme']);
  120. @trigger_error(sprintf('The "_scheme" requirement of route "%s" in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "schemes" option instead.', $name, $path), E_USER_DEPRECATED);
  121. }
  122. $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
  123. $collection->add($name, $route);
  124. }
  125. /**
  126. * Parses an import and adds the routes in the resource to the RouteCollection.
  127. *
  128. * @param RouteCollection $collection A RouteCollection instance
  129. * @param array $config Route definition
  130. * @param string $path Full path of the YAML file being processed
  131. * @param string $file Loaded file name
  132. */
  133. protected function parseImport(RouteCollection $collection, array $config, $path, $file)
  134. {
  135. $type = isset($config['type']) ? $config['type'] : null;
  136. $prefix = isset($config['prefix']) ? $config['prefix'] : '';
  137. $defaults = isset($config['defaults']) ? $config['defaults'] : array();
  138. $requirements = isset($config['requirements']) ? $config['requirements'] : array();
  139. $options = isset($config['options']) ? $config['options'] : array();
  140. $host = isset($config['host']) ? $config['host'] : null;
  141. $condition = isset($config['condition']) ? $config['condition'] : null;
  142. $schemes = isset($config['schemes']) ? $config['schemes'] : null;
  143. $methods = isset($config['methods']) ? $config['methods'] : null;
  144. $this->setCurrentDir(dirname($path));
  145. $subCollection = $this->import($config['resource'], $type, false, $file);
  146. /* @var $subCollection RouteCollection */
  147. $subCollection->addPrefix($prefix);
  148. if (null !== $host) {
  149. $subCollection->setHost($host);
  150. }
  151. if (null !== $condition) {
  152. $subCollection->setCondition($condition);
  153. }
  154. if (null !== $schemes) {
  155. $subCollection->setSchemes($schemes);
  156. }
  157. if (null !== $methods) {
  158. $subCollection->setMethods($methods);
  159. }
  160. $subCollection->addDefaults($defaults);
  161. $subCollection->addRequirements($requirements);
  162. $subCollection->addOptions($options);
  163. $collection->addCollection($subCollection);
  164. }
  165. /**
  166. * Validates the route configuration.
  167. *
  168. * @param array $config A resource config
  169. * @param string $name The config key
  170. * @param string $path The loaded file path
  171. *
  172. * @throws \InvalidArgumentException If one of the provided config keys is not supported,
  173. * something is missing or the combination is nonsense
  174. */
  175. protected function validate($config, $name, $path)
  176. {
  177. if (!is_array($config)) {
  178. throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
  179. }
  180. if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
  181. throw new \InvalidArgumentException(sprintf(
  182. 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
  183. $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
  184. ));
  185. }
  186. if (isset($config['resource']) && isset($config['path'])) {
  187. throw new \InvalidArgumentException(sprintf(
  188. 'The routing file "%s" must not specify both the "resource" key and the "path" key for "%s". Choose between an import and a route definition.',
  189. $path, $name
  190. ));
  191. }
  192. if (!isset($config['resource']) && isset($config['type'])) {
  193. throw new \InvalidArgumentException(sprintf(
  194. 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
  195. $name, $path
  196. ));
  197. }
  198. if (!isset($config['resource']) && !isset($config['path'])) {
  199. throw new \InvalidArgumentException(sprintf(
  200. 'You must define a "path" for the route "%s" in file "%s".',
  201. $name, $path
  202. ));
  203. }
  204. }
  205. }