|
| 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 | + |
| 13 | +namespace Hyperf\Phar; |
| 14 | + |
| 15 | +use FilesystemIterator; |
| 16 | +use Phar; |
| 17 | +use RecursiveDirectoryIterator; |
| 18 | +use RecursiveIteratorIterator; |
| 19 | +use RuntimeException; |
| 20 | +use SplFileInfo; |
| 21 | + |
| 22 | +class CustomPhar extends Phar |
| 23 | +{ |
| 24 | + private string $tempDir; |
| 25 | + |
| 26 | + public function __construct(string $filename, int $flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS, ?string $alias = null) |
| 27 | + { |
| 28 | + parent::__construct($filename, $flags, $alias); |
| 29 | + $this->tempDir = sys_get_temp_dir() . '/phar_cache_' . uniqid(); |
| 30 | + $this->createDirectory($this->tempDir); |
| 31 | + } |
| 32 | + |
| 33 | + public function addFile(string $filename, ?string $localName = null): void |
| 34 | + { |
| 35 | + $localName = $localName ?? basename($filename); |
| 36 | + $relativePath = $this->tempDir . '/' . $localName; |
| 37 | + $this->createDirectory(dirname($relativePath)); |
| 38 | + copy($filename, $relativePath); |
| 39 | + } |
| 40 | + |
| 41 | + public function addFromString(string $localName, string $contents): void |
| 42 | + { |
| 43 | + $relativePath = $this->tempDir . '/' . $localName; |
| 44 | + $this->createDirectory(dirname($relativePath)); |
| 45 | + file_put_contents($relativePath, $contents); |
| 46 | + } |
| 47 | + |
| 48 | + public function buildFromDirectory(string $directory, ?string $pattern = null): array |
| 49 | + { |
| 50 | + $this->recursiveCopy($directory, $this->tempDir, $pattern); |
| 51 | + return []; |
| 52 | + } |
| 53 | + |
| 54 | + public function buildFromIterator($iterator, ?string $baseDirectory = null): array |
| 55 | + { |
| 56 | + foreach ($iterator as $fileInfo) { |
| 57 | + /** @var SplFileInfo $fileInfo */ |
| 58 | + $relativePath = $baseDirectory ? str_replace(rtrim($baseDirectory, '/\\') . '/', '', $fileInfo->getRealPath()) : $fileInfo->getFilename(); |
| 59 | + $this->addFile($fileInfo->getRealPath(), $relativePath); |
| 60 | + } |
| 61 | + return []; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * 批量保存文件. |
| 66 | + */ |
| 67 | + public function save(): void |
| 68 | + { |
| 69 | + parent::buildFromDirectory($this->tempDir); |
| 70 | + $this->clearTempDir(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * 资源文件复制. |
| 75 | + */ |
| 76 | + private function recursiveCopy(string $source, string $destination, ?string $pattern = null): void |
| 77 | + { |
| 78 | + $iterator = new RecursiveIteratorIterator( |
| 79 | + new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS), |
| 80 | + RecursiveIteratorIterator::SELF_FIRST |
| 81 | + ); |
| 82 | + |
| 83 | + foreach ($iterator as $item) { |
| 84 | + $targetPath = $destination . '/' . substr($item->getRealPath(), strlen($source) + 1); |
| 85 | + if ($item->isDir()) { |
| 86 | + $this->createDirectory($targetPath); |
| 87 | + } elseif (! $pattern || preg_match($pattern, $item->getFilename())) { |
| 88 | + copy($item->getRealPath(), $targetPath); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * 创建目录结构. |
| 95 | + */ |
| 96 | + private function createDirectory(string $dir): void |
| 97 | + { |
| 98 | + if (! is_dir($dir) && ! mkdir($dir, 0777, true) && ! is_dir($dir)) { |
| 99 | + throw new RuntimeException("Directory {$dir} was not created"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * 清理临时目录. |
| 105 | + */ |
| 106 | + private function clearTempDir(): void |
| 107 | + { |
| 108 | + $files = new RecursiveIteratorIterator( |
| 109 | + new RecursiveDirectoryIterator($this->tempDir, FilesystemIterator::SKIP_DOTS), |
| 110 | + RecursiveIteratorIterator::CHILD_FIRST |
| 111 | + ); |
| 112 | + |
| 113 | + foreach ($files as $fileinfo) { |
| 114 | + ($fileinfo->isDir() ? 'rmdir' : 'unlink')($fileinfo->getRealPath()); |
| 115 | + } |
| 116 | + |
| 117 | + rmdir($this->tempDir); |
| 118 | + } |
| 119 | +} |
0 commit comments