1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SLLH\ComposerVersionsCheck; |
4
|
|
|
|
5
|
|
|
use Composer\Composer; |
6
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
7
|
|
|
use Composer\IO\IOInterface; |
8
|
|
|
use Composer\Package\RootPackageInterface; |
9
|
|
|
use Composer\Plugin\CommandEvent; |
10
|
|
|
use Composer\Plugin\PluginEvents; |
11
|
|
|
use Composer\Plugin\PluginInterface; |
12
|
|
|
use Composer\Repository\RepositoryManager; |
13
|
|
|
use Composer\Script\Event; |
14
|
|
|
use Composer\Script\ScriptEvents; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Sullivan Senechal <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
final class VersionsCheckPlugin implements PluginInterface, EventSubscriberInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Composer |
23
|
|
|
*/ |
24
|
|
|
private $composer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var IOInterface |
28
|
|
|
*/ |
29
|
|
|
private $io; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var VersionsCheck |
33
|
|
|
*/ |
34
|
|
|
private $versionsCheck; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
private $preferLowest; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $options = array(); |
45
|
|
|
|
46
|
|
|
/** @var boolean */ |
47
|
|
|
private $disabled = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $classes = array( |
53
|
|
|
"SLLH\ComposerVersionsCheck\VersionsCheckPlugin", |
54
|
|
|
"SLLH\ComposerVersionsCheck\VersionsCheck", |
55
|
|
|
"SLLH\ComposerVersionsCheck\OutdatedPackage" |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function activate(Composer $composer, IOInterface $io) |
62
|
|
|
{ |
63
|
|
|
// guard for self-update problem |
64
|
|
|
foreach ($this->classes as $class) { |
65
|
|
|
if (!class_exists($class)) { |
66
|
|
|
return $this->disable(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->composer = $composer; |
71
|
|
|
$this->io = $io; |
72
|
|
|
$this->versionsCheck = new VersionsCheck(); |
73
|
|
|
$this->options = $this->resolveOptions(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public static function getSubscribedEvents() |
80
|
|
|
{ |
81
|
|
|
return array( |
82
|
|
|
PluginEvents::COMMAND => array( |
83
|
|
|
array('command'), |
84
|
|
|
), |
85
|
|
|
ScriptEvents::POST_UPDATE_CMD => array( |
86
|
|
|
array('postUpdate', -100), |
87
|
|
|
), |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param CommandEvent $event |
93
|
|
|
*/ |
94
|
|
|
public function command(CommandEvent $event) |
95
|
|
|
{ |
96
|
|
|
if ($this->disabled) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$input = $event->getInput(); |
101
|
|
|
$this->preferLowest = $input->hasOption('prefer-lowest') && true === $input->getOption('prefer-lowest'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Event $event |
106
|
|
|
*/ |
107
|
|
|
public function postUpdate(Event $event) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
if ($this->disabled || true === $this->preferLowest) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->checkVersions($this->composer->getRepositoryManager(), $this->composer->getPackage()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Tries to get plugin options and resolves them. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
private function resolveOptions() |
122
|
|
|
{ |
123
|
|
|
if ($this->disabled) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$pluginConfig = $this->composer->getConfig() |
128
|
|
|
? $this->composer->getConfig()->get('sllh-composer-versions-check') |
129
|
|
|
: null |
130
|
|
|
; |
131
|
|
|
|
132
|
|
|
$options = array( |
133
|
|
|
'show-links' => false, |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
if (null === $pluginConfig) { |
137
|
|
|
return $options; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : $options['show-links']; |
141
|
|
|
|
142
|
|
|
return $options; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param RepositoryManager $repositoryManager |
147
|
|
|
* @param RootPackageInterface $rootPackage |
148
|
|
|
*/ |
149
|
|
|
private function checkVersions(RepositoryManager $repositoryManager, RootPackageInterface $rootPackage) |
150
|
|
|
{ |
151
|
|
|
if ($this->disabled) { |
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
foreach ($repositoryManager->getRepositories() as $repository) { |
156
|
|
|
$this->versionsCheck->checkPackages( |
157
|
|
|
$repository, |
|
|
|
|
158
|
|
|
$repositoryManager->getLocalRepository(), |
159
|
|
|
$rootPackage |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$this->io->write($this->versionsCheck->getOutput($this->options['show-links']), false); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function disable() |
167
|
|
|
{ |
168
|
|
|
$this->disabled = true; |
169
|
|
|
} |
170
|
|
|
} |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.