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