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() |
|
167 | { |
||
168 | 2 | $wrapper = []; |
|
169 | // git available here? |
||
170 | 2 | if ($this->commandAvailable($this->gitCmd)) { |
|
171 | // get current commit hash |
||
172 | 2 | $currentHash = trim($this->runCommandInContext($this->gitCmd.' rev-parse --short HEAD')); |
|
173 | // get version from hash: |
||
174 | 2 | $version = trim($this->runCommandInContext($this->gitCmd.' tag --points-at ' . $currentHash)); |
|
175 | // if empty, set dev- and current branchname to version: |
||
176 | 2 | if (!strlen($version)) { |
|
177 | $version = 'dev-' . trim($this->runCommandInContext($this->gitCmd.' rev-parse --abbrev-ref HEAD')); |
||
178 | } |
||
179 | 2 | $wrapper['id'] = 'self'; |
|
180 | 2 | $wrapper['version'] = $version; |
|
181 | 1 | } else { |
|
182 | throw new CommandNotFoundException( |
||
183 | 'getContextVersion: '. $this->gitCmd . ' not available in ' . $this->contextDir |
||
184 | ); |
||
185 | } |
||
186 | 2 | return $wrapper; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * returns version for every installed package |
||
191 | * |
||
192 | * @param array $versions versions array |
||
193 | * @return array |
||
194 | */ |
||
195 | 2 | private function getInstalledPackagesVersion($versions) |
|
217 | |||
218 | /** |
||
219 | * runs a command depending on the context |
||
220 | * |
||
221 | * @param string $command in this case composer or git |
||
222 | * @return string |
||
223 | * |
||
224 | * @throws \RuntimeException |
||
225 | */ |
||
226 | 2 | private function runCommandInContext($command) |
|
239 | |||
240 | /** |
||
241 | * Checks if a command is available in an enviroment and in the context. The command might be as well a path |
||
242 | * to a command. |
||
243 | * |
||
244 | * @param String $command the command to be checked for availability |
||
245 | * @return bool |
||
246 | */ |
||
247 | 2 | private function commandAvailable($command) |
|
256 | |||
257 | /** |
||
258 | * checks if the package version is configured |
||
259 | * |
||
260 | * @param string $packageName package name |
||
261 | * @return boolean |
||
262 | * |
||
263 | * @throws \RuntimeException |
||
264 | */ |
||
265 | 2 | private function isDesiredVersion($packageName) |
|
283 | |||
284 | /** |
||
285 | * reads configuration information from the given file into an array. |
||
286 | * |
||
287 | * @param string $filePath Absolute path to the configuration file. |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | 2 | private function getConfiguration($filePath) |
|
297 | |||
298 | /** |
||
299 | * Returns the version out of a given version string |
||
300 | * |
||
301 | * @param string $versionString SemVer version string |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getVersionNumber($versionString) |
||
314 | |||
315 | /** |
||
316 | * Get a version string string using a regular expression |
||
317 | * |
||
318 | * @param string $versionString SemVer version string |
||
319 | * @return string |
||
320 | */ |
||
321 | private function getVersionOrBranchName($versionString) |
||
338 | |||
339 | /** |
||
340 | * Normalizing the incorrect SemVer string to a valid one |
||
341 | * |
||
342 | * At the moment, we are getting the version of the root package ('self') using the |
||
343 | * 'composer show -s'-command. Unfortunately Composer is adding an unnecessary ending. |
||
344 | * |
||
345 | * @param string $versionString SemVer version string |
||
346 | * @param string $prefix Version prefix |
||
347 | * @return string |
||
348 | */ |
||
349 | private function normalizeVersionString($versionString, $prefix = 'v') |
||
360 | } |
||
361 |
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: