vendor/shopware/core/Framework/Adapter/Twig/SecurityExtension.php line 110

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Twig;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Twig\Extension\AbstractExtension;
  5. use Twig\TwigFilter;
  6. /**
  7.  * @internal
  8.  */
  9. #[Package('system-settings')]
  10. class SecurityExtension extends AbstractExtension
  11. {
  12.     /**
  13.      * @var array<string>
  14.      */
  15.     private array $allowedPHPFunctions;
  16.     /**
  17.      * @param array<string> $allowedPHPFunctions
  18.      */
  19.     public function __construct(array $allowedPHPFunctions)
  20.     {
  21.         $this->allowedPHPFunctions $allowedPHPFunctions;
  22.     }
  23.     /**
  24.      * @return TwigFilter[]
  25.      */
  26.     public function getFilters(): array
  27.     {
  28.         return [
  29.             new TwigFilter('map', [$this'map']),
  30.             new TwigFilter('reduce', [$this'reduce']),
  31.             new TwigFilter('filter', [$this'filter']),
  32.             new TwigFilter('sort', [$this'sort']),
  33.         ];
  34.     }
  35.     /**
  36.      * @param iterable<mixed> $array
  37.      * @param string|callable|\Closure $function
  38.      *
  39.      * @return array<mixed>
  40.      */
  41.     public function map(iterable $array$function): array
  42.     {
  43.         if (\is_string($function) && !\in_array($function$this->allowedPHPFunctionstrue)) {
  44.             throw new \RuntimeException(sprintf('Function "%s" is not allowed'$function));
  45.         }
  46.         $result = [];
  47.         foreach ($array as $key => $value) {
  48.             // @phpstan-ignore-next-line
  49.             $result[$key] = $function($value);
  50.         }
  51.         return $result;
  52.     }
  53.     /**
  54.      * @param iterable<mixed> $array
  55.      * @param string|callable|\Closure $function
  56.      * @param mixed $initial
  57.      *
  58.      * @return mixed
  59.      */
  60.     public function reduce(iterable $array$function$initial null)
  61.     {
  62.         if (\is_string($function) && !\in_array($function$this->allowedPHPFunctionstrue)) {
  63.             throw new \RuntimeException(sprintf('Function "%s" is not allowed'$function));
  64.         }
  65.         if (!\is_array($array)) {
  66.             $array iterator_to_array($array);
  67.         }
  68.         // @phpstan-ignore-next-line
  69.         return array_reduce($array$function$initial);
  70.     }
  71.     /**
  72.      * @param iterable<mixed> $array
  73.      * @param string|callable|\Closure $arrow
  74.      *
  75.      * @return iterable<mixed>
  76.      */
  77.     public function filter(iterable $array$arrow): iterable
  78.     {
  79.         if (\is_string($arrow) && !\in_array($arrow$this->allowedPHPFunctionstrue)) {
  80.             throw new \RuntimeException(sprintf('Function "%s" is not allowed'$arrow));
  81.         }
  82.         if (\is_array($array)) {
  83.             // @phpstan-ignore-next-line
  84.             return array_filter($array$arrow\ARRAY_FILTER_USE_BOTH);
  85.         }
  86.         // @phpstan-ignore-next-line
  87.         return new \CallbackFilterIterator(new \IteratorIterator($array), $arrow);
  88.     }
  89.     /**
  90.      * @param iterable<mixed> $array
  91.      * @param string|callable|\Closure|null $arrow
  92.      *
  93.      * @return array<mixed>
  94.      */
  95.     public function sort(iterable $array$arrow null): array
  96.     {
  97.         if (\is_string($arrow) && !\in_array($arrow$this->allowedPHPFunctionstrue)) {
  98.             throw new \RuntimeException(sprintf('Function "%s" is not allowed'$arrow));
  99.         }
  100.         if ($array instanceof \Traversable) {
  101.             $array iterator_to_array($array);
  102.         }
  103.         if ($arrow !== null) {
  104.             // @phpstan-ignore-next-line
  105.             uasort($array$arrow);
  106.         } else {
  107.             asort($array);
  108.         }
  109.         return $array;
  110.     }
  111. }