nystudio107 /
craft-templatecomments
| 1 | <?php |
||
| 2 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | /* |
||
| 4 | * This file is part of Twig. |
||
| 5 | * |
||
| 6 | * (c) Fabien Potencier |
||
| 7 | * (c) Armin Ronacher |
||
| 8 | * |
||
| 9 | * For the full copyright and license information, please view the LICENSE |
||
| 10 | * file that was distributed with this source code. |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace nystudio107\templatecomments\web\twig\tokenparsers; |
||
| 14 | |||
| 15 | use nystudio107\templatecomments\TemplateComments; |
||
| 16 | use nystudio107\templatecomments\web\twig\nodes\CommentBlockNode; |
||
| 17 | use Twig\Error\SyntaxError; |
||
| 18 | use Twig\Node\BlockNode; |
||
| 19 | use Twig\Node\BlockReferenceNode; |
||
| 20 | use Twig\Node\Node; |
||
| 21 | use Twig\Node\PrintNode; |
||
| 22 | use Twig\Token; |
||
| 23 | use Twig\TokenParser\AbstractTokenParser; |
||
| 24 | use function sprintf; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Marks a section of a template as being reusable. |
||
| 28 | * |
||
| 29 | * <pre> |
||
| 30 | * {% block head %} |
||
| 31 | * <link rel="stylesheet" href="style.css" /> |
||
| 32 | * <title>{% block title %}{% endblock %} - My Webpage</title> |
||
| 33 | * {% endblock %} |
||
| 34 | * </pre> |
||
| 35 | */ |
||
|
0 ignored issues
–
show
|
|||
| 36 | final class CommentBlockTokenParser extends AbstractTokenParser |
||
| 37 | { |
||
| 38 | public function parse(Token $token): Node |
||
|
0 ignored issues
–
show
|
|||
| 39 | { |
||
| 40 | $lineno = $token->getLine(); |
||
| 41 | $stream = $this->parser->getStream(); |
||
| 42 | $name = $stream->expect(Token::NAME_TYPE)->getValue(); |
||
| 43 | |||
| 44 | // Exclude certain blocks from being CommentBlockNodes |
||
| 45 | $blockClass = CommentBlockNode::class; |
||
| 46 | $settings = TemplateComments::$settings; |
||
| 47 | foreach ($settings->excludeBlocksThatContain as $excludeString) { |
||
| 48 | if (stripos($name, $excludeString) !== false) { |
||
| 49 | $blockClass = BlockNode::class; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | $this->parser->setBlock($name, $block = new $blockClass($name, new Node([]), $lineno)); |
||
| 54 | $this->parser->pushLocalScope(); |
||
| 55 | $this->parser->pushBlockStack($name); |
||
| 56 | |||
| 57 | if ($stream->nextIf(Token::BLOCK_END_TYPE)) { |
||
| 58 | $body = $this->parser->subparse([$this, 'decideBlockEnd'], true); |
||
| 59 | if ($token = $stream->nextIf(Token::NAME_TYPE)) { |
||
| 60 | $value = $token->getValue(); |
||
| 61 | |||
| 62 | if ($value != $name) { |
||
| 63 | throw new SyntaxError(sprintf('Expected endblock for block "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getSourceContext()); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } else { |
||
| 67 | $body = new Node([ |
||
|
0 ignored issues
–
show
|
|||
| 68 | new PrintNode($this->parser->getExpressionParser()->parseExpression(), $lineno), |
||
| 69 | ]); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 70 | } |
||
| 71 | $stream->expect(Token::BLOCK_END_TYPE); |
||
| 72 | |||
| 73 | $block->setNode('body', $body); |
||
| 74 | $this->parser->popBlockStack(); |
||
| 75 | $this->parser->popLocalScope(); |
||
| 76 | |||
| 77 | return new BlockReferenceNode($name, $lineno); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function decideBlockEnd(Token $token): bool |
||
|
0 ignored issues
–
show
|
|||
| 81 | { |
||
| 82 | return $token->test('endblock'); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function getTag(): string |
||
|
0 ignored issues
–
show
|
|||
| 86 | { |
||
| 87 | return 'block'; |
||
| 88 | } |
||
| 89 | } |
||
| 90 |