vendor/twig/twig/src/MacroNamespace.php line 57

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. use Twig\Error\RuntimeError;
  12. use Twig\Node\Expression\MacroReferenceExpression;
  13. /**
  14. * @internal
  15. */
  16. final class MacroNamespace
  17. {
  18. /**
  19. * @param array<string, TwigMacro> $macros
  20. */
  21. public function __construct(
  22. private Template $template,
  23. private array $macros,
  24. ) {
  25. }
  26. public function has(string $name, array $context): bool
  27. {
  28. if (null !== $declaration = $this->findDeclaredName($name, $context)) {
  29. [$declaredName, $templateName] = $declaration;
  30. if ($declaredName !== $name) {
  31. trigger_deprecation('twig/twig', '3.29', 'Testing whether the macro "%s" (defined in template "%s") is defined as "%s" is deprecated; macro names will be case-sensitive in Twig 4.0 and this test will return false.', $declaredName, $templateName, $name);
  32. }
  33. return true;
  34. }
  35. if (!str_starts_with($name, 'macro_') || null === $this->findDeclaredName($bareName = substr($name, \strlen('macro_')), $context)) {
  36. return false;
  37. }
  38. trigger_deprecation('twig/twig', '3.29', 'Testing whether the macro "%s" is defined via the "macro_"-prefixed name "%s" is deprecated; pass the bare macro name to "%s" instead.', $bareName, $name, MacroReferenceExpression::class);
  39. return true;
  40. }
  41. /**
  42. * @param array<int|string, mixed> $arguments
  43. */
  44. public function call(string $name, array $arguments, array $context, int $line, Source $source): string|Markup
  45. {
  46. if (null !== $macro = $this->getDeclared($name)) {
  47. return $macro->callLegacy($arguments, $source, $line);
  48. }
  49. if (null === $macro = $this->resolve($name, $context)) {
  50. if (!str_starts_with($name, 'macro_') || null === $macro = $this->resolve($bareName = substr($name, \strlen('macro_')), $context)) {
  51. throw new RuntimeError(\sprintf('Macro "%s" is not defined in template "%s".', $name, $this->template->getTemplateName()), $line, $source);
  52. }
  53. trigger_deprecation('twig/twig', '3.29', 'Calling the macro "%s" via the "macro_"-prefixed name "%s" is deprecated; pass the bare macro name to "%s" instead.', $bareName, $name, MacroReferenceExpression::class);
  54. }
  55. return $macro->callLegacy($arguments, $source, $line);
  56. }
  57. /**
  58. * @return array{string, string}|null
  59. */
  60. private function findDeclaredName(string $name, array $context): ?array
  61. {
  62. $namespace = $this;
  63. while (true) {
  64. if (isset($namespace->macros[$name])) {
  65. return [$name, $namespace->template->getTemplateName()];
  66. }
  67. foreach ($namespace->macros as $declaredName => $macro) {
  68. if (0 === strcasecmp($declaredName, $name)) {
  69. return [$declaredName, $namespace->template->getTemplateName()];
  70. }
  71. }
  72. if (null === $namespace = $namespace->getParent($context)) {
  73. return null;
  74. }
  75. }
  76. }
  77. private function getDeclared(string $name): ?TwigMacro
  78. {
  79. if (isset($this->macros[$name])) {
  80. $this->template->ensureSecurityChecked();
  81. return $this->macros[$name];
  82. }
  83. foreach ($this->macros as $declaredName => $macro) {
  84. if (0 === strcasecmp($declaredName, $name)) {
  85. trigger_deprecation('twig/twig', '3.29', 'Calling the macro "%s" (defined in template "%s") as "%s" is deprecated; macro names will be case-sensitive in Twig 4.0.', $declaredName, $this->template->getTemplateName(), $name);
  86. $this->template->ensureSecurityChecked();
  87. return $macro;
  88. }
  89. }
  90. return null;
  91. }
  92. private function resolve(string $name, array $context): ?TwigMacro
  93. {
  94. $namespace = $this;
  95. while (true) {
  96. if (null !== $macro = $namespace->getDeclared($name)) {
  97. return $macro;
  98. }
  99. if (null === $namespace = $namespace->getParent($context)) {
  100. return null;
  101. }
  102. }
  103. }
  104. private function getParent(array $context): ?self
  105. {
  106. if (!$parent = $this->template->getParent($context)) {
  107. return null;
  108. }
  109. return $parent->unwrap()->getMacroNamespace();
  110. }
  111. }