|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/cli package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Puli\Cli\Handler; |
|
13
|
|
|
|
|
14
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
|
15
|
|
|
use Puli\Repository\Api\ResourceRepository; |
|
16
|
|
|
use Webmozart\Console\Api\Args\Args; |
|
17
|
|
|
use Webmozart\Console\Api\IO\IO; |
|
18
|
|
|
use Webmozart\PathUtil\Path; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Handles the "tree" command. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 1.0 |
|
24
|
|
|
* |
|
25
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class TreeCommandHandler |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Prefix string for child resources. |
|
31
|
|
|
* |
|
32
|
|
|
* @internal |
|
33
|
|
|
*/ |
|
34
|
|
|
const CHILD_PREFIX = '├── '; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Prefix string for the last child resource of a parent resource. |
|
38
|
|
|
* |
|
39
|
|
|
* @internal |
|
40
|
|
|
*/ |
|
41
|
|
|
const LAST_CHILD_PREFIX = '└── '; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Prefix for nested resources when an ancestor resource is open. |
|
45
|
|
|
* |
|
46
|
|
|
* @internal |
|
47
|
|
|
*/ |
|
48
|
|
|
const NESTING_OPEN_PREFIX = '│ '; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Prefix for nested resources when no ancestor resource is open. |
|
52
|
|
|
* |
|
53
|
|
|
* @internal |
|
54
|
|
|
*/ |
|
55
|
|
|
const NESTING_CLOSED_PREFIX = ' '; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var ResourceRepository |
|
59
|
|
|
*/ |
|
60
|
|
|
private $repo; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
private $currentPath = '/'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Creates the handler. |
|
69
|
|
|
* |
|
70
|
|
|
* @param ResourceRepository $repo The resource repository. |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function __construct(ResourceRepository $repo) |
|
73
|
|
|
{ |
|
74
|
3 |
|
$this->repo = $repo; |
|
75
|
3 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Handles the "tree" command. |
|
79
|
|
|
* |
|
80
|
|
|
* @param Args $args The console arguments. |
|
81
|
|
|
* @param IO $io The I/O. |
|
82
|
|
|
* |
|
83
|
|
|
* @return int The status code. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function handle(Args $args, IO $io) |
|
86
|
|
|
{ |
|
87
|
|
|
$path = Path::makeAbsolute($args->getArgument('path'), $this->currentPath); |
|
88
|
|
|
|
|
89
|
|
|
$resource = $this->repo->get($path); |
|
90
|
|
|
$total = 0; |
|
91
|
|
|
|
|
92
|
|
|
$io->writeLine('<c1>'.$resource->getPath().'</c1>'); |
|
93
|
|
|
|
|
94
|
|
|
$this->printTree($io, $resource, $total); |
|
95
|
|
|
|
|
96
|
|
|
$io->writeLine(''); |
|
97
|
|
|
$io->writeLine($total.' resources'); |
|
98
|
|
|
|
|
99
|
|
|
return 0; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Recursively prints the tree for the given resource. |
|
104
|
|
|
* |
|
105
|
|
|
* @param IO $io The I/O. |
|
106
|
|
|
* @param PuliResource $resource The printed resource. |
|
107
|
|
|
* @param int $total Collects the total number of printed resources. |
|
108
|
|
|
* @param string $prefix The prefix for all printed resources. |
|
109
|
|
|
*/ |
|
110
|
|
|
private function printTree(IO $io, PuliResource $resource, &$total, $prefix = '') |
|
111
|
|
|
{ |
|
112
|
|
|
// The root node has an empty name |
|
113
|
|
|
$children = $resource->listChildren(); |
|
114
|
|
|
$lastIndex = count($children) - 1; |
|
115
|
|
|
$index = 0; |
|
116
|
|
|
|
|
117
|
|
|
foreach ($children as $child) { |
|
118
|
|
|
$isLastChild = $index === $lastIndex; |
|
119
|
|
|
$childPrefix = $isLastChild ? self::LAST_CHILD_PREFIX : self::CHILD_PREFIX; |
|
120
|
|
|
$nestingPrefix = $isLastChild ? self::NESTING_CLOSED_PREFIX : self::NESTING_OPEN_PREFIX; |
|
121
|
|
|
|
|
122
|
|
|
$name = $child->getName() ?: '/'; |
|
123
|
|
|
|
|
124
|
|
|
if ($child->hasChildren()) { |
|
125
|
|
|
$name = '<c1>'.$name.'</c1>'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$io->writeLine($prefix.$childPrefix.$name); |
|
129
|
|
|
|
|
130
|
|
|
$this->printTree($io, $child, $total, $prefix.$nestingPrefix); |
|
131
|
|
|
|
|
132
|
|
|
++$index; |
|
133
|
|
|
++$total; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|