Total Complexity | 48 |
Total Lines | 296 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 0 |
Complex classes like Binary 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 Binary, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class Binary |
||
49 | { |
||
50 | use FileOps; |
||
51 | |||
52 | private $php; |
||
53 | |||
54 | private $extName; |
||
55 | |||
56 | private $extVersion; |
||
57 | |||
58 | private $progress; |
||
59 | |||
60 | private $output; |
||
61 | |||
62 | private $extDll; |
||
63 | |||
64 | /** |
||
65 | * @param string $ext |
||
66 | */ |
||
67 | public function __construct($ext) |
||
68 | { |
||
69 | // used only if only the extension name is given |
||
70 | if (strpos('//', $ext) !== false) { |
||
71 | $this->extensionPeclExists(); |
||
72 | } |
||
73 | |||
74 | $this->extName = $ext; |
||
75 | $this->php = Engine::factory(); |
||
76 | } |
||
77 | |||
78 | public function setProgress($progress) |
||
79 | { |
||
80 | $this->progress = $progress; |
||
81 | } |
||
82 | |||
83 | public function setOutput(OutputInterface $output) |
||
84 | { |
||
85 | $this->output = $output; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * 1. check if ext exists |
||
90 | * 2. check if given version requested |
||
91 | * 2.1 yes? check if builds available |
||
92 | * 2.2 no? get latest version+build. |
||
93 | * |
||
94 | * @throws Exception |
||
95 | */ |
||
96 | public function install() |
||
97 | { |
||
98 | [$this->extName, $this->extVersion] = $this->getInfoFromPecl(); |
||
99 | $url = $this->fetchZipName(); |
||
100 | $pathArchive = $this->download($url); |
||
101 | $this->uncompress($pathArchive); |
||
102 | $this->copyFiles(); |
||
103 | $this->cleanup(); |
||
104 | $this->updateIni(); |
||
105 | } |
||
106 | |||
107 | public function getExtDllPaths() |
||
108 | { |
||
109 | $ret = []; |
||
110 | |||
111 | foreach ($this->extDll as $dll) { |
||
112 | $ret[] = $this->php->getExtensionDir() . DIRECTORY_SEPARATOR . $dll; |
||
113 | } |
||
114 | |||
115 | return $ret; |
||
116 | } |
||
117 | |||
118 | private function extensionPeclExists() |
||
119 | { |
||
120 | $url = WebsiteFactory::getWebsite()->getBaseUrl() . '/get/' . $this->extName; |
||
121 | $headers = get_headers($url, 1); |
||
122 | $status = $headers[0]; |
||
123 | if (strpos($status, '404')) { |
||
124 | throw new Exception("Extension <{$this->extName}> cannot be found"); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param string $url |
||
130 | */ |
||
131 | private function findInLinks($url, $toFind) |
||
132 | { |
||
133 | $page = @file_get_contents($url); |
||
|
|||
134 | $opts = [ |
||
135 | 'http' => [ |
||
136 | 'header' => 'User-Agent: pickle', |
||
137 | ], |
||
138 | ]; |
||
139 | $context = stream_context_create($opts); |
||
140 | $page = @file_get_contents($url, false, $context); |
||
141 | if (!$page) { |
||
142 | return false; |
||
143 | } |
||
144 | $dom = new DOMDocument(); |
||
145 | $dom->loadHTML($page); |
||
146 | $links = $dom->getElementsByTagName('a'); |
||
147 | if (!$links) { |
||
148 | return false; |
||
149 | } |
||
150 | |||
151 | foreach ($links as $link) { |
||
152 | if ($link->nodeValue[0] == '[') { |
||
153 | continue; |
||
154 | } |
||
155 | $value = trim($link->nodeValue); |
||
156 | if ($toFind == $value) { |
||
157 | return $value; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return false; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @throws Exception |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | private function fetchZipName() |
||
170 | { |
||
171 | $phpVc = $this->php->getCompiler(); |
||
172 | $phpArch = $this->php->getArchitecture(); |
||
173 | $phpZts = $this->php->getZts() ? '-ts' : '-nts'; |
||
174 | $phpVersion = $this->php->getMajorVersion() . '.' . $this->php->getMinorVersion(); |
||
175 | $pkgVersion = $this->extVersion; |
||
176 | $extName = strtolower($this->extName); |
||
177 | $baseUrl = 'https://windows.php.net/downloads/pecl/releases/'; |
||
178 | |||
179 | if ($this->findInLinks($baseUrl . $extName, $pkgVersion) === false) { |
||
180 | throw new Exception('Binary for <' . $extName . '-' . $pkgVersion . '> cannot be found'); |
||
181 | } |
||
182 | |||
183 | $fileToFind = 'php_' . $extName . '-' . $pkgVersion . '-' . $phpVersion . $phpZts . '-' . $phpVc . '-' . $phpArch . '.zip'; |
||
184 | $fileUrl = $this->findInLinks($baseUrl . $extName . '/' . $pkgVersion, $fileToFind); |
||
185 | |||
186 | if (!$fileUrl) { |
||
187 | throw new Exception('Binary for <' . $fileToFind . '> cannot be found'); |
||
188 | } |
||
189 | return $baseUrl . $extName . '/' . $pkgVersion . '/' . $fileToFind; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param string $zipFile |
||
194 | * |
||
195 | * @throws Exception |
||
196 | */ |
||
197 | private function uncompress($zipFile) |
||
198 | { |
||
199 | $this->createTempDir($this->extName); |
||
200 | $this->cleanup(); |
||
201 | $zipClass = Archive\Factory::getUnzipperClassName(); |
||
202 | $zipArchive = $zipClass($zipFile); |
||
203 | /** @var \Pickle\Base\Interfaces\Archive\Unzipper $zipArchive */ |
||
204 | $this->output->writeln('Extracting archives...'); |
||
205 | $zipArchive->extractTo($this->tempDir); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param string $url |
||
210 | * |
||
211 | * @throws Exception |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | private function download($url) |
||
216 | { |
||
217 | $progress = $this->progress; |
||
218 | $progress->setOverwrite(true); |
||
219 | $ctx = stream_context_create( |
||
220 | [ |
||
221 | 'http' => [ |
||
222 | 'header' => 'User-Agent: pickle', |
||
223 | ], |
||
224 | ], |
||
225 | [ |
||
226 | 'notification' => function ($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) use ($progress) { |
||
227 | switch ($notificationCode) { |
||
228 | case STREAM_NOTIFY_RESOLVE: |
||
229 | case STREAM_NOTIFY_AUTH_REQUIRED: |
||
230 | case STREAM_NOTIFY_COMPLETED: |
||
231 | case STREAM_NOTIFY_FAILURE: |
||
232 | case STREAM_NOTIFY_AUTH_RESULT: |
||
233 | break; |
||
234 | |||
235 | case STREAM_NOTIFY_REDIRECTED: |
||
236 | break; |
||
237 | |||
238 | case STREAM_NOTIFY_CONNECT: |
||
239 | break; |
||
240 | |||
241 | case STREAM_NOTIFY_FILE_SIZE_IS: |
||
242 | $progress->start($bytesMax); |
||
243 | break; |
||
244 | |||
245 | case STREAM_NOTIFY_MIME_TYPE_IS: |
||
246 | break; |
||
247 | |||
248 | case STREAM_NOTIFY_PROGRESS: |
||
249 | $progress->setProgress($bytesTransferred); |
||
250 | break; |
||
251 | } |
||
252 | }, |
||
253 | ] |
||
254 | ); |
||
255 | if ($this->output) { |
||
256 | $this->output->writeln("downloading {$url} "); |
||
257 | } |
||
258 | $fileContents = file_get_contents($url, false, $ctx); |
||
259 | $progress->finish(); |
||
260 | if (!$fileContents) { |
||
261 | throw new Exception('Cannot fetch <' . $url . '>'); |
||
262 | } |
||
263 | $tmpdir = Util\TmpDir::get(); |
||
264 | $path = $tmpdir . '/' . $this->extName . '.zip'; |
||
265 | if (!file_put_contents($path, $fileContents)) { |
||
266 | throw new Exception('Cannot save temporary file <' . $path . '>'); |
||
267 | } |
||
268 | |||
269 | return $path; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @throws Exception |
||
274 | */ |
||
275 | private function copyFiles() |
||
276 | { |
||
277 | $DLLs = glob($this->tempDir . '/*.dll'); |
||
278 | $this->extDll = []; |
||
279 | foreach ($DLLs as $dll) { |
||
280 | $dll = realpath($dll); |
||
281 | $basename = basename($dll); |
||
282 | $dest = $this->php->getExtensionDir() . DIRECTORY_SEPARATOR . $basename; |
||
283 | if (substr($basename, 0, 4) == 'php_') { |
||
284 | $this->extDll[] = $basename; |
||
285 | $this->output->writeln("copying {$dll} to " . $dest . "\n"); |
||
286 | $success = copy($dll, $this->php->getExtensionDir() . '/' . $basename); |
||
287 | if (!$success) { |
||
288 | throw new Exception('Cannot copy DLL <' . $dll . '> to <' . $dest . '>'); |
||
289 | } |
||
290 | } else { |
||
291 | $success = copy($dll, dirname($this->php->getPath()) . '/' . $basename); |
||
292 | if (!$success) { |
||
293 | throw new Exception('Cannot copy DLL <' . $dll . '> to <' . $dest . '>'); |
||
294 | } |
||
295 | } |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @throws Exception |
||
301 | */ |
||
302 | private function updateIni() |
||
303 | { |
||
304 | $ini = \Pickle\Engine\Ini::factory($this->php); |
||
305 | $ini->updatePickleSection($this->extDll); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @throws Exception |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | private function getInfoFromPecl() |
||
344 | } |
||
345 | } |
||
346 | |||
347 | /* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ |
||
348 |