Skip to content

Commit ac36db4

Browse files
authored
Add TreeHelper documentation to input-output.md (#8253)
1 parent fad5bfc commit ac36db4

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

docs/en/console-commands/input-output.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,84 @@ $io->helper('Banner')
182182
The `BannerHelper` was added in 5.1
183183
:::
184184

185+
### Tree Helper
186+
187+
The `TreeHelper` formats nested arrays into a tree structure with visual
188+
connectors, similar to the output of the Unix `tree` command. This is useful
189+
for displaying hierarchical data such as directory structures, menu trees,
190+
or nested categories:
191+
192+
```php
193+
$data = [
194+
'src' => [
195+
'Controller' => [
196+
'AppController.php',
197+
'UsersController.php',
198+
],
199+
'Model' => [
200+
'Entity' => [
201+
'User.php',
202+
],
203+
'Table' => [
204+
'UsersTable.php',
205+
],
206+
],
207+
],
208+
'config' => [
209+
'app.php',
210+
'routes.php',
211+
],
212+
];
213+
$io->helper('Tree')->output($data);
214+
215+
// Outputs:
216+
// ├── src
217+
// │ ├── Controller
218+
// │ │ ├── AppController.php
219+
// │ │ └── UsersController.php
220+
// │ └── Model
221+
// │ ├── Entity
222+
// │ │ └── User.php
223+
// │ └── Table
224+
// │ └── UsersTable.php
225+
// └── config
226+
// ├── app.php
227+
// └── routes.php
228+
```
229+
230+
The helper supports various value types including strings, booleans, enums,
231+
and closures for lazy evaluation:
232+
233+
```php
234+
$data = [
235+
'debug' => true,
236+
'cache' => false,
237+
'status' => fn () => 'computed value',
238+
];
239+
$io->helper('Tree')->output($data);
240+
241+
// Outputs:
242+
// ├── debug
243+
// │ └── true
244+
// ├── cache
245+
// │ └── false
246+
// └── status
247+
// └── computed value
248+
```
249+
250+
You can customize the indentation using the `baseIndent` and `elementIndent`
251+
configuration options:
252+
253+
```php
254+
$io->helper('Tree')
255+
->setConfig('baseIndent', 4)
256+
->output($data);
257+
```
258+
259+
::: info Added in version 5.3.0
260+
The `TreeHelper` was added in 5.3
261+
:::
262+
185263
## Getting User Input
186264

187265
`method` Cake\\Console\\ConsoleIo::**ask**(string $prompt, ?string $default = null): string

0 commit comments

Comments
 (0)