1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Commands; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Composer\Command\BaseCommand; |
22
|
|
|
use Composer\Plugin\Capability\CommandProvider; |
23
|
|
|
use function assert; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Limoncello\Commands |
27
|
|
|
*/ |
28
|
|
|
class ComposerCommandProvider implements CommandProvider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var BaseCommand[] |
32
|
|
|
*/ |
33
|
|
|
private static $commands = []; |
34
|
|
|
|
35
|
1 |
|
/** |
36
|
|
|
* @inheritdoc |
37
|
1 |
|
*/ |
38
|
|
|
public function getCommands() |
39
|
|
|
{ |
40
|
|
|
return static::$commands; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param BaseCommand[] $commands |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
1 |
|
* |
48
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
49
|
1 |
|
*/ |
50
|
|
|
public static function setCommands(array $commands): void |
51
|
1 |
|
{ |
52
|
|
|
assert(static::areCommands($commands) === true); |
|
|
|
|
53
|
|
|
|
54
|
|
|
static::$commands = $commands; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $commands |
59
|
1 |
|
* |
60
|
|
|
* @return bool |
61
|
1 |
|
*/ |
62
|
|
|
private static function areCommands(array $commands): bool |
63
|
1 |
|
{ |
64
|
1 |
|
$areCommands = true; |
65
|
|
|
|
66
|
|
|
foreach ($commands as $command) { |
67
|
1 |
|
$areCommands = $areCommands && ($command instanceof BaseCommand); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $areCommands; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: