1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Karma; |
4
|
|
|
|
5
|
|
|
use Karma\Configuration\Reader; |
6
|
|
|
use Karma\Configuration\Parser; |
7
|
|
|
use Gaufrette\Filesystem; |
8
|
|
|
use Gaufrette\Adapter\Local; |
9
|
|
|
use Gaufrette\Adapter\Cache; |
10
|
|
|
use Karma\VCS\Vcs; |
11
|
|
|
use Karma\VCS\Git; |
12
|
|
|
use Karma\VCS\Git\GitWrapperAdapter; |
13
|
|
|
use Karma\FormatterProviders\ProfileProvider; |
14
|
|
|
use Karma\Generator\NameTranslators\FilePrefixTranslator; |
15
|
|
|
use Karma\Generator\VariableProvider; |
16
|
|
|
use Karma\Generator\ConfigurationFileGenerators\YamlGenerator; |
17
|
|
|
use Karma\Filesystem\Adapters\MultipleAdapter; |
18
|
|
|
use Karma\Filesystem\Adapters\SingleLocalFile; |
19
|
|
|
use Pimple\Container; |
20
|
|
|
|
21
|
|
|
class Application extends Container |
22
|
|
|
{ |
23
|
|
|
const |
24
|
|
|
VERSION = '5.5.3', |
25
|
|
|
DEFAULT_DISTFILE_SUFFIX = '-dist', |
26
|
|
|
DEFAULT_CONF_DIRECTORY = 'env', |
27
|
|
|
DEFAULT_MASTER_FILE = 'master.conf', |
28
|
|
|
BACKUP_SUFFIX = '~', |
29
|
|
|
FINDER_CACHE_DIRECTORY = 'cache/karma', |
30
|
|
|
FINDER_CACHE_DURATION = 86400, |
31
|
|
|
PROFILE_FILENAME = '.karma'; |
32
|
|
|
|
33
|
44 |
|
public function __construct() |
34
|
|
|
{ |
35
|
44 |
|
parent::__construct(); |
36
|
|
|
|
37
|
44 |
|
$this->initializeParameters(); |
|
|
|
|
38
|
|
|
|
39
|
44 |
|
$this->initializeConfiguration(); |
40
|
44 |
|
$this->initializeProfile(); |
41
|
44 |
|
$this->initializeFinder(); |
42
|
44 |
|
$this->initializeSourceFileSystem(); |
43
|
44 |
|
$this->initializeVcs(); |
44
|
|
|
|
45
|
44 |
|
$this->initializeServices(); |
46
|
44 |
|
} |
47
|
|
|
|
48
|
44 |
|
private function initializeParameters() |
49
|
|
|
{ |
50
|
44 |
|
$this['configuration.path'] = 'conf'; |
51
|
44 |
|
$this['configuration.masterFile'] = 'master.conf'; |
52
|
|
|
|
53
|
44 |
|
$this['sources.path'] = 'src'; |
54
|
44 |
|
$this['target.path'] = null; |
55
|
44 |
|
$this['hydrator.allowNonDistFilesOverwrite'] = false; |
56
|
|
|
|
57
|
44 |
|
$this['distFiles.suffix'] = '-dist'; |
58
|
44 |
|
} |
59
|
|
|
|
60
|
44 |
|
private function initializeConfiguration() |
61
|
|
|
{ |
62
|
|
|
$this['configuration.fileSystem.adapter'] = $this->factory(function($c) { |
63
|
|
|
return new Local($c['configuration.path']); |
64
|
44 |
|
}); |
65
|
|
|
|
66
|
|
|
$this['configuration.fileSystem'] = $this->factory(function($c) { |
67
|
33 |
|
return new Filesystem($c['configuration.fileSystem.adapter']); |
68
|
44 |
|
}); |
69
|
|
|
|
70
|
|
|
$this['parser'] = function($c) { |
71
|
33 |
|
$parser = new Parser($c['configuration.fileSystem']); |
72
|
|
|
|
73
|
33 |
|
$parser->enableIncludeSupport() |
74
|
33 |
|
->enableExternalSupport() |
75
|
33 |
|
->enableGroupSupport() |
76
|
33 |
|
->setLogger($c['logger']) |
77
|
33 |
|
->parse($c['configuration.masterFile']); |
78
|
|
|
|
79
|
33 |
|
return $parser; |
80
|
|
|
}; |
81
|
|
|
|
82
|
|
|
$this['configuration'] = function($c) { |
83
|
32 |
|
$parser = $c['parser']; |
84
|
|
|
|
85
|
32 |
|
return new Reader( |
86
|
32 |
|
$parser->getVariables(), |
87
|
32 |
|
$parser->getExternalVariables(), |
88
|
32 |
|
$parser->getGroups(), |
89
|
32 |
|
$parser->getDefaultEnvironmentsForGroups() |
90
|
32 |
|
); |
91
|
|
|
}; |
92
|
44 |
|
} |
93
|
|
|
|
94
|
44 |
|
private function initializeProfile() |
95
|
|
|
{ |
96
|
|
|
$this['profile.fileSystem.adapter'] = $this->factory(function($c) { |
|
|
|
|
97
|
2 |
|
return new Local(getcwd()); |
98
|
44 |
|
}); |
99
|
|
|
|
100
|
|
|
$this['profile.fileSystem'] = $this->factory(function($c) { |
101
|
41 |
|
return new Filesystem($c['profile.fileSystem.adapter']); |
102
|
44 |
|
}); |
103
|
|
|
|
104
|
|
|
$this['profile'] = function($c) { |
105
|
41 |
|
return new ProfileReader($c['profile.fileSystem']); |
106
|
|
|
}; |
107
|
44 |
|
} |
108
|
|
|
|
109
|
44 |
|
private function initializeSourceFileSystem() |
110
|
|
|
{ |
111
|
|
|
$this['sources.fileSystem.adapter'] = $this->factory(function($c) { |
112
|
|
|
|
113
|
3 |
|
$paths = $c['sources.path']; |
114
|
|
|
|
115
|
3 |
|
if(! is_array($paths)) |
116
|
3 |
|
{ |
117
|
3 |
|
$paths = array($paths); |
118
|
3 |
|
} |
119
|
|
|
|
120
|
3 |
|
$adapter = new MultipleAdapter(); |
121
|
|
|
|
122
|
3 |
|
foreach($paths as $path) |
123
|
|
|
{ |
124
|
3 |
|
$localAdapter = new Local($path); |
125
|
|
|
|
126
|
3 |
|
if(is_file($path)) |
127
|
3 |
|
{ |
128
|
|
|
$filename = basename($path); |
129
|
|
|
$path = realpath(dirname($path)); |
130
|
|
|
$localAdapter = new SingleLocalFile($filename, new Local($path)); |
131
|
|
|
} |
132
|
|
|
|
133
|
3 |
|
$adapter->mount($path, $localAdapter); |
134
|
3 |
|
} |
135
|
|
|
|
136
|
3 |
|
return $adapter; |
137
|
44 |
|
}); |
138
|
|
|
|
139
|
|
|
$this['target.fileSystem.adapter'] = $this->factory(function($c) { |
140
|
|
|
|
141
|
4 |
|
if(! empty($c['target.path'])) |
142
|
4 |
|
{ |
143
|
|
|
$c['hydrator.allowNonDistFilesOverwrite'] = true; |
144
|
|
|
|
145
|
|
|
return new Local($c['target.path']); |
146
|
|
|
} |
147
|
|
|
|
148
|
4 |
|
return $this['sources.fileSystem.adapter']; |
149
|
44 |
|
}); |
150
|
|
|
|
151
|
|
|
$this['target.fileSystem'] = $this->factory(function($c) { |
152
|
14 |
|
return new Filesystem($c['target.fileSystem.adapter']); |
153
|
44 |
|
}); |
154
|
|
|
|
155
|
|
|
$this['sources.fileSystem'] = $this->factory(function($c) { |
156
|
25 |
|
return new Filesystem($c['sources.fileSystem.adapter']); |
157
|
44 |
|
}); |
158
|
|
|
|
159
|
|
|
$this['sources.fileSystem.finder'] = $this->factory(function($c) { |
160
|
17 |
|
return $c['sources.fileSystem']; |
161
|
44 |
|
}); |
162
|
|
|
|
163
|
|
|
$this['sources.fileSystem.cached'] = $this->factory(function($c) { |
164
|
2 |
|
$cache = $c['finder.cache.adapter']; |
165
|
2 |
|
$adapter = new Cache( |
166
|
2 |
|
$c['sources.fileSystem.adapter'], |
167
|
2 |
|
$cache, |
168
|
2 |
|
$c['finder.cache.duration'], |
169
|
|
|
$cache |
170
|
2 |
|
); |
171
|
|
|
|
172
|
2 |
|
return new Filesystem($adapter); |
173
|
44 |
|
}); |
174
|
44 |
|
} |
175
|
|
|
|
176
|
44 |
|
private function initializeFinder() |
177
|
|
|
{ |
178
|
44 |
|
$this['finder.cache.path'] = self::FINDER_CACHE_DIRECTORY; |
179
|
44 |
|
$this['finder.cache.duration'] = self::FINDER_CACHE_DURATION; |
180
|
|
|
|
181
|
|
|
$this['finder'] = $this->factory(function($c) { |
|
|
|
|
182
|
17 |
|
return new Finder($this['sources.fileSystem.finder']); |
183
|
44 |
|
}); |
184
|
|
|
|
185
|
|
|
$this['finder.cache.adapter'] = $this->factory(function($c) { |
186
|
|
|
return new Local($c['finder.cache.path'], true); |
187
|
44 |
|
}); |
188
|
44 |
|
} |
189
|
|
|
|
190
|
44 |
|
private function initializeVcs() |
191
|
|
|
{ |
192
|
44 |
|
$this['rootPath'] = getcwd(); |
193
|
|
|
|
194
|
|
|
$this['vcs.fileSystem.adapter'] = $this->factory(function($c) { |
195
|
|
|
return new Local($c['rootPath']); |
196
|
44 |
|
}); |
197
|
|
|
|
198
|
|
|
$this['vcs.fileSystem'] = $this->factory(function($c) { |
199
|
|
|
return new Filesystem($c['vcs.fileSystem.adapter']); |
200
|
44 |
|
}); |
201
|
|
|
|
202
|
|
|
$this['git.command'] = $this->factory(function($c) { |
|
|
|
|
203
|
|
|
return new GitWrapperAdapter(); |
204
|
44 |
|
}); |
205
|
|
|
|
206
|
|
|
$git = function($c) { |
|
|
|
|
207
|
|
|
return new Git($this['vcs.fileSystem'], $this['rootPath'], $this['git.command']); |
208
|
44 |
|
}; |
209
|
|
|
|
210
|
44 |
|
$this['git'] = $this->factory($git); |
211
|
44 |
|
$this['vcs'] = $this->factory($git); |
212
|
|
|
|
213
|
|
|
$this['vcsHandler'] = $this->protect(function (Vcs $vcs) { |
214
|
2 |
|
$handler = new VcsHandler($vcs, $this['finder']); |
215
|
|
|
|
216
|
2 |
|
$handler->setLogger($this['logger']) |
217
|
2 |
|
->setSuffix($this['distFiles.suffix']); |
218
|
|
|
|
219
|
2 |
|
return $handler; |
220
|
44 |
|
}); |
221
|
44 |
|
} |
222
|
|
|
|
223
|
44 |
|
private function initializeServices() |
224
|
|
|
{ |
225
|
|
|
$this['logger'] = $this->factory(function($c) { |
|
|
|
|
226
|
10 |
|
return new \Psr\Log\NullLogger(); |
227
|
44 |
|
}); |
228
|
|
|
|
229
|
|
|
$this['formatter.provider'] = function ($c) { |
230
|
11 |
|
return new ProfileProvider($c['profile']); |
231
|
|
|
}; |
232
|
|
|
|
233
|
|
|
$this['hydrator'] = $this->factory(function($c) { |
234
|
11 |
|
$hydrator = new Hydrator($c['sources.fileSystem'], $c['target.fileSystem'], $c['configuration'], $c['finder'], $c['formatter.provider']); |
235
|
11 |
|
$hydrator->allowNonDistFilesOverwrite($c['hydrator.allowNonDistFilesOverwrite']); |
236
|
|
|
|
237
|
11 |
|
$hydrator->setLogger($c['logger']) |
238
|
11 |
|
->setSuffix($c['distFiles.suffix']); |
239
|
|
|
|
240
|
11 |
|
return $hydrator; |
241
|
44 |
|
}); |
242
|
|
|
|
243
|
|
|
$this['generator.nameTranslator'] = function ($c) { |
244
|
2 |
|
$translator = new FilePrefixTranslator(); |
245
|
2 |
|
$translator->changeMasterFile($c['configuration.masterFile']); |
246
|
|
|
|
247
|
2 |
|
return $translator; |
248
|
|
|
}; |
249
|
|
|
|
250
|
|
|
$this['generator.variableProvider'] = $this->factory(function ($c) { |
251
|
8 |
|
$provider = new VariableProvider($c['parser'], $c['configuration.masterFile']); |
|
|
|
|
252
|
|
|
|
253
|
8 |
|
$profile = $c['profile']; |
254
|
8 |
|
$options = $profile->getGeneratorOptions(); |
255
|
8 |
|
if(! isset($options['translator']) || $options['translator'] === 'prefix') |
256
|
8 |
|
{ |
257
|
2 |
|
$provider->setNameTranslator($c['generator.nameTranslator']); |
258
|
2 |
|
} |
259
|
|
|
|
260
|
8 |
|
return $provider; |
261
|
44 |
|
}); |
262
|
|
|
|
263
|
6 |
|
$this['configurationFilesGenerator'] = function ($c) { |
264
|
6 |
|
return new YamlGenerator($c['sources.fileSystem'], $c['configuration'], $c['generator.variableProvider']); |
265
|
|
|
}; |
266
|
44 |
|
} |
267
|
|
|
} |
268
|
|
|
|
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: