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 | 3 | public function activate(Composer $composer, IO\IOInterface $io) |
|
|
|||
48 | { |
||
49 | // @codeCoverageIgnoreStart |
||
50 | // guard for self-update problem |
||
51 | if (__CLASS__ !== 'Hirak\Prestissimo\Plugin') { |
||
52 | return $this->disable(); |
||
53 | } |
||
54 | // @codeCoverageIgnoreEnd |
||
55 | |||
56 | // load all classes |
||
57 | 3 | foreach (self::$pluginClasses as $class) { |
|
58 | 3 | class_exists(__NAMESPACE__ . '\\' . $class); |
|
59 | } |
||
60 | |||
61 | 3 | $this->io = $io; |
|
62 | 3 | $this->config = $composer->getConfig(); |
|
63 | 3 | $this->package = $composer->getPackage(); |
|
64 | |||
65 | 3 | foreach ($GLOBALS['argv'] as $arg) { |
|
66 | switch ($arg) { |
||
67 | 3 | case 'create-project': |
|
68 | 3 | case 'update': |
|
69 | 3 | case 'outdated': |
|
70 | 3 | case 'require': |
|
71 | $this->prefetchComposerRepositories(); |
||
72 | break 2; |
||
73 | 3 | case 'install': |
|
74 | if (file_exists('composer.json') && !file_exists('composer.lock')) { |
||
75 | $this->prefetchComposerRepositories(); |
||
76 | } |
||
77 | 3 | break 2; |
|
78 | } |
||
79 | } |
||
80 | 3 | } |
|
81 | |||
82 | 1 | public static function getSubscribedEvents() |
|
83 | { |
||
84 | return array( |
||
85 | 1 | CPlugin\PluginEvents::PRE_FILE_DOWNLOAD => 'onPreFileDownload', |
|
86 | 1 | Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array( |
|
87 | 1 | array('onPostDependenciesSolving', PHP_INT_MAX), |
|
88 | ), |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Keep-Alived file downloader |
||
94 | */ |
||
95 | public function onPreFileDownload(CPlugin\PreFileDownloadEvent $ev) |
||
96 | { |
||
97 | if ($this->disabled) { |
||
98 | return; |
||
99 | } |
||
100 | $rfs = $ev->getRemoteFilesystem(); |
||
101 | $curlrfs = new CurlRemoteFilesystem( |
||
102 | $this->io, |
||
103 | $this->config, |
||
104 | $rfs->getOptions() |
||
105 | ); |
||
106 | $ev->setRemoteFilesystem($curlrfs); |
||
107 | } |
||
108 | |||
109 | 1 | public function prefetchComposerRepositories() |
|
110 | { |
||
111 | 1 | if ($this->disabled) { |
|
112 | 1 | return; |
|
113 | } |
||
114 | 1 | if ($this->cached) { |
|
115 | return; |
||
116 | } |
||
117 | 1 | $repos = $this->package->getRepositories(); |
|
118 | 1 | foreach ($repos as $label => $repo) { |
|
119 | if (isset($repo['type']) && $repo['type'] === 'composer') { |
||
120 | if ('http?://' === substr($repo['url'], 0, 8) && isset($repo['allow_ssl_downgrade']) && $repo['allow_ssl_downgrade']) { |
||
121 | $repo = array( |
||
122 | 'type' => $repo['type'], |
||
123 | 'url' => str_replace('https?://', 'https://', $repo['url']), |
||
124 | ); |
||
125 | } |
||
126 | $r = new ParallelizedComposerRepository($repo, $this->io, $this->config); |
||
127 | $r->prefetch(); |
||
128 | } |
||
129 | } |
||
130 | 1 | $this->cached = true; |
|
131 | 1 | } |
|
132 | |||
133 | /** |
||
134 | * pre-fetch parallel by curl_multi |
||
135 | */ |
||
136 | 1 | public function onPostDependenciesSolving(Installer\InstallerEvent $ev) |
|
137 | { |
||
138 | 1 | if ($this->disabled) { |
|
139 | 1 | return; |
|
140 | } |
||
141 | 1 | $prefetcher = new Prefetcher; |
|
142 | 1 | $prefetcher->fetchAllFromOperations( |
|
143 | 1 | $this->io, |
|
144 | 1 | $this->config, |
|
145 | 1 | $ev->getOperations() |
|
146 | ); |
||
147 | 1 | } |
|
148 | |||
149 | 2 | public function disable() |
|
153 | |||
154 | 1 | public function isDisabled() |
|
158 | } |
||
159 |
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: