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