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
|
3 |
|
public function handle(Args $args, IO $io) |
86
|
|
|
{ |
87
|
3 |
|
$path = Path::makeAbsolute($args->getArgument('path'), $this->currentPath); |
88
|
|
|
|
89
|
3 |
|
$resource = $this->repo->get($path); |
90
|
3 |
|
$total = 0; |
91
|
|
|
|
92
|
3 |
|
$io->writeLine('<c1>'.$resource->getPath().'</c1>'); |
93
|
|
|
|
94
|
3 |
|
$this->printTree($io, $resource, $total); |
95
|
|
|
|
96
|
3 |
|
$io->writeLine(''); |
97
|
3 |
|
$io->writeLine($total.' resources'); |
98
|
|
|
|
99
|
3 |
|
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
|
3 |
|
private function printTree(IO $io, PuliResource $resource, &$total, $prefix = '') |
111
|
|
|
{ |
112
|
|
|
// The root node has an empty name |
113
|
3 |
|
$children = $resource->listChildren(); |
114
|
3 |
|
$lastIndex = count($children) - 1; |
115
|
3 |
|
$index = 0; |
116
|
|
|
|
117
|
3 |
|
foreach ($children as $child) { |
118
|
3 |
|
$isLastChild = $index === $lastIndex; |
119
|
3 |
|
$childPrefix = $isLastChild ? self::LAST_CHILD_PREFIX : self::CHILD_PREFIX; |
120
|
3 |
|
$nestingPrefix = $isLastChild ? self::NESTING_CLOSED_PREFIX : self::NESTING_OPEN_PREFIX; |
121
|
|
|
|
122
|
3 |
|
$name = $child->getName() ?: '/'; |
123
|
|
|
|
124
|
3 |
|
if ($child->hasChildren()) { |
125
|
3 |
|
$name = '<c1>'.$name.'</c1>'; |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
$io->writeLine($prefix.$childPrefix.$name); |
129
|
|
|
|
130
|
3 |
|
$this->printTree($io, $child, $total, $prefix.$nestingPrefix); |
131
|
|
|
|
132
|
3 |
|
++$index; |
133
|
3 |
|
++$total; |
134
|
|
|
} |
135
|
3 |
|
} |
136
|
|
|
} |
137
|
|
|
|