1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* hirak/prestissimo |
4
|
|
|
* @author Hiraku NAKANO |
5
|
|
|
* @license MIT https://github.com/hirak/prestissimo |
6
|
|
|
*/ |
7
|
|
|
namespace Hirak\Prestissimo; |
8
|
|
|
|
9
|
|
|
use Composer\Composer; |
10
|
|
|
use Composer\IO; |
11
|
|
|
use Composer\Plugin as CPlugin; |
12
|
|
|
use Composer\EventDispatcher; |
13
|
|
|
use Composer\Installer; |
14
|
|
|
|
15
|
|
|
class Plugin implements |
16
|
|
|
CPlugin\PluginInterface, |
17
|
|
|
EventDispatcher\EventSubscriberInterface |
18
|
|
|
{ |
19
|
|
|
/** @var IO\IOInterface */ |
20
|
|
|
private $io; |
21
|
|
|
|
22
|
|
|
/** @var Composer\Config */ |
23
|
|
|
private $config; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
private $package; |
27
|
|
|
private $cached = false; |
28
|
|
|
|
29
|
|
|
/** @var boolean */ |
30
|
|
|
private $disabled = false; |
31
|
|
|
|
32
|
|
|
private static $pluginClasses = array( |
33
|
|
|
'BaseRequest', |
34
|
|
|
'ConfigFacade', |
35
|
|
|
'CopyRequest', |
36
|
|
|
'CurlMulti', |
37
|
|
|
'CurlRemoteFilesystem', |
38
|
|
|
'FetchException', |
39
|
|
|
'FetchRequest', |
40
|
|
|
'FileDownloaderDummy', |
41
|
|
|
'ParallelizedComposerRepository', |
42
|
|
|
'Plugin', |
43
|
|
|
'Prefetcher', |
44
|
|
|
'Share', |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
private static $supportedSchemes = array( |
48
|
|
|
'http', |
49
|
|
|
'https' |
50
|
|
|
); |
51
|
|
|
|
52
|
3 |
|
public function activate(Composer $composer, IO\IOInterface $io) |
53
|
|
|
{ |
54
|
|
|
// @codeCoverageIgnoreStart |
55
|
|
|
// guard for self-update problem |
56
|
|
|
if (__CLASS__ !== 'Hirak\Prestissimo\Plugin') { |
57
|
|
|
return $this->disable(); |
58
|
|
|
} |
59
|
|
|
// guard for missing curl extension problem |
60
|
|
|
if (!extension_loaded('curl')) { |
61
|
|
|
$io->writeError('<error>Error: "curl" PHP extension not loaded; Prestissmo Composer plugin disabled.</error>'); |
62
|
|
|
return $this->disable(); |
63
|
|
|
} |
64
|
|
|
// @codeCoverageIgnoreEnd |
65
|
|
|
|
66
|
|
|
// load all classes |
67
|
3 |
|
foreach (self::$pluginClasses as $class) { |
68
|
3 |
|
class_exists(__NAMESPACE__ . '\\' . $class); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
$this->io = $io; |
72
|
3 |
|
$this->config = $composer->getConfig(); |
|
|
|
|
73
|
3 |
|
$this->package = $composer->getPackage(); |
|
|
|
|
74
|
|
|
|
75
|
3 |
|
$cacheDir = rtrim($this->config->get('cache-files-dir'), '\/'); |
76
|
3 |
|
|
77
|
|
|
// disable when cache is not usable |
78
|
|
|
if (preg_match('{(^|[\\\\/])(\$null|nul|NUL|/dev/null)([\\\\/]|$)}', $cacheDir)) { |
79
|
|
|
return $this->disable(); |
80
|
3 |
|
} |
81
|
3 |
|
|
82
|
3 |
|
if (array_key_exists('argv', $GLOBALS)) { |
83
|
3 |
|
if (in_array('help', $GLOBALS['argv'])) { |
84
|
3 |
|
return $this->disable(); |
85
|
3 |
|
} |
86
|
|
|
|
87
|
|
|
foreach ($GLOBALS['argv'] as $arg) { |
88
|
3 |
|
switch ($arg) { |
89
|
|
|
case 'create-project': |
90
|
|
|
case 'update': |
91
|
|
|
case 'outdated': |
92
|
|
|
case 'require': |
93
|
|
|
$this->prefetchComposerRepositories(); |
94
|
|
|
break 2; |
95
|
|
|
case 'install': |
96
|
3 |
|
if (file_exists('composer.json') && !file_exists('composer.lock')) { |
97
|
|
|
$this->prefetchComposerRepositories(); |
98
|
1 |
|
} |
99
|
|
|
break 2; |
100
|
|
|
} |
101
|
1 |
|
} |
102
|
1 |
|
} |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
public static function getSubscribedEvents() |
106
|
|
|
{ |
107
|
|
|
return array( |
108
|
|
|
CPlugin\PluginEvents::PRE_FILE_DOWNLOAD => 'onPreFileDownload', |
109
|
|
|
Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array( |
110
|
|
|
array('onPostDependenciesSolving', PHP_INT_MAX), |
111
|
|
|
), |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Keep-Alived file downloader |
117
|
|
|
*/ |
118
|
|
|
public function onPreFileDownload(CPlugin\PreFileDownloadEvent $ev) |
119
|
|
|
{ |
120
|
|
|
if ($this->disabled) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$scheme = parse_url($ev->getProcessedUrl(), PHP_URL_SCHEME); |
125
|
|
|
if (!in_array($scheme, self::$supportedSchemes, true)) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$rfs = $ev->getRemoteFilesystem(); |
130
|
|
|
$curlrfs = new CurlRemoteFilesystem( |
131
|
1 |
|
$this->io, |
132
|
|
|
$this->config, |
133
|
1 |
|
$rfs->getOptions() |
134
|
1 |
|
); |
135
|
|
|
$ev->setRemoteFilesystem($curlrfs); |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
public function prefetchComposerRepositories() |
139
|
1 |
|
{ |
140
|
1 |
|
if ($this->disabled) { |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
if ($this->cached) { |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
$repos = $this->package->getRepositories(); |
|
|
|
|
147
|
|
|
foreach ($repos as $label => $repo) { |
148
|
|
|
if (isset($repo['type']) && $repo['type'] === 'composer') { |
149
|
|
|
if (!empty($repo['force-lazy-providers'])) { |
150
|
|
|
continue; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (substr($repo['url'], 0, 6) !== 'https?') { |
154
|
|
|
$scheme = parse_url($repo['url'], PHP_URL_SCHEME); |
155
|
|
|
if (!in_array($scheme, self::$supportedSchemes, true)) { |
156
|
|
|
continue; |
157
|
1 |
|
} |
158
|
1 |
|
} |
159
|
|
|
|
160
|
|
|
$r = new ParallelizedComposerRepository($repo, $this->io, $this->config); |
161
|
|
|
$r->prefetch(); |
162
|
|
|
} |
163
|
1 |
|
} |
164
|
|
|
$this->cached = true; |
165
|
1 |
|
} |
166
|
1 |
|
|
167
|
|
|
/** |
168
|
1 |
|
* pre-fetch parallel by curl_multi |
169
|
1 |
|
*/ |
170
|
1 |
|
public function onPostDependenciesSolving(Installer\InstallerEvent $ev) |
171
|
1 |
|
{ |
172
|
1 |
|
if ($this->disabled) { |
173
|
|
|
return; |
174
|
1 |
|
} |
175
|
|
|
$prefetcher = new Prefetcher; |
176
|
2 |
|
$prefetcher->fetchAllFromOperations( |
177
|
|
|
$this->io, |
178
|
2 |
|
$this->config, |
179
|
2 |
|
$ev->getOperations() |
180
|
|
|
); |
181
|
1 |
|
} |
182
|
|
|
|
183
|
1 |
|
public function disable() |
184
|
|
|
{ |
185
|
|
|
$this->disabled = true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function isDisabled() |
189
|
|
|
{ |
190
|
|
|
return $this->disabled; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..