1 | <?php |
||
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) |
|
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) |
|
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) |
|
204 | } |
||
205 |