vendor/pimcore/pimcore/lib/Templating/HelperBroker/HelperShortcuts.php line 109

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\HelperBroker;
  15. use Pimcore\Http\RequestHelper;
  16. use Pimcore\Templating\Helper\Translate;
  17. use Pimcore\Templating\PhpEngine;
  18. use Symfony\Bundle\FrameworkBundle\Templating\Helper\RouterHelper;
  19. use Symfony\Component\HttpFoundation\Request;
  20. /**
  21.  * Shortcuts available as $this->method() on the engine
  22.  */
  23. class HelperShortcuts implements HelperBrokerInterface
  24. {
  25.     /**
  26.      * @var RequestHelper
  27.      */
  28.     protected $requestHelper;
  29.     /**
  30.      * Supported methods
  31.      *
  32.      * @var array
  33.      */
  34.     protected $shortcuts = [
  35.         'getLocale',
  36.         'getRequest',
  37.         'path',
  38.         'url',
  39.         't'
  40.     ];
  41.     /**
  42.      * @param RequestHelper $requestHelper
  43.      */
  44.     public function __construct(RequestHelper $requestHelper)
  45.     {
  46.         $this->requestHelper $requestHelper;
  47.     }
  48.     /**
  49.      * @inheritDoc
  50.      */
  51.     public function supports(PhpEngine $engine$method)
  52.     {
  53.         return in_array($method$this->shortcuts);
  54.     }
  55.     /**
  56.      * @inheritDoc
  57.      */
  58.     public function helper(PhpEngine $engine$method, array $arguments)
  59.     {
  60.         return call_user_func_array([$this$method], [$engine$arguments]);
  61.     }
  62.     /**
  63.      * @return string
  64.      */
  65.     protected function getLocale()
  66.     {
  67.         return $this->requestHelper->getCurrentRequest()->getLocale();
  68.     }
  69.     /**
  70.      * @return Request
  71.      */
  72.     protected function getRequest()
  73.     {
  74.         return $this->requestHelper->getCurrentRequest();
  75.     }
  76.     /**
  77.      * @param PhpEngine $engine
  78.      * @param array $arguments
  79.      *
  80.      * @return string
  81.      */
  82.     protected function url(PhpEngine $engine, array $arguments)
  83.     {
  84.         /** @var RouterHelper $helper */
  85.         $helper $engine->get('router');
  86.         return call_user_func_array([$helper'url'], $arguments);
  87.     }
  88.     /**
  89.      * @param PhpEngine $engine
  90.      * @param array $arguments
  91.      *
  92.      * @return string
  93.      */
  94.     protected function path(PhpEngine $engine, array $arguments)
  95.     {
  96.         /** @var RouterHelper $helper */
  97.         $helper $engine->get('router');
  98.         return call_user_func_array([$helper'path'], $arguments);
  99.     }
  100.     /**
  101.      * @param PhpEngine $engine
  102.      * @param array $arguments
  103.      *
  104.      * @return string
  105.      */
  106.     protected function t(PhpEngine $engine, array $arguments)
  107.     {
  108.         /** @var Translate $helper */
  109.         $helper $engine->get('translate');
  110.         return $helper(...$arguments);
  111.     }
  112. }