vendor/pimcore/pimcore/lib/Routing/Staticroute/Router.php line 300

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Routing\Staticroute;
  15. use Pimcore\Config;
  16. use Pimcore\Controller\Config\ConfigNormalizer;
  17. use Pimcore\Model\Site;
  18. use Pimcore\Model\Staticroute;
  19. use Pimcore\Tool;
  20. use Psr\Log\LoggerAwareInterface;
  21. use Psr\Log\LoggerAwareTrait;
  22. use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  25. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  26. use Symfony\Component\Routing\Generator\UrlGenerator;
  27. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  28. use Symfony\Component\Routing\RequestContext;
  29. use Symfony\Component\Routing\RouteCollection;
  30. use Symfony\Component\Routing\RouterInterface;
  31. /**
  32.  * A custom router implementation handling pimcore static routes.
  33.  */
  34. class Router implements RouterInterfaceRequestMatcherInterfaceVersatileGeneratorInterfaceLoggerAwareInterface
  35. {
  36.     use LoggerAwareTrait;
  37.     /**
  38.      * @var RequestContext
  39.      */
  40.     protected $context;
  41.     /**
  42.      * @var ConfigNormalizer
  43.      */
  44.     protected $configNormalizer;
  45.     /**
  46.      * @var Staticroute[]
  47.      */
  48.     protected $staticRoutes;
  49.     /**
  50.      * @var array
  51.      */
  52.     protected $supportedNames;
  53.     /**
  54.      * Params which are treated as _locale if no _locale attribute is set
  55.      *
  56.      * @var array
  57.      */
  58.     protected $localeParams = [];
  59.     public function __construct(RequestContext $contextConfigNormalizer $configNormalizer)
  60.     {
  61.         $this->context $context;
  62.         $this->configNormalizer $configNormalizer;
  63.     }
  64.     /**
  65.      * @inheritDoc
  66.      */
  67.     public function setContext(RequestContext $context)
  68.     {
  69.         $this->context $context;
  70.     }
  71.     /**
  72.      * @inheritDoc
  73.      */
  74.     public function getContext()
  75.     {
  76.         return $this->context;
  77.     }
  78.     public function getLocaleParams(): array
  79.     {
  80.         return $this->localeParams;
  81.     }
  82.     public function setLocaleParams(array $localeParams)
  83.     {
  84.         $this->localeParams $localeParams;
  85.     }
  86.     /**
  87.      * @inheritDoc
  88.      */
  89.     public function supports($name)
  90.     {
  91.         return is_string($name) && in_array($name$this->getSupportedNames());
  92.     }
  93.     /**
  94.      * @inheritDoc
  95.      */
  96.     public function getRouteCollection()
  97.     {
  98.         return new RouteCollection();
  99.     }
  100.     /**
  101.      * @inheritDoc
  102.      */
  103.     public function getRouteDebugMessage($name, array $parameters = [])
  104.     {
  105.         return (string)$name;
  106.     }
  107.     /**
  108.      * @inheritDoc
  109.      */
  110.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH)
  111.     {
  112.         // when using $name = false we don't use the default route (happens when $name = null / ZF default behavior)
  113.         // but just the query string generation using the given parameters
  114.         // eg. $this->url(["foo" => "bar"], false) => /?foo=bar
  115.         if ($name === null) {
  116.             if (Staticroute::getCurrentRoute() instanceof Staticroute) {
  117.                 $name Staticroute::getCurrentRoute()->getName();
  118.             }
  119.         }
  120.         // ABSOLUTE_URL = http://example.com
  121.         // NETWORK_PATH = //example.com
  122.         $needsHostname self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType;
  123.         $siteId null;
  124.         if (Site::isSiteRequest()) {
  125.             $siteId Site::getCurrentSite()->getId();
  126.         }
  127.         // check for a site in the options, if valid remove it from the options
  128.         $hostname null;
  129.         if (isset($parameters['site'])) {
  130.             $config Config::getSystemConfig();
  131.             $site $parameters['site'];
  132.             if (!empty($site)) {
  133.                 if ($site Site::getBy($site)) {
  134.                     unset($parameters['site']);
  135.                     $hostname $site->getMainDomain();
  136.                     if ($site->getId() !== $siteId) {
  137.                         $needsHostname true;
  138.                         $siteId $site->getId();
  139.                     }
  140.                 } else {
  141.                     $this->logger->warning('The site {site} does not exist for route {route}', [
  142.                         'site' => $siteId,
  143.                         'route' => $name,
  144.                     ]);
  145.                 }
  146.             } else {
  147.                 if ($needsHostname && !empty($config->general->domain)) {
  148.                     $hostname $config->general->domain;
  149.                 }
  150.             }
  151.         }
  152.         if (null === $hostname && $needsHostname) {
  153.             $hostname $this->context->getHost();
  154.         }
  155.         if ($name && $route Staticroute::getByName($name$siteId)) {
  156.             $reset = isset($parameters['reset']) ? (bool)$parameters['reset'] : false;
  157.             $encode = isset($parameters['encode']) ? (bool)$parameters['encode'] : true;
  158.             unset($parameters['encode']);
  159.             // assemble the route / url in Staticroute::assemble()
  160.             $url $route->assemble($parameters$reset$encode);
  161.             if ($needsHostname) {
  162.                 if (self::ABSOLUTE_URL === $referenceType) {
  163.                     $url $this->context->getScheme() . '://' $hostname $url;
  164.                 } else {
  165.                     $url '//' $hostname $url;
  166.                 }
  167.             } else {
  168.                 if (self::RELATIVE_PATH === $referenceType) {
  169.                     $url UrlGenerator::getRelativePath($this->context->getPathInfo(), $url);
  170.                 }
  171.             }
  172.             return $url;
  173.         }
  174.         throw new RouteNotFoundException(sprintf('Could not generate URL for route %s as the static route wasn\'t found'$name));
  175.     }
  176.     /**
  177.      * @inheritDoc
  178.      */
  179.     public function matchRequest(Request $request)
  180.     {
  181.         return $this->doMatch($request->getPathInfo());
  182.     }
  183.     /**
  184.      * @inheritDoc
  185.      */
  186.     public function match($pathinfo)
  187.     {
  188.         return $this->doMatch($pathinfo);
  189.     }
  190.     /**
  191.      * @param string $pathinfo
  192.      *
  193.      * @return array
  194.      */
  195.     protected function doMatch($pathinfo)
  196.     {
  197.         $pathinfo urldecode($pathinfo);
  198.         $params $this->context->getParameters();
  199.         $params array_merge(Tool::getRoutingDefaults(), $params);
  200.         foreach ($this->getStaticRoutes() as $route) {
  201.             if ($routeParams $route->match($pathinfo$params)) {
  202.                 Staticroute::setCurrentRoute($route);
  203.                 // add the route object also as parameter to the request object, this is needed in
  204.                 // Pimcore_Controller_Action_Frontend::getRenderScript()
  205.                 // to determine if a call to an action was made through a staticroute or not
  206.                 // more on that infos see Pimcore_Controller_Action_Frontend::getRenderScript()
  207.                 $routeParams['pimcore_request_source'] = 'staticroute';
  208.                 $routeParams['_route'] = $route->getName();
  209.                 $routeParams $this->processRouteParams($routeParams);
  210.                 return $routeParams;
  211.             }
  212.         }
  213.         throw new ResourceNotFoundException(sprintf('No routes found for "%s".'$pathinfo));
  214.     }
  215.     /**
  216.      * @param array $routeParams
  217.      *
  218.      * @return array
  219.      */
  220.     protected function processRouteParams(array $routeParams)
  221.     {
  222.         $keys = [
  223.             'module',
  224.             'controller',
  225.             'action'
  226.         ];
  227.         $controllerParams = [];
  228.         foreach ($keys as $key) {
  229.             $value null;
  230.             if (isset($routeParams[$key])) {
  231.                 $value $routeParams[$key];
  232.             }
  233.             $controllerParams[$key] = $value;
  234.         }
  235.         $controller $this->configNormalizer->formatControllerReference(
  236.             $controllerParams['module'],
  237.             $controllerParams['controller'],
  238.             $controllerParams['action']
  239.         );
  240.         $routeParams['_controller'] = $controller;
  241.         // map common language properties (e.g. language) to _locale if not set
  242.         if (!isset($routeParams['_locale'])) {
  243.             foreach ($this->localeParams as $localeParam) {
  244.                 if (isset($routeParams[$localeParam])) {
  245.                     $routeParams['_locale'] = $routeParams[$localeParam];
  246.                     break;
  247.                 }
  248.             }
  249.         }
  250.         return $routeParams;
  251.     }
  252.     /**
  253.      * @return Staticroute[]
  254.      */
  255.     protected function getStaticRoutes()
  256.     {
  257.         if (null === $this->staticRoutes) {
  258.             /** @var Staticroute\Listing|Staticroute\Listing\Dao $list */
  259.             $list = new Staticroute\Listing();
  260.             $list->setOrder(function ($a$b) {
  261.                 // give site ids a higher priority
  262.                 if ($a['siteId'] && !$b['siteId']) {
  263.                     return -1;
  264.                 }
  265.                 if (!$a['siteId'] && $b['siteId']) {
  266.                     return 1;
  267.                 }
  268.                 if ($a['priority'] == $b['priority']) {
  269.                     return 0;
  270.                 }
  271.                 return ($a['priority'] < $b['priority']) ? : -1;
  272.             });
  273.             $this->staticRoutes $list->load();
  274.         }
  275.         return $this->staticRoutes;
  276.     }
  277.     /**
  278.      * @return array
  279.      */
  280.     protected function getSupportedNames()
  281.     {
  282.         if (null === $this->supportedNames) {
  283.             $this->supportedNames = [];
  284.             foreach ($this->getStaticRoutes() as $route) {
  285.                 $this->supportedNames[] = $route->getName();
  286.             }
  287.             $this->supportedNames array_unique($this->supportedNames);
  288.         }
  289.         return $this->supportedNames;
  290.     }
  291. }