Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
[Console] Document theTreeHelper
#20786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
Tree Helper | ||
=========== | ||
The Tree Helper allows you to build and display tree structures in the console. | ||
.. versionadded:: 7.3 | ||
The ``TreeHelper`` class was introduced in Symfony 7.3. | ||
Rendering a Tree | ||
---------------- | ||
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` | ||
object that can be rendered in the console. | ||
Building a Tree from an Array | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
You can build a tree from an array by passing the array to the :method:`Symfony\\Component\\Console\\Helper\\TreeHelper::createTree` | ||
method:: | ||
use Symfony\Component\Console\Helper\TreeHelper; | ||
$tree = TreeHelper::createTree($io, null, [ | ||
'src' => [ | ||
'Command', | ||
'Controller' => [ | ||
'DefaultController.php', | ||
], | ||
'Kernel.php', | ||
], | ||
'templates' => [ | ||
'base.html.twig', | ||
], | ||
]); | ||
$tree->render(); | ||
The above code will output the following tree: | ||
.. code-block:: text | ||
├── src | ||
│ ├── Command | ||
│ ├── Controller | ||
│ │ └── DefaultController.php | ||
│ └── Kernel.php | ||
└── templates | ||
└── base.html.twig | ||
Manually Creating a Tree | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
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:: | ||
use Symfony\Component\Console\Helper\TreeHelper; | ||
use Symfony\Component\Console\Helper\TreeNode; | ||
$node = TreeNode::fromValues([ | ||
'Command', | ||
'Controller' => [ | ||
'DefaultController.php', | ||
], | ||
'Kernel.php', | ||
]); | ||
$node->addChild('templates'); | ||
$node->addChild('tests'); | ||
$tree = TreeHelper::createTree($io, $node); | ||
$tree->render(); | ||
Customizing the Tree Style | ||
-------------------------- | ||
Built-in Tree Styles | ||
~~~~~~~~~~~~~~~~~~~~ | ||
The tree helper provides a few built-in styles that you can use to customize the output of the tree. | ||
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::default` | ||
.. code-block:: text | ||
├── config | ||
│ ├── packages | ||
│ └── routes | ||
│ ├── framework.yaml | ||
│ └── web_profiler.yaml | ||
├── src | ||
│ ├── Command | ||
│ ├── Controller | ||
│ │ └── DefaultController.php | ||
│ └── Kernel.php | ||
└── templates | ||
└── base.html.twig | ||
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::box` | ||
.. code-block:: text | ||
┃╸ config | ||
┃ ┃╸ packages | ||
┃ ┗╸ routes | ||
┃ ┃╸ framework.yaml | ||
┃ ┗╸ web_profiler.yaml | ||
┃╸ src | ||
┃ ┃╸ Command | ||
┃ ┃╸ Controller | ||
┃ ┃ ┗╸ DefaultController.php | ||
┃ ┗╸ Kernel.php | ||
┗╸ templates | ||
┗╸ base.html.twig | ||
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::doubleBox` | ||
.. code-block:: text | ||
╠═ config | ||
║ ╠═ packages | ||
║ ╚═ routes | ||
║ ╠═ framework.yaml | ||
║ ╚═ web_profiler.yaml | ||
╠═ src | ||
║ ╠═ Command | ||
║ ╠═ Controller | ||
║ ║ ╚═ DefaultController.php | ||
║ ╚═ Kernel.php | ||
╚═ templates | ||
╚═ base.html.twig | ||
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::compact` | ||
.. code-block:: text | ||
├ config | ||
│ ├ packages | ||
│ └ routes | ||
│ ├ framework.yaml | ||
│ └ web_profiler.yaml | ||
├ src | ||
│ ├ Command | ||
│ ├ Controller | ||
│ │ └ DefaultController.php | ||
│ └ Kernel.php | ||
└ templates | ||
└ base.html.twig | ||
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::light` | ||
.. code-block:: text | ||
|-- config | ||
| |-- packages | ||
| `-- routes | ||
| |-- framework.yaml | ||
| `-- web_profiler.yaml | ||
|-- src | ||
| |-- Command | ||
| |-- Controller | ||
| | `-- DefaultController.php | ||
| `-- Kernel.php | ||
`-- templates | ||
`-- base.html.twig | ||
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::minimal` | ||
.. code-block:: text | ||
. config | ||
. . packages | ||
. . routes | ||
. . framework.yaml | ||
. . web_profiler.yaml | ||
. src | ||
. . Command | ||
. . Controller | ||
. . . DefaultController.php | ||
. . Kernel.php | ||
. templates | ||
. base.html.twig | ||
:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::rounded` | ||
.. code-block:: text | ||
├─ config | ||
│ ├─ packages | ||
│ ╰─ routes | ||
│ ├─ framework.yaml | ||
│ ╰─ web_profiler.yaml | ||
├─ src | ||
│ ├─ Command | ||
│ ├─ Controller | ||
│ │ ╰─ DefaultController.php | ||
│ ╰─ Kernel.php | ||
╰─ templates | ||
╰─ base.html.twig | ||
Making a Custom Tree Style | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
You can create your own tree style by passing the characters to the constructor | ||
of the :class:`Symfony\\Component\\Console\\Helper\\TreeStyle` class:: | ||
use Symfony\Component\Console\Helper\TreeHelper; | ||
use Symfony\Component\Console\Helper\TreeStyle; | ||
$customStyle = new TreeStyle('🟣 ', '🟠 ', '🔵 ', '🟢 ', '🔴 ', '🟡 '); | ||
// Pass the custom style to the createTree method | ||
$tree = TreeHelper::createTree($io, null, [ | ||
'src' => [ | ||
'Command', | ||
'Controller' => [ | ||
'DefaultController.php', | ||
], | ||
'Kernel.php', | ||
], | ||
'templates' => [ | ||
'base.html.twig', | ||
], | ||
], $customStyle); | ||
$tree->render(); | ||
The above code will output the following tree: | ||
.. code-block:: text | ||
🔵 🟣 🟡 src | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I love this example ❤️ | ||
🔵 🟢 🟣 🟡 Command | ||
🔵 🟢 🟣 🟡 Controller | ||
🔵 🟢 🟢 🟠 🟡 DefaultController.php | ||
🔵 🟢 🟠 🟡 Kernel.php | ||
🔵 🟠 🟡 templates | ||
🔵 🔴 🟠 🟡 base.html.twig | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Please add a versionadded directive below, like for other new features, thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. The whole page is about the new tree helper, should i put the versionadded at the very start of the page ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think yes, and add it also in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yes please, so that a reader knows, since when this can be used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Would this work ? (last changes) |
Uh oh!
There was an error while loading.Please reload this page.