@@ -96,25 +96,51 @@ The above code will output the following tree:
9696 └── templates
9797 └── base.html.twig
9898
99- Building and Rendering a Tree
100- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99+ Building a Tree Programmatically
100+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101101
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::
104105
105106 use Symfony\Component\Console\Helper\TreeHelper;
106107 use Symfony\Component\Console\Helper\TreeNode;
107108
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
117126
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+ // ...
118144 $tree = TreeHelper::createTree($io, $node);
119145 $tree->render();
120146
@@ -125,15 +151,13 @@ Built-in Tree Styles
125151~~~~~~~~~~~~~~~~~~~~
126152
127153The 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.
129155
130- use Symfony\Component\Console\Helper\TreeStyle;
131- // ...
156+ **Default **::
132157
133- $tree = TreeHelper::createTree($io, $node, [], TreeStyle::compact());
134- $tree->render();
158+ TreeHelper::createTree($io, $node, [], TreeStyle::default());
135159
136- `` TreeHelper::createTree($io, $node, [], TreeStyle::default()) `` (` details `_)
160+ This outputs:
137161
138162.. code-block :: terminal
139163
@@ -150,7 +174,11 @@ output of the tree::
150174 └── templates
151175 └── base.html.twig
152176
153- ``TreeHelper::createTree($io, $node, [], TreeStyle::box()) `` (`details `_)
177+ **Box **::
178+
179+ TreeHelper::createTree($io, $node, [], TreeStyle::box());
180+
181+ This outputs:
154182
155183.. code-block :: terminal
156184
@@ -167,7 +195,11 @@ output of the tree::
167195 ┗╸ templates
168196 ┗╸ base.html.twig
169197
170- ``TreeHelper::createTree($io, $node, [], TreeStyle::doubleBox()) `` (`details `_)
198+ **Double box **::
199+
200+ TreeHelper::createTree($io, $node, [], TreeStyle::doubleBox());
201+
202+ This outputs:
171203
172204.. code-block :: terminal
173205
@@ -184,7 +216,11 @@ output of the tree::
184216 ╚═ templates
185217 ╚═ base.html.twig
186218
187- ``TreeHelper::createTree($io, $node, [], TreeStyle::compact()) `` (`details `_)
219+ **Compact **::
220+
221+ TreeHelper::createTree($io, $node, [], TreeStyle::compact());
222+
223+ This outputs:
188224
189225.. code-block :: terminal
190226
@@ -201,7 +237,11 @@ output of the tree::
201237 └ templates
202238 └ base.html.twig
203239
204- ``TreeHelper::createTree($io, $node, [], TreeStyle::light()) `` (`details `_)
240+ **Light **::
241+
242+ TreeHelper::createTree($io, $node, [], TreeStyle::light());
243+
244+ This outputs:
205245
206246.. code-block :: terminal
207247
@@ -218,7 +258,11 @@ output of the tree::
218258 `-- templates
219259 `-- base.html.twig
220260
221- ``TreeHelper::createTree($io, $node, [], TreeStyle::minimal()) `` (`details `_)
261+ **Minimal **::
262+
263+ TreeHelper::createTree($io, $node, [], TreeStyle::minimal());
264+
265+ This outputs:
222266
223267.. code-block :: terminal
224268
@@ -235,7 +279,11 @@ output of the tree::
235279 . templates
236280 . base.html.twig
237281
238- ``TreeHelper::createTree($io, $node, [], TreeStyle::rounded()) `` (`details `_)
282+ **Rounded **::
283+
284+ TreeHelper::createTree($io, $node, [], TreeStyle::rounded());
285+
286+ This outputs:
239287
240288.. code-block :: terminal
241289
@@ -291,5 +339,3 @@ The above code will output the following tree:
291339 🔵 🟢 🟠 🟡 Kernel.php
292340 🔵 🟠 🟡 templates
293341 🔵 🔴 🟠 🟡 base.html.twig
294-
295- .. _`details` : https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/Console/Helper/TreeStyle.php
0 commit comments