@@ -96,25 +96,51 @@ The above code will output the following tree:
96
96
βββ templates
97
97
βββ base.html.twig
98
98
99
- Building and Rendering a Tree
100
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99
+ Building a Tree Programmatically
100
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101
101
102
- You can build a tree by creating a new instance of the
103
- :class: `Symfony\\ Component\\ Console\\ Helper\\ Tree ` class and adding nodes to it::
102
+ If you don't know the tree elements beforehand, you can build the tree programmatically
103
+ by creating a new instance of the :class: `Symfony\\ Component\\ Console\\ Helper\\ Tree `
104
+ class and adding nodes to it::
104
105
105
106
use Symfony\Component\Console\Helper\TreeHelper;
106
107
use Symfony\Component\Console\Helper\TreeNode;
107
108
108
- $node = TreeNode::fromValues([
109
- 'Command',
110
- 'Controller' => [
111
- 'DefaultController.php',
112
- ],
113
- 'Kernel.php',
114
- ]);
115
- $node->addChild('templates');
116
- $node->addChild('tests');
109
+ $root = new TreeNode('my-project/');
110
+ // you can pass a string directly or create a TreeNode object
111
+ $root->addChild('src/');
112
+ $root->addChild(new TreeNode('templates/'));
113
+
114
+ // create nested structures by adding child nodes to other nodes
115
+ $testsNode = new TreeNode('tests/');
116
+ $functionalTestsNode = new TreeNode('Functional/');
117
+ $testsNode->addChild($functionalTestsNode);
118
+ $root->addChild(testsNode);
119
+
120
+ $tree = TreeHelper::createTree($io, $root);
121
+ $tree->render();
122
+
123
+ This example outputs:
124
+
125
+ .. code-block :: terminal
117
126
127
+ my-project/
128
+ βββ src/
129
+ βββ templates/
130
+ βββ tests/
131
+ βββ Functional/
132
+
133
+ If you prefer, you can build the array of elements programmatically and then
134
+ create and render the tree like this::
135
+
136
+ $tree = TreeHelper::createTree($io, null, $array);
137
+ $tree->render();
138
+
139
+ You can also build part of the tree from an array and then add other nodes::
140
+
141
+ $node = TreeNode::fromValues($array);
142
+ $node->addChild('templates');
143
+ // ...
118
144
$tree = TreeHelper::createTree($io, $node);
119
145
$tree->render();
120
146
@@ -125,15 +151,13 @@ Built-in Tree Styles
125
151
~~~~~~~~~~~~~~~~~~~~
126
152
127
153
The tree helper provides a few built-in styles that you can use to customize the
128
- output of the tree::
154
+ output of the tree.
129
155
130
- use Symfony\Component\Console\Helper\TreeStyle;
131
- // ...
156
+ **Default **::
132
157
133
- $tree = TreeHelper::createTree($io, $node, [], TreeStyle::compact());
134
- $tree->render();
158
+ TreeHelper::createTree($io, $node, [], TreeStyle::default());
135
159
136
- `` TreeHelper::createTree($io, $node, [], TreeStyle::default()) `` (` details `_)
160
+ This outputs:
137
161
138
162
.. code-block :: terminal
139
163
@@ -150,7 +174,11 @@ output of the tree::
150
174
βββ templates
151
175
βββ base.html.twig
152
176
153
- ``TreeHelper::createTree($io, $node, [], TreeStyle::box()) `` (`details `_)
177
+ **Box **::
178
+
179
+ TreeHelper::createTree($io, $node, [], TreeStyle::box());
180
+
181
+ This outputs:
154
182
155
183
.. code-block :: terminal
156
184
@@ -167,7 +195,11 @@ output of the tree::
167
195
ββΈ templates
168
196
ββΈ base.html.twig
169
197
170
- ``TreeHelper::createTree($io, $node, [], TreeStyle::doubleBox()) `` (`details `_)
198
+ **Double box **::
199
+
200
+ TreeHelper::createTree($io, $node, [], TreeStyle::doubleBox());
201
+
202
+ This outputs:
171
203
172
204
.. code-block :: terminal
173
205
@@ -184,7 +216,11 @@ output of the tree::
184
216
ββ templates
185
217
ββ base.html.twig
186
218
187
- ``TreeHelper::createTree($io, $node, [], TreeStyle::compact()) `` (`details `_)
219
+ **Compact **::
220
+
221
+ TreeHelper::createTree($io, $node, [], TreeStyle::compact());
222
+
223
+ This outputs:
188
224
189
225
.. code-block :: terminal
190
226
@@ -201,7 +237,11 @@ output of the tree::
201
237
β templates
202
238
β base.html.twig
203
239
204
- ``TreeHelper::createTree($io, $node, [], TreeStyle::light()) `` (`details `_)
240
+ **Light **::
241
+
242
+ TreeHelper::createTree($io, $node, [], TreeStyle::light());
243
+
244
+ This outputs:
205
245
206
246
.. code-block :: terminal
207
247
@@ -218,7 +258,11 @@ output of the tree::
218
258
`-- templates
219
259
`-- base.html.twig
220
260
221
- ``TreeHelper::createTree($io, $node, [], TreeStyle::minimal()) `` (`details `_)
261
+ **Minimal **::
262
+
263
+ TreeHelper::createTree($io, $node, [], TreeStyle::minimal());
264
+
265
+ This outputs:
222
266
223
267
.. code-block :: terminal
224
268
@@ -235,7 +279,11 @@ output of the tree::
235
279
. templates
236
280
. base.html.twig
237
281
238
- ``TreeHelper::createTree($io, $node, [], TreeStyle::rounded()) `` (`details `_)
282
+ **Rounded **::
283
+
284
+ TreeHelper::createTree($io, $node, [], TreeStyle::rounded());
285
+
286
+ This outputs:
239
287
240
288
.. code-block :: terminal
241
289
@@ -291,5 +339,3 @@ The above code will output the following tree:
291
339
π΅ π’ π π‘ Kernel.php
292
340
π΅ π π‘ templates
293
341
π΅ π΄ π π‘ base.html.twig
294
-
295
- .. _`details` : https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/Console/Helper/TreeStyle.php
0 commit comments