1 | <?php |
||
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 | if (array_key_exists('argv', $GLOBALS)) { |
|
76 | 3 | foreach ($GLOBALS['argv'] as $arg) { |
|
77 | switch ($arg) { |
||
78 | 3 | case 'create-project': |
|
79 | 3 | case 'update': |
|
80 | 3 | case 'outdated': |
|
81 | 3 | case 'require': |
|
82 | $this->prefetchComposerRepositories(); |
||
83 | break 2; |
||
84 | 3 | case 'install': |
|
85 | if (file_exists('composer.json') && !file_exists('composer.lock')) { |
||
86 | $this->prefetchComposerRepositories(); |
||
87 | } |
||
88 | 3 | break 2; |
|
89 | } |
||
90 | } |
||
91 | } |
||
92 | 3 | } |
|
93 | |||
94 | 1 | public static function getSubscribedEvents() |
|
95 | { |
||
96 | return array( |
||
97 | 1 | CPlugin\PluginEvents::PRE_FILE_DOWNLOAD => 'onPreFileDownload', |
|
98 | 1 | Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array( |
|
99 | 1 | array('onPostDependenciesSolving', PHP_INT_MAX), |
|
100 | ), |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Keep-Alived file downloader |
||
106 | */ |
||
107 | public function onPreFileDownload(CPlugin\PreFileDownloadEvent $ev) |
||
108 | { |
||
109 | if ($this->disabled) { |
||
110 | return; |
||
111 | } |
||
112 | |||
113 | $scheme = parse_url($ev->getProcessedUrl(), PHP_URL_SCHEME); |
||
114 | if (!in_array($scheme, self::$supportedSchemes, true)) { |
||
115 | return; |
||
116 | } |
||
117 | |||
118 | $rfs = $ev->getRemoteFilesystem(); |
||
119 | $curlrfs = new CurlRemoteFilesystem( |
||
120 | $this->io, |
||
121 | $this->config, |
||
122 | $rfs->getOptions() |
||
123 | ); |
||
124 | $ev->setRemoteFilesystem($curlrfs); |
||
125 | } |
||
126 | |||
127 | 1 | public function prefetchComposerRepositories() |
|
128 | { |
||
129 | 1 | if ($this->disabled) { |
|
130 | 1 | return; |
|
131 | } |
||
132 | 1 | if ($this->cached) { |
|
133 | return; |
||
134 | } |
||
135 | 1 | $repos = $this->package->getRepositories(); |
|
136 | 1 | foreach ($repos as $label => $repo) { |
|
137 | if (isset($repo['type']) && $repo['type'] === 'composer') { |
||
138 | if (!empty($repo['force-lazy-providers'])) { |
||
139 | continue; |
||
140 | } |
||
141 | |||
142 | if (substr($repo['url'], 0, 6) !== 'https?') { |
||
143 | $scheme = parse_url($repo['url'], PHP_URL_SCHEME); |
||
144 | if (!in_array($scheme, self::$supportedSchemes, true)) { |
||
145 | continue; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | $r = new ParallelizedComposerRepository($repo, $this->io, $this->config); |
||
150 | $r->prefetch(); |
||
151 | } |
||
152 | } |
||
153 | 1 | $this->cached = true; |
|
154 | 1 | } |
|
155 | |||
156 | /** |
||
157 | * pre-fetch parallel by curl_multi |
||
158 | */ |
||
159 | 1 | public function onPostDependenciesSolving(Installer\InstallerEvent $ev) |
|
171 | |||
172 | 2 | public function disable() |
|
176 | |||
177 | 1 | public function isDisabled() |
|
181 | } |
||
182 |
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: