1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Magedownload CLI |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category MageDownload |
8
|
|
|
* @package MageDownload |
9
|
|
|
* @author Steve Robbins <[email protected]> |
10
|
|
|
* @copyright 2015 Steve Robbins |
11
|
|
|
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0 |
12
|
|
|
* @link https://github.com/steverobbins/magedownload-cli |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace MageDownload\Command; |
16
|
|
|
|
17
|
|
|
use MageDownload\Info; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Files command |
24
|
|
|
* |
25
|
|
|
* @category MageDownload |
26
|
|
|
* @package MageDownload |
27
|
|
|
* @author Steve Robbins <[email protected]> |
28
|
|
|
* @copyright 2015 Steve Robbins |
29
|
|
|
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0 |
30
|
|
|
* @link https://github.com/steverobbins/magedownload-cli |
31
|
|
|
*/ |
32
|
|
|
class FilesCommand extends AbstractCommand |
33
|
|
|
{ |
34
|
|
|
const NAME = 'files'; |
35
|
|
|
|
36
|
|
|
const API_ACTION_FILES = 'files'; |
37
|
|
|
const API_ACTION_FILTER = 'filter'; |
38
|
|
|
|
39
|
|
|
const OPTION_FILTER_TYPE = 'filter-type'; |
40
|
|
|
const OPTION_FILTER_VERSION = 'filter-version'; |
41
|
|
|
|
42
|
|
|
protected $typeFilters = array( |
43
|
|
|
'ce-full', |
44
|
|
|
'ce-patch', |
45
|
|
|
'ee-full', |
46
|
|
|
'ee-patch', |
47
|
|
|
'other', |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Configure command |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
protected function configure() |
56
|
|
|
{ |
57
|
|
|
$this |
58
|
|
|
->setName(self::NAME) |
59
|
|
|
->setDescription('List files available for download') |
60
|
|
|
->addOption( |
61
|
|
|
self::OPTION_FILTER_VERSION, |
62
|
|
|
null, |
63
|
|
|
InputOption::VALUE_REQUIRED, |
64
|
|
|
'Version to filter by (1.9.2.1, 1.9.*, etc)' |
65
|
|
|
) |
66
|
|
|
->addOption( |
67
|
|
|
self::OPTION_FILTER_TYPE, |
68
|
|
|
null, |
69
|
|
|
InputOption::VALUE_REQUIRED, |
70
|
|
|
'Type to filter by (' . implode(', ', $this->typeFilters) . ')' |
71
|
|
|
); |
72
|
|
|
parent::configure(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Execute command |
77
|
|
|
* |
78
|
|
|
* @param InputInterface $input |
79
|
|
|
* @param OutputInterface $output |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
84
|
|
|
{ |
85
|
|
|
$info = new Info; |
86
|
|
|
$action = self::API_ACTION_FILES; |
87
|
|
|
$filters = $this->getFilters(); |
88
|
|
|
if ($filters) { |
89
|
|
|
$action = self::API_ACTION_FILTER; |
90
|
|
|
} |
91
|
|
|
return $this->render($info->sendCommand( |
92
|
|
|
$action . $filters, |
93
|
|
|
$this->getAccountId(), |
94
|
|
|
$this->getAccessToken() |
95
|
|
|
)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get any applied filters |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
protected function getFilters() |
104
|
|
|
{ |
105
|
|
|
$filters = array(); |
106
|
|
|
if ($this->input->getOption(self::OPTION_FILTER_VERSION)) { |
107
|
|
|
$filters['version'] = $this->input->getOption(self::OPTION_FILTER_VERSION); |
108
|
|
|
} |
109
|
|
|
if ($this->input->getOption(self::OPTION_FILTER_TYPE)) { |
110
|
|
|
$filters['type'] = $this->input->getOption(self::OPTION_FILTER_TYPE); |
111
|
|
|
if (!in_array($filters['type'], $this->typeFilters)) { |
112
|
|
|
throw new \InvalidArgumentException( |
113
|
|
|
"Invalid filter type. Must be one of: \n " . implode("\n ", $this->typeFilters) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
if (!count($filters)) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
$result = '/'; |
121
|
|
|
foreach ($filters as $type => $value) { |
122
|
|
|
$result .= $type . '/' . $value; |
123
|
|
|
} |
124
|
|
|
return $result; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Render the files action |
129
|
|
|
* |
130
|
|
|
* @param string $result |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
protected function render($result) |
135
|
|
|
{ |
136
|
|
|
$bits = preg_split('/\-{5,}/', $result); |
137
|
|
|
if (count($bits) == 1) { |
138
|
|
|
return $this->out(trim($result)); |
139
|
|
|
} |
140
|
|
|
$headers = array(); |
141
|
|
|
foreach (preg_split('/ {2,}/', $bits[0]) as $value) { |
142
|
|
|
$headers[] = trim($value); |
143
|
|
|
} |
144
|
|
|
unset($headers[0]); |
145
|
|
|
$rows = array(); |
146
|
|
|
foreach (explode("\n", $bits[1]) as $row) { |
147
|
|
|
if (empty($row)) { |
148
|
|
|
continue; |
149
|
|
|
} |
150
|
|
|
$row = preg_split('/ {2,}/', $row); |
151
|
|
|
unset($row[0]); |
152
|
|
|
$rows[] = $row; |
153
|
|
|
} |
154
|
|
|
usort($rows, array($this, 'sortFiles')); |
155
|
|
|
$this->out(array(array( |
156
|
|
|
'type' => 'table', |
157
|
|
|
'data' => array( |
158
|
|
|
$headers, |
159
|
|
|
$rows |
160
|
|
|
) |
161
|
|
|
))); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Sort files by type and name |
166
|
|
|
* |
167
|
|
|
* @param string[] $a |
168
|
|
|
* @param string[] $b |
169
|
|
|
* |
170
|
|
|
* @return integer |
171
|
|
|
*/ |
172
|
|
|
protected function sortFiles($a, $b) |
173
|
|
|
{ |
174
|
|
|
return strcmp($a[1], $b[1]) ?: strcmp($a[2], $a[2]); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|