vendor/ezsystems/ezcommerce-shop/src/Silversolutions/Bundle/EshopBundle/Routing/StandardRouter.php line 14

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 Silversolutions\Bundle\EshopBundle\Routing;
  7. use eZ\Bundle\EzPublishCoreBundle\Routing\UrlAliasRouter;
  8. use Silversolutions\Bundle\EshopBundle\Service\CatalogHelper;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  11. class StandardRouter extends UrlAliasRouter
  12. {
  13.     /**
  14.      * @var CatalogHelper
  15.      */
  16.     protected $catalogHelper;
  17.     /**
  18.      * inject services.
  19.      *
  20.      * @param CatalogHelper $catalogHelper
  21.      */
  22.     public function setServices(
  23.         CatalogHelper $catalogHelper
  24.     ) {
  25.         $this->catalogHelper $catalogHelper;
  26.     }
  27.     /**
  28.      * @param Request $request
  29.      *
  30.      * @return array|null
  31.      */
  32.     public function matchRequest(Request $request)
  33.     {
  34.         $routeInfo null;
  35.         $currentUrl rawurldecode($request->getPathInfo());
  36.         $currentUrlWithOutSiteaccess rawurldecode($request->attributes->get('semanticPathinfo'$request->getPathInfo()));
  37.         //if legacy_mode, let the parent handle the request
  38.         if ($this->hasLegacyMode()) {
  39.             return parent::matchRequest($request);
  40.         }
  41.         // check if is a product URL
  42.         if ($this->isFrontEnd($currentUrl)
  43.             && is_array($catalogUrls $this->catalogHelper->getCatalogElementUrls())
  44.         ) {
  45.             if (is_array($catalogUrls)) {
  46.                 foreach ($catalogUrls as $catalogUrl) {
  47.                     if (
  48.                            isset($catalogUrl['url']) &&
  49.                            (
  50.                                 strpos($currentUrl$catalogUrl['url'], 0) === ||
  51.                                 strpos($currentUrlWithOutSiteaccess$catalogUrl['url'], 0) === 0
  52.                            )
  53.                         ) {
  54.                         $currentUrl $request->attributes->get('semanticPathinfo'$request->getPathInfo());
  55.                         return [
  56.                             '_route' => $request->server->get('SCRIPT_URL'),
  57.                             '_controller' => 'silver_eshop.catalog_controller:showAction',
  58.                             'url' => $currentUrl,
  59.                         ];
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         if ($routeInfo) {
  65.             return $routeInfo;
  66.         }
  67.         throw new ResourceNotFoundException('Route for silver-eshop not found, not an product url');
  68.     }
  69.     /**
  70.      * returns true if current siteAccess has legacy_mode: true.
  71.      *
  72.      * @return bool
  73.      */
  74.     protected function hasLegacyMode()
  75.     {
  76.         if ($this->configResolver->hasParameter('legacy_mode')) {
  77.             return $this->configResolver->getParameter('legacy_mode');
  78.         }
  79.         return false;
  80.     }
  81.     /**
  82.      * Returns true, if the current URL is not related to AJAX, Symfony toolbar, profiler etc.
  83.      *
  84.      * @param $url
  85.      *
  86.      * @return bool
  87.      */
  88.     protected function isFrontEnd($url)
  89.     {
  90.         return strpos($url'/_ajax_/') === false           // ajax requests (Phalanx etc.)
  91.         && strpos($url'/_wdt/') === false             // web-developer toolbar (Symfony)
  92.         && strpos($url'/_profiler/') === false        // profiler (Symfony)
  93.         && strpos($url'/admin') === false        // Admin backnd
  94.         && strpos($url'/bundles/') === false;         // static asserts from bundles
  95.     }
  96. }