1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Reallyli\LaravelDeployer; |
4
|
|
|
|
5
|
|
|
class ConfigFileBuilder |
6
|
|
|
{ |
7
|
|
|
const DEFAULT_PHP_VERSION = '7.2'; |
8
|
|
|
|
9
|
|
|
protected $laravelHooks = [ |
10
|
|
|
// 'artisan:storage:link', |
11
|
|
|
'artisan:view:clear', |
12
|
|
|
'artisan:cache:clear', |
13
|
|
|
'artisan:config:clear', |
14
|
|
|
'artisan:config:cache', |
15
|
|
|
'artisan:optimize', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
protected $lumenHooks = [ |
19
|
|
|
'artisan:cache:clear', |
20
|
|
|
'artisan:optimize', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected $configs = [ |
24
|
|
|
'default' => 'basic', |
25
|
|
|
'strategies' => [], |
26
|
|
|
|
27
|
|
|
'hooks' => [ |
28
|
|
|
'start' => [ |
29
|
|
|
], |
30
|
|
|
'build' => [], |
31
|
|
|
'ready' => [], |
32
|
|
|
'done' => [], |
33
|
|
|
'success' => [ |
34
|
|
|
'success:notify', |
35
|
|
|
'record:revision:log', |
36
|
|
|
], |
37
|
|
|
'fail' => [ |
38
|
|
|
'failed:notify', |
39
|
|
|
], |
40
|
|
|
], |
41
|
|
|
'options' => [ |
42
|
|
|
'application' => 'LaravelDeployer', |
43
|
|
|
'keep_releases' => 6, |
44
|
|
|
'php_fpm_service' => 'php7.2-fpm', |
45
|
|
|
'group_notify' => false, |
46
|
|
|
'notify_channel_url' => '', |
47
|
|
|
], |
48
|
|
|
'hosts' => [ |
49
|
|
|
'example.com' => [ |
50
|
|
|
'deploy_path' => '/var/www/example.com', |
51
|
|
|
'user' => 'root', |
52
|
|
|
'branch' => 'master', |
53
|
|
|
'forwardAgent' => true, |
54
|
|
|
'multiplexing' => true, |
55
|
|
|
'stage' => 'devel', |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
'localhost' => [], |
59
|
|
|
'include' => [], |
60
|
|
|
'custom_deployer_file' => false, |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
public function __construct() |
64
|
|
|
{ |
65
|
|
|
$basePath = base_path(); |
66
|
|
|
$this->set('options.repository', exec("cd $basePath && git config --get remote.origin.url") ?? ''); |
67
|
|
|
|
68
|
|
|
$lumen = preg_match('/Lumen/', app()->version()); |
69
|
|
|
$this->set('hooks.ready', $lumen ? $this->lumenHooks : $this->laravelHooks); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the configuration value at the given key. |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function get($key, $default = null) |
78
|
|
|
{ |
79
|
|
|
return array_get($this->configs, $key, $default); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Update the configuration array with the given key/value pair. |
84
|
|
|
* |
85
|
|
|
* @return ConfigFileGenerator |
86
|
|
|
*/ |
87
|
|
|
public function set($key, $value) |
88
|
|
|
{ |
89
|
|
|
array_set($this->configs, $key, $value); |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Append the given value to the configuration array at the given key. |
96
|
|
|
* |
97
|
|
|
* @return ConfigFileGenerator |
98
|
|
|
*/ |
99
|
|
|
public function add($key, $value) |
100
|
|
|
{ |
101
|
|
|
$array = array_get($this->configs, $key); |
102
|
|
|
|
103
|
|
|
if (is_array($array)) { |
104
|
|
|
$array[] = $value; |
105
|
|
|
array_set($this->configs, $key, $array); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return current host configurations at the given key. |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function getHost($key) |
117
|
|
|
{ |
118
|
|
|
return array_get(head($this->configs['hosts']), $key); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return the name of the first host in the configurations. |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getHostname() |
127
|
|
|
{ |
128
|
|
|
return array_search(head($this->configs['hosts']), $this->configs['hosts']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Update the host configurations with the given key/value pair. |
133
|
|
|
* |
134
|
|
|
* @return ConfigFileGenerator |
135
|
|
|
*/ |
136
|
|
|
public function setHost($key, $value) |
137
|
|
|
{ |
138
|
|
|
$hostname = $this->getHostname(); |
139
|
|
|
|
140
|
|
|
if ($key !== 'name') { |
141
|
|
|
$this->configs['hosts'][$hostname][$key] = $value; |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($hostname === $value) { |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->configs['hosts'][$value] = $this->configs['hosts'][$hostname]; |
151
|
|
|
unset($this->configs['hosts'][$hostname]); |
152
|
|
|
$this->setHost('deploy_path', "/var/www/$value"); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set up defaults values more suitable for forge servers. |
159
|
|
|
* |
160
|
|
|
* @return ConfigFileGenerator |
161
|
|
|
*/ |
162
|
|
|
public function useForge($phpVersion = self::DEFAULT_PHP_VERSION) |
163
|
|
|
{ |
164
|
|
|
$this->reloadFpm($phpVersion); |
165
|
|
|
$this->setHost('deploy_path', '/home/forge/'.$this->getHostname()); |
166
|
|
|
$this->setHost('user', 'forge'); |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Reload PHP-FPM after every deployment. |
173
|
|
|
* |
174
|
|
|
* @param string $phpVersion The php-fpm version to reload |
175
|
|
|
* @return ConfigFileGenerator |
176
|
|
|
*/ |
177
|
|
|
public function reloadFpm($phpVersion = self::DEFAULT_PHP_VERSION) |
178
|
|
|
{ |
179
|
|
|
$this->add('hooks.done', 'fpm:reload'); |
180
|
|
|
$this->set('options.php_fpm_service', "php$phpVersion-fpm"); |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Build a config file object based on the information |
187
|
|
|
* collected so far. |
188
|
|
|
* |
189
|
|
|
* @return ConfigFile |
190
|
|
|
*/ |
191
|
|
|
public function build() |
192
|
|
|
{ |
193
|
|
|
return new ConfigFile($this->configs); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|