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

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 Commercial License (PCL)
  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 PCL
  13.  */
  14. namespace Pimcore\Templating\Renderer;
  15. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  16. use Pimcore\Model\Document;
  17. use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
  18. use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
  19. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  20. /**
  21.  * @internal
  22.  */
  23. class ActionRenderer
  24. {
  25.     /**
  26.      * @var HttpKernelRuntime
  27.      */
  28.     protected $httpKernelRuntime;
  29.     /**
  30.      * @param HttpKernelRuntime $httpKernelRuntime
  31.      */
  32.     public function __construct(HttpKernelRuntime $httpKernelRuntime)
  33.     {
  34.         $this->httpKernelRuntime $httpKernelRuntime;
  35.     }
  36.     /**
  37.      * Render an URI
  38.      *
  39.      * @param mixed $uri     A URI
  40.      * @param array  $options An array of options
  41.      *
  42.      * @return string
  43.      *
  44.      * @see HttpKernelRuntime::renderFragment()
  45.      */
  46.     public function render($uri, array $options = [])
  47.     {
  48.         if ($uri instanceof Document\PageSnippet) {
  49.             $uri $this->createDocumentReference($uri$options);
  50.         }
  51.         return $this->httpKernelRuntime->renderFragment($uri$options);
  52.     }
  53.     /**
  54.      * Create a document controller reference
  55.      *
  56.      * @param Document\PageSnippet $document
  57.      * @param array $attributes
  58.      * @param array $query
  59.      *
  60.      * @return ControllerReference
  61.      */
  62.     public function createDocumentReference(Document\PageSnippet $document, array $attributes = [], array $query = [])
  63.     {
  64.         $attributes $this->addDocumentAttributes($document$attributes);
  65.         return new ControllerReference($document->getController(), $attributes$query);
  66.     }
  67.     /**
  68.      * Add document params to params array
  69.      *
  70.      * @param Document\PageSnippet $document
  71.      * @param array $attributes
  72.      * @param string $context
  73.      *
  74.      * @return array
  75.      */
  76.     public function addDocumentAttributes(Document\PageSnippet $document, array $attributes = [], string $context PimcoreContextResolver::CONTEXT_DEFAULT)
  77.     {
  78.         if (null !== $context) {
  79.             // document needs to be rendered with default context as the context guesser can't resolve the
  80.             // context from a fragment route
  81.             $attributes[PimcoreContextResolver::ATTRIBUTE_PIMCORE_CONTEXT] = $context;
  82.         }
  83.         // The CMF dynamic router sets the 2 attributes contentDocument and contentTemplate to set
  84.         // a route's document and template. Those attributes are later used by controller listeners to
  85.         // determine what to render. By injecting those attributes into the sub-request we can rely on
  86.         // the same rendering logic as in the routed request.
  87.         $attributes[DynamicRouter::CONTENT_KEY] = $document;
  88.         if ($document->getTemplate()) {
  89.             $attributes[DynamicRouter::CONTENT_TEMPLATE] = $document->getTemplate();
  90.         }
  91.         if ($language $document->getProperty('language')) {
  92.             $attributes['_locale'] = $language;
  93.         }
  94.         return $attributes;
  95.     }
  96. }