|
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 DateTime; |
|
15
|
|
|
use Puli\Cli\Util\StringUtil; |
|
16
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
|
17
|
|
|
use Puli\Repository\Api\ResourceCollection; |
|
18
|
|
|
use Puli\Repository\Api\ResourceRepository; |
|
19
|
|
|
use RuntimeException; |
|
20
|
|
|
use Webmozart\Console\Api\Args\Args; |
|
21
|
|
|
use Webmozart\Console\Api\IO\IO; |
|
22
|
|
|
use Webmozart\Console\UI\Component\Grid; |
|
23
|
|
|
use Webmozart\Console\UI\Component\Table; |
|
24
|
|
|
use Webmozart\Console\UI\Style\Alignment; |
|
25
|
|
|
use Webmozart\Console\UI\Style\GridStyle; |
|
26
|
|
|
use Webmozart\Console\UI\Style\TableStyle; |
|
27
|
|
|
use Webmozart\PathUtil\Path; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Handles the "ls" command. |
|
31
|
|
|
* |
|
32
|
|
|
* @since 1.0 |
|
33
|
|
|
* |
|
34
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class LsCommandHandler |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var ResourceRepository |
|
40
|
|
|
*/ |
|
41
|
|
|
private $repo; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $currentPath = '/'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Creates the handler. |
|
50
|
|
|
* |
|
51
|
|
|
* @param ResourceRepository $repo The resource repository. |
|
52
|
|
|
*/ |
|
53
|
31 |
|
public function __construct(ResourceRepository $repo) |
|
54
|
|
|
{ |
|
55
|
31 |
|
$this->repo = $repo; |
|
56
|
31 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Handles the "ls" command. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Args $args The console arguments. |
|
62
|
|
|
* @param IO $io The I/O. |
|
63
|
|
|
* |
|
64
|
|
|
* @return int The status code. |
|
65
|
|
|
*/ |
|
66
|
31 |
|
public function handle(Args $args, IO $io) |
|
67
|
|
|
{ |
|
68
|
31 |
|
$path = Path::makeAbsolute($args->getArgument('path'), $this->currentPath); |
|
69
|
|
|
|
|
70
|
31 |
|
$resource = $this->repo->get($path); |
|
71
|
|
|
|
|
72
|
31 |
|
if (!$resource->hasChildren()) { |
|
73
|
1 |
|
throw new RuntimeException(sprintf( |
|
74
|
1 |
|
'The resource "%s" does not have children.', |
|
75
|
1 |
|
$resource->getPath() |
|
76
|
|
|
)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
30 |
|
if ($args->isOptionSet('long')) { |
|
80
|
27 |
|
$this->listLong($io, $resource->listChildren()); |
|
81
|
|
|
|
|
82
|
27 |
|
return 0; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
$this->listShort($io, $resource->listChildren()); |
|
86
|
|
|
|
|
87
|
3 |
|
return 0; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Prints the resources in the short style (without the "-l" option). |
|
92
|
|
|
* |
|
93
|
|
|
* @param IO $io The I/O. |
|
94
|
|
|
* @param ResourceCollection $resources The resources. |
|
95
|
|
|
*/ |
|
96
|
3 |
|
private function listShort(IO $io, ResourceCollection $resources) |
|
97
|
|
|
{ |
|
98
|
3 |
|
$style = GridStyle::borderless(); |
|
99
|
3 |
|
$style->getBorderStyle()->setLineVCChar(' '); |
|
100
|
3 |
|
$grid = new Grid($style); |
|
101
|
|
|
|
|
102
|
3 |
|
foreach ($resources as $resource) { |
|
103
|
3 |
|
$grid->addCell($this->formatName($resource)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
$grid->render($io); |
|
107
|
3 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Prints the resources in the long style (with the "-l" option). |
|
111
|
|
|
* |
|
112
|
|
|
* @param IO $io The I/O. |
|
113
|
|
|
* @param ResourceCollection $resources The resources. |
|
114
|
|
|
*/ |
|
115
|
27 |
|
private function listLong(IO $io, ResourceCollection $resources) |
|
116
|
|
|
{ |
|
117
|
27 |
|
$style = TableStyle::borderless(); |
|
118
|
27 |
|
$style->setColumnAlignments(array( |
|
119
|
27 |
|
Alignment::LEFT, |
|
120
|
|
|
Alignment::RIGHT, |
|
121
|
|
|
Alignment::LEFT, |
|
122
|
|
|
Alignment::RIGHT, |
|
123
|
|
|
Alignment::RIGHT, |
|
124
|
|
|
Alignment::LEFT, |
|
125
|
|
|
)); |
|
126
|
27 |
|
$table = new Table($style); |
|
127
|
|
|
|
|
128
|
27 |
|
$today = new DateTime(); |
|
129
|
27 |
|
$currentYear = (int) $today->format('Y'); |
|
130
|
|
|
|
|
131
|
27 |
|
foreach ($resources as $resource) { |
|
132
|
|
|
// Create date from timestamp. Result is in the UTC timezone. |
|
133
|
27 |
|
$modifiedAt = new DateTime('@'.$resource->getMetadata()->getModificationTime()); |
|
134
|
|
|
|
|
135
|
|
|
// Set timezone to server timezone. |
|
136
|
27 |
|
$modifiedAt->setTimezone($today->getTimezone()); |
|
137
|
|
|
|
|
138
|
27 |
|
$year = (int) $modifiedAt->format('Y'); |
|
139
|
|
|
|
|
140
|
27 |
|
$table->addRow(array( |
|
141
|
27 |
|
StringUtil::getShortClassName(get_class($resource)), |
|
142
|
27 |
|
$this->formatSize($resource->getMetadata()->getSize()), |
|
143
|
27 |
|
$modifiedAt->format('M'), |
|
144
|
27 |
|
$modifiedAt->format('j'), |
|
145
|
27 |
|
$year < $currentYear ? $year : $modifiedAt->format('H:i'), |
|
146
|
27 |
|
$this->formatName($resource), |
|
147
|
|
|
)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
27 |
|
$table->render($io); |
|
151
|
27 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Formats the name of the resource. |
|
155
|
|
|
* |
|
156
|
|
|
* Resources with children are colored. |
|
157
|
|
|
* |
|
158
|
|
|
* @param PuliResource $resource The resource. |
|
159
|
|
|
* |
|
160
|
|
|
* @return string|null The formatted name. |
|
161
|
|
|
*/ |
|
162
|
30 |
|
private function formatName(PuliResource $resource) |
|
163
|
|
|
{ |
|
164
|
30 |
|
$name = $resource->getName(); |
|
165
|
|
|
|
|
166
|
30 |
|
if ($resource->hasChildren()) { |
|
167
|
|
|
return '<c1>'.$name.'</c1>'; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
30 |
|
return $name; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Formats the given size. |
|
175
|
|
|
* |
|
176
|
|
|
* @param int $size The size in bytes. |
|
177
|
|
|
* |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
27 |
|
private function formatSize($size) |
|
181
|
|
|
{ |
|
182
|
27 |
|
$suffixes = array('', 'K', 'M', 'G', 'T', 'P'); |
|
183
|
27 |
|
reset($suffixes); |
|
184
|
|
|
|
|
185
|
27 |
|
$suffix = current($suffixes); |
|
186
|
|
|
|
|
187
|
27 |
|
while ($size > 1023) { |
|
188
|
20 |
|
next($suffixes); |
|
189
|
|
|
|
|
190
|
20 |
|
if (null === key($suffixes)) { |
|
191
|
|
|
break; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
20 |
|
$size /= 1024; |
|
195
|
20 |
|
$suffix = current($suffixes); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
27 |
|
if ($size < 10) { |
|
199
|
14 |
|
return number_format($size, 1).$suffix; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
13 |
|
return round($size).$suffix; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|