vendor/pimcore/pimcore/lib/Routing/Dynamic/DataObjectRouteHandler.php line 94

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Routing\Dynamic;
  16. use Pimcore\Bundle\CoreBundle\EventListener\Frontend\ElementListener;
  17. use Pimcore\Config;
  18. use Pimcore\Http\Request\Resolver\SiteResolver;
  19. use Pimcore\Http\RequestHelper;
  20. use Pimcore\Model\DataObject;
  21. use Pimcore\Model\Document;
  22. use Pimcore\Routing\DataObjectRoute;
  23. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  24. use Symfony\Component\Routing\RouteCollection;
  25. /**
  26.  * @internal
  27.  */
  28. final class DataObjectRouteHandler implements DynamicRouteHandlerInterface
  29. {
  30.     /**
  31.      * @var Document\Service
  32.      */
  33.     private $documentService;
  34.     /**
  35.      * @var SiteResolver
  36.      */
  37.     private $siteResolver;
  38.     /**
  39.      * @var RequestHelper
  40.      */
  41.     private $requestHelper;
  42.     /**
  43.      * @var Config
  44.      */
  45.     private $config;
  46.     /**
  47.      * @param Document\Service $documentService
  48.      * @param SiteResolver $siteResolver
  49.      * @param RequestHelper $requestHelper
  50.      */
  51.     public function __construct(
  52.         Document\Service $documentService,
  53.         SiteResolver $siteResolver,
  54.         RequestHelper $requestHelper,
  55.         Config $config
  56.     ) {
  57.         $this->documentService $documentService;
  58.         $this->siteResolver $siteResolver;
  59.         $this->requestHelper $requestHelper;
  60.         $this->config $config;
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function getRouteByName(string $name)
  66.     {
  67.         if (preg_match('/^data_object_(\d+)_(.*)$/'$name$match)) {
  68.             $slug DataObject\Data\UrlSlug::resolveSlug($match[2]);
  69.             if ($slug && $slug->getObjectId() == $match[1]) {
  70.                 /** @var DataObject\Concrete $object * */
  71.                 $object DataObject::getById($match[1]);
  72.                 if ($object instanceof DataObject\Concrete && $object->isPublished()) {
  73.                     return $this->buildRouteForFromSlug($slug$object);
  74.                 }
  75.             }
  76.         }
  77.         throw new RouteNotFoundException(sprintf("Route for name '%s' was not found"$name));
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function matchRequest(RouteCollection $collectionDynamicRequestContext $context)
  83.     {
  84.         $slug null;
  85.         $site $this->siteResolver->getSite($context->getRequest());
  86.         $slug DataObject\Data\UrlSlug::resolveSlug($context->getOriginalPath(), $site $site->getId() : 0);
  87.         if ($slug) {
  88.             $object DataObject::getById($slug->getObjectId());
  89.             if ($object instanceof DataObject\Concrete && $object->isPublished()) {
  90.                 $route $this->buildRouteForFromSlug($slug$object);
  91.                 $collection->add($route->getRouteKey(), $route);
  92.             }
  93.         }
  94.     }
  95.     /**
  96.      * @param DataObject\Data\UrlSlug $slug
  97.      * @param DataObject\Concrete $object
  98.      *
  99.      * @return DataObjectRoute
  100.      *
  101.      * @throws \Exception
  102.      */
  103.     private function buildRouteForFromSlug(DataObject\Data\UrlSlug $slugDataObject\Concrete $object): DataObjectRoute
  104.     {
  105.         $route = new DataObjectRoute($slug->getSlug());
  106.         $route->setOption('utf8'true);
  107.         $route->setObject($object);
  108.         $route->setSlug($slug);
  109.         $route->setDefault('_controller'$slug->getAction());
  110.         $route->setDefault('object'$object);
  111.         $route->setDefault('urlSlug'$slug);
  112.         $route->setDefault(
  113.             ElementListener::FORCE_ALLOW_PROCESSING_UNPUBLISHED_ELEMENTS,
  114.             $this->config['routing']['allow_processing_unpublished_fallback_document']
  115.         );
  116.         return $route;
  117.     }
  118. }