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