Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a0758ac

Browse filesBrowse files
committed
Tweaks
1 parent b39f1c1 commit a0758ac
Copy full SHA for a0758ac

File tree

Expand file treeCollapse file tree

1 file changed

+68
-18
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+68
-18
lines changed

‎components/console/helpers/tree.rst

Copy file name to clipboardExpand all lines: components/console/helpers/tree.rst
+68-18Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Tree Helper
22
===========
33

44
The Tree Helper allows you to build and display tree structures in the console.
5+
It's commonly used to render directory hierarchies, but you can also use it to render
6+
any tree-like content, such us organizational charts, product category trees, taxonomies, etc.
57

68
.. versionadded:: 7.3
79

@@ -10,16 +12,62 @@ The Tree Helper allows you to build and display tree structures in the console.
1012
Rendering a Tree
1113
----------------
1214

13-
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`
15+
The :method:`Symfony\\Component\\Console\\Helper\\TreeHelper::createTree` method
16+
creates a tree structure from an array and returns a :class:`Symfony\\Component\\Console\\Helper\\Tree`
1417
object that can be rendered in the console.
1518

16-
Building a Tree from an Array
17-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
Rendering a Tree from an Array
20+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21+
22+
You can build a tree from an array by passing the array to the
23+
:method:`Symfony\\Component\\Console\\Helper\\TreeHelper::createTree` method
24+
inside your console command::
1825

19-
You can build a tree from an array by passing the array to the :method:`Symfony\\Component\\Console\\Helper\\TreeHelper::createTree`
20-
method::
26+
namespace App\Command;
2127

28+
use Symfony\Component\Console\Attribute\AsCommand;
29+
use Symfony\Component\Console\Command\Command;
30+
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
use Symfony\Component\Console\Style\SymfonyStyle;
2233
use Symfony\Component\Console\Helper\TreeHelper;
34+
use Symfony\Component\Console\Helper\TreeNode;
35+
36+
#[AsCommand(name: 'app:some-command', description: '...')]
37+
class SomeCommand extends Command
38+
{
39+
// ...
40+
41+
protected function execute(InputInterface $input, OutputInterface $output): int
42+
{
43+
$io = new SymfonyStyle($input, $output);
44+
45+
$node = TreeNode::fromValues([
46+
'config/',
47+
'public/',
48+
'src/',
49+
'templates/',
50+
'tests/',
51+
]);
52+
53+
$tree = TreeHelper::createTree($io, $node);
54+
$tree->render();
55+
56+
// ...
57+
}
58+
}
59+
60+
This exampe would output the following:
61+
62+
.. code-block:: terminal
63+
64+
├── config/
65+
├── public/
66+
├── src/
67+
├── templates/
68+
└── tests/
69+
70+
The given contents can be defined in a multi-dimensional array:
2371

2472
$tree = TreeHelper::createTree($io, null, [
2573
'src' => [
@@ -38,7 +86,7 @@ method::
3886

3987
The above code will output the following tree:
4088

41-
.. code-block:: text
89+
.. code-block:: terminal
4290
4391
├── src
4492
│ ├── Command
@@ -48,10 +96,11 @@ The above code will output the following tree:
4896
└── templates
4997
└── base.html.twig
5098
51-
Manually Creating a Tree
52-
~~~~~~~~~~~~~~~~~~~~~~~~
99+
Building and Rendering a Tree
100+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53101

54-
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::
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::
55104

56105
use Symfony\Component\Console\Helper\TreeHelper;
57106
use Symfony\Component\Console\Helper\TreeNode;
@@ -75,11 +124,12 @@ Customizing the Tree Style
75124
Built-in Tree Styles
76125
~~~~~~~~~~~~~~~~~~~~
77126

78-
The tree helper provides a few built-in styles that you can use to customize the output of the tree.
127+
The tree helper provides a few built-in styles that you can use to customize the
128+
output of the tree.
79129

80130
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::default`
81131

82-
.. code-block:: text
132+
.. code-block:: terminal
83133
84134
├── config
85135
│ ├── packages
@@ -96,7 +146,7 @@ The tree helper provides a few built-in styles that you can use to customize the
96146
97147
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::box`
98148

99-
.. code-block:: text
149+
.. code-block:: terminal
100150
101151
┃╸ config
102152
┃ ┃╸ packages
@@ -113,7 +163,7 @@ The tree helper provides a few built-in styles that you can use to customize the
113163
114164
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::doubleBox`
115165

116-
.. code-block:: text
166+
.. code-block:: terminal
117167
118168
╠═ config
119169
║ ╠═ packages
@@ -130,7 +180,7 @@ The tree helper provides a few built-in styles that you can use to customize the
130180
131181
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::compact`
132182

133-
.. code-block:: text
183+
.. code-block:: terminal
134184
135185
├ config
136186
│ ├ packages
@@ -147,7 +197,7 @@ The tree helper provides a few built-in styles that you can use to customize the
147197
148198
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::light`
149199

150-
.. code-block:: text
200+
.. code-block:: terminal
151201
152202
|-- config
153203
| |-- packages
@@ -164,7 +214,7 @@ The tree helper provides a few built-in styles that you can use to customize the
164214
165215
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::minimal`
166216

167-
.. code-block:: text
217+
.. code-block:: terminal
168218
169219
. config
170220
. . packages
@@ -181,7 +231,7 @@ The tree helper provides a few built-in styles that you can use to customize the
181231
182232
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::rounded`
183233

184-
.. code-block:: text
234+
.. code-block:: terminal
185235
186236
├─ config
187237
│ ├─ packages
@@ -226,7 +276,7 @@ of the :class:`Symfony\\Component\\Console\\Helper\\TreeStyle` class::
226276

227277
The above code will output the following tree:
228278

229-
.. code-block:: text
279+
.. code-block:: terminal
230280
231281
🔵 🟣 🟡 src
232282
🔵 🟢 🟣 🟡 Command

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.