Skip to content

Commit

Permalink
removed variable Tag::$data
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 8, 2023
1 parent f820240 commit 84fa5e0
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/Latte/Compiler/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ public function __construct(
public /*readonly*/ ?ElementNode $htmlElement = null,
public ?self $parent = null,
public /*readonly*/ ?string $prefix = null,
public ?\stdClass $data = null,
public ?AreaNode $node = null,
public ?AreaNode $attrNode = null,
) {
$this->data ??= new \stdClass;
$this->parser = new TagParser($tokens);
}

Expand Down Expand Up @@ -111,7 +110,7 @@ public function expectArguments(string $what = 'arguments'): void

public function replaceNAttribute(Node $node): void
{
$index = array_search($this->data->node, $this->htmlElement->attributes->children, true);
$index = array_search($this->attrNode, $this->htmlElement->attributes->children, true);
$this->htmlElement->attributes->children[$index] = $node;
}
}
8 changes: 5 additions & 3 deletions src/Latte/Compiler/TemplateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class TemplateParser
private int $counter = 0;
private ?Tag $tag = null;
private $lastResolver;
private \WeakMap $lookFor;


public function __construct()
Expand All @@ -59,6 +60,7 @@ public function parse(string $template): Nodes\TemplateNode
{
$this->html = new TemplateParserHtml($this, $this->completeAttrParsers());
$this->stream = new TokenStream($this->lexer->tokenize($template));
$this->lookFor = new \WeakMap;

$headLength = 0;
$findLength = function (FragmentNode $fragment) use (&$headLength) {
Expand Down Expand Up @@ -163,7 +165,7 @@ public function parseLatteStatement(?callable $resolver = null): ?Node
{
$this->lexer->pushState(TemplateLexer::StateLatteTag);
if ($this->stream->peek(1)->is(Token::Slash)
|| isset($this->tag->data->filters) && in_array($this->stream->peek(1)->text, $this->tag->data->filters, true)
|| (isset($this->tag, $this->lookFor[$this->tag]) && in_array($this->stream->peek(1)->text, $this->lookFor[$this->tag], true))
) {
$this->lexer->popState();
return null; // go back to previous parseLatteStatement()
Expand All @@ -189,7 +191,7 @@ public function parseLatteStatement(?callable $resolver = null): ?Node
$res->send([new FragmentNode, $startTag]);
} else {
while ($res->valid()) {
$startTag->data->filters = $res->current() ?: null;
$this->lookFor[$startTag] = $res->current() ?: null;
$content = $this->parseFragment($resolver ?? $this->lastResolver);

if (!$this->stream->is(Token::Latte_TagOpen)) {
Expand All @@ -209,7 +211,7 @@ public function parseLatteStatement(?callable $resolver = null): ?Node
$res->send([$content, $tag]);
$this->ensureIsConsumed($tag);
break;
} elseif (in_array($tag->name, $startTag->data->filters ?? [], true)) {
} elseif (in_array($tag->name, $this->lookFor[$startTag] ?? [], true)) {
$this->pushTag($tag);
$res->send([$content, $tag]);
$this->ensureIsConsumed($tag);
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Compiler/TemplateParserHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ private function parseNAttribute(): Nodes\TextNode
prefix: $this->getPrefix($name),
inTag: true,
htmlElement: $this->element,
data: (object) ['node' => $node = new Nodes\TextNode('')], // TODO: better
attrNode: $node = new Nodes\TextNode(''),
);
return $node;
}
Expand Down
1 change: 0 additions & 1 deletion src/Latte/Essential/Nodes/BlockNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public static function create(Tag $tag, TemplateParser $parser): \Generator

if (!$node->block->isDynamic()) {
$parser->checkBlockIsUnique($node->block);
$tag->data->block = $node->block; // for {include}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/Latte/Essential/Nodes/DefineNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public static function create(Tag $tag, TemplateParser $parser): \Generator
$node->block = new Block($name, $layer, $tag);
if (!$node->block->isDynamic()) {
$parser->checkBlockIsUnique($node->block);
$tag->data->block = $node->block; // for {include}
$tag->parser->stream->tryConsume(',');
$node->block->parameters = self::parseParameters($tag);
}
Expand Down
3 changes: 0 additions & 3 deletions src/Latte/Essential/Nodes/ExtendsNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ public static function create(Tag $tag): static
$node = $tag->node = new static;
if (!$tag->isInHead()) {
throw new CompileException("{{$tag->name}} must be placed in template head.", $tag->position);
} elseif (isset($tag->data->extends)) {
throw new CompileException("Multiple {{$tag->name}} declarations are not allowed.", $tag->position);
} elseif ($tag->parser->stream->tryConsume('auto')) {
$node->extends = new NullNode;
} elseif ($tag->parser->stream->tryConsume('none')) {
$node->extends = new BooleanNode(false);
} else {
$node->extends = $tag->parser->parseUnquotedStringOrExpression();
}
$tag->data->extends = true;
return $node;
}

Expand Down
1 change: 0 additions & 1 deletion src/Latte/Essential/Nodes/ForeachNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static function create(Tag $tag): \Generator
$tag->expectArguments();
$node = $tag->node = new static;
self::parseArguments($tag->parser, $node);
$tag->data->iterateWhile = [$node->key, $node->value];

$modifier = $tag->parser->parseModifier();
foreach ($modifier->filters as $filter) {
Expand Down
4 changes: 2 additions & 2 deletions src/Latte/Essential/Nodes/IncludeBlockNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public static function create(Tag $tag, TemplateParser $parser): static
} elseif ($node->parent || $tokenName->is('this')) {
$item = $tag->closestTag(
[BlockNode::class, DefineNode::class],
fn($item) => isset($item->data->block) && $item->data->block->name !== ''
fn($item) => $item->node?->block && !$item->node->block->isDynamic() && $item->node->block->name !== ''
);
if (!$item) {
throw new CompileException("Cannot include $tokenName->text block outside of any block.", $tag->position);
}

$node->name = $item->data->block->name;
$node->name = $item->node->block->name;
}

$node->blocks = &$parser->blocks;
Expand Down
5 changes: 3 additions & 2 deletions src/Latte/Essential/Nodes/IterateWhileNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class IterateWhileNode extends StatementNode
/** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static> */
public static function create(Tag $tag): \Generator
{
$foreach = $tag->closestTag([ForeachNode::class]);
$foreach = $tag->closestTag([ForeachNode::class])?->node;
if (!$foreach) {
throw new CompileException("Tag {{$tag->name}} must be inside {foreach} ... {/foreach}.", $tag->position);
}
Expand All @@ -44,7 +44,8 @@ public static function create(Tag $tag): \Generator
$node->condition = $tag->parser->parseExpression();
}

[$node->key, $node->value] = $foreach->data->iterateWhile;
$node->key = $foreach->key;
$node->value = $foreach->value;
[$node->content, $nextTag] = yield;
if ($node->postTest) {
$nextTag->expectArguments();
Expand Down

0 comments on commit 84fa5e0

Please sign in to comment.