1 | <?php |
||
24 | class Defer extends Twig_TokenParser |
||
25 | { |
||
26 | protected $blockPrefix; |
||
27 | |||
28 | /** |
||
29 | * @param string $blockPrefix |
||
30 | */ |
||
31 | 9 | public function __construct($blockPrefix) |
|
35 | |||
36 | /** |
||
37 | * Parses a token and returns a node. |
||
38 | * |
||
39 | * @param Twig_Token $token A Twig_Token instance |
||
40 | * @return null|Twig_Token A Twig_NodeInterface instance |
||
41 | */ |
||
42 | 8 | public function parse(Twig_Token $token) |
|
43 | { |
||
44 | 8 | $lineno = $token->getLine(); |
|
45 | 8 | $stream = $this->parser->getStream(); |
|
46 | 8 | $reference = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); |
|
47 | |||
48 | 8 | $name = $stream->nextIf(\Twig_Token::STRING_TYPE); |
|
49 | 8 | $name = $name !== null ? $name->getValue() : false; |
|
50 | |||
51 | 8 | $unique = $name !== false; |
|
52 | |||
53 | 8 | $variableName = $stream->nextIf(\Twig_Token::NAME_TYPE); |
|
54 | 8 | $variableName = $variableName !== null ? $variableName->getValue() : false; |
|
55 | |||
56 | 8 | $offset = $stream->nextIf(\Twig_Token::NUMBER_TYPE); |
|
57 | 8 | $offset = $offset !== null ? $offset->getValue() : false; |
|
58 | |||
59 | 8 | if ($name) { |
|
|
|||
60 | 3 | $name = $this->blockPrefix . $reference . $name; |
|
61 | 3 | if ($this->parser->hasBlock($name)) { |
|
62 | 1 | $this->bodyParse($stream, $name); |
|
63 | |||
64 | 1 | return null; |
|
65 | } |
||
66 | 3 | } else { |
|
67 | 6 | $name = $this->createUniqueBlockName($token, $reference); |
|
68 | } |
||
69 | |||
70 | 8 | $this->parser->setBlock($name, $block = new Node\Defer($name, new Twig_Node(array()), $lineno)); |
|
71 | 8 | $this->parser->pushLocalScope(); |
|
72 | |||
73 | 8 | $body = $this->bodyParse($stream, $name); |
|
74 | |||
75 | 5 | $block->setNode('body', $body); |
|
76 | 5 | $this->parser->popLocalScope(); |
|
77 | |||
78 | 5 | return new Node\DeferReference($name, $variableName, $unique, $reference, $offset, $lineno, $this->getTag()); |
|
79 | } |
||
80 | |||
81 | 7 | public function decideBlockEnd(Twig_Token $token) |
|
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 8 | public function getTag() |
|
93 | |||
94 | /** |
||
95 | * @param Twig_TokenStream $stream |
||
96 | * @param string $name |
||
97 | * @return Twig_Node |
||
98 | */ |
||
99 | 8 | protected function bodyParse(Twig_TokenStream $stream, $name) |
|
125 | |||
126 | /** |
||
127 | * @param Twig_Token $token |
||
128 | * @param $reference |
||
129 | * @return string |
||
130 | */ |
||
131 | 6 | private function createUniqueBlockName(Twig_Token $token, $reference) |
|
145 | } |
||
146 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: