Tree Helper
The Tree Helper allows you to build and display tree structures in the console. It's commonly used to render directory hierarchies, but you can also use it to render any tree-like content, such us organizational charts, product category trees, taxonomies, etc.
7.3
The TreeHelper
class was introduced in Symfony 7.3.
Rendering a Tree
The createTree() method creates a tree structure from an array and returns a Tree object that can be rendered in the console.
Rendering a Tree from an Array
You can build a tree from an array by passing the array to the createTree() method inside your console command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
namespace App\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TreeHelper;
use Symfony\Component\Console\Helper\TreeNode;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(name: 'app:some-command', description: '...')]
class SomeCommand extends Command
{
// ...
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$node = TreeNode::fromValues([
'config/',
'public/',
'src/',
'templates/',
'tests/',
]);
$tree = TreeHelper::createTree($io, $node);
$tree->render();
// ...
}
}
This exampe would output the following:
1 2 3 4 5
├── config/
├── public/
├── src/
├── templates/
└── tests/
The given contents can be defined in a multi-dimensional array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$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:
1 2 3 4 5 6 7
├── src
│ ├── Command
│ ├── Controller
│ │ └── DefaultController.php
│ └── Kernel.php
└── templates
└── base.html.twig
Building a Tree Programmatically
If you don't know the tree elements beforehand, you can build the tree programmatically by creating a new instance of the Tree class and adding nodes to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
use Symfony\Component\Console\Helper\TreeHelper;
use Symfony\Component\Console\Helper\TreeNode;
$root = new TreeNode('my-project/');
// you can pass a string directly or create a TreeNode object
$root->addChild('src/');
$root->addChild(new TreeNode('templates/'));
// create nested structures by adding child nodes to other nodes
$testsNode = new TreeNode('tests/');
$functionalTestsNode = new TreeNode('Functional/');
$testsNode->addChild($functionalTestsNode);
$root->addChild(testsNode);
$tree = TreeHelper::createTree($io, $root);
$tree->render();
This example outputs:
1 2 3 4 5
my-project/
├── src/
├── templates/
└── tests/
└── Functional/
If you prefer, you can build the array of elements programmatically and then create and render the tree like this:
1 2
$tree = TreeHelper::createTree($io, null, $array);
$tree->render();
You can also build part of the tree from an array and then add other nodes:
1 2 3 4 5
$node = TreeNode::fromValues($array);
$node->addChild('templates');
// ...
$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.
Default:
1
TreeHelper::createTree($io, $node, [], TreeStyle::default());
This outputs:
1 2 3 4 5 6 7 8 9 10 11 12
├── config
│ ├── packages
│ └── routes
│ ├── framework.yaml
│ └── web_profiler.yaml
├── src
│ ├── Command
│ ├── Controller
│ │ └── DefaultController.php
│ └── Kernel.php
└── templates
└── base.html.twig
Box:
1
TreeHelper::createTree($io, $node, [], TreeStyle::box());
This outputs:
1 2 3 4 5 6 7 8 9 10 11 12
┃╸ config
┃ ┃╸ packages
┃ ┗╸ routes
┃ ┃╸ framework.yaml
┃ ┗╸ web_profiler.yaml
┃╸ src
┃ ┃╸ Command
┃ ┃╸ Controller
┃ ┃ ┗╸ DefaultController.php
┃ ┗╸ Kernel.php
┗╸ templates
┗╸ base.html.twig
Double box:
1
TreeHelper::createTree($io, $node, [], TreeStyle::doubleBox());
This outputs:
1 2 3 4 5 6 7 8 9 10 11 12
╠═ config
║ ╠═ packages
║ ╚═ routes
║ ╠═ framework.yaml
║ ╚═ web_profiler.yaml
╠═ src
║ ╠═ Command
║ ╠═ Controller
║ ║ ╚═ DefaultController.php
║ ╚═ Kernel.php
╚═ templates
╚═ base.html.twig
Compact:
1
TreeHelper::createTree($io, $node, [], TreeStyle::compact());
This outputs:
1 2 3 4 5 6 7 8 9 10 11 12
├ config
│ ├ packages
│ └ routes
│ ├ framework.yaml
│ └ web_profiler.yaml
├ src
│ ├ Command
│ ├ Controller
│ │ └ DefaultController.php
│ └ Kernel.php
└ templates
└ base.html.twig
Light:
1
TreeHelper::createTree($io, $node, [], TreeStyle::light());
This outputs:
1 2 3 4 5 6 7 8 9 10 11 12
|-- config
| |-- packages
| `-- routes
| |-- framework.yaml
| `-- web_profiler.yaml
|-- src
| |-- Command
| |-- Controller
| | `-- DefaultController.php
| `-- Kernel.php
`-- templates
`-- base.html.twig
Minimal:
1
TreeHelper::createTree($io, $node, [], TreeStyle::minimal());
This outputs:
1 2 3 4 5 6 7 8 9 10 11 12
. config
. . packages
. . routes
. . framework.yaml
. . web_profiler.yaml
. src
. . Command
. . Controller
. . . DefaultController.php
. . Kernel.php
. templates
. base.html.twig
Rounded:
1
TreeHelper::createTree($io, $node, [], TreeStyle::rounded());
This outputs:
1 2 3 4 5 6 7 8 9 10 11 12
├─ 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 TreeStyle class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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:
1 2 3 4 5 6 7
🔵 🟣 🟡 src
🔵 🟢 🟣 🟡 Command
🔵 🟢 🟣 🟡 Controller
🔵 🟢 🟢 🟠 🟡 DefaultController.php
🔵 🟢 🟠 🟡 Kernel.php
🔵 🟠 🟡 templates
🔵 🔴 🟠 🟡 base.html.twig