vendor/twig/twig/src/NodeVisitor/YieldNotReadyNodeVisitor.php line 44

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\NodeVisitor;
  11. use Twig\Attribute\YieldReady;
  12. use Twig\Environment;
  13. use Twig\Node\Expression\AbstractExpression;
  14. use Twig\Node\Node;
  15. /**
  16. * @internal to be removed in Twig 4
  17. */
  18. final class YieldNotReadyNodeVisitor implements NodeVisitorInterface
  19. {
  20. private $yieldReadyNodes = [];
  21. public function __construct(
  22. private bool $useYield,
  23. ) {
  24. }
  25. public function enterNode(Node $node, Environment $env): Node
  26. {
  27. $class = $node::class;
  28. if ($node instanceof AbstractExpression || isset($this->yieldReadyNodes[$class])) {
  29. return $node;
  30. }
  31. if (!$this->yieldReadyNodes[$class] = (bool) (new \ReflectionClass($class))->getAttributes(YieldReady::class)) {
  32. if ($this->useYield) {
  33. throw new \LogicException(\sprintf('You cannot enable the "use_yield" option of Twig as node "%s" is not marked as ready for it; please make it ready and then flag it with the #[\Twig\Attribute\YieldReady] attribute.', $class));
  34. }
  35. trigger_deprecation('twig/twig', '3.9', 'Twig node "%s" is not marked as ready for using "yield" instead of "echo"; please make it ready and then flag it with the #[\Twig\Attribute\YieldReady] attribute.', $class);
  36. }
  37. return $node;
  38. }
  39. public function leaveNode(Node $node, Environment $env): ?Node
  40. {
  41. return $node;
  42. }
  43. public function getPriority(): int
  44. {
  45. return 255;
  46. }
  47. }