Total Complexity | 43 |
Total Lines | 247 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 1 | Features | 1 |
Complex classes like DependencyLib often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DependencyLib, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class DependencyLib |
||
47 | { |
||
48 | use FileOps; |
||
49 | |||
50 | public const DLL_MAP_URL = 'https://windows.php.net/downloads/pecl/deps/dllmapping.json'; |
||
51 | |||
52 | public const DEPLISTER_URL = 'https://windows.php.net/downloads/pecl/tools/deplister.exe'; |
||
53 | |||
54 | public const DEPS_URL = 'https://windows.php.net/downloads/pecl/deps'; |
||
55 | |||
56 | private $dllMap; |
||
57 | |||
58 | private $php; |
||
59 | |||
60 | private $progress; |
||
61 | |||
62 | private $output; |
||
63 | |||
64 | private $fetchedZips = []; |
||
65 | |||
66 | public function __construct(\Pickle\Base\Interfaces\Engine $php) |
||
67 | { |
||
68 | $this->php = $php; |
||
69 | $this->checkDepListerExe(); |
||
70 | $this->fetchDllMap(); |
||
71 | } |
||
72 | |||
73 | public function getZipUrlsForDll($binary, $ignore_installed = false) |
||
74 | { |
||
75 | $dll = $this->getDllsForBinary($binary); |
||
76 | $packages = []; |
||
77 | foreach ($this->dllMap as $pkg_name => $pkg) { |
||
78 | foreach ($dll as $dll_name => $dll_installed) { |
||
79 | if (in_array($dll_name, $pkg)) { |
||
80 | if ($ignore_installed && $dll_installed) { |
||
81 | continue; |
||
82 | } |
||
83 | $packages[] = $pkg_name; |
||
84 | continue 2; |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | return $packages; |
||
90 | } |
||
91 | |||
92 | public function resolveForBin($dll, $resolve_multiple_cb = null) |
||
93 | { |
||
94 | $dep_zips = $this->getZipUrlsForDll($dll, false); |
||
95 | |||
96 | if (count($dep_zips) == 1) { |
||
97 | $dep_zip = $dep_zips[0]; |
||
98 | |||
99 | if (in_array($dep_zip, $this->fetchedZips)) { |
||
100 | return true; |
||
101 | } |
||
102 | } elseif (count($dep_zips) > 1) { |
||
103 | foreach ($dep_zips as $dep_zip) { |
||
104 | /* The user has already picked one here, ignore it. */ |
||
105 | if (in_array($dep_zip, $this->fetchedZips)) { |
||
106 | return true; |
||
107 | } |
||
108 | } |
||
109 | if ($resolve_multiple_cb !== null) { |
||
110 | $dep_zip = $resolve_multiple_cb($dep_zips); |
||
111 | } else { |
||
112 | throw new Exception("Multiple choice for dependencies, couldn't resolve"); |
||
113 | } |
||
114 | } else { |
||
115 | /* That might be not quite true, as we might just not have the |
||
116 | corresponding dependency package. However it's fetched from |
||
117 | the PECL build dependencies, no extension build should have |
||
118 | been exist if there's no dependency package uploaded. */ |
||
119 | return true; |
||
120 | } |
||
121 | |||
122 | return $this->resolveForZip($dep_zip, $resolve_multiple_cb); |
||
123 | } |
||
124 | |||
125 | public function resolveForZip($zip_name, $resolve_multiple_cb = null) |
||
126 | { |
||
127 | if (in_array($zip_name, $this->fetchedZips)) { |
||
128 | return true; |
||
129 | } |
||
130 | |||
131 | $url = self::DEPS_URL . "/{$zip_name}"; |
||
132 | $path = $this->download($url); |
||
133 | try { |
||
134 | $this->uncompress($path); |
||
135 | $lst = $this->copyFiles(); |
||
136 | } catch (Exception $e) { |
||
137 | $this->cleanup(); |
||
138 | throw new Exception($e->getMessage()); |
||
139 | } |
||
140 | $this->cleanup(); |
||
141 | $this->fetchedZips[] = $zip_name; |
||
142 | |||
143 | foreach ($lst as $bin) { |
||
144 | $this->resolveForBin($bin, $resolve_multiple_cb); |
||
145 | } |
||
146 | |||
147 | return true; |
||
148 | } |
||
149 | |||
150 | public function setProgress($progress) |
||
153 | } |
||
154 | |||
155 | public function setOutput(OutputInterface $output) |
||
156 | { |
||
157 | $this->output = $output; |
||
158 | } |
||
159 | |||
160 | private function fetchDllMap() |
||
161 | { |
||
162 | $dllMap = null; |
||
163 | |||
164 | if ($this->dllMap === null) { |
||
165 | $opts = [ |
||
166 | 'http' => [ |
||
167 | 'header' => 'User-Agent: pickle', |
||
168 | ], |
||
169 | ]; |
||
170 | $context = stream_context_create($opts); |
||
171 | $data = @file_get_contents(self::DLL_MAP_URL, false, $context); |
||
172 | if (!$data) { |
||
173 | throw new RuntimeException('Cannot fetch the DLL mapping file'); |
||
174 | } |
||
175 | $dllMap = json_decode($data); |
||
176 | if (!$dllMap) { |
||
177 | throw new RuntimeException('Cannot parse the DLL mapping file'); |
||
178 | } |
||
179 | } |
||
180 | $compiler = $this->php->getCompiler(); |
||
181 | $architecture = $this->php->getArchitecture(); |
||
182 | if (!isset($dllMap->{$compiler}->{$architecture})) { |
||
183 | /* Just for the case the given compiler/arch set isn't defined in the dllmap, |
||
184 | or we've got a corrupted file, or ... |
||
185 | The dllMap property should be ensured an array. */ |
||
186 | $this->dllMap = []; |
||
187 | } else { |
||
188 | $this->dllMap = $dllMap->{$compiler}->{$architecture}; |
||
189 | } |
||
190 | |||
191 | return true; |
||
192 | } |
||
193 | |||
194 | private function checkDepListerExe() |
||
195 | { |
||
196 | $ret = exec('deplister.exe ' . $this->php->getPath() . ' .'); |
||
197 | if (empty($ret)) { |
||
198 | $depexe = @file_get_contents(self::DEPLISTER_URL); |
||
199 | if (!$depexe) { |
||
200 | throw new RuntimeException('Cannot fetch deplister.exe'); |
||
201 | } |
||
202 | $dir = dirname($this->php->getPath()); |
||
203 | $path = $dir . DIRECTORY_SEPARATOR . 'deplister.exe'; |
||
204 | if (!@file_put_contents($path, $depexe)) { |
||
205 | throw new RuntimeException('Cannot copy deplister.exe to ' . $dir); |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | private function getDllsForBinary($binary) |
||
226 | } |
||
227 | |||
228 | private function copyFiles() |
||
229 | { |
||
230 | $ret = []; |
||
231 | $DLLs = glob($this->tempDir . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . '*.dll'); |
||
232 | |||
233 | /* Copying ALL files from the zip, not just required. */ |
||
234 | foreach ($DLLs as $dll) { |
||
235 | $dll = realpath($dll); |
||
236 | $basename = basename($dll); |
||
237 | $dest = dirname($this->php->getPath()) . DIRECTORY_SEPARATOR . $basename; |
||
238 | $success = @copy($dll, dirname($this->php->getPath()) . '/' . $basename); |
||
239 | if (!$success) { |
||
240 | throw new Exception('Cannot copy DLL <' . $dll . '> to <' . $dest . '>'); |
||
241 | } |
||
242 | |||
243 | $ret[] = $dest; |
||
244 | } |
||
245 | |||
246 | return $ret; |
||
247 | } |
||
248 | |||
249 | private function download($url) |
||
250 | { |
||
251 | $output = $this->output; |
||
252 | $progress = $this->progress; |
||
253 | |||
254 | $ctx = stream_context_create( |
||
255 | [], |
||
256 | [ |
||
257 | 'notification' => function ($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) use ($output, $progress) { |
||
258 | switch ($notificationCode) { |
||
259 | case STREAM_NOTIFY_FILE_SIZE_IS: |
||
260 | $progress->start($output, $bytesMax); |
||
261 | break; |
||
262 | case STREAM_NOTIFY_PROGRESS: |
||
263 | $progress->setCurrent($bytesTransferred); |
||
264 | break; |
||
265 | } |
||
266 | }, |
||
267 | ] |
||
268 | ); |
||
269 | $output->writeln("downloading {$url} "); |
||
270 | $fileContents = file_get_contents($url, false, $ctx); |
||
271 | $progress->finish(); |
||
272 | if (!$fileContents) { |
||
273 | throw new Exception('Cannot fetch <' . $url . '>'); |
||
274 | } |
||
275 | $tmpdir = Util\TmpDir::get(); |
||
276 | $path = $tmpdir . DIRECTORY_SEPARATOR . basename($url); |
||
277 | if (!file_put_contents($path, $fileContents)) { |
||
278 | throw new Exception('Cannot save temporary file <' . $path . '>'); |
||
279 | } |
||
280 | |||
281 | return $path; |
||
282 | } |
||
283 | |||
284 | private function uncompress($zipFile) |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ |
||
297 |