|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acacha\ForgePublish\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Acacha\ForgePublish\Commands\Traits\InteractsWithEnvironment; |
|
6
|
|
|
use Acacha\ForgePublish\Commands\Traits\ShowsErrorResponse; |
|
7
|
|
|
use Acacha\ForgePublish\Commands\Traits\DiesIfEnvVariableIsnotInstalled; |
|
8
|
|
|
use Acacha\ForgePublish\Commands\Traits\DiesIfNoEnvFileExists; |
|
9
|
|
|
use Acacha\ForgePublish\Commands\Traits\WaitsForSite; |
|
10
|
|
|
use GuzzleHttp\Client; |
|
11
|
|
|
use Illuminate\Console\Command; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class PublishCreateSite. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Acacha\ForgePublish\Commands |
|
17
|
|
|
*/ |
|
18
|
|
|
class PublishCreateSite extends Command |
|
19
|
|
|
{ |
|
20
|
|
|
use WaitsForSite, InteractsWithEnvironment, ShowsErrorResponse, DiesIfNoEnvFileExists, DiesIfEnvVariableIsnotInstalled; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Project type. |
|
24
|
|
|
* |
|
25
|
|
|
* @var String |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $project_type; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Site directory. |
|
31
|
|
|
* |
|
32
|
|
|
* @var String |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $site_directory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Domain. |
|
38
|
|
|
* |
|
39
|
|
|
* @var String |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $domain; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The name and signature of the console command. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $signature = 'publish:create_site {server?} {domain?} {project_type?} {site_directory?}'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The console command description. |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $description = 'Create site on forge for the current user'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Guzzle Http client |
|
60
|
|
|
* |
|
61
|
|
|
* @var Client |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $http; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Laravel Forge site name. |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $sites; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Laravel Forge site name. |
|
74
|
|
|
* |
|
75
|
|
|
* @var String |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $site; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Laravel Forge server. |
|
81
|
|
|
* |
|
82
|
|
|
* @var String |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $server; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Laravel Forge site id. |
|
88
|
|
|
* |
|
89
|
|
|
* @var integer |
|
90
|
|
|
*/ |
|
91
|
|
|
protected $site_id; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* API endpoint URL |
|
95
|
|
|
* |
|
96
|
|
|
* @var String |
|
97
|
|
|
*/ |
|
98
|
|
|
protected $url; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* PublishCreateSite constructor. |
|
102
|
|
|
* |
|
103
|
|
|
* @param Client $client |
|
|
|
|
|
|
104
|
|
|
*/ |
|
105
|
|
|
public function __construct(Client $http) |
|
106
|
|
|
{ |
|
107
|
|
|
parent::__construct(); |
|
108
|
|
|
$this->http = $http; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get Value. |
|
113
|
|
|
* |
|
114
|
|
|
* @return array|int|null|string |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function getValue($value, $env_var, $command) |
|
117
|
|
|
{ |
|
118
|
|
|
if ($this->argument($value)) { |
|
119
|
|
|
return $this->argument($value); |
|
120
|
|
|
} else { |
|
121
|
|
|
return fp_env($env_var) ? fp_env($env_var) : $this->call("publish:$command"); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get Forge server. |
|
127
|
|
|
* |
|
128
|
|
|
* @return array|int|null|string |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function getForgeServer() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->getValue('server', 'ACACHA_FORGE_SERVER', 'server'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get Domain. |
|
137
|
|
|
* |
|
138
|
|
|
* @return array|int|null|string |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function getDomain() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->getValue('domain', 'ACACHA_FORGE_DOMAIN', 'domain'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get project type. |
|
147
|
|
|
* |
|
148
|
|
|
* @return array|int|null|string |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getProjectType() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->getValue('project_type', 'ACACHA_FORGE_PROJECT_TYPE', 'project_type'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get site directory. |
|
157
|
|
|
* |
|
158
|
|
|
* @return array|int|null|string |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getSiteDirectory() |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->getValue('site_directory', 'ACACHA_FORGE_SITE_DIRECTORY', 'site_directory'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Execute the console command. |
|
167
|
|
|
* |
|
168
|
|
|
*/ |
|
169
|
|
|
public function handle() |
|
170
|
|
|
{ |
|
171
|
|
|
$this->abortCommandExecution(); |
|
172
|
|
|
$this->obtainFields(); |
|
173
|
|
|
if ($this->siteIsAlreadyCreated()) { |
|
174
|
|
|
$this->info("Site $this->site ($this->site_id) is already created. Skipping..."); |
|
175
|
|
|
return; |
|
176
|
|
|
} |
|
177
|
|
|
$this->url = $this->obtainApiEndPointURL(); |
|
178
|
|
|
$site = $this->createSiteOnForge(); |
|
179
|
|
|
$this->waitForSite($site['id']); |
|
180
|
|
|
$this->addValueToEnv('ACACHA_FORGE_SITE', $site['id']); |
|
181
|
|
|
$this->info('The site has been added to Forge'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Obtain fields. |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function obtainFields() |
|
189
|
|
|
{ |
|
190
|
|
|
$this->server = $this->getForgeServer(); |
|
|
|
|
|
|
191
|
|
|
$this->domain = $this->getDomain(); |
|
|
|
|
|
|
192
|
|
|
$this->project_type = $this->getProjectType(); |
|
|
|
|
|
|
193
|
|
|
$this->site_directory = $this->getSiteDirectory(); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Create site on Forge. |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function createSiteOnForge() |
|
200
|
|
|
{ |
|
201
|
|
|
try { |
|
202
|
|
|
$this->info('Creating site ' . $this->domain . ' on server ' . $this->server); |
|
203
|
|
|
$response = $this->http->post($this->url, [ |
|
204
|
|
|
'form_params' => [ |
|
205
|
|
|
'domain' => $this->domain, |
|
206
|
|
|
'project_type' => $this->project_type, |
|
207
|
|
|
'directory' => $this->site_directory |
|
208
|
|
|
], |
|
209
|
|
|
'headers' => [ |
|
210
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
|
211
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
|
212
|
|
|
] |
|
213
|
|
|
]); |
|
214
|
|
|
return json_decode($response->getBody(), true); |
|
215
|
|
|
} catch (\Exception $e) { |
|
216
|
|
|
$this->showErrorAndDie($e); |
|
217
|
|
|
return null; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Obtain API endpoint URL. |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function obtainApiEndPointURL() |
|
225
|
|
|
{ |
|
226
|
|
|
$uri = str_replace('{forgeserver}', $this->server, config('forge-publish.post_sites_uri')); |
|
227
|
|
|
return config('forge-publish.url') . $uri; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Check if command have to be skipped. |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function abortCommandExecution() |
|
234
|
|
|
{ |
|
235
|
|
|
$this->dieIfNoEnvFileIsFound(); |
|
236
|
|
|
$this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN'); |
|
237
|
|
|
$this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_SERVER'); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Is site already created? |
|
242
|
|
|
*/ |
|
243
|
|
|
protected function siteIsAlreadyCreated() |
|
244
|
|
|
{ |
|
245
|
|
|
$this->sites = $this->fetchSites(fp_env('ACACHA_FORGE_SERVER')); |
|
|
|
|
|
|
246
|
|
|
$this->site = fp_env('ACACHA_FORGE_SITE'); |
|
247
|
|
|
if (in_array($this->site, collect($this->sites)->pluck('id')->toArray())) { |
|
248
|
|
|
return true; |
|
249
|
|
|
} |
|
250
|
|
|
return in_array($this->domain, collect($this->sites)->pluck('name')->toArray()); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.