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

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\Model\DataObject;
  20. use Pimcore\Routing\DataObjectRoute;
  21. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  22. use Symfony\Component\Routing\RouteCollection;
  23. /**
  24.  * @internal
  25.  */
  26. final class DataObjectRouteHandler implements DynamicRouteHandlerInterface
  27. {
  28.     /**
  29.      * @var SiteResolver
  30.      */
  31.     private $siteResolver;
  32.     /**
  33.      * @var Config
  34.      */
  35.     private $config;
  36.     /**
  37.      * @param SiteResolver $siteResolver
  38.      * @param Config $config
  39.      */
  40.     public function __construct(
  41.         SiteResolver $siteResolver,
  42.         Config $config
  43.     ) {
  44.         $this->siteResolver $siteResolver;
  45.         $this->config $config;
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public function getRouteByName(string $name)
  51.     {
  52.         if (preg_match('/^data_object_(\d+)_(\d+)_(.*)$/'$name$match)) {
  53.             $slug DataObject\Data\UrlSlug::resolveSlug($match[3], $match[2]);
  54.             if ($slug && $slug->getObjectId() == $match[1]) {
  55.                 /** @var DataObject\Concrete $object * */
  56.                 $object DataObject::getById($match[1]);
  57.                 if ($object instanceof DataObject\Concrete && $object->isPublished()) {
  58.                     return $this->buildRouteForFromSlug($slug$object);
  59.                 }
  60.             }
  61.         }
  62.         throw new RouteNotFoundException(sprintf("Route for name '%s' was not found"$name));
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function matchRequest(RouteCollection $collectionDynamicRequestContext $context)
  68.     {
  69.         $site $this->siteResolver->getSite($context->getRequest());
  70.         $slug DataObject\Data\UrlSlug::resolveSlug($context->getOriginalPath(), $site $site->getId() : 0);
  71.         if ($slug) {
  72.             $object DataObject::getById($slug->getObjectId());
  73.             if ($object instanceof DataObject\Concrete && $object->isPublished()) {
  74.                 $route $this->buildRouteForFromSlug($slug$object);
  75.                 $collection->add($route->getRouteKey(), $route);
  76.             }
  77.         }
  78.     }
  79.     /**
  80.      * @param DataObject\Data\UrlSlug $slug
  81.      * @param DataObject\Concrete $object
  82.      *
  83.      * @return DataObjectRoute
  84.      *
  85.      * @throws \Exception
  86.      */
  87.     private function buildRouteForFromSlug(DataObject\Data\UrlSlug $slugDataObject\Concrete $object): DataObjectRoute
  88.     {
  89.         $site $this->siteResolver->getSite();
  90.         $route = new DataObjectRoute($slug->getSlug());
  91.         $route->setOption('utf8'true);
  92.         $route->setObject($object);
  93.         $route->setSlug($slug);
  94.         $route->setSite($site);
  95.         $route->setDefault('_controller'$slug->getAction());
  96.         $route->setDefault('object'$object);
  97.         $route->setDefault('urlSlug'$slug);
  98.         if ($slug->getOwnertype() === 'localizedfield') {
  99.             $route->setDefault('_locale'$slug->getPosition());
  100.         }
  101.         $route->setDefault(
  102.             ElementListener::FORCE_ALLOW_PROCESSING_UNPUBLISHED_ELEMENTS,
  103.             $this->config['routing']['allow_processing_unpublished_fallback_document']
  104.         );
  105.         return $route;
  106.     }
  107. }