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

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