vendor/pimcore/pimcore/lib/Templating/Helper/PimcoreUrl.php line 114

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\Templating\Helper;
  15. use Pimcore\Http\RequestHelper;
  16. use Pimcore\Model\DataObject\Concrete;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Templating\Helper\Helper;
  19. class PimcoreUrl extends Helper
  20. {
  21.     /**
  22.      * @var UrlGeneratorInterface
  23.      */
  24.     protected $generator;
  25.     /**
  26.      * @var RequestHelper
  27.      */
  28.     protected $requestHelper;
  29.     /**
  30.      * @param UrlGeneratorInterface $generator
  31.      * @param RequestHelper $requestHelper
  32.      */
  33.     public function __construct(UrlGeneratorInterface $generatorRequestHelper $requestHelper)
  34.     {
  35.         $this->generator $generator;
  36.         $this->requestHelper $requestHelper;
  37.     }
  38.     /**
  39.      * @param array $urlOptions
  40.      * @param string|null $name
  41.      * @param bool $reset
  42.      * @param bool $encode
  43.      * @param bool $relative
  44.      *
  45.      * @return string
  46.      */
  47.     public function __invoke(array $urlOptions = [], $name null$reset false$encode true$relative false)
  48.     {
  49.         // merge all parameters from request to parameters
  50.         if (!$reset && $this->requestHelper->hasMasterRequest()) {
  51.             $urlOptions array_replace($this->requestHelper->getMasterRequest()->query->all(), $urlOptions);
  52.         }
  53.         return $this->generateUrl($name$urlOptions$relative UrlGeneratorInterface::RELATIVE_PATH UrlGeneratorInterface::ABSOLUTE_PATH$encode);
  54.     }
  55.     /**
  56.      * Generate URL with support to only pass parameters ZF1 style (defaults to current route).
  57.      *
  58.      * @param string|null $name
  59.      * @param array $parameters
  60.      * @param int $referenceType
  61.      * @param bool $encode
  62.      *
  63.      * @return string
  64.      */
  65.     protected function generateUrl($name null$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_PATH$encode true)
  66.     {
  67.         if ($encode !== true) {
  68.             // encoding is default anyway, so we only set it when really necessary, to minimize the risk of
  69.             // side-effects when using parameters for that purpose (other routers may not be aware of param `encode`
  70.             $parameters['encode'] = $encode;
  71.         }
  72.         // if name is an array, treat it as parameters
  73.         if (is_array($name)) {
  74.             if (is_array($parameters)) {
  75.                 $parameters array_merge($name$parameters);
  76.             } else {
  77.                 $parameters $name;
  78.             }
  79.             $name null;
  80.         }
  81.         // get name from current route
  82.         if (null === $name) {
  83.             $name $this->getCurrentRoute();
  84.         }
  85.         if (isset($parameters['object']) && $parameters['object'] instanceof Concrete) {
  86.             /** @var Concrete $object */
  87.             $object $parameters['object'];
  88.             if ($linkGenerator $object->getClass()->getLinkGenerator()) {
  89.                 unset($parameters['object']);
  90.                 $path $linkGenerator->generate($object, [
  91.                     'route' => $name,
  92.                     'parameters' => $parameters,
  93.                     'context' => $this,
  94.                     'referenceType' => $referenceType,
  95.                 ]);
  96.                 return $path;
  97.             }
  98.         }
  99.         if ($name !== null) {
  100.             return $this->generator->generate($name$parameters$referenceType);
  101.         }
  102.         return '';
  103.     }
  104.     /**
  105.      * Tries to get the current route name from current or master request
  106.      *
  107.      * @return string|null
  108.      */
  109.     protected function getCurrentRoute()
  110.     {
  111.         $route null;
  112.         if ($this->requestHelper->hasCurrentRequest()) {
  113.             $route $this->requestHelper->getCurrentRequest()->attributes->get('_route');
  114.         }
  115.         if (!$route && $this->requestHelper->hasMasterRequest()) {
  116.             $route $this->requestHelper->getMasterRequest()->attributes->get('_route');
  117.         }
  118.         return $route;
  119.     }
  120.     /**
  121.      * Returns the canonical name of this helper.
  122.      *
  123.      * @return string The canonical name
  124.      */
  125.     public function getName()
  126.     {
  127.         return 'pimcoreUrl';
  128.     }
  129. }