|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + * @contact group@hyperf.io |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | +namespace App\DownloadHandler; |
| 13 | + |
| 14 | +use App\Exception\BoxException; |
| 15 | +use App\Exception\NotSupportVersionsException; |
| 16 | +use Phar; |
| 17 | +use SplFileInfo; |
| 18 | + |
| 19 | +class PintHandler extends AbstractDownloadHandler |
| 20 | +{ |
| 21 | + public function handle(string $pkgName, string $version, array $options = []): ?SplFileInfo |
| 22 | + { |
| 23 | + $definition = $this->getDefinition($pkgName); |
| 24 | + if (! $definition) { |
| 25 | + throw new \RuntimeException('The package not found'); |
| 26 | + } |
| 27 | + if ($definition->getRepo()) { |
| 28 | + $url = $this->fetchDownloadUrlFromGithubRelease($definition->getBin(), $definition->getRepo(), $version); |
| 29 | + } elseif ($definition->getUrl()) { |
| 30 | + if ($version === 'latest') { |
| 31 | + if ($definition->getLatest() && $definition->getLatest() !== 'latest') { |
| 32 | + $specifiedVersion = $definition->getLatest(); |
| 33 | + } else { |
| 34 | + $versions = $this->versions($pkgName); |
| 35 | + $specifiedVersion = array_shift($versions); |
| 36 | + } |
| 37 | + } else { |
| 38 | + $specifiedVersion = $version; |
| 39 | + } |
| 40 | + $url = str_replace('${{version}}', $specifiedVersion, $definition->getUrl()); |
| 41 | + } else { |
| 42 | + throw new \RuntimeException('The definition of package is invalid'); |
| 43 | + } |
| 44 | + |
| 45 | + $savePath = Phar::running(false) ?: $this->runtimePath . '/'; |
| 46 | + $file = $this->download($url, $savePath, 0755); |
| 47 | + if (! file_exists($savePath)) { |
| 48 | + throw new \RuntimeException('Download failed, cannot locate the file in local.'); |
| 49 | + } |
| 50 | + |
| 51 | + // Is file name ends with .zip? |
| 52 | + if (str_ends_with($file->getFilename(), '.zip')) { |
| 53 | + $zip = new \ZipArchive(); |
| 54 | + $zip->open($file->getRealPath()); |
| 55 | + $this->logger->info('Unpacking zip file ' . $savePath); |
| 56 | + for ($i = 0; $i < $zip->numFiles; ++$i) { |
| 57 | + $filename = $zip->getNameIndex($i); |
| 58 | + if (str_ends_with($filename, 'builds/pint')) { |
| 59 | + copy('zip://' . $file->getRealPath() . '#' . $filename, $savePath . $definition->getBin()); |
| 60 | + } |
| 61 | + } |
| 62 | + $zip->close(); |
| 63 | + $this->logger->info('Unpacked zip file ' . $savePath); |
| 64 | + unlink($file->getRealPath()); |
| 65 | + } |
| 66 | + |
| 67 | + $licenseFile = $savePath . 'LICENSE'; |
| 68 | + // If license file exists, delete it. |
| 69 | + |
| 70 | + if (file_exists($licenseFile)) { |
| 71 | + unlink($licenseFile); |
| 72 | + } |
| 73 | + |
| 74 | + return new SplFileInfo($savePath . $definition->getBin()); |
| 75 | + } |
| 76 | + |
| 77 | + public function versions(string $pkgName, array $options = []): array |
| 78 | + { |
| 79 | + $definition = $this->getDefinition($pkgName); |
| 80 | + if (! $definition) { |
| 81 | + throw new \RuntimeException('The package not found'); |
| 82 | + } |
| 83 | + if (! $definition->getRepo() && ! $definition->getComposerName()) { |
| 84 | + throw new NotSupportVersionsException($pkgName); |
| 85 | + } |
| 86 | + if ($definition->getLatestFetchType() === 'github') { |
| 87 | + return $this->fetchVersionsFromGithubRelease($definition->getRepo(), $definition->getBin()); |
| 88 | + } |
| 89 | + if ($definition->getLatestFetchType() === 'packagist') { |
| 90 | + return $this->fetchVersionsFromPackagist($definition->getPkgName(), $definition->getComposerName()); |
| 91 | + } |
| 92 | + throw new BoxException('The definition of package is invalid'); |
| 93 | + } |
| 94 | +} |
0 commit comments