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. public function getCallable()
  69. {
  70. return $this->callable;
  71. }
  72. public function getNodeClass(): string
  73. {
  74. return $this->options['node_class'];
  75. }
  76. public function needsCharset(): bool
  77. {
  78. return $this->options['needs_charset'];
  79. }
  80. public function needsEnvironment(): bool
  81. {
  82. return $this->options['needs_environment'];
  83. }
  84. public function needsContext(): bool
  85. {
  86. return $this->options['needs_context'];
  87. }
  88. /**
  89. * @return static
  90. */
  91. public function withDynamicArguments(string $name, string $dynamicName, array $arguments): self
  92. {
  93. $new = clone $this;
  94. $new->name = $name;
  95. $new->dynamicName = $dynamicName;
  96. $new->arguments = $arguments;
  97. return $new;
  98. }
  99. /**
  100. * @deprecated since Twig 3.12, use withDynamicArguments() instead
  101. */
  102. public function setArguments(array $arguments): void
  103. {
  104. trigger_deprecation('twig/twig', '3.12', 'The "%s::setArguments()" method is deprecated, use "%s::withDynamicArguments()" instead.', static::class, static::class);
  105. $this->arguments = $arguments;
  106. }
  107. public function getArguments(): array
  108. {
  109. return $this->arguments;
  110. }
  111. public function isVariadic(): bool
  112. {
  113. return $this->options['is_variadic'];
  114. }
  115. public function isDeprecated(): bool
  116. {
  117. return (bool) $this->options['deprecation_info'];
  118. }
  119. public function triggerDeprecation(?string $file = null, ?int $line = null): void
  120. {
  121. $this->options['deprecation_info']->triggerDeprecation($file, $line);
  122. }
  123. /**
  124. * @deprecated since Twig 3.15
  125. */
  126. public function getDeprecatingPackage(): string
  127. {
  128. trigger_deprecation('twig/twig', '3.15', 'The "%s" method is deprecated, use "%s::triggerDeprecation()" instead.', __METHOD__, static::class);
  129. return $this->options['deprecating_package'];
  130. }
  131. /**
  132. * @deprecated since Twig 3.15
  133. */
  134. public function getDeprecatedVersion(): string
  135. {
  136. trigger_deprecation('twig/twig', '3.15', 'The "%s" method is deprecated, use "%s::triggerDeprecation()" instead.', __METHOD__, static::class);
  137. return \is_bool($this->options['deprecated']) ? '' : $this->options['deprecated'];
  138. }
  139. /**
  140. * @deprecated since Twig 3.15
  141. */
  142. public function getAlternative(): ?string
  143. {
  144. trigger_deprecation('twig/twig', '3.15', 'The "%s" method is deprecated, use "%s::triggerDeprecation()" instead.', __METHOD__, static::class);
  145. return $this->options['alternative'];
  146. }
  147. public function getMinimalNumberOfRequiredArguments(): int
  148. {
  149. return ($this->options['needs_charset'] ? 1 : 0) + ($this->options['needs_environment'] ? 1 : 0) + ($this->options['needs_context'] ? 1 : 0) + \count($this->arguments);
  150. }
  151. }