Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ShowTreeCommand
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 showTree
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 showNode
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace Baril\Bonsai\Console;
4
5use Baril\Bonsai\Console\Concerns\InteractsWithTree;
6use Illuminate\Console\Command;
7
8class ShowTreeCommand extends Command
9{
10    use InteractsWithTree;
11
12    protected $signature = 'bonsai:show {model : The model class}
13        {--label= : The property to use as label}
14        {--depth= : The depth limit}';
15    protected $description = 'Outputs the content of the table in tree form';
16
17    protected $model;
18    protected $label;
19    protected $currentDepth = 0;
20    protected $flags = [];
21
22    public function handle()
23    {
24        $this->model = $this->input->getArgument('model');
25        $this->label = $this->input->getOption('label');
26
27        $this->checkModel($this->model);
28
29        $this->showTree($this->input->getOption('depth'));
30    }
31
32    protected function showTree($depth)
33    {
34        $tree = $this->model::getTree($depth);
35        $count = count($tree);
36        foreach ($tree as $k => $node) {
37            $this->showNode($node, $k == $count - 1);
38        }
39    }
40
41    protected function showNode($node, $isLast)
42    {
43        $this->flags[$this->currentDepth] = $isLast;
44        $line = '';
45        for ($i = 0; $i < $this->currentDepth; $i++) {
46            $line .= $this->flags[$i] ? "   " : "\u{2502}  ";
47        }
48        $line .= ($isLast ? "\u{2514}" : "\u{251C}") . "\u{2500}<info> #" . $node->getKey() . '</info>';
49        if ($this->label) {
50            $line .= ': ' . $node->{$this->label};
51        }
52        $this->line($line);
53        if ($node->relationLoaded('children')) {
54            $this->currentDepth++;
55            $subCount = count($node->children);
56            foreach ($node->children as $k => $child) {
57                $this->showNode($child, $k == $subCount - 1);
58            }
59            $this->currentDepth--;
60        }
61    }
62}