1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Karma; |
6
|
|
|
|
7
|
|
|
use Karma\Configuration\Reader; |
8
|
|
|
use Karma\Configuration\Parser; |
9
|
|
|
use Gaufrette\Filesystem; |
10
|
|
|
use Gaufrette\Adapter\Local; |
11
|
|
|
use Gaufrette\Adapter\Cache; |
12
|
|
|
use Karma\FormatterProviders\ProfileProvider; |
13
|
|
|
use Karma\Generator\NameTranslators\FilePrefixTranslator; |
14
|
|
|
use Karma\Generator\VariableProvider; |
15
|
|
|
use Karma\Generator\ConfigurationFileGenerators\YamlGenerator; |
16
|
|
|
use Karma\Filesystem\Adapters\MultipleAdapter; |
17
|
|
|
use Karma\Filesystem\Adapters\SingleLocalFile; |
18
|
|
|
use Pimple\Container; |
19
|
|
|
|
20
|
|
|
class Application extends Container |
21
|
|
|
{ |
22
|
|
|
const |
23
|
|
|
VERSION = '7.4.0', |
24
|
|
|
DEFAULT_DISTFILE_SUFFIX = '-dist', |
25
|
|
|
DEFAULT_CONF_DIRECTORY = 'env', |
26
|
|
|
DEFAULT_MASTER_FILE = 'master.conf', |
27
|
|
|
BACKUP_SUFFIX = '~', |
28
|
|
|
FINDER_CACHE_DIRECTORY = 'cache/karma', |
29
|
|
|
FINDER_CACHE_DURATION = 86400, |
30
|
|
|
PROFILE_FILENAME = '.karma'; |
31
|
|
|
|
32
|
41 |
|
public function __construct() |
33
|
|
|
{ |
34
|
41 |
|
parent::__construct(); |
35
|
|
|
|
36
|
41 |
|
$this->initializeParameters(); |
|
|
|
|
37
|
|
|
|
38
|
41 |
|
$this->initializeConfiguration(); |
39
|
41 |
|
$this->initializeProfile(); |
40
|
41 |
|
$this->initializeFinder(); |
41
|
41 |
|
$this->initializeSourceFileSystem(); |
42
|
|
|
|
43
|
41 |
|
$this->initializeServices(); |
44
|
41 |
|
} |
45
|
|
|
|
46
|
41 |
|
private function initializeParameters(): void |
47
|
|
|
{ |
48
|
41 |
|
$this['configuration.path'] = 'conf'; |
49
|
41 |
|
$this['configuration.masterFile'] = 'master.conf'; |
50
|
|
|
|
51
|
41 |
|
$this['sources.path'] = ['src']; |
52
|
41 |
|
$this['target.path'] = null; |
53
|
41 |
|
$this['hydrator.allowNonDistFilesOverwrite'] = false; |
54
|
|
|
|
55
|
41 |
|
$this['distFiles.suffix'] = '-dist'; |
56
|
41 |
|
} |
57
|
|
|
|
58
|
41 |
|
private function initializeConfiguration(): void |
59
|
|
|
{ |
60
|
41 |
|
$this['configuration.fileSystem.adapter'] = $this->factory(function(Container $c) { |
61
|
|
|
return new Local($c['configuration.path']); |
62
|
41 |
|
}); |
63
|
|
|
|
64
|
41 |
|
$this['configuration.fileSystem'] = $this->factory(function(Container $c) { |
65
|
32 |
|
return new Filesystem($c['configuration.fileSystem.adapter']); |
66
|
41 |
|
}); |
67
|
|
|
|
68
|
41 |
|
$this['parser'] = function(Container $c) { |
69
|
32 |
|
$parser = new Parser($c['configuration.fileSystem']); |
70
|
|
|
|
71
|
32 |
|
$parser->enableIncludeSupport() |
72
|
32 |
|
->enableExternalSupport() |
73
|
32 |
|
->enableGroupSupport() |
74
|
32 |
|
->setLogger($c['logger']) |
75
|
32 |
|
->parse($c['configuration.masterFile']); |
76
|
|
|
|
77
|
32 |
|
return $parser; |
78
|
|
|
}; |
79
|
|
|
|
80
|
41 |
|
$this['configuration'] = function(Container $c) { |
81
|
31 |
|
$parser = $c['parser']; |
82
|
|
|
|
83
|
31 |
|
return new Reader( |
84
|
31 |
|
$parser->getVariables(), |
85
|
31 |
|
$parser->getExternalVariables(), |
86
|
31 |
|
$parser->getGroups(), |
87
|
31 |
|
$parser->getDefaultEnvironmentsForGroups() |
88
|
|
|
); |
89
|
|
|
}; |
90
|
41 |
|
} |
91
|
|
|
|
92
|
41 |
|
private function initializeProfile(): void |
93
|
|
|
{ |
94
|
41 |
|
$this['profile.fileSystem.adapter'] = $this->factory(function(Container $c) { |
|
|
|
|
95
|
2 |
|
return new Local(getcwd()); |
96
|
41 |
|
}); |
97
|
|
|
|
98
|
41 |
|
$this['profile.fileSystem'] = $this->factory(function(Container $c) { |
99
|
38 |
|
return new Filesystem($c['profile.fileSystem.adapter']); |
100
|
41 |
|
}); |
101
|
|
|
|
102
|
41 |
|
$this['profile'] = function(Container $c) { |
103
|
38 |
|
return new ProfileReader($c['profile.fileSystem']); |
104
|
|
|
}; |
105
|
41 |
|
} |
106
|
|
|
|
107
|
41 |
|
private function initializeSourceFileSystem(): void |
108
|
|
|
{ |
109
|
41 |
|
$this['sources.fileSystem.adapter'] = $this->factory(function(Container $c) { |
110
|
|
|
|
111
|
2 |
|
$paths = $c['sources.path']; |
112
|
|
|
|
113
|
2 |
|
if(! is_array($paths)) |
114
|
|
|
{ |
115
|
|
|
$paths = [$paths]; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
$adapter = new MultipleAdapter(); |
119
|
|
|
|
120
|
2 |
|
foreach($paths as $path) |
121
|
|
|
{ |
122
|
2 |
|
$localAdapter = new Local($path); |
123
|
|
|
|
124
|
2 |
|
if(is_file($path)) |
125
|
|
|
{ |
126
|
|
|
$filename = basename($path); |
127
|
|
|
$path = realpath(dirname($path)); |
128
|
|
|
$localAdapter = new SingleLocalFile($filename, new Local($path)); |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
$adapter->mount($path, $localAdapter); |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
return $adapter; |
135
|
41 |
|
}); |
136
|
|
|
|
137
|
41 |
|
$this['target.fileSystem.adapter'] = $this->factory(function(Container $c) { |
138
|
|
|
|
139
|
3 |
|
if(! empty($c['target.path'])) |
140
|
|
|
{ |
141
|
|
|
$c['hydrator.allowNonDistFilesOverwrite'] = true; |
142
|
|
|
|
143
|
|
|
return new Local($c['target.path']); |
144
|
|
|
} |
145
|
|
|
|
146
|
3 |
|
return $this['sources.fileSystem.adapter']; |
147
|
41 |
|
}); |
148
|
|
|
|
149
|
41 |
|
$this['target.fileSystem'] = $this->factory(function(Container $c) { |
150
|
11 |
|
return new Filesystem($c['target.fileSystem.adapter']); |
151
|
41 |
|
}); |
152
|
|
|
|
153
|
41 |
|
$this['sources.fileSystem'] = $this->factory(function(Container $c) { |
154
|
12 |
|
return new Filesystem($c['sources.fileSystem.adapter']); |
155
|
41 |
|
}); |
156
|
|
|
|
157
|
41 |
|
$this['generate.sources.fileSystem.adapter'] = $this->factory(function(Container $c) { |
158
|
|
|
|
159
|
1 |
|
$sources = $c['sources.path']; |
160
|
|
|
|
161
|
1 |
|
if(count($sources) > 1) |
162
|
|
|
{ |
163
|
|
|
throw new \InvalidArgumentException("Generate command does not allow multiple source paths"); |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
return new Local( |
167
|
1 |
|
array_shift($sources) |
168
|
|
|
); |
169
|
41 |
|
}); |
170
|
|
|
|
171
|
41 |
|
$this['generate.sources.fileSystem'] = $this->factory(function(Container $c) { |
172
|
6 |
|
return new Filesystem($c['generate.sources.fileSystem.adapter']); |
173
|
41 |
|
}); |
174
|
|
|
|
175
|
41 |
|
$this['sources.fileSystem.finder'] = $this->factory(function(Container $c) { |
176
|
12 |
|
return $c['sources.fileSystem']; |
177
|
41 |
|
}); |
178
|
|
|
|
179
|
41 |
|
$this['sources.fileSystem.cached'] = $this->factory(function(Container $c) { |
180
|
2 |
|
$cache = $c['finder.cache.adapter']; |
181
|
2 |
|
$adapter = new Cache( |
|
|
|
|
182
|
2 |
|
$c['sources.fileSystem.adapter'], |
183
|
|
|
$cache, |
184
|
2 |
|
$c['finder.cache.duration'], |
185
|
|
|
$cache |
186
|
|
|
); |
187
|
|
|
|
188
|
2 |
|
return new Filesystem($adapter); |
189
|
41 |
|
}); |
190
|
41 |
|
} |
191
|
|
|
|
192
|
41 |
|
private function initializeFinder(): void |
193
|
|
|
{ |
194
|
41 |
|
$this['finder.cache.path'] = self::FINDER_CACHE_DIRECTORY; |
195
|
41 |
|
$this['finder.cache.duration'] = self::FINDER_CACHE_DURATION; |
196
|
|
|
|
197
|
41 |
|
$this['finder'] = $this->factory(function(Container $c) { |
|
|
|
|
198
|
12 |
|
return new Finder($this['sources.fileSystem.finder']); |
199
|
41 |
|
}); |
200
|
|
|
|
201
|
41 |
|
$this['finder.cache.adapter'] = $this->factory(function(Container $c) { |
202
|
|
|
return new Local($c['finder.cache.path'], true); |
203
|
41 |
|
}); |
204
|
41 |
|
} |
205
|
|
|
|
206
|
41 |
|
private function initializeServices(): void |
207
|
|
|
{ |
208
|
41 |
|
$this['logger'] = $this->factory(function(Container $c) { |
|
|
|
|
209
|
5 |
|
return new \Psr\Log\NullLogger(); |
210
|
41 |
|
}); |
211
|
|
|
|
212
|
41 |
|
$this['formatter.provider'] = function (Container $c) { |
213
|
11 |
|
return new ProfileProvider($c['profile']); |
214
|
|
|
}; |
215
|
|
|
|
216
|
41 |
|
$this['hydrator'] = $this->factory(function(Container $c) { |
217
|
11 |
|
$hydrator = new Hydrator($c['sources.fileSystem'], $c['target.fileSystem'], $c['configuration'], $c['finder'], $c['formatter.provider']); |
218
|
11 |
|
$hydrator->allowNonDistFilesOverwrite($c['hydrator.allowNonDistFilesOverwrite']); |
219
|
|
|
|
220
|
11 |
|
$hydrator->setLogger($c['logger']) |
221
|
11 |
|
->setSuffix($c['distFiles.suffix']); |
222
|
|
|
|
223
|
11 |
|
return $hydrator; |
224
|
41 |
|
}); |
225
|
|
|
|
226
|
41 |
|
$this['generator.nameTranslator'] = function (Container $c) { |
227
|
2 |
|
$translator = new FilePrefixTranslator(); |
228
|
2 |
|
$translator->changeMasterFile($c['configuration.masterFile']); |
229
|
|
|
|
230
|
2 |
|
return $translator; |
231
|
|
|
}; |
232
|
|
|
|
233
|
41 |
|
$this['generator.variableProvider'] = $this->factory(function (Container $c) { |
234
|
6 |
|
$provider = new VariableProvider($c['parser']); |
235
|
|
|
|
236
|
6 |
|
$profile = $c['profile']; |
237
|
6 |
|
$options = $profile->getGeneratorOptions(); |
238
|
6 |
|
if(! isset($options['translator']) || $options['translator'] === 'prefix') |
239
|
|
|
{ |
240
|
2 |
|
$provider->setNameTranslator($c['generator.nameTranslator']); |
241
|
|
|
} |
242
|
|
|
|
243
|
6 |
|
return $provider; |
244
|
41 |
|
}); |
245
|
|
|
|
246
|
41 |
|
$this['configurationFilesGenerator'] = function (Container $c) { |
247
|
6 |
|
return new YamlGenerator($c['generate.sources.fileSystem'], $c['configuration'], $c['generator.variableProvider']); |
248
|
|
|
}; |
249
|
41 |
|
} |
250
|
|
|
} |
251
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: