-
Notifications
You must be signed in to change notification settings - Fork 102
/
DefaultConfiguration.php
410 lines (330 loc) · 13 KB
/
DefaultConfiguration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/*
* This file is part of the EasyDeploy project.
*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EasyCorp\Bundle\EasyDeployBundle\Configuration;
use EasyCorp\Bundle\EasyDeployBundle\Exception\InvalidConfigurationException;
use EasyCorp\Bundle\EasyDeployBundle\Helper\Str;
use EasyCorp\Bundle\EasyDeployBundle\Server\Property;
use EasyCorp\Bundle\EasyDeployBundle\Server\Server;
use Symfony\Component\HttpKernel\Kernel;
/**
* It implements the "Builder" pattern to define the configuration of the
* default deployer using a fluent interface and enabling the IDE autocompletion.
*/
final class DefaultConfiguration extends AbstractConfiguration
{
// variables starting with an underscore are for internal use only
private $_symfonyEnvironmentEnvVarName; // SYMFONY_ENV or APP_ENV
// properties are defined as private so the developer doesn't see them when using
// their IDE autocompletion. To simplify things, the builder defines setter
// methods named the same as each option.
private $symfonyEnvironment = 'prod';
private $keepReleases = 5;
private $repositoryUrl;
private $repositoryBranch = 'master';
private $remotePhpBinaryPath = 'php';
private $updateRemoteComposerBinary = false;
private $remoteComposerBinaryPath = '/usr/local/bin/composer';
private $composerInstallFlags = '--no-dev --prefer-dist --no-interaction --quiet';
private $composerOptimizeFlags = '--optimize';
private $installWebAssets = true;
private $dumpAsseticAssets = false;
private $warmupCache = true;
private $consoleBinaryPath;
private $localProjectDir;
private $binDir;
private $configDir;
private $cacheDir;
private $deployDir;
private $logDir;
private $srcDir;
private $templatesDir;
private $webDir;
private $controllersToRemove = [];
private $writableDirs = [];
private $permissionMethod = 'chmod';
private $permissionMode = '0777';
private $permissionUser;
private $permissionGroup;
private $sharedFiles = [];
private $sharedDirs = [];
private $resetOpCacheFor;
public function __construct(string $localProjectDir)
{
parent::__construct();
$this->localProjectDir = $localProjectDir;
$this->setDefaultConfiguration(Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION);
}
// this proxy method is needed because the autocompletion breaks
// if the parent method is used directly
public function server(string $sshDsn, array $roles = [Server::ROLE_APP], array $properties = []): self
{
parent::server($sshDsn, $roles, $properties);
return $this;
}
// this proxy method is needed because the autocompletion breaks
// if the parent method is used directly
public function useSshAgentForwarding(bool $useIt): self
{
parent::useSshAgentForwarding($useIt);
return $this;
}
public function symfonyEnvironment(string $name): self
{
$this->symfonyEnvironment = $name;
return $this;
}
public function keepReleases(int $numReleases): self
{
$this->keepReleases = $numReleases;
return $this;
}
public function repositoryUrl(string $url): self
{
// SSH agent forwarding only works when using SSH URLs, not https URLs. Check these URLs:
// https://github.com/<user>/<repo>
// https://bitbucket.org/<user>/<repo>
// https://gitlab.com/<user>/<repo>.git
if (Str::startsWith($url, 'http://') || Str::startsWith($url, 'https://')) {
$sshUrl = preg_replace('/https?:\/\/(?<server>.*)\/(?<vendor>.*)\/(?<repository>.*)/', 'git@$1:$2/$3', $url);
if (!Str::endsWith($sshUrl, '.git')) {
$sshUrl .= '.git';
}
throw new InvalidConfigurationException(sprintf('The repository URL must use the SSH syntax instead of the HTTPs syntax to make it work on any remote server. Replace "%s" by "%s"', $url, $sshUrl));
}
$this->repositoryUrl = $url;
return $this;
}
public function repositoryBranch(string $branchName): self
{
$this->repositoryBranch = $branchName;
return $this;
}
public function remotePhpBinaryPath(string $path): self
{
$this->remotePhpBinaryPath = $path;
return $this;
}
public function updateRemoteComposerBinary(bool $updateBeforeInstall): self
{
$this->updateRemoteComposerBinary = $updateBeforeInstall;
return $this;
}
public function remoteComposerBinaryPath(string $path): self
{
$this->remoteComposerBinaryPath = $path;
return $this;
}
public function composerInstallFlags(string $flags): self
{
$this->composerInstallFlags = $flags;
return $this;
}
public function composerOptimizeFlags(string $flags): self
{
$this->composerOptimizeFlags = $flags;
return $this;
}
public function installWebAssets(bool $install): self
{
$this->installWebAssets = $install;
return $this;
}
public function dumpAsseticAssets(bool $dump): self
{
$this->dumpAsseticAssets = $dump;
return $this;
}
public function warmupCache(bool $warmUp): self
{
$this->warmupCache = $warmUp;
return $this;
}
public function consoleBinaryPath(string $path): self
{
$this->consoleBinaryPath = $path;
return $this;
}
// Relative to the project root directory
public function binDir(string $path): self
{
$this->validatePathIsRelativeToProject($path, __METHOD__);
$this->binDir = rtrim($path, '/');
return $this;
}
// Relative to the project root directory
public function configDir(string $path): self
{
$this->validatePathIsRelativeToProject($path, __METHOD__);
$this->configDir = rtrim($path, '/');
return $this;
}
// Relative to the project root directory
public function cacheDir(string $path): self
{
$this->validatePathIsRelativeToProject($path, __METHOD__);
$this->cacheDir = rtrim($path, '/');
return $this;
}
public function deployDir(string $path): self
{
$this->deployDir = rtrim($path, '/');
return $this;
}
// Relative to the project root directory
public function logDir(string $path): self
{
$this->validatePathIsRelativeToProject($path, __METHOD__);
$this->logDir = rtrim($path, '/');
return $this;
}
// Relative to the project root directory
public function srcDir(string $path): self
{
$this->validatePathIsRelativeToProject($path, __METHOD__);
$this->srcDir = rtrim($path, '/');
return $this;
}
// Relative to the project root directory
public function templatesDir(string $path): self
{
$this->validatePathIsRelativeToProject($path, __METHOD__);
$this->templatesDir = rtrim($path, '/');
return $this;
}
// Relative to the project root directory
public function webDir(string $path): self
{
$this->validatePathIsRelativeToProject($path, __METHOD__);
$this->webDir = rtrim($path, '/');
return $this;
}
// Relative to the project root directory
// the $paths can be glob() patterns, so this method needs to resolve them
public function controllersToRemove(array $paths): self
{
$absoluteGlobPaths = array_map(function ($globPath) {
return $this->localProjectDir.DIRECTORY_SEPARATOR.$globPath;
}, $paths);
$localAbsolutePaths = [];
foreach ($absoluteGlobPaths as $path) {
$localAbsolutePaths = array_merge($localAbsolutePaths, glob($path));
}
$localRelativePaths = array_map(function ($absolutePath) {
$relativePath = str_replace($this->localProjectDir, '', $absolutePath);
$this->validatePathIsRelativeToProject($relativePath, 'controllersToRemove');
return trim($relativePath, DIRECTORY_SEPARATOR);
}, $localAbsolutePaths);
$this->controllersToRemove = $localRelativePaths;
return $this;
}
// Relative to the project root directory
public function writableDirs(array $paths): self
{
foreach ($paths as $path) {
$this->validatePathIsRelativeToProject($path, __METHOD__);
}
$this->writableDirs = $paths;
return $this;
}
public function fixPermissionsWithChmod(string $mode = '0777'): self
{
$this->permissionMethod = 'chmod';
$this->permissionMode = $mode;
return $this;
}
public function fixPermissionsWithChown(string $webServerUser): self
{
$this->permissionMethod = 'chown';
$this->permissionUser = $webServerUser;
return $this;
}
public function fixPermissionsWithChgrp(string $webServerGroup): self
{
$this->permissionMethod = 'chgrp';
$this->permissionGroup = $webServerGroup;
return $this;
}
public function fixPermissionsWithAcl(string $webServerUser): self
{
$this->permissionMethod = 'acl';
$this->permissionUser = $webServerUser;
return $this;
}
// Relative to the project root directory
public function sharedFilesAndDirs(array $paths = []): self
{
$this->sharedDirs = [];
$this->sharedFiles = [];
foreach ($paths as $path) {
$this->validatePathIsRelativeToProject($path, __METHOD__);
if (is_dir($this->localProjectDir.DIRECTORY_SEPARATOR.$path)) {
$this->sharedDirs[] = rtrim($path, DIRECTORY_SEPARATOR);
} else {
$this->sharedFiles[] = $path;
}
}
return $this;
}
// the $homepageUrl (e.g. 'https://symfony.com') is needed because OPcache contents can't
// be deleted from the terminal and deployer must make a HTTP request to a real website URL
public function resetOpCacheFor(string $homepageUrl): self
{
if (!Str::startsWith($homepageUrl, 'http')) {
throw new InvalidConfigurationException(sprintf('The value of %s option must be the valid URL of your homepage (it must start with http:// or https://).', Option::resetOpCacheFor));
}
$this->resetOpCacheFor = rtrim($homepageUrl, '/');
return $this;
}
protected function getReservedServerProperties(): array
{
return [Property::bin_dir, Property::config_dir, Property::console_bin, Property::cache_dir, Property::deploy_dir, Property::log_dir, Property::src_dir, Property::templates_dir, Property::web_dir];
}
private function setDefaultConfiguration(int $symfonyMajorVersion, $symfonyMinorVersion): void
{
if (2 === $symfonyMajorVersion) {
$this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV';
$this->setDirs('app', 'app/config', 'app/cache', 'app/logs', 'src', 'app/Resources/views', 'web');
$this->controllersToRemove(['web/app_*.php']);
$this->sharedFiles = ['app/config/parameters.yml'];
$this->sharedDirs = ['app/logs'];
$this->writableDirs = ['app/cache/', 'app/logs/'];
$this->dumpAsseticAssets = true;
} elseif (3 === $symfonyMajorVersion && 4 < $symfonyMinorVersion) {
$this->_symfonyEnvironmentEnvVarName = 'SYMFONY_ENV';
$this->setDirs('bin', 'app/config', 'var/cache', 'var/logs', 'src', 'app/Resources/views', 'web');
$this->controllersToRemove(['web/app_*.php']);
$this->sharedFiles = ['app/config/parameters.yml'];
$this->sharedDirs = ['var/logs'];
$this->writableDirs = ['var/cache/', 'var/logs/'];
} elseif (4 === $symfonyMajorVersion || (3 === $symfonyMajorVersion && 4 >= $symfonyMinorVersion)) {
$this->_symfonyEnvironmentEnvVarName = 'APP_ENV';
$this->setDirs('bin', 'config', 'var/cache', 'var/log', 'src', 'templates', 'public');
$this->controllersToRemove([]);
$this->sharedDirs = ['var/log'];
$this->writableDirs = ['var/cache/', 'var/log/'];
}
}
private function setDirs(string $binDir, string $configDir, string $cacheDir, string $logDir, string $srcDir, string $templatesDir, string $webDir): void
{
$this->binDir = $binDir;
$this->configDir = $configDir;
$this->cacheDir = $cacheDir;
$this->logDir = $logDir;
$this->srcDir = $srcDir;
$this->templatesDir = $templatesDir;
$this->webDir = $webDir;
}
private function validatePathIsRelativeToProject($path, $methodName): void
{
if (!is_readable($this->localProjectDir.DIRECTORY_SEPARATOR.$path)) {
throw new InvalidConfigurationException(sprintf('The "%s" value given in %s() is not relative to the project root directory or is not readable.', $path, $methodName));
}
}
}