vendor/pimcore/pimcore/lib/Templating/Renderer/ActionRenderer.php line 62

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\Renderer;
  15. use Pimcore\Controller\Config\ConfigNormalizer;
  16. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  17. use Pimcore\Model\Document;
  18. use Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper;
  19. use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
  20. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  21. class ActionRenderer
  22. {
  23.     /**
  24.      * @var ActionsHelper
  25.      */
  26.     protected $actionsHelper;
  27.     /**
  28.      * @var ConfigNormalizer
  29.      */
  30.     protected $configNormalizer;
  31.     /**
  32.      * @param ActionsHelper $actionsHelper
  33.      * @param ConfigNormalizer $configNormalizer
  34.      */
  35.     public function __construct(ActionsHelper $actionsHelperConfigNormalizer $configNormalizer)
  36.     {
  37.         $this->actionsHelper $actionsHelper;
  38.         $this->configNormalizer $configNormalizer;
  39.     }
  40.     /**
  41.      * Render an URI
  42.      *
  43.      * @param string $uri     A URI
  44.      * @param array  $options An array of options
  45.      *
  46.      * @return string
  47.      *
  48.      * @see ActionsHelper::render()
  49.      */
  50.     public function render($uri, array $options = [])
  51.     {
  52.         if ($uri instanceof Document\PageSnippet) {
  53.             $uri $this->createDocumentReference($uri);
  54.         }
  55.         return $this->actionsHelper->render($uri$options);
  56.     }
  57.     /**
  58.      * Create a controller reference
  59.      *
  60.      * @param string $bundle
  61.      * @param string $controller
  62.      * @param string $action
  63.      * @param array $attributes
  64.      * @param array $query
  65.      *
  66.      * @return ControllerReference
  67.      */
  68.     public function createControllerReference($bundle$controller$action, array $attributes = [], array $query = [])
  69.     {
  70.         $controller $this->configNormalizer->formatControllerReference(
  71.             $bundle,
  72.             $controller,
  73.             $action
  74.         );
  75.         return $this->actionsHelper->controller($controller$attributes$query);
  76.     }
  77.     /**
  78.      * Create a document controller reference
  79.      *
  80.      * @param Document\PageSnippet $document
  81.      * @param array $attributes
  82.      * @param array $query
  83.      *
  84.      * @return ControllerReference
  85.      */
  86.     public function createDocumentReference(Document\PageSnippet $document, array $attributes = [], array $query = [])
  87.     {
  88.         $attributes $this->addDocumentAttributes($document$attributes);
  89.         return $this->createControllerReference(
  90.             $document->getModule(),
  91.             $document->getController(),
  92.             $document->getAction(),
  93.             $attributes,
  94.             $query
  95.         );
  96.     }
  97.     /**
  98.      * Add document params to params array
  99.      *
  100.      * @param Document\PageSnippet $document
  101.      * @param array $attributes
  102.      * @param string $context
  103.      *
  104.      * @return array
  105.      */
  106.     public function addDocumentAttributes(Document\PageSnippet $document, array $attributes = [], string $context PimcoreContextResolver::CONTEXT_DEFAULT)
  107.     {
  108.         if (null !== $context) {
  109.             // document needs to be rendered with default context as the context guesser can't resolve the
  110.             // context from a fragment route
  111.             $attributes[PimcoreContextResolver::ATTRIBUTE_PIMCORE_CONTEXT] = $context;
  112.         }
  113.         // The CMF dynamic router sets the 2 attributes contentDocument and contentTemplate to set
  114.         // a route's document and template. Those attributes are later used by controller listeners to
  115.         // determine what to render. By injecting those attributes into the sub-request we can rely on
  116.         // the same rendering logic as in the routed request.
  117.         $attributes[DynamicRouter::CONTENT_KEY] = $document;
  118.         if ($document->getTemplate()) {
  119.             $template $this->configNormalizer->normalizeTemplateName($document->getTemplate());
  120.             $attributes[DynamicRouter::CONTENT_TEMPLATE] = $template;
  121.         }
  122.         if ($language $document->getProperty('language')) {
  123.             $attributes['_locale'] = $language;
  124.         }
  125.         return $attributes;
  126.     }
  127. }