1 | <?php |
||
13 | abstract class AbstractFileGenerator implements ConfigurationFileGenerator |
||
14 | { |
||
15 | protected Filesystem |
||
|
|||
16 | $fs; |
||
17 | protected Configuration |
||
18 | $reader; |
||
19 | protected VariableProvider |
||
20 | $variableProvider; |
||
21 | protected bool |
||
22 | $dryRun, |
||
23 | $enableBackup; |
||
24 | protected ?string |
||
25 | $systemEnvironment; |
||
26 | |||
27 | 14 | public function __construct(Filesystem $fs, Configuration $reader, VariableProvider $variableProvider) |
|
28 | { |
||
29 | 14 | $this->fs = $fs; |
|
30 | 14 | $this->reader = $reader; |
|
31 | 14 | $this->variableProvider = $variableProvider; |
|
32 | 14 | $this->dryRun = false; |
|
33 | 14 | $this->enableBackup = false; |
|
34 | 14 | $this->systemEnvironment = null; |
|
35 | 14 | } |
|
36 | |||
37 | 1 | public function setDryRun(bool $value = true): ConfigurableProcessor |
|
38 | { |
||
39 | 1 | $this->dryRun = (bool) $value; |
|
40 | |||
41 | 1 | return $this; |
|
42 | } |
||
43 | |||
44 | 3 | public function enableBackup(bool $value = true): ConfigurableProcessor |
|
45 | { |
||
46 | 3 | $this->enableBackup = (bool) $value; |
|
47 | |||
48 | 3 | return $this; |
|
49 | } |
||
50 | |||
51 | 13 | public function generate(string $environment): void |
|
52 | { |
||
53 | 13 | $this->preGenerate(); |
|
54 | |||
55 | 13 | $variables = $this->variableProvider->getAllVariables(); |
|
56 | |||
57 | 13 | foreach($variables as $variable => $translatedVariableName) |
|
58 | { |
||
59 | 13 | $value = $this->read($variable, $environment); |
|
60 | 13 | $this->generateVariable($translatedVariableName, $value); |
|
61 | } |
||
62 | |||
63 | 12 | $this->postGenerate(); |
|
64 | 12 | } |
|
65 | |||
66 | 13 | private function read($variable, $environment) |
|
67 | { |
||
68 | 13 | if($this->systemEnvironment !== null && $this->reader->isSystem($variable)) |
|
69 | { |
||
70 | 1 | $environment = $this->systemEnvironment; |
|
71 | } |
||
72 | |||
73 | 13 | return $this->reader->read($variable, $environment); |
|
74 | } |
||
75 | |||
76 | 2 | public function setSystemEnvironment(?string $environment): ConfigurableProcessor |
|
77 | { |
||
78 | 2 | $this->systemEnvironment = $environment; |
|
79 | |||
80 | 2 | return $this; |
|
81 | } |
||
82 | |||
83 | abstract protected function generateVariable(string $variableName, $value): void; |
||
84 | |||
85 | 13 | protected function preGenerate(): void |
|
86 | { |
||
87 | |||
88 | 13 | } |
|
89 | |||
90 | protected function postGenerate(): void |
||
91 | { |
||
95 |