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