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

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.     /**
  60.      * @var Config
  61.      */
  62.     protected $config;
  63.     public function __construct(RequestContext $contextConfigNormalizer $configNormalizerConfig $config)
  64.     {
  65.         $this->context $context;
  66.         $this->configNormalizer $configNormalizer;
  67.         $this->config $config;
  68.     }
  69.     /**
  70.      * @inheritDoc
  71.      */
  72.     public function setContext(RequestContext $context)
  73.     {
  74.         $this->context $context;
  75.     }
  76.     /**
  77.      * @inheritDoc
  78.      */
  79.     public function getContext()
  80.     {
  81.         return $this->context;
  82.     }
  83.     public function getLocaleParams(): array
  84.     {
  85.         return $this->localeParams;
  86.     }
  87.     public function setLocaleParams(array $localeParams)
  88.     {
  89.         $this->localeParams $localeParams;
  90.     }
  91.     /**
  92.      * @inheritDoc
  93.      */
  94.     public function supports($name)
  95.     {
  96.         return is_string($name) && in_array($name$this->getSupportedNames());
  97.     }
  98.     /**
  99.      * @inheritDoc
  100.      */
  101.     public function getRouteCollection()
  102.     {
  103.         return new RouteCollection();
  104.     }
  105.     /**
  106.      * @inheritDoc
  107.      */
  108.     public function getRouteDebugMessage($name, array $parameters = [])
  109.     {
  110.         return (string)$name;
  111.     }
  112.     /**
  113.      * @inheritDoc
  114.      */
  115.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH)
  116.     {
  117.         // when using $name = false we don't use the default route (happens when $name = null / ZF default behavior)
  118.         // but just the query string generation using the given parameters
  119.         // eg. $this->url(["foo" => "bar"], false) => /?foo=bar
  120.         if ($name === null) {
  121.             if (Staticroute::getCurrentRoute() instanceof Staticroute) {
  122.                 $name Staticroute::getCurrentRoute()->getName();
  123.             }
  124.         }
  125.         // ABSOLUTE_URL = http://example.com
  126.         // NETWORK_PATH = //example.com
  127.         $needsHostname self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType;
  128.         $siteId null;
  129.         if (Site::isSiteRequest()) {
  130.             $siteId Site::getCurrentSite()->getId();
  131.         }
  132.         // check for a site in the options, if valid remove it from the options
  133.         $hostname null;
  134.         if (isset($parameters['site'])) {
  135.             $site $parameters['site'];
  136.             if (!empty($site)) {
  137.                 if ($site Site::getBy($site)) {
  138.                     unset($parameters['site']);
  139.                     $hostname $site->getMainDomain();
  140.                     if ($site->getId() !== $siteId) {
  141.                         $needsHostname true;
  142.                         $siteId $site->getId();
  143.                     }
  144.                 } else {
  145.                     $this->logger->warning('The site {site} does not exist for route {route}', [
  146.                         'site' => $siteId,
  147.                         'route' => $name,
  148.                     ]);
  149.                 }
  150.             } else {
  151.                 if ($needsHostname && !empty($this->config['general']['domain'])) {
  152.                     $hostname $this->config['general']['domain'];
  153.                 }
  154.             }
  155.         }
  156.         if (null === $hostname && $needsHostname) {
  157.             $hostname $this->context->getHost();
  158.         }
  159.         if ($name && $route Staticroute::getByName($name$siteId)) {
  160.             $reset = isset($parameters['reset']) ? (bool)$parameters['reset'] : false;
  161.             $encode = isset($parameters['encode']) ? (bool)$parameters['encode'] : true;
  162.             unset($parameters['encode']);
  163.             // assemble the route / url in Staticroute::assemble()
  164.             $url $route->assemble($parameters$reset$encode);
  165.             $port '';
  166.             $scheme $this->context->getScheme() ;
  167.             if ('http' === $scheme && 80 !== $this->context->getHttpPort()) {
  168.                 $port ':'.$this->context->getHttpPort();
  169.             } elseif ('https' === $scheme && 443 !== $this->context->getHttpsPort()) {
  170.                 $port ':'.$this->context->getHttpsPort();
  171.             }
  172.             $schemeAuthority self::NETWORK_PATH === $referenceType || '' === $scheme '//' "$scheme://";
  173.             $schemeAuthority .= $hostname.$port;
  174.             if ($needsHostname) {
  175.                 $url $schemeAuthority.$this->context->getBaseUrl().$url;
  176.             } else {
  177.                 if (self::RELATIVE_PATH === $referenceType) {
  178.                     $url UrlGenerator::getRelativePath($this->context->getPathInfo(), $url);
  179.                 } else {
  180.                     $url $this->context->getBaseUrl().$url;
  181.                 }
  182.             }
  183.             return $url;
  184.         }
  185.         throw new RouteNotFoundException(sprintf('Could not generate URL for route %s as the static route wasn\'t found'$name));
  186.     }
  187.     /**
  188.      * @inheritDoc
  189.      */
  190.     public function matchRequest(Request $request)
  191.     {
  192.         return $this->doMatch($request->getPathInfo());
  193.     }
  194.     /**
  195.      * @inheritDoc
  196.      */
  197.     public function match($pathinfo)
  198.     {
  199.         return $this->doMatch($pathinfo);
  200.     }
  201.     /**
  202.      * @param string $pathinfo
  203.      *
  204.      * @return array
  205.      */
  206.     protected function doMatch($pathinfo)
  207.     {
  208.         $pathinfo urldecode($pathinfo);
  209.         $params $this->context->getParameters();
  210.         $params array_merge(Tool::getRoutingDefaults(), $params);
  211.         foreach ($this->getStaticRoutes() as $route) {
  212.             if ($routeParams $route->match($pathinfo$params)) {
  213.                 Staticroute::setCurrentRoute($route);
  214.                 // add the route object also as parameter to the request object, this is needed in
  215.                 // Pimcore_Controller_Action_Frontend::getRenderScript()
  216.                 // to determine if a call to an action was made through a staticroute or not
  217.                 // more on that infos see Pimcore_Controller_Action_Frontend::getRenderScript()
  218.                 $routeParams['pimcore_request_source'] = 'staticroute';
  219.                 $routeParams['_route'] = $route->getName();
  220.                 $routeParams $this->processRouteParams($routeParams);
  221.                 return $routeParams;
  222.             }
  223.         }
  224.         throw new ResourceNotFoundException(sprintf('No routes found for "%s".'$pathinfo));
  225.     }
  226.     /**
  227.      * @param array $routeParams
  228.      *
  229.      * @return array
  230.      */
  231.     protected function processRouteParams(array $routeParams)
  232.     {
  233.         $keys = [
  234.             'module',
  235.             'controller',
  236.             'action'
  237.         ];
  238.         $controllerParams = [];
  239.         foreach ($keys as $key) {
  240.             $value null;
  241.             if (isset($routeParams[$key])) {
  242.                 $value $routeParams[$key];
  243.             }
  244.             $controllerParams[$key] = $value;
  245.         }
  246.         $controller $this->configNormalizer->formatControllerReference(
  247.             $controllerParams['module'],
  248.             $controllerParams['controller'],
  249.             $controllerParams['action']
  250.         );
  251.         $routeParams['_controller'] = $controller;
  252.         // map common language properties (e.g. language) to _locale if not set
  253.         if (!isset($routeParams['_locale'])) {
  254.             foreach ($this->localeParams as $localeParam) {
  255.                 if (isset($routeParams[$localeParam])) {
  256.                     $routeParams['_locale'] = $routeParams[$localeParam];
  257.                     break;
  258.                 }
  259.             }
  260.         }
  261.         return $routeParams;
  262.     }
  263.     /**
  264.      * @return Staticroute[]
  265.      */
  266.     protected function getStaticRoutes()
  267.     {
  268.         if (null === $this->staticRoutes) {
  269.             /** @var Staticroute\Listing|Staticroute\Listing\Dao $list */
  270.             $list = new Staticroute\Listing();
  271.             $list->setOrder(function ($a$b) {
  272.                 // give site ids a higher priority
  273.                 if ($a['siteId'] && !$b['siteId']) {
  274.                     return -1;
  275.                 }
  276.                 if (!$a['siteId'] && $b['siteId']) {
  277.                     return 1;
  278.                 }
  279.                 if ($a['priority'] == $b['priority']) {
  280.                     return 0;
  281.                 }
  282.                 return ($a['priority'] < $b['priority']) ? : -1;
  283.             });
  284.             $this->staticRoutes $list->load();
  285.         }
  286.         return $this->staticRoutes;
  287.     }
  288.     /**
  289.      * @return array
  290.      */
  291.     protected function getSupportedNames()
  292.     {
  293.         if (null === $this->supportedNames) {
  294.             $this->supportedNames = [];
  295.             foreach ($this->getStaticRoutes() as $route) {
  296.                 $this->supportedNames[] = $route->getName();
  297.             }
  298.             $this->supportedNames array_unique($this->supportedNames);
  299.         }
  300.         return $this->supportedNames;
  301.     }
  302. }