vendor/pimcore/pimcore/lib/Controller/FrontendController.php line 35

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\Controller;
  15. use Pimcore\Controller\Traits\TemplateControllerTrait;
  16. use Pimcore\Http\Request\Resolver\DocumentResolver;
  17. use Pimcore\Http\Request\Resolver\EditmodeResolver;
  18. use Pimcore\Http\Request\Resolver\ResponseHeaderResolver;
  19. use Pimcore\Http\Request\Resolver\ViewModelResolver;
  20. use Pimcore\Model\Document;
  21. use Pimcore\Templating\Model\ViewModel;
  22. use Pimcore\Templating\Renderer\EditableRenderer;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  26. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  27. /**
  28.  * @property ViewModel $view
  29.  * @property Document|Document\PageSnippet $document
  30.  * @property bool $editmode
  31.  */
  32. abstract class FrontendController extends Controller implements EventedControllerInterfaceTemplateControllerInterface
  33. {
  34.     use TemplateControllerTrait;
  35.     /**
  36.      * Expose view, document and editmode as properties and proxy them to request attributes through
  37.      * their resolvers.
  38.      *
  39.      * @inheritDoc
  40.      */
  41.     public function __get($name)
  42.     {
  43.         if ('view' === $name) {
  44.             return $this->get(ViewModelResolver::class)->getViewModel();
  45.         }
  46.         if ('document' === $name) {
  47.             return $this->get(DocumentResolver::class)->getDocument();
  48.         }
  49.         if ('editmode' === $name) {
  50.             return $this->get(EditmodeResolver::class)->isEditmode();
  51.         }
  52.         throw new \RuntimeException(sprintf('Trying to read undefined property "%s"'$name));
  53.     }
  54.     /**
  55.      * @inheritDoc
  56.      */
  57.     public function __set($name$value)
  58.     {
  59.         $requestAttributes = ['view''document''editmode'];
  60.         if (in_array($name$requestAttributes)) {
  61.             throw new \RuntimeException(sprintf(
  62.                 'Property "%s" is a request attribute and can\'t be set on the controller instance',
  63.                 $name
  64.             ));
  65.         }
  66.         throw new \RuntimeException(sprintf('Trying to set unknown property "%s"'$name));
  67.     }
  68.     /**
  69.      * @inheritDoc
  70.      */
  71.     public function onKernelController(FilterControllerEvent $event)
  72.     {
  73.         // enable view auto-rendering
  74.         $this->setViewAutoRender($event->getRequest(), true'php');
  75.     }
  76.     /**
  77.      * @inheritDoc
  78.      */
  79.     public function onKernelResponse(FilterResponseEvent $event)
  80.     {
  81.     }
  82.     /**
  83.      * Enable view autorendering for the current request
  84.      *
  85.      * @param Request $request
  86.      * @param string $engine
  87.      *
  88.      * @deprecated
  89.      */
  90.     protected function enableViewAutoRender(Request $request null$engine 'php')
  91.     {
  92.         if (null === $request) {
  93.             $request $this->get('request_stack')->getCurrentRequest();
  94.         }
  95.         $this->setViewAutoRender($requesttrue$engine);
  96.     }
  97.     /**
  98.      * Disable view autorendering for the current request
  99.      *
  100.      * @param Request $request
  101.      *
  102.      * @deprecated
  103.      */
  104.     protected function disableViewAutoRender(Request $request null)
  105.     {
  106.         if (null === $request) {
  107.             $request $this->get('request_stack')->getCurrentRequest();
  108.         }
  109.         $this->setViewAutoRender($requestfalse);
  110.     }
  111.     /**
  112.      * We don't have a response object at this point, but we can add headers here which will be
  113.      * set by the ResponseHeaderListener which reads and adds this headers in the kernel.response event.
  114.      *
  115.      * @param string $key
  116.      * @param array|string $values
  117.      * @param bool $replace
  118.      * @param Request|null $request
  119.      */
  120.     protected function addResponseHeader(string $key$valuesbool $replace falseRequest $request null)
  121.     {
  122.         if (null === $request) {
  123.             $request $this->get('request_stack')->getCurrentRequest();
  124.         }
  125.         $this->get(ResponseHeaderResolver::class)->addResponseHeader($request$key$values$replace);
  126.     }
  127.     /**
  128.      * Loads a document editable
  129.      *
  130.      * e.g. `$this->getDocumentTag('input', 'foobar')`
  131.      *
  132.      * @param string $type
  133.      * @param string $inputName
  134.      * @param array $options
  135.      * @param Document\PageSnippet|null $document
  136.      *
  137.      * @return null|Document\Tag
  138.      *
  139.      * @deprecated since v6.8 and will be removed in 7. Use getDocumentEditable() instead.
  140.      */
  141.     public function getDocumentTag($type$inputName, array $options = [], Document\PageSnippet $document null)
  142.     {
  143.         return $this->getDocumentEditable($type$inputName$options$document);
  144.     }
  145.     /**
  146.      * Loads a document editable
  147.      *
  148.      * e.g. `$this->getDocumentEditable('input', 'foobar')`
  149.      *
  150.      * @param string $type
  151.      * @param string $inputName
  152.      * @param array $options
  153.      * @param Document\PageSnippet|null $document
  154.      *
  155.      * @return null|Document\Tag
  156.      */
  157.     public function getDocumentEditable($type$inputName, array $options = [], Document\PageSnippet $document null)
  158.     {
  159.         if (null === $document) {
  160.             $document $this->document;
  161.         }
  162.         $editableRenderer $this->container->get(EditableRenderer::class);
  163.         return $editableRenderer->getEditable($document$type$inputName$options);
  164.     }
  165.     /**
  166.      * @param string $view
  167.      * @param array $parameters
  168.      * @param Response|null $response
  169.      *
  170.      * @return \Symfony\Component\HttpFoundation\Response
  171.      */
  172.     public function renderTemplate($view, array $parameters = [], Response $response null)
  173.     {
  174.         $viewModel $this->get(ViewModelResolver::class)->getViewModel();
  175.         $parameters array_merge($viewModel->getAllParameters(), $parameters);
  176.         return $this->render($view$parameters$response);
  177.     }
  178. }