Skip to content

Commit bc0d884

Browse files
committed
[Console] Document the TreeHelper
PR #59588 fix symfony#20692
1 parent d688f63 commit bc0d884

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed

β€Žcomponents/console/helpers/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
* :doc:`/components/console/helpers/progressbar`
44
* :doc:`/components/console/helpers/questionhelper`
55
* :doc:`/components/console/helpers/table`
6+
* :doc:`/components/console/helpers/tree`
67
* :doc:`/components/console/helpers/debug_formatter`
78
* :doc:`/components/console/helpers/cursor`
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
Tree Helper
2+
===========
3+
4+
The Tree Helper allows you to build and display tree structures in the console.
5+
6+
Rendering a Tree
7+
----------------
8+
9+
The :method:`Symfony\\Component\\Console\\Helper\\TreeHelper::createTree` method creates a tree structure from an array and returns a :class:`Symfony\\Component\\Console\\Helper\\Tree`
10+
object that can be rendered in the console.
11+
12+
Building a Tree from an Array
13+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
15+
You can build a tree from an array by passing the array to the :method:`Symfony\\Component\\Console\\Helper\\TreeHelper::createTree`
16+
method::
17+
18+
use Symfony\\Component\\Console\\Helper\\TreeHelper;
19+
20+
$tree = TreeHelper::createTree($io, null, [
21+
'src' => [
22+
'Command',
23+
'Controller' => [
24+
'DefaultController.php',
25+
],
26+
'Kernel.php',
27+
],
28+
'templates' => [
29+
'base.html.twig',
30+
],
31+
]);
32+
33+
$tree->render();
34+
35+
The above code will output the following tree:
36+
37+
.. code-block:: text
38+
39+
β”œβ”€β”€ src
40+
β”‚ β”œβ”€β”€ Command
41+
β”‚ β”œβ”€β”€ Controller
42+
β”‚ β”‚ └── DefaultController.php
43+
β”‚ └── Kernel.php
44+
└── templates
45+
└── base.html.twig
46+
47+
48+
Manually Creating a Tree
49+
~~~~~~~~~~~~~~~~~~~~~~~~
50+
51+
You can manually create a tree by creating a new instance of the :class:`Symfony\\Component\\Console\\Helper\\Tree` class and adding nodes to it::
52+
53+
use Symfony\\Component\\Console\\Helper\\Tree;
54+
use Symfony\\Component\\Console\\Helper\\TreeHelper;
55+
56+
$tree = new Tree($io);
57+
$tree->add('src', [
58+
'Command',
59+
'Controller' => [
60+
'DefaultController.php',
61+
],
62+
'Kernel.php',
63+
]);
64+
$tree->add('templates', [
65+
'base.html.twig',
66+
]);
67+
68+
$tree->render();
69+
70+
Customizing the Tree Style
71+
--------------------------
72+
73+
Built-in Tree Styles
74+
~~~~~~~~~~~~~~~~~~~~
75+
76+
The tree helper provides a few built-in styles that you can use to customize the output of the tree.
77+
78+
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::default()`
79+
80+
.. code-block:: text
81+
82+
β”œβ”€β”€ config
83+
β”‚ β”œβ”€β”€ packages
84+
β”‚ └── routes
85+
β”‚ β”œβ”€β”€ framework.yaml
86+
β”‚ └── web_profiler.yaml
87+
β”œβ”€β”€ src
88+
β”‚ β”œβ”€β”€ Command
89+
β”‚ β”œβ”€β”€ Controller
90+
β”‚ β”‚ └── DefaultController.php
91+
β”‚ └── Kernel.php
92+
└── templates
93+
└── base.html.twig
94+
95+
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::box()`
96+
97+
.. code-block:: text
98+
99+
┃╸ config
100+
┃ ┃╸ packages
101+
┃ β”—β•Έ routes
102+
┃ ┃╸ framework.yaml
103+
┃ β”—β•Έ web_profiler.yaml
104+
┃╸ src
105+
┃ ┃╸ Command
106+
┃ ┃╸ Controller
107+
┃ ┃ β”—β•Έ DefaultController.php
108+
┃ β”—β•Έ Kernel.php
109+
β”—β•Έ templates
110+
β”—β•Έ base.html.twig
111+
112+
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::doubleBox()`
113+
114+
.. code-block:: text
115+
116+
╠═ config
117+
β•‘ ╠═ packages
118+
β•‘ β•šβ• routes
119+
β•‘ ╠═ framework.yaml
120+
β•‘ β•šβ• web_profiler.yaml
121+
╠═ src
122+
β•‘ ╠═ Command
123+
β•‘ ╠═ Controller
124+
β•‘ β•‘ β•šβ• DefaultController.php
125+
β•‘ β•šβ• Kernel.php
126+
β•šβ• templates
127+
β•šβ• base.html.twig
128+
129+
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::compact()`
130+
131+
.. code-block:: text
132+
133+
β”œ config
134+
β”‚ β”œ packages
135+
β”‚ β”” routes
136+
β”‚ β”œ framework.yaml
137+
β”‚ β”” web_profiler.yaml
138+
β”œ src
139+
β”‚ β”œ Command
140+
β”‚ β”œ Controller
141+
β”‚ β”‚ β”” DefaultController.php
142+
β”‚ β”” Kernel.php
143+
β”” templates
144+
β”” base.html.twig
145+
146+
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::light()`
147+
148+
.. code-block:: text
149+
150+
|-- config
151+
| |-- packages
152+
| `-- routes
153+
| |-- framework.yaml
154+
| `-- web_profiler.yaml
155+
|-- src
156+
| |-- Command
157+
| |-- Controller
158+
| | `-- DefaultController.php
159+
| `-- Kernel.php
160+
`-- templates
161+
`-- base.html.twig
162+
163+
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::minimal()`
164+
165+
.. code-block:: text
166+
167+
. config
168+
. . packages
169+
. . routes
170+
. . framework.yaml
171+
. . web_profiler.yaml
172+
. src
173+
. . Command
174+
. . Controller
175+
. . . DefaultController.php
176+
. . Kernel.php
177+
. templates
178+
. base.html.twig
179+
180+
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::rounded()`
181+
182+
.. code-block:: text
183+
184+
β”œβ”€ config
185+
β”‚ β”œβ”€ packages
186+
β”‚ ╰─ routes
187+
β”‚ β”œβ”€ framework.yaml
188+
β”‚ ╰─ web_profiler.yaml
189+
β”œβ”€ src
190+
β”‚ β”œβ”€ Command
191+
β”‚ β”œβ”€ Controller
192+
β”‚ β”‚ ╰─ DefaultController.php
193+
β”‚ ╰─ Kernel.php
194+
╰─ templates
195+
╰─ base.html.twig
196+
197+
Making a Custom Tree Style
198+
~~~~~~~~~~~~~~~~~~~~~~~~~~
199+
200+
You can create your own tree style by passing the characters to the constructor
201+
of the :class:`Symfony\\Component\\Console\\Helper\\TreeStyle` class::
202+
203+
use Symfony\\Component\\Console\\Helper\\TreeHelper;
204+
use Symfony\\Component\\Console\\Helper\\TreeStyle;
205+
206+
$customStyle = new TreeStyle('🟣 ', '🟠 ', 'πŸ”΅ ', '🟒 ', 'πŸ”΄ ', '🟑 ');
207+
208+
// Pass the custom style to the createTree method
209+
210+
$tree = TreeHelper::createTree($io, null, [
211+
'src' => [
212+
'Command',
213+
'Controller' => [
214+
'DefaultController.php',
215+
],
216+
'Kernel.php',
217+
],
218+
'templates' => [
219+
'base.html.twig',
220+
],
221+
], $customStyle);
222+
223+
$tree->render();
224+
225+
The above code will output the following tree:
226+
227+
..code-block:: text
228+
229+
πŸ”΅ 🟣 🟑 src
230+
πŸ”΅ 🟒 🟣 🟑 Command
231+
πŸ”΅ 🟒 🟣 🟑 Controller
232+
πŸ”΅ 🟒 🟒 🟠 🟑 DefaultController.php
233+
πŸ”΅ 🟒 🟠 🟑 Kernel.php
234+
πŸ”΅ 🟠 🟑 templates
235+
πŸ”΅ πŸ”΄ 🟠 🟑 base.html.twig

β€Žconsole/style.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,27 @@ Content Methods
169169
styled according to the Symfony Style Guide, which allows you to use
170170
features such as dynamically appending rows.
171171

172+
:method:`Symfony\\Component\\Console\\Style\\SymfonyStyle::tree`
173+
It displays the given nested array as a formatted directory/file tree
174+
structure in the console output::
175+
176+
$io->tree([
177+
'src' => [
178+
'Controller' => [
179+
'DefaultController.php',
180+
],
181+
'Kernel.php',
182+
],
183+
'templates' => [
184+
'base.html.twig',
185+
],
186+
]);
187+
188+
:method:`Symfony\\Component\\Console\\Style\\SymfonyStyle::createTree`
189+
Creates an instance of :class:`Symfony\\Component\\Console\\Helper\\TreeHelper`
190+
styled according to the Symfony Style Guide, which allows you to use
191+
features such as dynamically nesting nodes.
192+
172193
:method:`Symfony\\Component\\Console\\Style\\SymfonyStyle::newLine`
173194
It displays a blank line in the command output. Although it may seem useful,
174195
most of the times you won't need it at all. The reason is that every helper

0 commit comments

Comments
Β (0)