lib/composer/RoutingBundle/src/VisualMedia/RoutingBundle/Service/Router.php line 140

Open in your IDE?
  1. <?php
  2. /**
  3. * Router
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\RoutingBundle\Service;
  8. use Exception;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  13. use Symfony\Component\Routing\RequestContext;
  14. use Symfony\Component\Routing\Router as SymfonyRouter;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Symfony\Component\Routing\RouteCollection;
  17. use VisualMedia\LisaBundle\Component\ManagerData;
  18. use VisualMedia\RoutingBundle\Controller\RedirectController;
  19. use VisualMedia\RoutingBundle\Entity\BaseRedirect;
  20. use VisualMedia\RoutingBundle\Entity\BaseRoute;
  21. use VisualMedia\TranslationBundle\Service\ChannelProvider;
  22. /**
  23. * Router
  24. */
  25. class Router implements RouterInterface, WarmableInterface
  26. {
  27. /**
  28. * @var RequestStack
  29. */
  30. protected $requestStack;
  31. /**
  32. * @var RouterInterface
  33. */
  34. protected $router;
  35. /**
  36. * @var EntityManagerInterface
  37. */
  38. protected $manager;
  39. /**
  40. * @var ChannelProvider
  41. */
  42. protected $channelProvider;
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function __construct(RequestStack $requestStack, RouterInterface $router, EntityManagerInterface $manager)
  47. {
  48. $this->requestStack = $requestStack;
  49. $this->router = $router;
  50. $this->manager = $manager;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function initialize(ChannelProvider $channelProvider)
  56. {
  57. $this->channelProvider = $channelProvider;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function warmup(string $cacheDir)
  63. {
  64. // ...
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getRouteCollection()
  70. {
  71. return $this->router->getRouteCollection();
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getContext()
  77. {
  78. return $this->router->getContext();
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function setContext(RequestContext $context)
  84. {
  85. $this->router->setContext($context);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH)
  91. {
  92. try {
  93. return $this->router->generate($name, $parameters, $referenceType);
  94. }
  95. catch (Exception $exception) {
  96. return '#';
  97. }
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function match($pathinfo)
  103. {
  104. // Match non-fallback system routes.
  105. try {
  106. if (is_array($match = $this->router->match($pathinfo))) {
  107. $collection = $this->router->getRouteCollection();
  108. if (null !== $route = $collection->get($match['_route'])) {
  109. if (!$route->getOption('fallback')) {
  110. return $match;
  111. }
  112. //dd($this->router, $match, $match['_route']);
  113. }
  114. }
  115. } catch (Exception $exception) {}
  116. // Match database routes.
  117. $locale = $this->channelProvider->getCurrentLocale();
  118. if (null !== $domain = $this->channelProvider->getCurrentDomain()) {
  119. $request = $this->requestStack->getMasterRequest();
  120. $manager = $this->manager->getRepository(BaseRoute::class);
  121. $data = [
  122. 'url' => $pathinfo,
  123. 'active' => true
  124. ];
  125. if($this->channelProvider->hasMultiple()){
  126. $data = [
  127. 'url' => $pathinfo,
  128. 'channel_id' => $domain->channel->id,
  129. 'locale_id' => $locale->id,
  130. 'active' => true
  131. ];
  132. }
  133. $route = $manager->findOneBy($data);
  134. if ($route) {
  135. if ($request->getLocale() !== $locale = $route->locale->code) {
  136. $request->setLocale($locale);
  137. }
  138. $data = array_merge([
  139. '_route' => $route->name,
  140. '_controller' => $route->getController(),
  141. ], $route->getParameters());
  142. return $data;
  143. }
  144. }
  145. // Match redirects.
  146. $manager = $this->manager->getRepository(BaseRedirect::class);
  147. $data = [
  148. 'urlFrom' => $pathinfo,
  149. 'active' => true,
  150. ];
  151. if($this->channelProvider->hasMultiple()){
  152. $data = [
  153. 'urlFrom' => $pathinfo,
  154. 'active' => true,
  155. 'channel_id' => $domain->channel->id,
  156. 'locale_id' => $locale->id,
  157. ];
  158. }
  159. $redirect = $manager->getFirst(new ManagerData($data));
  160. if ($redirect) {
  161. $result = [
  162. '_route' => 'redirect',
  163. '_controller' => sprintf('%s::%s', RedirectController::class, 'redirectAction'),
  164. 'url' => $redirect->urlTo,
  165. 'code' => $redirect->type,
  166. ];
  167. return $result;
  168. }
  169. // Match fallback system routes.
  170. return $this->router->match($pathinfo);
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function getOption($option)
  176. {
  177. return $this->router->getOption($option);
  178. }
  179. }