1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Acacha\ForgePublish\Commands; |
4
|
|
|
|
5
|
|
|
use Acacha\ForgePublish\Commands\Traits\ItFetchesSites; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PublishSite. |
10
|
|
|
* |
11
|
|
|
* @package Acacha\ForgePublish\Commands |
12
|
|
|
*/ |
13
|
|
|
class PublishSite extends SaveEnvVariable |
14
|
|
|
{ |
15
|
|
|
use ItFetchesSites; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'publish:site {site?}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Save AcachaForge site'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Sites. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $sites; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Default value. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $default; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Site names. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $site_names; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Site already exists?. |
54
|
|
|
* |
55
|
|
|
* @var boolean |
56
|
|
|
*/ |
57
|
|
|
protected $site_already_exists = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Server names. |
61
|
|
|
* |
62
|
|
|
* @var Client |
63
|
|
|
*/ |
64
|
|
|
protected $http; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* SaveEnvVariable constructor. |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
|
public function __construct(Client $http) |
71
|
|
|
{ |
72
|
|
|
parent::__construct(); |
73
|
|
|
$this->http = $http; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Env var to set. |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
protected function envVar() |
82
|
|
|
{ |
83
|
|
|
return 'ACACHA_FORGE_SITE'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Argument key. |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
protected function argKey() |
92
|
|
|
{ |
93
|
|
|
return 'site'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Question text. |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
protected function questionText() |
102
|
|
|
{ |
103
|
|
|
return 'Acacha forge site id?'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Before hook. |
108
|
|
|
*/ |
109
|
|
|
protected function before() |
110
|
|
|
{ |
111
|
|
|
$this->sites = $this->fetchSites(fp_env('ACACHA_FORGE_SERVER')); |
112
|
|
|
$this->site_names = collect($this->sites)->pluck('name')->toArray(); |
113
|
|
|
$this->default = $this->default(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Default proposed value when asking. |
118
|
|
|
* |
119
|
|
|
*/ |
120
|
|
|
protected function default() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$current_value = fp_env('ACACHA_FORGE_SITE'); |
|
|
|
|
123
|
|
|
if ($current_value) { |
|
|
|
|
124
|
|
|
$site_name = $this->getSiteName($this->sites, $current_value); |
125
|
|
|
if ($site_name) { |
|
|
|
|
126
|
|
|
return $site_name; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
return fp_env('ACACHA_FORGE_DOMAIN'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Value. |
134
|
|
|
*/ |
135
|
|
|
protected function value() |
136
|
|
|
{ |
137
|
|
|
$default=null; |
|
|
|
|
138
|
|
|
$choices = $this->site_names ; |
139
|
|
|
if ($result = array_search($this->default, $this->site_names)) { |
140
|
|
|
$default = $result; |
141
|
|
|
} else { |
142
|
|
|
$newChoice = 'Create new Site using domain name: ' . fp_env('ACACHA_FORGE_DOMAIN'); |
143
|
|
|
$choices = array_merge([$newChoice], $this->site_names); |
144
|
|
|
$default = array_search($newChoice, $this->site_names); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$site_name = $this->choice($this->questionText(), $choices, $default); |
148
|
|
|
$site_id = $this->getSiteId($this->sites, $site_name); |
149
|
|
|
if (! $site_id) { |
150
|
|
|
$this->call('publish:create_site'); |
151
|
|
|
return fp_env('ACACHA_FORGE_SITE'); |
152
|
|
|
} else { |
153
|
|
|
$this->info("Site ($site_name) already exists on Laravel Forge with site_id: $site_id"); |
154
|
|
|
return $site_id; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.