vendor/bitbag/cms-plugin/src/Twig/Parser/ContentParser.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file was created by developers working at BitBag
  4.  * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5.  * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\SyliusCmsPlugin\Twig\Parser;
  9. use Webmozart\Assert\Assert;
  10. final class ContentParser implements ContentParserInterface
  11. {
  12.     /** @var \Twig_Environment */
  13.     private $twigEnvironment;
  14.     /** @var array */
  15.     private $enabledFunctions;
  16.     public function __construct(\Twig_Environment $twigEnvironment, array $enabledFunctions)
  17.     {
  18.         $this->twigEnvironment $twigEnvironment;
  19.         $this->enabledFunctions $enabledFunctions;
  20.     }
  21.     public function parse(string $input): string
  22.     {
  23.         $functions $this->twigEnvironment->getFunctions();
  24.         preg_match_all('`{{\s*(?P<method>[^\(]+)\s*\((?P<arguments>[^\)]*)\)\s*}}`'$input$callMatches);
  25.         foreach ($callMatches[0] as $index => $call) {
  26.             $function $callMatches['method'][$index];
  27.             if (!in_array($function$this->enabledFunctionstrue)) {
  28.                 continue;
  29.             }
  30.             if (null !== $arguments $this->getFunctionArguments($function$call)) {
  31.                 try {
  32.                     $functionResult $this->callFunction($functions$function$arguments);
  33.                 } catch (\Exception $exception) {
  34.                     $functionResult '';
  35.                 }
  36.                 $input str_replace($call$functionResult$input);
  37.             }
  38.         }
  39.         return $input;
  40.     }
  41.     private function getFunctionArguments(string $functionNamestring $input): ?array
  42.     {
  43.         $start '{{ ' $functionName '(';
  44.         $end ') }}';
  45.         /** @var string[]|false $functionParts */
  46.         $functionParts explode($start$input);
  47.         if (false !== $functionParts && isset($functionParts[1])) {
  48.             $functionParts explode($end$functionParts[1]);
  49.             $arguments explode(','$functionParts[0]);
  50.             return array_map(function (string $element): string {
  51.                 return trim(trim($element), '\'');
  52.             }, $arguments);
  53.         }
  54.         return null;
  55.     }
  56.     private function callFunction(
  57.         array $functions,
  58.         string $functionName,
  59.         array $arguments
  60.     ): string {
  61.         Assert::keyExists($functions$functionNamesprintf('Function %s does not exist!'$functionName));
  62.         /** @var \Twig_Function $function */
  63.         $function $functions[$functionName];
  64.         $callable $function->getCallable();
  65.         Assert::isArray($callablesprintf('Function with name "%s" is not callable'$functionName));
  66.         $extension $callable[0];
  67.         $extensionMethod $callable[1];
  68.         $callback = [$extension$extensionMethod];
  69.         Assert::isCallable($callback);
  70.         return call_user_func_array($callback$arguments);
  71.     }
  72. }