-
Notifications
You must be signed in to change notification settings - Fork 611
/
NavItem_Node.php
151 lines (147 loc) · 6.05 KB
/
NavItem_Node.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\web\twig\nodes;
use craft\helpers\Template as TemplateHelper;
use Twig\Compiler;
use Twig\Node\Expression\AssignNameExpression;
use Twig\Node\Node;
use Twig\Template;
/**
* Internal node used by the nav node.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class NavItem_Node extends Node
{
/**
* NavItem_Node constructor.
*
* @param AssignNameExpression $valueTarget
* @param Node|null $indent
* @param Node|null $outdent
* @param Node|null $lowerBody
* @param int $lineno
* @param string|null $tag
*/
public function __construct(AssignNameExpression $valueTarget, ?Node $indent, ?Node $outdent, ?Node $lowerBody, int $lineno, string $tag = null)
{
parent::__construct([
'value_target' => $valueTarget,
'indent' => $indent,
'outdent' => $outdent,
'lower_body' => $lowerBody,
], [], $lineno, $tag);
}
/**
* @inheritdoc
*/
public function compile(Compiler $compiler): void
{
$compiler
// Get this item's level
->write('$_thisItemLevel = (int)' . TemplateHelper::class . '::attribute($this->env, $this->getSourceContext(), ')
->subcompile($this->getNode('value_target'))
->raw(', \'level\', [], ' . Template::class . "::ANY_CALL, false, true);\n")
->write('$_thisItemLft = (int)' . TemplateHelper::class . '::attribute($this->env, $this->getSourceContext(), ')
->subcompile($this->getNode('value_target'))
->raw(', \'lft\', [], ' . Template::class . "::ANY_CALL, false, true);\n")
->write('$_thisItemRgt = (int)' . TemplateHelper::class . '::attribute($this->env, $this->getSourceContext(), ')
->subcompile($this->getNode('value_target'))
->raw(', \'rgt\', [], ' . Template::class . "::ANY_CALL, false, true);\n")
// Was there a previous item?
->write("if (isset(\$_contextsByLevel)) {\n")
->indent()
// Make sure the current item is nested within the item at the previous level, if there was one
->write("if (\n")
->indent()
->write("\$_thisItemLevel > \$context['nav']['level'] + 1 ||\n")
->write("(isset(\$_contextsByLevel[\$_thisItemLevel-1]['nav']['rgt']) && \$_thisItemLft > \$_contextsByLevel[\$_thisItemLevel-1]['nav']['rgt'])\n")
->outdent()
->write(") {\n")
->indent()
->write("continue;\n")
->outdent()
->write("}\n")
// Temporarily set the context to the previous one
->write("\$_tmpContext = \$context;\n")
->write("\$context = \$_contextsByLevel[\$context['nav']['level']];\n")
// Does this one have a greater level than the last one?
->write("if (\$_thisItemLevel > \$context['nav']['level']) {\n")
->indent()
->write("for (\$_i = \$context['nav']['level']; \$_i < \$_thisItemLevel; \$_i++) {\n")
->indent()
->subcompile($this->getNode('indent'))
->outdent()
->write("}\n")
->outdent()
->write("} else {\n")
->indent()
->subcompile($this->getNode('lower_body'))
// Does this one have a lower level than the last one?
->write("if (\$_thisItemLevel < \$context['nav']['level']) {\n")
->indent()
->write("for (\$_i = \$context['nav']['level']-1; \$_i >= \$_thisItemLevel; \$_i--) {\n")
->indent()
// Did we output an item at that level?
->write("if (isset(\$_contextsByLevel[\$_i])) {\n")
->indent()
// Temporarily set the context to the element at this level
->write("\$context = \$_contextsByLevel[\$_i];\n")
->subcompile($this->getNode('outdent'))
->subcompile($this->getNode('lower_body'))
->write("unset(\$_contextsByLevel[\$_i]);\n")
->outdent()
->write("}\n")
->outdent()
->write("}\n")
->outdent()
->write("}\n")
->outdent()
->write("}\n")
// Set the context back
->write("\$context = \$_tmpContext;\n")
->write("unset(\$_tmpContext);\n")
->outdent()
->write("} else {\n")
->indent()
// This is the first item so save its level
->write("\$_firstItemLevel = \$_thisItemLevel;\n")
->outdent()
->write("}\n")
// Create the nav array for this item
->write("\$context['nav'] = [\n")
->indent()
->write("'level' => \$_thisItemLevel,\n")
->write("'lft' => \$_thisItemLft,\n")
->write("'rgt' => \$_thisItemRgt,\n")
->outdent()
->write("];\n")
->write("if (isset(\$_contextsByLevel[\$_thisItemLevel-1])) {\n")
->indent()
->write("\$context['nav']['parent'] = \$_contextsByLevel[\$_thisItemLevel-1];\n")
// Might as well set the item's parent so long as we have it
->write('if (method_exists(')
->subcompile($this->getNode('value_target'))
->raw(", 'setParent')) {\n")
->indent()
->subcompile($this->getNode('value_target'), false)
->raw("->setParent(\$context['nav']['parent'][")
->string($this->getNode('value_target')->getAttribute('name'))
->raw("]);\n")
->outdent()
->write("}\n")
->outdent()
->write("} else {\n")
->indent()
->write("\$context['nav']['parent'] = null;\n")
->outdent()
->write("}\n")
// Save a reference of this item for the next iteration
->write("\$_contextsByLevel[\$_thisItemLevel] = \$context;\n");
}
}