1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acacha\ForgePublish\Commands; |
4
|
|
|
|
5
|
|
|
use Acacha\ForgePublish\Commands\Traits\ChecksEnv; |
6
|
|
|
use Acacha\ForgePublish\Commands\Traits\DiesIfEnvVariableIsnotInstalled; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Illuminate\Support\Facades\File; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PublishDeploymentScript. |
13
|
|
|
* |
14
|
|
|
* @package Acacha\ForgePublish\Commands |
15
|
|
|
*/ |
16
|
|
|
class PublishDeploymentScript extends Command |
17
|
|
|
{ |
18
|
|
|
use ChecksEnv, DiesIfEnvVariableIsnotInstalled; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The name and signature of the console command. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $signature = 'publish:deployment_script {file?} {--server=} {--site=}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Deployment script on Laravel Forge site'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Server forge id. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $server; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Laravel forge site id. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $site; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* File with new script content. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $file; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Guzzle http client. |
58
|
|
|
* |
59
|
|
|
* @var Client |
60
|
|
|
*/ |
61
|
|
|
protected $http; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create a new command instance. |
65
|
|
|
* |
66
|
|
|
*/ |
67
|
|
|
public function __construct(Client $http) |
68
|
|
|
{ |
69
|
|
|
parent::__construct(); |
70
|
|
|
$this->http = $http; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Execute the console command. |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
public function handle() |
78
|
|
|
{ |
79
|
|
|
$this->abortCommandExecution(); |
80
|
|
|
|
81
|
|
|
if ($this->file = $this->argument('file')) { |
82
|
|
|
$this->updateDeploymentScript(); |
83
|
|
|
} else { |
84
|
|
|
$this->showDeploymentScript(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function showDeploymentScript() |
89
|
|
|
{ |
90
|
|
|
$this->info('Deployment script for site ' . $this->site . ':'); |
91
|
|
|
|
92
|
|
|
$this->url = $this->obtainAPIURLEndpoint('show_deployment_script_uri'); |
93
|
|
|
|
94
|
|
|
$script = $this->http->get($this->url, [ |
95
|
|
|
'headers' => [ |
96
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
97
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
98
|
|
|
] |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$this->info(''); |
103
|
|
|
$this->line($script->getBody()->getContents()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Update deployment script. |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
protected function updateDeploymentScript() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$this->info('Updating deployment script for site ' . $this->site . '...'); |
112
|
|
|
|
113
|
|
|
$this->url = $this->obtainAPIURLEndpoint('update_deployment_script_uri'); |
114
|
|
|
|
115
|
|
|
if (! File::exists($this->file)) { |
116
|
|
|
$this->error("File " . $this->file . " doesn't exists"); |
117
|
|
|
die(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->http->put($this->url, [ |
121
|
|
|
'form_params' => [ |
122
|
|
|
'content' => file_get_contents($this->file) |
123
|
|
|
], |
124
|
|
|
'headers' => [ |
125
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
126
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
127
|
|
|
] |
128
|
|
|
] |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Obtain API URL endpoint. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
protected function obtainAPIURLEndpoint($type) |
138
|
|
|
{ |
139
|
|
|
$uri = str_replace('{forgeserver}', $this->server, config('forge-publish.' . $type)); |
140
|
|
|
$uri = str_replace('{forgesite}', $this->site, $uri); |
141
|
|
|
return config('forge-publish.url') . $uri; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Abort command execution. |
146
|
|
|
*/ |
147
|
|
|
protected function abortCommandExecution() |
148
|
|
|
{ |
149
|
|
|
$this->server = $this->checkEnv('server', 'ACACHA_FORGE_SERVER'); |
150
|
|
|
$this->site = $this->checkEnv('site', 'ACACHA_FORGE_SITE'); |
151
|
|
|
$this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN'); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.