vendor/pimcore/pimcore/lib/Twig/Extension/TemplatingHelperExtension.php line 120

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Enterprise License (PEL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  14.  */
  15. namespace Pimcore\Twig\Extension;
  16. use Pimcore\Templating\PhpEngine;
  17. use Symfony\Component\Templating\Helper\HelperInterface;
  18. use Twig\Extension\AbstractExtension;
  19. use Twig\TwigFunction;
  20. /**
  21.  * Delegates calls to PHP templating helpers. Use this only with templating helpers which do not rely
  22.  * on PHP rendering!
  23.  */
  24. class TemplatingHelperExtension extends AbstractExtension
  25. {
  26.     /**
  27.      * @var PhpEngine
  28.      */
  29.     private $phpEngine;
  30.     /**
  31.      * @param PhpEngine $phpEngine
  32.      */
  33.     public function __construct(PhpEngine $phpEngine)
  34.     {
  35.         $this->phpEngine $phpEngine;
  36.     }
  37.     public function getFunctions(): array
  38.     {
  39.         $helperNames = [
  40.             'headLink' => 'pimcore_head_link',
  41.             'headMeta' => 'pimcore_head_meta',
  42.             'headScript' => 'pimcore_head_script',
  43.             'headStyle' => 'pimcore_head_style',
  44.             'headTitle' => 'pimcore_head_title',
  45.             'inlineScript' => 'pimcore_inline_script',
  46.             'breachAttackRandomContent' => 'pimcore_breach_attack_random_content',
  47.             'placeholder' => 'pimcore_placeholder',
  48.             'cache' => 'pimcore_cache',
  49.             'device' => 'pimcore_device',
  50.             'pimcoreUrl' => [
  51.                 'name' => 'pimcore_url',
  52.                 'is_safe' => null,
  53.             ],
  54.         ];
  55.         $functions = [];
  56.         foreach ($helperNames as $helperName => $helperOptions) {
  57.             $functionName null;
  58.             $options = [
  59.                 'is_safe' => ['html'],
  60.             ];
  61.             if (is_string($helperOptions)) {
  62.                 $functionName $helperOptions;
  63.             } else {
  64.                 if (!isset($helperOptions['name'])) {
  65.                     throw new \LogicException('A helper declaration needs to define a Twig function name');
  66.                 }
  67.                 $functionName $helperOptions['name'];
  68.                 unset($helperOptions['name']);
  69.                 $options array_merge($options$helperOptions);
  70.             }
  71.             $callable = function () use ($helperName) {
  72.                 return $this->callHelper($helperNamefunc_get_args());
  73.             };
  74.             $functions[] = new TwigFunction($functionName$callable$options);
  75.         }
  76.         return $functions;
  77.         // those are just for auto-complete, not nice, but works ;-)
  78.         new TwigFunction('pimcore_head_link');
  79.         new TwigFunction('pimcore_head_meta');
  80.         new TwigFunction('pimcore_head_script');
  81.         new TwigFunction('pimcore_head_style');
  82.         new TwigFunction('pimcore_head_title');
  83.         new TwigFunction('pimcore_inline_script');
  84.         new TwigFunction('pimcore_placeholder');
  85.         new TwigFunction('pimcore_cache');
  86.         new TwigFunction('pimcore_device');
  87.         new TwigFunction('pimcore_url');
  88.         new TwigFunction('pimcore_breach_attack_random_content');
  89.     }
  90.     /**
  91.      * Calls a helper with arguments
  92.      *
  93.      * @param string $helperName
  94.      * @param array $arguments
  95.      *
  96.      * @return mixed|HelperInterface
  97.      */
  98.     public function callHelper(string $helperName, array $arguments = [])
  99.     {
  100.         $helper $this->phpEngine->get($helperName);
  101.         // helper implements __invoke -> run it directly
  102.         if (is_callable($helper)) {
  103.             return call_user_func_array($helper$arguments);
  104.         }
  105.         return $helper;
  106.     }
  107. }