Skip to content

Commit

Permalink
cs whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 12, 2021
1 parent 89ca56f commit 617e47e
Show file tree
Hide file tree
Showing 27 changed files with 241 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/Bridges/Tracy/BlueScreenPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static function renderError(?\Throwable $e): ?array
];
}
}

return null;
}

Expand All @@ -80,6 +81,7 @@ public static function renderUnknownMacro(?\Throwable $e): ?array
'label' => 'fix it',
];
}

return null;
}
}
24 changes: 23 additions & 1 deletion src/Latte/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function addMacro(string $name, Macro $macro, int $flags = null)
} elseif ($flags && $this->flags[$name] !== $flags) {
throw new \LogicException("Incompatible flags for tag '$name'.");
}

$this->macros[$name][] = $macro;
return $this;
}
Expand Down Expand Up @@ -188,23 +189,27 @@ private function buildClassBody(array $tokens): string
)) {
$this->inHead = false;
}

$this->{"process$token->type"}($token);
}

while ($this->htmlNode) {
if (!empty($this->htmlNode->macroAttrs)) {
throw new CompileException('Missing ' . self::printEndTag($this->htmlNode));
}

$this->htmlNode = $this->htmlNode->parentNode;
}

while ($this->macroNode) {
if ($this->macroNode->parentNode) {
throw new CompileException('Missing {/' . $this->macroNode->name . '}');
}

if (~$this->flags[$this->macroNode->name] & Macro::AUTO_CLOSE) {
throw new CompileException('Missing ' . self::printEndTag($this->macroNode));
}

$this->closeMacro($this->macroNode->name);
}

Expand All @@ -221,6 +226,7 @@ private function buildClassBody(array $tokens): string
if ($prepare) {
$this->addMethod('prepare', $extractParams . "?>$prepare<?php", '', 'void');
}

if ($this->contentType !== self::CONTENT_HTML) {
$this->addConstant('CONTENT_TYPE', $this->contentType);
}
Expand All @@ -229,9 +235,11 @@ private function buildClassBody(array $tokens): string
foreach ($this->constants as $name => $value) {
$members[] = "\tprotected const $name = " . PhpHelpers::dump($value, true) . ';';
}

foreach ($this->properties as $name => $value) {
$members[] = "\tpublic $$name = " . PhpHelpers::dump($value, true) . ';';
}

foreach (array_filter($this->methods) as $name => $method) {
$members[] = ($method['comment'] === null ? '' : "\n\t/** " . str_replace('*/', '* /', $method['comment']) . ' */')
. "\n\tpublic function $name($method[arguments])"
Expand Down Expand Up @@ -390,6 +398,7 @@ private function processText(Token $token): void
) {
$this->lastAttrValue = $token->text;
}

$this->output .= $this->escape($token->text);
}

Expand Down Expand Up @@ -417,11 +426,13 @@ private function processMacroTag(Token $token): void
&& ($t->type !== Token::HTML_ATTRIBUTE_BEGIN || $t->name !== Parser::N_PREFIX . $token->name));
$token->empty = $t ? !$t->closing : true;
}

$node = $this->openMacro($token->name, $token->value, $token->modifiers, $isRightmost);
if ($token->empty) {
if ($node->empty) {
throw new CompileException("Unexpected /} in tag {$token->text}");
}

$this->closeMacro($token->name, '', '', $isRightmost);
}
}
Expand All @@ -435,14 +446,18 @@ private function processHtmlTagBegin(Token $token): void
if (strcasecmp($this->htmlNode->name, $token->name) === 0) {
break;
}

if ($this->htmlNode->macroAttrs) {
throw new CompileException("Unexpected </$token->name>, expecting " . self::printEndTag($this->htmlNode));
}

$this->htmlNode = $this->htmlNode->parentNode;
}

if (!$this->htmlNode) {
$this->htmlNode = new HtmlNode($token->name);
}

$this->htmlNode->closing = true;
$this->htmlNode->endLine = $this->getLine();
$this->context = self::CONTEXT_HTML_TEXT;
Expand All @@ -458,6 +473,7 @@ private function processHtmlTagBegin(Token $token): void
$this->htmlNode->startLine = $this->getLine();
$this->context = self::CONTEXT_HTML_TAG;
}

$this->tagOffset = strlen($this->output);
$this->output .= $this->escape($token->text);
}
Expand Down Expand Up @@ -532,6 +548,7 @@ private function processHtmlAttributeBegin(Token $token): void
} elseif ($this->macroNode && $this->macroNode->htmlNode === $this->htmlNode) {
throw new CompileException("n:attribute must not appear inside tags; found {$token->name} inside {{$this->macroNode->name}}.");
}

$this->htmlNode->macroAttrs[$name] = $token->value;
return;
}
Expand Down Expand Up @@ -619,6 +636,7 @@ public function openMacro(
$this->output = &$node->content;
$this->output = '';
}

return $node;
}

Expand Down Expand Up @@ -672,6 +690,7 @@ public function closeMacro(
if ($node->prefix && $node->prefix !== MacroNode::PREFIX_TAG) {
$this->htmlNode->attrCode .= $node->attrCode;
}

$this->output = &$node->saved[0];
$this->writeCode((string) $node->openingCode, $node->replaced, $node->saved[1]);
$this->output .= $node->content;
Expand All @@ -688,6 +707,7 @@ private function writeCode(string $code, ?bool $isReplaced, ?bool $isRightmost,
if ($isReplaced === null) {
$isReplaced = preg_match('#<\?php.*\secho\s#As', $code);
}

if ($isLeftmost && !$isReplaced) {
$this->output = substr($this->output, 0, $leftOfs); // alone macro without output -> remove indentation
if (!$isClosing && substr($code, -2) !== '?>') {
Expand All @@ -697,6 +717,7 @@ private function writeCode(string $code, ?bool $isReplaced, ?bool $isRightmost,
$code .= "\n"; // double newline to avoid newline eating by PHP
}
}

$this->output .= $code;
}

Expand Down Expand Up @@ -729,6 +750,7 @@ public function writeAttrsMacro(string $html): void
}
});
}

