|
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\Package; |
|
14
|
|
|
use Composer\Script; |
|
15
|
|
|
use Composer\Installer; |
|
16
|
|
|
use Composer\DependencyResolver; |
|
17
|
|
|
|
|
18
|
|
|
class Plugin implements |
|
19
|
|
|
CPlugin\PluginInterface, |
|
20
|
|
|
EventDispatcher\EventSubscriberInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var IO\IOInterface */ |
|
23
|
|
|
private $io; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Composer\Config */ |
|
26
|
|
|
private $config; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array */ |
|
29
|
|
|
private $package; |
|
30
|
|
|
private $cached = false; |
|
31
|
|
|
|
|
32
|
|
|
/** @var boolean */ |
|
33
|
|
|
private $disabled = false; |
|
34
|
|
|
|
|
35
|
|
|
private static $pluginClasses = array( |
|
36
|
|
|
'BaseRequest', |
|
37
|
|
|
'ConfigFacade', |
|
38
|
|
|
'CopyRequest', |
|
39
|
|
|
'CurlMulti', |
|
40
|
|
|
'CurlRemoteFilesystem', |
|
41
|
|
|
'FetchException', |
|
42
|
|
|
'FetchRequest', |
|
43
|
|
|
'FileDownloaderDummy', |
|
44
|
|
|
'ParallelizedComposerRepository', |
|
45
|
|
|
'Plugin', |
|
46
|
|
|
'Prefetcher', |
|
47
|
|
|
'Share', |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
3 |
|
public function activate(Composer $composer, IO\IOInterface $io) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
// @codeCoverageIgnoreStart |
|
53
|
|
|
// guard for self-update problem |
|
54
|
|
|
if (__CLASS__ !== 'Hirak\Prestissimo\Plugin') { |
|
55
|
|
|
return $this->disable(); |
|
56
|
|
|
} |
|
57
|
|
|
// @codeCoverageIgnoreEnd |
|
58
|
|
|
|
|
59
|
|
|
// load all classes |
|
60
|
3 |
|
foreach (self::$pluginClasses as $class) { |
|
61
|
3 |
|
class_exists(__NAMESPACE__ . '\\' . $class); |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
$this->io = $io; |
|
65
|
3 |
|
$this->config = $composer->getConfig(); |
|
|
|
|
|
|
66
|
3 |
|
$this->package = $composer->getPackage(); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
3 |
|
foreach ($GLOBALS['argv'] as $arg) { |
|
69
|
|
|
switch ($arg) { |
|
70
|
3 |
|
case 'create-project': |
|
71
|
3 |
|
case 'update': |
|
72
|
3 |
|
case 'outdated': |
|
73
|
3 |
|
case 'require': |
|
74
|
|
|
$this->prefetchComposerRepositories(); |
|
75
|
|
|
break 2; |
|
76
|
3 |
|
case 'install': |
|
77
|
|
|
if (file_exists('composer.json') && !file_exists('composer.lock')) { |
|
78
|
|
|
$this->prefetchComposerRepositories(); |
|
79
|
|
|
} |
|
80
|
|
|
break 2; |
|
81
|
|
|
} |
|
82
|
3 |
|
} |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public static function getSubscribedEvents() |
|
86
|
|
|
{ |
|
87
|
|
|
return array( |
|
88
|
1 |
|
CPlugin\PluginEvents::PRE_FILE_DOWNLOAD => 'onPreFileDownload', |
|
89
|
|
|
// Script\ScriptEvents::POST_ROOT_PACKAGE_INSTALL => 'prefetchComposerRepositories', |
|
|
|
|
|
|
90
|
|
|
// Installer\InstallerEvents::PRE_DEPENDENCIES_SOLVING => 'prefetchComposerRepositories', |
|
91
|
1 |
|
Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array( |
|
92
|
1 |
|
array('onPostDependenciesSolving', PHP_INT_MAX), |
|
93
|
1 |
|
), |
|
94
|
1 |
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Keep-Alived file downloader |
|
99
|
|
|
*/ |
|
100
|
|
|
public function onPreFileDownload(CPlugin\PreFileDownloadEvent $ev) |
|
101
|
|
|
{ |
|
102
|
|
|
if ($this->disabled) { |
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
$rfs = $ev->getRemoteFilesystem(); |
|
106
|
|
|
$curlrfs = new CurlRemoteFilesystem( |
|
107
|
|
|
$this->io, |
|
108
|
|
|
$this->config, |
|
109
|
|
|
$rfs->getOptions() |
|
110
|
|
|
); |
|
111
|
|
|
$ev->setRemoteFilesystem($curlrfs); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
public function prefetchComposerRepositories() |
|
115
|
|
|
{ |
|
116
|
1 |
|
if ($this->disabled) { |
|
117
|
1 |
|
return; |
|
118
|
|
|
} |
|
119
|
1 |
|
if ($this->cached) { |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
1 |
|
$repos = $this->package->getRepositories(); |
|
|
|
|
|
|
123
|
1 |
|
foreach ($repos as $label => $repo) { |
|
124
|
|
|
if (isset($repo['type']) && $repo['type'] === 'composer') { |
|
125
|
|
|
if ('http?://' === substr($repo['url'], 0, 8) && isset($repo['allow_ssl_downgrade']) && $repo['allow_ssl_downgrade']) { |
|
126
|
|
|
$repo = array( |
|
127
|
|
|
'type' => $repo['type'], |
|
128
|
1 |
|
'url' => str_replace('https?://', 'https://', $repo['url']), |
|
129
|
1 |
|
); |
|
130
|
1 |
|
} |
|
131
|
|
|
$r = new ParallelizedComposerRepository($repo, $this->io, $this->config); |
|
132
|
|
|
$r->prefetch(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
1 |
|
$this->cached = true; |
|
136
|
|
|
} |
|
137
|
1 |
|
|
|
138
|
1 |
|
/** |
|
139
|
|
|
* pre-fetch parallel by curl_multi |
|
140
|
1 |
|
*/ |
|
141
|
1 |
|
public function onPostDependenciesSolving(Installer\InstallerEvent $ev) |
|
142
|
1 |
|
{ |
|
143
|
1 |
|
if ($this->disabled) { |
|
144
|
1 |
|
return; |
|
145
|
1 |
|
} |
|
146
|
1 |
|
$prefetcher = new Prefetcher; |
|
147
|
|
|
$prefetcher->fetchAllFromOperations( |
|
148
|
2 |
|
$this->io, |
|
149
|
|
|
$this->config, |
|
150
|
2 |
|
$ev->getOperations() |
|
151
|
2 |
|
); |
|
152
|
|
|
} |
|
153
|
1 |
|
|
|
154
|
|
|
public function disable() |
|
155
|
1 |
|
{ |
|
156
|
|
|
$this->disabled = true; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function isDisabled() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->disabled; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: