|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Demv\Tmpdir; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use RecursiveDirectoryIterator; |
| 7 | +use RecursiveIteratorIterator; |
| 8 | +use RuntimeException; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class TmpDirRegistry |
| 12 | + * @package Demv\Tmpdir |
| 13 | + */ |
| 14 | +final class TmpDirRegistry |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var string[] |
| 18 | + */ |
| 19 | + private $dirs = []; |
| 20 | + /** |
| 21 | + * @var string[] |
| 22 | + */ |
| 23 | + private $files = []; |
| 24 | + /** |
| 25 | + * @var TmpDirRegistry |
| 26 | + */ |
| 27 | + private static $instance; |
| 28 | + |
| 29 | + public function __destruct() |
| 30 | + { |
| 31 | + array_map( |
| 32 | + static function (string $filepath): void { |
| 33 | + self::deletefile($filepath); |
| 34 | + }, |
| 35 | + $this->files |
| 36 | + ); |
| 37 | + $this->files = []; |
| 38 | + array_map( |
| 39 | + static function (string $dir): void { |
| 40 | + self::deleteDir($dir); |
| 41 | + }, |
| 42 | + $this->dirs |
| 43 | + ); |
| 44 | + $this->dirs = []; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return static |
| 49 | + */ |
| 50 | + public static function instance(): self |
| 51 | + { |
| 52 | + if (self::$instance === null) { |
| 53 | + self::$instance = new self(); |
| 54 | + } |
| 55 | + |
| 56 | + return self::$instance; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param string $dirname |
| 61 | + * |
| 62 | + * @return string |
| 63 | + * @throws RuntimeException |
| 64 | + */ |
| 65 | + public function createDirInSystemTmp(string $dirname): string |
| 66 | + { |
| 67 | + $dirname = trim($dirname); |
| 68 | + if ($dirname === '') { |
| 69 | + throw new RuntimeException('Dirname must not be empty so we do not delete entire temp directory.'); |
| 70 | + } |
| 71 | + |
| 72 | + $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . sprintf('%s_%s', $dirname, $this->getUniqid()); |
| 73 | + if (!mkdir($dir, 0755, true) && !is_dir($dir)) { |
| 74 | + throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); |
| 75 | + } |
| 76 | + $this->dirs[] = $dir; |
| 77 | + |
| 78 | + return $dir; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param string $dir |
| 83 | + * @param string $filename |
| 84 | + * |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + public function createFileInSystemTmp(string $dir, string $filename): string |
| 88 | + { |
| 89 | + $filename = trim($filename); |
| 90 | + if ($filename === '') { |
| 91 | + throw new RuntimeException('Filename must not be empty so we do not delete entire temp directory.'); |
| 92 | + } |
| 93 | + |
| 94 | + $info = pathinfo($filename); |
| 95 | + if (!array_key_exists('extension', $info) || !array_key_exists('filename', $info)) { |
| 96 | + throw new RuntimeException('Can\'t read info from filename.'); |
| 97 | + } |
| 98 | + |
| 99 | + $filepath = $dir . DIRECTORY_SEPARATOR . sprintf('%s_%s.%s', $info['filename'], $this->getUniqid(), $info['extension']); |
| 100 | + $handle = fopen($filepath, 'wb+'); |
| 101 | + fclose($handle); |
| 102 | + if (!file_exists($filepath)) { |
| 103 | + throw new RuntimeException(sprintf('File "%s" was not created', $filepath)); |
| 104 | + } |
| 105 | + $this->files[] = $filepath; |
| 106 | + |
| 107 | + return $filepath; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param string $dirPath |
| 112 | + * |
| 113 | + * @throws InvalidArgumentException |
| 114 | + */ |
| 115 | + private static function deleteDir(string $dirPath): void |
| 116 | + { |
| 117 | + $dirPath = self::addSeperatorIfNecessary($dirPath); |
| 118 | + |
| 119 | + if (!is_dir($dirPath)) { |
| 120 | + throw new InvalidArgumentException(sprintf('%s must be a directory', $dirPath)); |
| 121 | + } |
| 122 | + |
| 123 | + $files = self::getFiles($dirPath); |
| 124 | + |
| 125 | + foreach ($files as $fileinfo) { |
| 126 | + $rm = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); |
| 127 | + $rm($fileinfo->getRealPath()); |
| 128 | + } |
| 129 | + |
| 130 | + rmdir($dirPath); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param string $filepath |
| 135 | + */ |
| 136 | + private static function deleteFile(string $filepath): void |
| 137 | + { |
| 138 | + if (!file_exists($filepath)) { |
| 139 | + throw new InvalidArgumentException(sprintf('%s must be a file', $filepath)); |
| 140 | + } |
| 141 | + |
| 142 | + unlink($filepath); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param string $dirPath |
| 147 | + * |
| 148 | + * @return RecursiveIteratorIterator |
| 149 | + */ |
| 150 | + private static function getFiles(string $dirPath): RecursiveIteratorIterator |
| 151 | + { |
| 152 | + return new RecursiveIteratorIterator( |
| 153 | + new RecursiveDirectoryIterator($dirPath, RecursiveDirectoryIterator::SKIP_DOTS), |
| 154 | + RecursiveIteratorIterator::CHILD_FIRST |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @param string $dirPath |
| 160 | + * |
| 161 | + * @return string |
| 162 | + */ |
| 163 | + private static function addSeperatorIfNecessary(string $dirPath): string |
| 164 | + { |
| 165 | + return rtrim(trim($dirPath), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @return string |
| 170 | + */ |
| 171 | + public function getUniqid(): string |
| 172 | + { |
| 173 | + return uniqid('', true); |
| 174 | + } |
| 175 | +} |
0 commit comments