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

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. 'deprecation_info' => null,
  33. 'deprecated' => false,
  34. 'deprecating_package' => '',
  35. 'alternative' => null,
  36. ], $options);
  37. if ($this->options['deprecation_info'] && !$this->options['deprecation_info'] instanceof DeprecatedCallableInfo) {
  38. throw new \LogicException(\sprintf('The "deprecation_info" option must be an instance of "%s".', DeprecatedCallableInfo::class));
  39. }
  40. if ($this->options['deprecated']) {
  41. if ($this->options['deprecation_info']) {
  42. throw new \LogicException('When setting the "deprecation_info" option, you need to remove the obsolete deprecated options.');
  43. }
  44. trigger_deprecation('twig/twig', '3.15', 'Using the "deprecated", "deprecating_package", and "alternative" options is deprecated, pass a "deprecation_info" one instead.');
  45. $this->options['deprecation_info'] = new DeprecatedCallableInfo(
  46. $this->options['deprecating_package'],
  47. $this->options['deprecated'],
  48. null,
  49. $this->options['alternative'],
  50. );
  51. }
  52. if ($this->options['deprecation_info']) {
  53. $this->options['deprecation_info']->setName($name);
  54. $this->options['deprecation_info']->setType($this->getType());
  55. }
  56. }
  57. public function __toString(): string
  58. {
  59. return \sprintf('%s(%s)', static::class, $this->name);
  60. }
  61. public function getName(): string
  62. {
  63. return $this->name;
  64. }
  65. public function getDynamicName(): string
  66. {
  67. return $this->dynamicName;
  68. }
  69. /**
  70. * @return callable|array{class-string, string}|null
  71. */
  72. public function getCallable()
  73. {
  74. return $this->callable;
  75. }
  76. public function getNodeClass(): string
  77. {
  78. return $this->options['node_class'];
  79. }
  80. public function needsCharset(): bool
  81. {
  82. return $this->options['needs_charset'];
  83. }
  84. public function needsEnvironment(): bool
  85. {
  86. return $this->options['needs_environment'];
  87. }
  88. public function needsContext(): bool
  89. {
  90. return $this->options['needs_context'];
  91. }
  92. public function needsIsSandboxed(): bool
  93. {
  94. return $this->options['needs_is_sandboxed'];
  95. }
  96. /**
  97. * @return static
  98. */
  99. public function withDynamicArguments(string $name, string $dynamicName, array $arguments): self
  100. {
  101. $new = clone $this;
  102. $new->name = $name;
  103. $new->dynamicName = $dynamicName;
  104. $new->arguments = $arguments;
  105. return $new;
  106. }
  107. /**
  108. * @deprecated since Twig 3.12, use withDynamicArguments() instead
  109. */
  110. public function setArguments(array $arguments): void
  111. {
  112. trigger_deprecation('twig/twig', '3.12', 'The "%s::setArguments()" method is deprecated, use "%s::withDynamicArguments()" instead.', static::class, static::class);
  113. $this->arguments = $arguments;
  114. }
  115. public function getArguments(): array
  116. {
  117. return $this->arguments;
  118. }
  119. public function isVariadic(): bool
  120. {
  121. return $this->options['is_variadic'];
  122. }
  123. public function isDeprecated(): bool
  124. {
  125. return (bool) $this->options['deprecation_info'];
  126. }
  127. public function triggerDeprecation(?string $file = null, ?int $line = null): void
  128. {
  129. $this->options['deprecation_info']->triggerDeprecation($file, $line);
  130. }
  131. /**
  132. * @deprecated since Twig 3.15
  133. */
  134. public function getDeprecatingPackage(): string
  135. {
  136. trigger_deprecation('twig/twig', '3.15', 'The "%s" method is deprecated, use "%s::triggerDeprecation()" instead.', __METHOD__, static::class);
  137. return $this->options['deprecating_package'];
  138. }
  139. /**
  140. * @deprecated since Twig 3.15
  141. */
  142. public function getDeprecatedVersion(): string
  143. {
  144. trigger_deprecation('twig/twig', '3.15', 'The "%s" method is deprecated, use "%s::triggerDeprecation()" instead.', __METHOD__, static::class);
  145. return \is_bool($this->options['deprecated']) ? '' : $this->options['deprecated'];
  146. }
  147. /**
  148. * @deprecated since Twig 3.15
  149. */
  150. public function getAlternative(): ?string
  151. {
  152. trigger_deprecation('twig/twig', '3.15', 'The "%s" method is deprecated, use "%s::triggerDeprecation()" instead.', __METHOD__, static::class);
  153. return $this->options['alternative'];
  154. }
  155. public function getMinimalNumberOfRequiredArguments(): int
  156. {
  157. return ($this->options['needs_charset'] ? 1 : 0) + ($this->options['needs_environment'] ? 1 : 0) + ($this->options['needs_context'] ? 1 : 0) + ($this->options['needs_is_sandboxed'] ? 1 : 0) + \count($this->arguments);
  158. }
  159. }