1 | <?php |
||
26 | class GenerateVersionsCommand extends Command |
||
27 | { |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $composerCmd; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $gitCmd; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $contextDir; |
||
42 | |||
43 | /* |
||
44 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
45 | */ |
||
46 | private $output; |
||
47 | |||
48 | /* |
||
49 | * @var \Symfony\Component\Filesystem\Filesystem |
||
50 | */ |
||
51 | private $filesystem; |
||
52 | |||
53 | /* |
||
54 | * @var |Symfony\Component\Yaml\Dumper |
||
55 | */ |
||
56 | private $dumper; |
||
57 | |||
58 | /* |
||
59 | * @var |Symfony\Component\Yaml\Parser |
||
60 | */ |
||
61 | private $parser; |
||
62 | |||
63 | /** |
||
64 | * {@inheritDoc} |
||
65 | * |
||
66 | * @return void |
||
67 | */ |
||
68 | 4 | protected function configure() |
|
77 | |||
78 | |||
79 | /** |
||
80 | * set filesystem (in service-definition) |
||
81 | * |
||
82 | * @param \Symfony\Component\Filesystem\Filesystem $filesystem filesystem |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | 4 | public function setFilesystem(Filesystem $filesystem) |
|
90 | |||
91 | /** |
||
92 | * set dumper (in service-definition) |
||
93 | * |
||
94 | * @param \Symfony\Component\Yaml\Dumper $dumper dumper |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | 4 | public function setDumper(Dumper $dumper) |
|
102 | |||
103 | /** |
||
104 | * set parser (in service-definition) |
||
105 | * |
||
106 | * @param \Symfony\Component\Yaml\Parser $parser parser |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | 4 | public function setParser(Parser $parser) |
|
114 | |||
115 | /** |
||
116 | * {@inheritDoc} |
||
117 | * |
||
118 | * @param InputInterface $input input |
||
119 | * @param OutputInterface $output output |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | 2 | protected function execute(InputInterface $input, OutputInterface $output) |
|
139 | |||
140 | /** |
||
141 | * gets all versions |
||
142 | * |
||
143 | * @return array version numbers of packages |
||
144 | */ |
||
145 | 2 | public function getPackageVersions() |
|
158 | |||
159 | /** |
||
160 | * returns the version of graviton or wrapper (the context) using git |
||
161 | * |
||
162 | * @return array |
||
163 | * |
||
164 | * @throws CommandNotFoundException |
||
165 | */ |
||
166 | 2 | private function getContextVersion() |
|
188 | |||
189 | /** |
||
190 | * returns version for every installed package |
||
191 | * |
||
192 | * @param array $versions versions array |
||
193 | * @throws CommandNotFoundException | \RuntimeException |
||
194 | * @return array |
||
195 | */ |
||
196 | 2 | private function getInstalledPackagesVersion($versions) |
|
197 | { |
||
198 | 2 | $composerFile = $this->contextDir . 'composer.lock'; |
|
199 | 2 | if (!file_exists($composerFile)) { |
|
200 | throw new CommandNotFoundException('Composer lock file not found: '.$composerFile); |
||
201 | } |
||
202 | |||
203 | 2 | $composerJson = json_decode(file_get_contents($composerFile)); |
|
204 | 2 | if (!$composerJson || json_last_error()) { |
|
205 | throw new \RuntimeException('Error on parsing json file: '.$composerFile); |
||
206 | } |
||
207 | |||
208 | 2 | foreach ($composerJson->packages as $package) { |
|
209 | 2 | if ($this->isDesiredVersion($package->name)) { |
|
210 | 1 | array_push($versions, array('id' => $package->name, 'version' => $package->version)); |
|
211 | } |
||
212 | 1 | } |
|
213 | |||
214 | 2 | return $versions; |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * runs a command depending on the context |
||
219 | * |
||
220 | * @param string $command in this case composer or git |
||
221 | * @return string |
||
222 | * |
||
223 | * @throws \RuntimeException |
||
224 | */ |
||
225 | 2 | private function runCommandInContext($command) |
|
226 | { |
||
227 | 2 | $process = new Process( |
|
228 | 2 | 'cd ' . escapeshellarg($this->contextDir) |
|
229 | 2 | . ' && ' . escapeshellcmd($command) |
|
230 | 1 | ); |
|
231 | try { |
||
232 | 2 | $process->mustRun(); |
|
233 | 1 | } catch (ProcessFailedException $pFe) { |
|
234 | $this->output->writeln($pFe->getMessage()); |
||
235 | } |
||
236 | 2 | return $process->getOutput(); |
|
237 | } |
||
238 | |||
239 | /** |
||
240 | * Checks if a command is available in an enviroment and in the context. The command might be as well a path |
||
241 | * to a command. |
||
242 | * |
||
243 | * @param String $command the command to be checked for availability |
||
244 | * @return bool |
||
245 | */ |
||
246 | 2 | private function commandAvailable($command) |
|
247 | { |
||
248 | 2 | $process = new Process( |
|
249 | 2 | 'cd ' . escapeshellarg($this->contextDir) |
|
250 | 2 | . ' && which ' . escapeshellcmd($command) |
|
251 | 1 | ); |
|
252 | 2 | $process->run(); |
|
253 | 2 | return (boolean) strlen(trim($process->getOutput())); |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * checks if the package version is configured |
||
258 | * |
||
259 | * @param string $packageName package name |
||
260 | * @return boolean |
||
261 | * |
||
262 | * @throws \RuntimeException |
||
263 | */ |
||
264 | 2 | private function isDesiredVersion($packageName) |
|
265 | { |
||
266 | 2 | if (empty($packageName)) { |
|
267 | throw new \RuntimeException('Missing package name'); |
||
268 | } |
||
269 | |||
270 | 2 | $config = $this->getConfiguration($this->contextDir . "/app/config/version_service.yml"); |
|
271 | |||
272 | 2 | if (!empty($config['desiredVersions'])) { |
|
273 | 2 | foreach ($config['desiredVersions'] as $confEntry) { |
|
274 | 2 | if ($confEntry == $packageName) { |
|
275 | 2 | return true; |
|
276 | } |
||
277 | 1 | } |
|
278 | 1 | } |
|
279 | |||
280 | 2 | return false; |
|
281 | } |
||
282 | |||
283 | /** |
||
284 | * reads configuration information from the given file into an array. |
||
285 | * |
||
286 | * @param string $filePath Absolute path to the configuration file. |
||
287 | * |
||
288 | * @return array |
||
289 | */ |
||
290 | 2 | private function getConfiguration($filePath) |
|
296 | |||
297 | /** |
||
298 | * Returns the version out of a given version string |
||
299 | * |
||
300 | * @param string $versionString SemVer version string |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getVersionNumber($versionString) |
||
313 | |||
314 | /** |
||
315 | * Get a version string string using a regular expression |
||
316 | * |
||
317 | * @param string $versionString SemVer version string |
||
318 | * @return string |
||
319 | */ |
||
320 | private function getVersionOrBranchName($versionString) |
||
337 | |||
338 | /** |
||
339 | * Normalizing the incorrect SemVer string to a valid one |
||
340 | * |
||
341 | * At the moment, we are getting the version of the root package ('self') using the |
||
342 | * 'composer show -s'-command. Unfortunately Composer is adding an unnecessary ending. |
||
343 | * |
||
344 | * @param string $versionString SemVer version string |
||
345 | * @param string $prefix Version prefix |
||
346 | * @return string |
||
347 | */ |
||
348 | private function normalizeVersionString($versionString, $prefix = 'v') |
||
359 | } |
||
360 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: