1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Karma\Generator\ConfigurationFileGenerators; |
6
|
|
|
|
7
|
|
|
use Karma\ConfigurableProcessor; |
8
|
|
|
use Karma\Generator\ConfigurationFileGenerator; |
9
|
|
|
use Gaufrette\Filesystem; |
10
|
|
|
use Karma\Configuration; |
11
|
|
|
use Karma\Generator\VariableProvider; |
12
|
|
|
|
13
|
|
|
abstract class AbstractFileGenerator implements ConfigurationFileGenerator |
14
|
|
|
{ |
15
|
|
|
protected |
16
|
|
|
$fs, |
|
|
|
|
17
|
|
|
$reader, |
18
|
|
|
$variableProvider, |
19
|
|
|
$dryRun, |
20
|
|
|
$enableBackup, |
21
|
|
|
$systemEnvironment; |
22
|
|
|
|
23
|
14 |
|
public function __construct(Filesystem $fs, Configuration $reader, VariableProvider $variableProvider) |
24
|
|
|
{ |
25
|
14 |
|
$this->fs = $fs; |
26
|
14 |
|
$this->reader = $reader; |
27
|
14 |
|
$this->variableProvider = $variableProvider; |
28
|
14 |
|
$this->dryRun = false; |
29
|
14 |
|
$this->enableBackup = false; |
30
|
14 |
|
$this->systemEnvironment = null; |
31
|
14 |
|
} |
32
|
|
|
|
33
|
1 |
|
public function setDryRun($value = true) |
34
|
|
|
{ |
35
|
1 |
|
$this->dryRun = (bool) $value; |
36
|
|
|
|
37
|
1 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
public function enableBackup($value = true) |
41
|
|
|
{ |
42
|
3 |
|
$this->enableBackup = (bool) $value; |
43
|
|
|
|
44
|
3 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
13 |
|
public function generate(string $environment): void |
48
|
|
|
{ |
49
|
13 |
|
$this->preGenerate(); |
50
|
|
|
|
51
|
13 |
|
$variables = $this->variableProvider->getAllVariables(); |
52
|
|
|
|
53
|
13 |
|
foreach($variables as $variable => $translatedVariableName) |
54
|
|
|
{ |
55
|
13 |
|
$value = $this->read($variable, $environment); |
56
|
13 |
|
$this->generateVariable($translatedVariableName, $value); |
57
|
|
|
} |
58
|
|
|
|
59
|
12 |
|
$this->postGenerate(); |
60
|
12 |
|
} |
61
|
|
|
|
62
|
13 |
|
private function read($variable, $environment) |
63
|
|
|
{ |
64
|
13 |
|
if($this->systemEnvironment !== null && $this->reader->isSystem($variable)) |
65
|
|
|
{ |
66
|
1 |
|
$environment = $this->systemEnvironment; |
67
|
|
|
} |
68
|
|
|
|
69
|
13 |
|
return $this->reader->read($variable, $environment); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function setSystemEnvironment(string $environment): ConfigurableProcessor |
73
|
|
|
{ |
74
|
2 |
|
$this->systemEnvironment = $environment; |
75
|
|
|
|
76
|
2 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
abstract protected function generateVariable($variableName, $value); |
80
|
|
|
|
81
|
13 |
|
protected function preGenerate() |
82
|
|
|
{ |
83
|
|
|
|
84
|
13 |
|
} |
85
|
|
|
|
86
|
|
|
protected function postGenerate() |
87
|
|
|
{ |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.