PageRenderTime 72ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/routing/RouteCollectionBuilder.php

https://gitlab.com/ealexis.t/trends
PHP | 372 lines | 167 code | 56 blank | 149 comment | 14 complexity | 2f2f9bdbaab6d996fc66190610fadfc6 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;
  11. use Symfony\Component\Config\Exception\FileLoaderLoadException;
  12. use Symfony\Component\Config\Loader\LoaderInterface;
  13. use Symfony\Component\Config\Resource\ResourceInterface;
  14. /**
  15. * Helps add and import routes into a RouteCollection.
  16. *
  17. * @author Ryan Weaver <ryan@knpuniversity.com>
  18. */
  19. class RouteCollectionBuilder
  20. {
  21. /**
  22. * @var Route[]|RouteCollectionBuilder[]
  23. */
  24. private $routes = array();
  25. private $loader;
  26. private $defaults = array();
  27. private $prefix;
  28. private $host;
  29. private $condition;
  30. private $requirements = array();
  31. private $options = array();
  32. private $schemes;
  33. private $methods;
  34. private $resources = array();
  35. /**
  36. * @param LoaderInterface $loader
  37. */
  38. public function __construct(LoaderInterface $loader = null)
  39. {
  40. $this->loader = $loader;
  41. }
  42. /**
  43. * Import an external routing resource and returns the RouteCollectionBuilder.
  44. *
  45. * $routes->import('blog.yml', '/blog');
  46. *
  47. * @param mixed $resource
  48. * @param string|null $prefix
  49. * @param string $type
  50. *
  51. * @return RouteCollectionBuilder
  52. *
  53. * @throws FileLoaderLoadException
  54. */
  55. public function import($resource, $prefix = '/', $type = null)
  56. {
  57. /** @var RouteCollection $collection */
  58. $collection = $this->load($resource, $type);
  59. // create a builder from the RouteCollection
  60. $builder = $this->createBuilder();
  61. foreach ($collection->all() as $name => $route) {
  62. $builder->addRoute($route, $name);
  63. }
  64. foreach ($collection->getResources() as $resource) {
  65. $builder->addResource($resource);
  66. }
  67. // mount into this builder
  68. $this->mount($prefix, $builder);
  69. return $builder;
  70. }
  71. /**
  72. * Adds a route and returns it for future modification.
  73. *
  74. * @param string $path The route path
  75. * @param string $controller The route's controller
  76. * @param string|null $name The name to give this route
  77. *
  78. * @return Route
  79. */
  80. public function add($path, $controller, $name = null)
  81. {
  82. $route = new Route($path);
  83. $route->setDefault('_controller', $controller);
  84. $this->addRoute($route, $name);
  85. return $route;
  86. }
  87. /**
  88. * Returns a RouteCollectionBuilder that can be configured and then added with mount().
  89. *
  90. * @return RouteCollectionBuilder
  91. */
  92. public function createBuilder()
  93. {
  94. return new self($this->loader);
  95. }
  96. /**
  97. * Add a RouteCollectionBuilder.
  98. *
  99. * @param string $prefix
  100. * @param RouteCollectionBuilder $builder
  101. */
  102. public function mount($prefix, RouteCollectionBuilder $builder)
  103. {
  104. $builder->prefix = trim(trim($prefix), '/');
  105. $this->routes[] = $builder;
  106. }
  107. /**
  108. * Adds a Route object to the builder.
  109. *
  110. * @param Route $route
  111. * @param string|null $name
  112. *
  113. * @return $this
  114. */
  115. public function addRoute(Route $route, $name = null)
  116. {
  117. if (null === $name) {
  118. // used as a flag to know which routes will need a name later
  119. $name = '_unnamed_route_'.spl_object_hash($route);
  120. }
  121. $this->routes[$name] = $route;
  122. return $this;
  123. }
  124. /**
  125. * Sets the host on all embedded routes (unless already set).
  126. *
  127. * @param string $pattern
  128. *
  129. * @return $this
  130. */
  131. public function setHost($pattern)
  132. {
  133. $this->host = $pattern;
  134. return $this;
  135. }
  136. /**
  137. * Sets a condition on all embedded routes (unless already set).
  138. *
  139. * @param string $condition
  140. *
  141. * @return $this
  142. */
  143. public function setCondition($condition)
  144. {
  145. $this->condition = $condition;
  146. return $this;
  147. }
  148. /**
  149. * Sets a default value that will be added to all embedded routes (unless that
  150. * default value is already set).
  151. *
  152. * @param string $key
  153. * @param mixed $value
  154. *
  155. * @return $this
  156. */
  157. public function setDefault($key, $value)
  158. {
  159. $this->defaults[$key] = $value;
  160. return $this;
  161. }
  162. /**
  163. * Sets a requirement that will be added to all embedded routes (unless that
  164. * requirement is already set).
  165. *
  166. * @param string $key
  167. * @param mixed $regex
  168. *
  169. * @return $this
  170. */
  171. public function setRequirement($key, $regex)
  172. {
  173. $this->requirements[$key] = $regex;
  174. return $this;
  175. }
  176. /**
  177. * Sets an opiton that will be added to all embedded routes (unless that
  178. * option is already set).
  179. *
  180. * @param string $key
  181. * @param mixed $value
  182. *
  183. * @return $this
  184. */
  185. public function setOption($key, $value)
  186. {
  187. $this->options[$key] = $value;
  188. return $this;
  189. }
  190. /**
  191. * Sets the schemes on all embedded routes (unless already set).
  192. *
  193. * @param array|string $schemes
  194. *
  195. * @return $this
  196. */
  197. public function setSchemes($schemes)
  198. {
  199. $this->schemes = $schemes;
  200. return $this;
  201. }
  202. /**
  203. * Sets the methods on all embedded routes (unless already set).
  204. *
  205. * @param array|string $methods
  206. *
  207. * @return $this
  208. */
  209. public function setMethods($methods)
  210. {
  211. $this->methods = $methods;
  212. return $this;
  213. }
  214. /**
  215. * Adds a resource for this collection.
  216. *
  217. * @param ResourceInterface $resource
  218. *
  219. * @return $this
  220. */
  221. private function addResource(ResourceInterface $resource)
  222. {
  223. $this->resources[] = $resource;
  224. return $this;
  225. }
  226. /**
  227. * Creates the final RouteCollection and returns it.
  228. *
  229. * @return RouteCollection
  230. */
  231. public function build()
  232. {
  233. $routeCollection = new RouteCollection();
  234. foreach ($this->routes as $name => $route) {
  235. if ($route instanceof Route) {
  236. $route->setDefaults(array_merge($this->defaults, $route->getDefaults()));
  237. $route->setOptions(array_merge($this->options, $route->getOptions()));
  238. foreach ($this->requirements as $key => $val) {
  239. if (!$route->hasRequirement($key)) {
  240. $route->setRequirement($key, $val);
  241. }
  242. }
  243. if (null !== $this->prefix) {
  244. $route->setPath('/'.$this->prefix.$route->getPath());
  245. }
  246. if (!$route->getHost()) {
  247. $route->setHost($this->host);
  248. }
  249. if (!$route->getCondition()) {
  250. $route->setCondition($this->condition);
  251. }
  252. if (!$route->getSchemes()) {
  253. $route->setSchemes($this->schemes);
  254. }
  255. if (!$route->getMethods()) {
  256. $route->setMethods($this->methods);
  257. }
  258. // auto-generate the route name if it's been marked
  259. if ('_unnamed_route_' === substr($name, 0, 15)) {
  260. $name = $this->generateRouteName($route);
  261. }
  262. $routeCollection->add($name, $route);
  263. } else {
  264. /* @var self $route */
  265. $subCollection = $route->build();
  266. $subCollection->addPrefix($this->prefix);
  267. $routeCollection->addCollection($subCollection);
  268. }
  269. foreach ($this->resources as $resource) {
  270. $routeCollection->addResource($resource);
  271. }
  272. }
  273. return $routeCollection;
  274. }
  275. /**
  276. * Generates a route name based on details of this route.
  277. *
  278. * @return string
  279. */
  280. private function generateRouteName(Route $route)
  281. {
  282. $methods = implode('_', $route->getMethods()).'_';
  283. $routeName = $methods.$route->getPath();
  284. $routeName = str_replace(array('/', ':', '|', '-'), '_', $routeName);
  285. $routeName = preg_replace('/[^a-z0-9A-Z_.]+/', '', $routeName);
  286. // Collapse consecutive underscores down into a single underscore.
  287. $routeName = preg_replace('/_+/', '_', $routeName);
  288. return $routeName;
  289. }
  290. /**
  291. * Finds a loader able to load an imported resource and loads it.
  292. *
  293. * @param mixed $resource A resource
  294. * @param string|null $type The resource type or null if unknown
  295. *
  296. * @return RouteCollection
  297. *
  298. * @throws FileLoaderLoadException If no loader is found
  299. */
  300. private function load($resource, $type = null)
  301. {
  302. if (null === $this->loader) {
  303. throw new \BadMethodCallException('Cannot import other routing resources: you must pass a LoaderInterface when constructing RouteCollectionBuilder.');
  304. }
  305. if ($this->loader->supports($resource, $type)) {
  306. return $this->loader->load($resource, $type);
  307. }
  308. if (null === $resolver = $this->loader->getResolver()) {
  309. throw new FileLoaderLoadException($resource);
  310. }
  311. if (false === $loader = $resolver->resolve($resource, $type)) {
  312. throw new FileLoaderLoadException($resource);
  313. }
  314. return $loader->load($resource, $type);
  315. }
  316. }