vendor/twig/twig/src/AbstractTwigCallable.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Twig;
  11. /**
  12.  * @author Fabien Potencier <fabien@symfony.com>
  13.  */
  14. abstract class AbstractTwigCallable implements TwigCallableInterface
  15. {
  16.     protected $options;
  17.     private $name;
  18.     private $dynamicName;
  19.     private $callable;
  20.     private $arguments;
  21.     public function __construct(string $name$callable null, array $options = [])
  22.     {
  23.         $this->name $this->dynamicName $name;
  24.         $this->callable $callable;
  25.         $this->arguments = [];
  26.         $this->options array_merge([
  27.             'needs_environment' => false,
  28.             'needs_context' => false,
  29.             'needs_charset' => false,
  30.             'needs_is_sandboxed' => false,
  31.             'is_variadic' => false,
  32.             'always_allowed_in_sandbox' => false,
  33.             'deprecation_info' => null,
  34.             'deprecated' => false,
  35.             'deprecating_package' => '',
  36.             'alternative' => null,
  37.         ], $options);
  38.         if ($this->options['deprecation_info'] && !$this->options['deprecation_info'] instanceof DeprecatedCallableInfo) {
  39.             throw new \LogicException(\sprintf('The "deprecation_info" option must be an instance of "%s".'DeprecatedCallableInfo::class));
  40.         }
  41.         if ($this->options['deprecated']) {
  42.             if ($this->options['deprecation_info']) {
  43.                 throw new \LogicException('When setting the "deprecation_info" option, you need to remove the obsolete deprecated options.');
  44.             }
  45.             trigger_deprecation('twig/twig''3.15''Using the "deprecated", "deprecating_package", and "alternative" options is deprecated, pass a "deprecation_info" one instead.');
  46.             $this->options['deprecation_info'] = new DeprecatedCallableInfo(
  47.                 $this->options['deprecating_package'],
  48.                 $this->options['deprecated'],
  49.                 null,
  50.                 $this->options['alternative'],
  51.             );
  52.         }
  53.         if ($this->options['deprecation_info']) {
  54.             $this->options['deprecation_info']->setName($name);
  55.             $this->options['deprecation_info']->setType($this->getType());
  56.         }
  57.     }
  58.     public function __toString(): string
  59.     {
  60.         return \sprintf('%s(%s)', static::class, $this->name);
  61.     }
  62.     public function getName(): string
  63.     {
  64.         return $this->name;
  65.     }
  66.     public function getDynamicName(): string
  67.     {
  68.         return $this->dynamicName;
  69.     }
  70.     /**
  71.      * @return callable|array{class-string, string}|null
  72.      */
  73.     public function getCallable()
  74.     {
  75.         return $this->callable;
  76.     }
  77.     public function getNodeClass(): string
  78.     {
  79.         return $this->options['node_class'];
  80.     }
  81.     public function needsCharset(): bool
  82.     {
  83.         return $this->options['needs_charset'];
  84.     }
  85.     public function needsEnvironment(): bool
  86.     {
  87.         return $this->options['needs_environment'];
  88.     }
  89.     public function needsContext(): bool
  90.     {
  91.         return $this->options['needs_context'];
  92.     }
  93.     public function needsIsSandboxed(): bool
  94.     {
  95.         return $this->options['needs_is_sandboxed'];
  96.     }
  97.     public function isAlwaysAllowedInSandbox(): bool
  98.     {
  99.         return $this->options['always_allowed_in_sandbox'];
  100.     }
  101.     /**
  102.      * @return static
  103.      */
  104.     public function withDynamicArguments(string $namestring $dynamicName, array $arguments): self
  105.     {
  106.         $new = clone $this;
  107.         $new->name $name;
  108.         $new->dynamicName $dynamicName;
  109.         $new->arguments $arguments;
  110.         return $new;
  111.     }
  112.     /**
  113.      * @deprecated since Twig 3.12, use withDynamicArguments() instead
  114.      */
  115.     public function setArguments(array $arguments): void
  116.     {
  117.         trigger_deprecation('twig/twig''3.12''The "%s::setArguments()" method is deprecated, use "%s::withDynamicArguments()" instead.', static::class, static::class);
  118.         $this->arguments $arguments;
  119.     }
  120.     public function getArguments(): array
  121.     {
  122.         return $this->arguments;
  123.     }
  124.     public function isVariadic(): bool
  125.     {
  126.         return $this->options['is_variadic'];
  127.     }
  128.     public function isDeprecated(): bool
  129.     {
  130.         return (bool) $this->options['deprecation_info'];
  131.     }
  132.     public function triggerDeprecation(?string $file null, ?int $line null): void
  133.     {
  134.         $this->options['deprecation_info']->triggerDeprecation($file$line);
  135.     }
  136.     /**
  137.      * @deprecated since Twig 3.15
  138.      */
  139.     public function getDeprecatingPackage(): string
  140.     {
  141.         trigger_deprecation('twig/twig''3.15''The "%s" method is deprecated, use "%s::triggerDeprecation()" instead.'__METHOD__, static::class);
  142.         return $this->options['deprecating_package'];
  143.     }
  144.     /**
  145.      * @deprecated since Twig 3.15
  146.      */
  147.     public function getDeprecatedVersion(): string
  148.     {
  149.         trigger_deprecation('twig/twig''3.15''The "%s" method is deprecated, use "%s::triggerDeprecation()" instead.'__METHOD__, static::class);
  150.         return \is_bool($this->options['deprecated']) ? '' $this->options['deprecated'];
  151.     }
  152.     /**
  153.      * @deprecated since Twig 3.15
  154.      */
  155.     public function getAlternative(): ?string
  156.     {
  157.         trigger_deprecation('twig/twig''3.15''The "%s" method is deprecated, use "%s::triggerDeprecation()" instead.'__METHOD__, static::class);
  158.         return $this->options['alternative'];
  159.     }
  160.     public function getMinimalNumberOfRequiredArguments(): int
  161.     {
  162.         return ($this->options['needs_charset'] ? 0) + ($this->options['needs_environment'] ? 0) + ($this->options['needs_context'] ? 0) + ($this->options['needs_is_sandboxed'] ? 0) + \count($this->arguments);
  163.     }
  164. }