vendor/ezsystems/ezplatform-kernel/eZ/Bundle/EzPublishCoreBundle/Routing/DefaultRouter.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Bundle\EzPublishCoreBundle\Routing;
  7. use eZ\Publish\Core\MVC\ConfigResolverInterface;
  8. use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest;
  9. use eZ\Publish\Core\MVC\Symfony\SiteAccess;
  10. use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware;
  11. use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessRouterInterface;
  12. use eZ\Publish\Core\MVC\Symfony\SiteAccess\URILexer;
  13. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  16. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  17. /**
  18.  * Extension of Symfony default router implementing RequestMatcherInterface.
  19.  */
  20. class DefaultRouter extends Router implements RequestMatcherInterfaceSiteAccessAware
  21. {
  22.     /** @var SiteAccess */
  23.     protected $siteAccess;
  24.     protected $nonSiteAccessAwareRoutes = [];
  25.     /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
  26.     protected $configResolver;
  27.     /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessRouterInterface */
  28.     protected $siteAccessRouter;
  29.     public function setConfigResolver(ConfigResolverInterface $configResolver)
  30.     {
  31.         $this->configResolver $configResolver;
  32.     }
  33.     public function setSiteAccess(SiteAccess $siteAccess null)
  34.     {
  35.         $this->siteAccess $siteAccess;
  36.     }
  37.     /**
  38.      * Injects route names that are not supposed to be SiteAccess aware.
  39.      * i.e. Routes pointing to asset generation (like assetic).
  40.      *
  41.      * @param array $routes
  42.      */
  43.     public function setNonSiteAccessAwareRoutes(array $routes)
  44.     {
  45.         $this->nonSiteAccessAwareRoutes $routes;
  46.     }
  47.     /**
  48.      * @param \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessRouterInterface $siteAccessRouter
  49.      */
  50.     public function setSiteAccessRouter(SiteAccessRouterInterface $siteAccessRouter)
  51.     {
  52.         $this->siteAccessRouter $siteAccessRouter;
  53.     }
  54.     public function matchRequest(Request $request)
  55.     {
  56.         return $this->match($request->attributes->get('semanticPathinfo'$request->getPathInfo()));
  57.     }
  58.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH)
  59.     {
  60.         $siteAccess $this->siteAccess;
  61.         $originalContext $context $this->getContext();
  62.         $isSiteAccessAware $this->isSiteAccessAwareRoute($name);
  63.         // Retrieving the appropriate SiteAccess to generate the link for.
  64.         if (isset($parameters['siteaccess']) && $isSiteAccessAware) {
  65.             $siteAccess $this->siteAccessRouter->matchByName($parameters['siteaccess']);
  66.             if ($siteAccess instanceof SiteAccess && $siteAccess->matcher instanceof SiteAccess\VersatileMatcher) {
  67.                 // Switch request context for link generation.
  68.                 $context $this->getContextBySimplifiedRequest($siteAccess->matcher->getRequest());
  69.                 $this->setContext($context);
  70.             } elseif ($this->logger) {
  71.                 $siteAccess $this->siteAccess;
  72.                 $this->logger->notice("Could not generate a link using provided 'siteaccess' parameter: {$parameters['siteaccess']}. Generating using current context.");
  73.             }
  74.             unset($parameters['siteaccess']);
  75.         }
  76.         try {
  77.             $url parent::generate($name$parameters$referenceType);
  78.         } catch (RouteNotFoundException $e) {
  79.             // Switch back to original context, for next links generation.
  80.             $this->setContext($originalContext);
  81.             throw $e;
  82.         }
  83.         // Now putting back SiteAccess URI if needed.
  84.         if ($isSiteAccessAware && $siteAccess && $siteAccess->matcher instanceof URILexer) {
  85.             if ($referenceType === self::ABSOLUTE_URL || $referenceType === self::NETWORK_PATH) {
  86.                 $scheme $context->getScheme();
  87.                 $port '';
  88.                 if ($scheme === 'http' && $this->context->getHttpPort() != 80) {
  89.                     $port ':' $this->context->getHttpPort();
  90.                 } elseif ($scheme === 'https' && $this->context->getHttpsPort() != 443) {
  91.                     $port ':' $this->context->getHttpsPort();
  92.                 }
  93.                 $base $context->getHost() . $port $context->getBaseUrl();
  94.             } else {
  95.                 $base $context->getBaseUrl();
  96.             }
  97.             $linkUri $base substr($urlstrpos($url$base) + strlen($base)) : $url;
  98.             $url str_replace($linkUri$siteAccess->matcher->analyseLink($linkUri), $url);
  99.         }
  100.         // Switch back to original context, for next links generation.
  101.         $this->setContext($originalContext);
  102.         return $url;
  103.     }
  104.     /**
  105.      * Checks if $routeName is a siteAccess aware route, and thus needs to have siteAccess URI prepended.
  106.      * Will be used for link generation, only in the case of URI SiteAccess matching.
  107.      *
  108.      * @param $routeName
  109.      *
  110.      * @return bool
  111.      */
  112.     protected function isSiteAccessAwareRoute($routeName)
  113.     {
  114.         foreach ($this->nonSiteAccessAwareRoutes as $ignoredPrefix) {
  115.             if (strpos($routeName$ignoredPrefix) === 0) {
  116.                 return false;
  117.             }
  118.         }
  119.         return true;
  120.     }
  121.     /**
  122.      * Merges context from $simplifiedRequest into a clone of the current context.
  123.      *
  124.      * @param \eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest $simplifiedRequest
  125.      *
  126.      * @return \Symfony\Component\Routing\RequestContext
  127.      */
  128.     public function getContextBySimplifiedRequest(SimplifiedRequest $simplifiedRequest)
  129.     {
  130.         $context = clone $this->context;
  131.         if ($simplifiedRequest->scheme) {
  132.             $context->setScheme($simplifiedRequest->scheme);
  133.         }
  134.         if ($simplifiedRequest->port) {
  135.             switch ($simplifiedRequest->scheme) {
  136.                 case 'https':
  137.                     $context->setHttpsPort($simplifiedRequest->port);
  138.                     break;
  139.                 default:
  140.                     $context->setHttpPort($simplifiedRequest->port);
  141.                     break;
  142.             }
  143.         }
  144.         if ($simplifiedRequest->host) {
  145.             $context->setHost($simplifiedRequest->host);
  146.         }
  147.         if ($simplifiedRequest->pathinfo) {
  148.             $context->setPathInfo($simplifiedRequest->pathinfo);
  149.         }
  150.         return $context;
  151.     }
  152. }