|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Latte (https://latte.nette.org) |
| 5 | + * Copyright (c) 2008 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Bridges\ApplicationLatte\Nodes; |
| 11 | + |
| 12 | +use Latte; |
| 13 | +use Latte\Compiler\Escaper; |
| 14 | +use Latte\Compiler\Nodes\Php\Expression\ArrayItemNode; |
| 15 | +use Latte\Compiler\Nodes\Php\Expression\ArrayNode; |
| 16 | +use Latte\Compiler\Nodes\Php\ExpressionNode; |
| 17 | +use Latte\Compiler\Nodes\Php\ModifierNode; |
| 18 | +use Latte\Compiler\Nodes\Php\Scalar\StringNode; |
| 19 | +use Latte\Compiler\Nodes\StatementNode; |
| 20 | +use Latte\Compiler\PrintContext; |
| 21 | +use Latte\Compiler\Tag; |
| 22 | +use Nette\Utils\Strings; |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * {control name[:method] [params]} |
| 27 | + */ |
| 28 | +class ControlNode extends StatementNode |
| 29 | +{ |
| 30 | + public ExpressionNode $name; |
| 31 | + public ?ExpressionNode $method = null; |
| 32 | + public ArrayNode $args; |
| 33 | + public ?bool $escape = null; |
| 34 | + |
| 35 | + |
| 36 | + public static function create(Tag $tag): static |
| 37 | + { |
| 38 | + $tag->outputMode = $tag::OutputRemoveIndentation; |
| 39 | + $tag->expectArguments(); |
| 40 | + $stream = $tag->parser->stream; |
| 41 | + $node = new static; |
| 42 | + $node->name = $tag->parser->parseUnquotedStringOrExpression(colon: false); |
| 43 | + if ($stream->tryConsume(':')) { |
| 44 | + $node->method = $tag->parser->parseExpression(); |
| 45 | + } |
| 46 | + |
| 47 | + $stream->tryConsume(','); |
| 48 | + $start = $stream->getIndex(); |
| 49 | + $node->args = $tag->parser->parseArguments(); |
| 50 | + $start -= $stream->getIndex(); |
| 51 | + $depth = $wrap = null; |
| 52 | + for (; $start < 0; $start++) { |
| 53 | + $token = $stream->peek($start); |
| 54 | + match (true) { |
| 55 | + $token->is('[') => $depth++, |
| 56 | + $token->is(']') => $depth--, |
| 57 | + $token->is('=>') && !$depth => $wrap = true, |
| 58 | + default => null, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + if ($wrap) { |
| 63 | + $node->args = new ArrayNode([new ArrayItemNode($node->args)]); |
| 64 | + } |
| 65 | + |
| 66 | + $modifier = $tag->parser->parseModifier(); |
| 67 | + foreach ($modifier->filters as $filter) { |
| 68 | + match ($filter->name->name) { |
| 69 | + 'noescape' => $node->escape = false, |
| 70 | + default => throw new Latte\CompileException('Only modifier |noescape is allowed here.', $tag->position), |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + return $node; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + public function print(PrintContext $context): string |
| 79 | + { |
| 80 | + if ($this->escape === null && $context->getEscaper()->getState() !== Escaper::HtmlText) { |
| 81 | + $this->escape = true; |
| 82 | + } |
| 83 | + |
| 84 | + $method = match (true) { |
| 85 | + !$this->method => 'render', |
| 86 | + $this->method instanceof StringNode && Strings::match($this->method->value, '#^\w*$#D') => 'render' . ucfirst($this->method->value), |
| 87 | + default => "{'render' . " . $this->method->print($context) . '}', |
| 88 | + }; |
| 89 | + |
| 90 | + $fetchCode = $context->format( |
| 91 | + $this->name instanceof StringNode |
| 92 | + ? '$ʟ_tmp = $this->global->uiControl->getComponent(%node);' |
| 93 | + : 'if (!is_object($ʟ_tmp = %node)) $ʟ_tmp = $this->global->uiControl->getComponent($ʟ_tmp);', |
| 94 | + $this->name, |
| 95 | + ); |
| 96 | + |
| 97 | + if ($this->escape) { |
| 98 | + return $context->format( |
| 99 | + <<<'XX' |
| 100 | + %raw |
| 101 | + if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false); |
| 102 | + ob_start(fn() => ''); |
| 103 | + $ʟ_tmp->%raw(%args) %line; |
| 104 | + $ʟ_fi = new LR\FilterInfo(%dump); echo %modifyContent(ob_get_clean()); |
| 105 | + |
| 106 | + |
| 107 | + XX, |
| 108 | + $fetchCode, |
| 109 | + $method, |
| 110 | + $this->args, |
| 111 | + $this->position, |
| 112 | + Latte\ContentType::Html, |
| 113 | + new ModifierNode([], $this->escape), |
| 114 | + ); |
| 115 | + |
| 116 | + } else { |
| 117 | + return $context->format( |
| 118 | + <<<'XX' |
| 119 | + %raw |
| 120 | + if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false); |
| 121 | + $ʟ_tmp->%raw(%args) %line; |
| 122 | + |
| 123 | + |
| 124 | + XX, |
| 125 | + $fetchCode, |
| 126 | + $method, |
| 127 | + $this->args, |
| 128 | + $this->position, |
| 129 | + ); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + public function &getIterator(): \Generator |
| 135 | + { |
| 136 | + yield $this->name; |
| 137 | + if ($this->method) { |
| 138 | + yield $this->method; |
| 139 | + } |
| 140 | + yield $this->args; |
| 141 | + } |
| 142 | +} |
0 commit comments