Skip to content

Tree Helper

Edit this page

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 and Rendering a Tree

You can build a tree 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
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:

1
2
3
4
5
use Symfony\Component\Console\Helper\TreeStyle;
// ...

$tree = TreeHelper::createTree($io, $node, [], TreeStyle::compact());
$tree->render();

TreeHelper::createTree($io, $node, [], TreeStyle::default()) (details)

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

TreeHelper::createTree($io, $node, [], TreeStyle::box()) (details)

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

TreeHelper::createTree($io, $node, [], TreeStyle::doubleBox()) (details)

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

TreeHelper::createTree($io, $node, [], TreeStyle::compact()) (details)

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

TreeHelper::createTree($io, $node, [], TreeStyle::light()) (details)

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

TreeHelper::createTree($io, $node, [], TreeStyle::minimal()) (details)

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

TreeHelper::createTree($io, $node, [], TreeStyle::rounded()) (details)

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
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version