1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PCextreme\Cloudstack\Console; |
4
|
|
|
|
5
|
|
|
use PCextreme\Cloudstack\Client; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\Question; |
11
|
|
|
|
12
|
|
|
class ApiListCommand extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Configures the current command. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
protected function configure() |
20
|
|
|
{ |
21
|
|
|
$this->setName('api:list') |
22
|
|
|
->setDescription('Generate API list cache.') |
23
|
|
|
->setHelp("This will generate the API list cache file usign the 'listApis' command."); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Executes the current command. |
28
|
|
|
* |
29
|
|
|
* @param InputInterface $input |
30
|
|
|
* @param OutputInterface $output |
31
|
|
|
* @return null|int |
32
|
|
|
*/ |
33
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
34
|
|
|
{ |
35
|
|
|
$urlApi = $this->askUrlApi($input, $output); |
36
|
|
|
$apiKey = $this->askApiKey($input, $output); |
37
|
|
|
$secretKey = $this->askSecretKey($input, $output); |
38
|
|
|
|
39
|
|
|
$output->writeLn(''); |
40
|
|
|
$output->writeLn("<info>Processing API list. Please Wait...</info>"); |
41
|
|
|
|
42
|
|
|
$client = new Client([ |
43
|
|
|
'urlApi' => $urlApi, |
44
|
|
|
'apiKey' => $apiKey, |
45
|
|
|
'secretKey' => $secretKey, |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
$command = 'listApis'; |
49
|
|
|
$method = $client->getCommandMethod($command); |
50
|
|
|
$url = $client->getCommandUrl($command, []); |
51
|
|
|
$request = $client->getRequest($method, $url, []); |
52
|
|
|
|
53
|
|
|
$list = $client->getResponse($request); |
54
|
|
|
$this->processList($input, $output, $list); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Ask for URL API. |
59
|
|
|
* |
60
|
|
|
* @param InputInterface $input |
61
|
|
|
* @param OutputInterface $output |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
protected function askUrlApi(InputInterface $input, OutputInterface $output) |
65
|
|
|
{ |
66
|
|
|
$question = new Question( |
67
|
|
|
'Enter target API url [default: https://api.auroracompute.eu/ams]: ', |
68
|
|
|
'https://api.auroracompute.eu/ams' |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Ask for API key. |
76
|
|
|
* |
77
|
|
|
* @param InputInterface $input |
78
|
|
|
* @param OutputInterface $output |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
protected function askApiKey(InputInterface $input, OutputInterface $output) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$question = (new Question('Enter API key: ')) |
84
|
|
|
->setValidator(function ($answer) { |
85
|
|
|
if (is_null($answer)) { |
86
|
|
|
throw new \InvalidArgumentException("API key can't be null."); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $answer; |
90
|
|
|
}) |
91
|
|
|
->setMaxAttempts(2); |
92
|
|
|
|
93
|
|
|
return $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Ask for secret key. |
98
|
|
|
* |
99
|
|
|
* @param InputInterface $input |
100
|
|
|
* @param OutputInterface $output |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
protected function askSecretKey(InputInterface $input, OutputInterface $output) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$question = (new Question('Enter secret key: ')) |
106
|
|
|
->setValidator(function ($answer) { |
107
|
|
|
if (is_null($answer)) { |
108
|
|
|
throw new \InvalidArgumentException("Secret key can't be null."); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $answer; |
112
|
|
|
}) |
113
|
|
|
->setMaxAttempts(2); |
114
|
|
|
|
115
|
|
|
return $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Dump cache file of APIs list. |
120
|
|
|
* |
121
|
|
|
* @param InputInterface $input |
122
|
|
|
* @param OutputInterface $output |
123
|
|
|
* @param array $list |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
protected function processList(InputInterface $input, OutputInterface $output, array $list = []) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
if (empty($list)) { |
129
|
|
|
throw new \RuntimeException("API list is empty."); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$progress = new ProgressBar($output, $list['listapisresponse']['count']); |
133
|
|
|
$progress->start(); |
134
|
|
|
|
135
|
|
|
$listFile = "<?php\n\n"; |
136
|
|
|
$listFile .= "// api_list.php @generated by api:list command\n\n"; |
137
|
|
|
$listFile .= "return array(\n"; |
138
|
|
|
|
139
|
|
|
foreach ($list['listapisresponse']['api'] as $api) { |
140
|
|
|
$name = $api['name']; |
141
|
|
|
$description = addslashes($api['description']); |
142
|
|
|
$isAsync = $api['isasync']; |
143
|
|
|
|
144
|
|
|
$listFile .= " '$name' => array(\n"; |
145
|
|
|
$listFile .= " 'description' => '$description',\n"; |
146
|
|
|
$listFile .= " 'isasync' => " . ($isAsync ? "true" : "false") . ",\n"; |
147
|
|
|
$listFile .= " 'params' => array(\n"; |
148
|
|
|
|
149
|
|
|
foreach ($api['params'] as $param) { |
150
|
|
|
$paramName = $param['name']; |
151
|
|
|
$paramDescription = addslashes($param['description']); |
152
|
|
|
$paramType = $param['type']; |
153
|
|
|
$paramRequired = $param['required']; |
154
|
|
|
|
155
|
|
|
$listFile .= " '$paramName' => array(\n"; |
156
|
|
|
$listFile .= " 'description' => '$paramDescription',\n"; |
157
|
|
|
$listFile .= " 'type' => '$paramType',\n"; |
158
|
|
|
$listFile .= " 'required' => " . ($paramRequired ? "true" : "false") . ",\n"; |
159
|
|
|
$listFile .= " ),\n"; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$listFile .= " ),\n"; |
163
|
|
|
$listFile .= " ),\n"; |
164
|
|
|
|
165
|
|
|
$progress->advance(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$listFile .= ");\n"; |
169
|
|
|
|
170
|
|
|
file_put_contents(__DIR__ . '/../../cache/api_list.php', $listFile); |
171
|
|
|
|
172
|
|
|
$progress->finish(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.