These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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) |
|
0 ignored issues
–
show
|
|||
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 | $r = new ParallelizedComposerRepository($repo, $this->io, $this->config); |
||
126 | $r->prefetch(); |
||
127 | } |
||
128 | 1 | } |
|
129 | 1 | $this->cached = true; |
|
130 | 1 | } |
|
131 | |||
132 | /** |
||
133 | * pre-fetch parallel by curl_multi |
||
134 | */ |
||
135 | 1 | public function onPostDependenciesSolving(Installer\InstallerEvent $ev) |
|
136 | { |
||
137 | 1 | if ($this->disabled) { |
|
138 | 1 | return; |
|
139 | } |
||
140 | 1 | $prefetcher = new Prefetcher; |
|
141 | 1 | $prefetcher->fetchAllFromOperations( |
|
142 | 1 | $this->io, |
|
143 | 1 | $this->config, |
|
144 | 1 | $ev->getOperations() |
|
145 | 1 | ); |
|
146 | 1 | } |
|
147 | |||
148 | 2 | public function disable() |
|
149 | { |
||
150 | 2 | $this->disabled = true; |
|
151 | 2 | } |
|
152 | |||
153 | 1 | public function isDisabled() |
|
154 | { |
||
155 | 1 | return $this->disabled; |
|
156 | } |
||
157 | } |
||
158 |
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: