src/Module/Cms/Routing/CmsRouter.php line 54

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Module\Cms\Routing;
  4. use App\Module\Cms\Provider\CmsContentProvider;
  5. use BestIt\Routing\Router\AbstractRouter;
  6. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  7. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  8. /**
  9. * Router for cms pages
  10. *
  11. * @author André Varelmann <andre.varelmann@bestit-online.de>
  12. * @package App\Module\Cms\Routing
  13. */
  14. class CmsRouter extends AbstractRouter
  15. {
  16. private CmsContentProvider $provider;
  17. /**
  18. * CmsRouter constructor.
  19. *
  20. * @param CmsContentProvider $provider
  21. */
  22. public function __construct(CmsContentProvider $provider)
  23. {
  24. $this->provider = $provider;
  25. }
  26. /**
  27. * Generates a URL or path for a specific route based on the given parameters.
  28. *
  29. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  30. *
  31. * @throws RouteNotFoundException
  32. *
  33. * @return string
  34. */
  35. public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
  36. {
  37. throw new RouteNotFoundException('Not implemented. This router only handles cms routes');
  38. }
  39. /**
  40. * Tries to match a URL path with a set of routes.
  41. *
  42. * @param string $pathinfo
  43. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  44. *
  45. * @return array
  46. */
  47. public function match(string $pathinfo): array
  48. {
  49. $content = $this->provider->getContentBySlugForCurrentPage();
  50. if ($content !== null) {
  51. return [
  52. '_controller' => 'App\Module\Cms\Action\GetContentAction::__invoke',
  53. 'cmsContent' => $content
  54. ];
  55. }
  56. if (preg_match('#(/.{2})?/story/(?<story>\d{8,15})?#i', $pathinfo, $matches) && !empty($matches['story'])) {
  57. $content = $this->provider->getContentById($matches['story']);
  58. if ($content !== null) {
  59. return [
  60. '_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction',
  61. 'path' => $content->slug ,
  62. 'permanent' => true,
  63. ];
  64. }
  65. }
  66. throw new ResourceNotFoundException('Path could not be matched.');
  67. }
  68. }