unset($attrs[$attrName]);
}

Expand All @@ -743,7 +765,6 @@ public function writeAttrsMacro(string $html): void
});
}


foreach (array_reverse($this->macros) as $name => $foo) {
$attrName = MacroNode::PREFIX_TAG . "-$name";
if (!isset($attrs[$attrName])) {
Expand Down Expand Up @@ -778,6 +799,7 @@ public function writeAttrsMacro(string $html): void
}
});
}

unset($attrs[$name]);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Latte/Compiler/MacroNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function closest(array $names, callable $condition = null): ?self
)) {
$node = $node->parentNode;
}

return $node;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Latte/Compiler/MacroTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function append($val, int $position = null)
is_array($val) ? [$val] : $this->parse($val)
);
}

return $this;
}

Expand All @@ -96,6 +97,7 @@ public function prepend($val)
if ($val != null) { // intentionally @
array_splice($this->tokens, 0, 0, is_array($val) ? [$val] : $this->parse($val));
}

return $this;
}

Expand All @@ -117,6 +119,7 @@ public function fetchWord(): ?string
$expr .= $this->joinUntilSameDepth(',');
}
}

$this->nextToken(',');
$this->nextAll(self::T_WHITESPACE, self::T_COMMENT);
return $expr === '' ? null : $expr;
Expand All @@ -136,6 +139,7 @@ public function fetchWords(): array
&& (($dot = $this->nextValue('.')) || $this->isPrev('.'))) {
$words[0] .= $space . $dot . $this->joinUntil(',');
}

$this->nextToken(',');
$this->nextAll(self::T_WHITESPACE, self::T_COMMENT);
return $words === [''] ? [] : $words;
Expand All @@ -154,6 +158,7 @@ public function joinUntilSameDepth(...$args): string
if ($this->depth === $depth) {
return $res;
}

$res .= $this->nextValue();
} while (true);
}
Expand All @@ -174,6 +179,7 @@ public function fetchWordWithModifier($modifiers): ?array
) {
return [$name, $mod];
}

$this->position = $pos;
$name = $this->fetchWord();
return $name === null ? null : [$name, null];
Expand Down
15 changes: 15 additions & 0 deletions src/Latte/Compiler/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,20 @@ public function parse(string $input): array
if ($this->{'context' . $this->context[0]}() === false) {
break;
}

while ($tokenCount < count($this->output)) {
$this->filter($this->output[$tokenCount++]);
}
}

if ($this->context[0] === self::CONTEXT_MACRO) {
throw new CompileException('Malformed tag.');
}

if ($this->offset < strlen($input)) {
$this->addToken(Token::TEXT, substr($this->input, $this->offset));
}

return $this->output;
}

Expand Down Expand Up @@ -179,6 +182,7 @@ private function contextHtmlCData(): bool
if (empty($matches['tag'])) {
return $this->processMacro($matches);
}

// </tag
$token = $this->addToken(Token::HTML_TAG_BEGIN, $matches[0]);
$token->name = $this->lastHtmlTag;
Expand Down Expand Up @@ -222,6 +226,7 @@ private function contextHtmlTag(): bool
$this->setContext(self::CONTEXT_HTML_ATTRIBUTE, $matches['value']);
}
}

return true;

} else {
Expand All @@ -243,6 +248,7 @@ private function contextHtmlAttribute(): bool
if (empty($matches['quote'])) {
return $this->processMacro($matches);
}

// (attribute end) '"
$this->addToken(Token::HTML_ATTRIBUTE_END, $matches[0]);
$this->setContext(self::CONTEXT_HTML_TAG);
Expand All @@ -263,6 +269,7 @@ private function contextHtmlComment(): bool
if (empty($matches['htmlcomment'])) {
return $this->processMacro($matches);
}

// -->
$this->addToken(Token::HTML_TAG_END, $matches[0]);
$this->setContext(self::CONTEXT_HTML_TEXT);
Expand Down Expand Up @@ -323,6 +330,7 @@ private function processMacro(array $matches): bool
if (empty($matches['macro'])) {
return false;
}

// {macro} or {* *}
$this->setContext(self::CONTEXT_MACRO, [$this->context, $matches['macro']]);
return true;
Expand All @@ -339,17 +347,20 @@ private function match(string $re): array
if (preg_last_error()) {
throw new RegexpException(null, preg_last_error());
}

return [];
}

$value = substr($this->input, $this->offset, $matches[0][1] - $this->offset);
if ($value !== '') {
$this->addToken(Token::TEXT, $value);
}

$this->offset = $matches[0][1] + strlen($matches[0][0]);
foreach ($matches as $k => $v) {
$matches[$k] = $v[0];
}

return $matches;
}

Expand All @@ -366,6 +377,7 @@ public function setContentType(string $type)
} else {
$this->setContext(self::CONTEXT_NONE);
}

return $this;
}

Expand Down Expand Up @@ -428,11 +440,14 @@ public function parseMacroTag(string $tag): ?array
if (preg_last_error()) {
throw new RegexpException(null, preg_last_error());
}

return null;
}

if ($match['name'] === '') {
$match['name'] = $match['shortname'] ?: ($match['closing'] ? '' : '=');
}

return [$match['name'], trim($match['args']), $match['modifiers'], (bool) $match['empty'], (bool) $match['closing']];
}

Expand Down
Loading

0 comments on commit 617e47e

Please sign in to comment.