| 1 |
<?php |
| 2 |
|
| 3 |
namespace Spatie\TemporaryDirectory; |
| 4 |
|
| 5 |
use FilesystemIterator; |
| 6 |
use Spatie\TemporaryDirectory\Exceptions\InvalidDirectoryName; |
| 7 |
use Spatie\TemporaryDirectory\Exceptions\PathAlreadyExists; |
| 8 |
use Throwable; |
| 9 |
|
| 10 |
class TemporaryDirectory |
| 11 |
{ |
| 12 |
protected string $location; |
| 13 |
|
| 14 |
protected string $name = ''; |
| 15 |
|
| 16 |
protected bool $forceCreate = false; |
| 17 |
|
| 18 |
protected bool $deleteWhenDestroyed = false; |
| 19 |
|
| 20 |
protected int $permission = 0777; |
| 21 |
|
| 22 |
public function __construct(string $location = '') |
| 23 |
{ |
| 24 |
$this->location = $this->sanitizePath($location); |
| 25 |
} |
| 26 |
|
| 27 |
public static function make(string $location = ''): self |
| 28 |
{ |
| 29 |
return (new self($location))->create(); |
| 30 |
} |
| 31 |
|
| 32 |
public function create(): self |
| 33 |
{ |
| 34 |
if (empty($this->location)) { |
| 35 |
$this->location = $this->getSystemTemporaryDirectory(); |
| 36 |
} |
| 37 |
|
| 38 |
if (empty($this->name)) { |
| 39 |
$this->name = mt_rand().'-'.str_replace([' ', '.'], '', microtime()); |
| 40 |
} |
| 41 |
|
| 42 |
if ($this->forceCreate && file_exists($this->getFullPath())) { |
| 43 |
$this->deleteDirectory($this->getFullPath()); |
| 44 |
} |
| 45 |
|
| 46 |
if ($this->exists()) { |
| 47 |
throw PathAlreadyExists::create($this->getFullPath()); |
| 48 |
} |
| 49 |
|
| 50 |
mkdir($this->getFullPath(), $this->permission, true); |
| 51 |
|
| 52 |
return $this; |
| 53 |
} |
| 54 |
|
| 55 |
public function force(): self |
| 56 |
{ |
| 57 |
$this->forceCreate = true; |
| 58 |
|
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
public function permission(int $permission): self |
| 63 |
{ |
| 64 |
$this->permission = $permission; |
| 65 |
|
| 66 |
return $this; |
| 67 |
} |
| 68 |
|
| 69 |
public function name(string $name): self |
| 70 |
{ |
| 71 |
$this->name = $this->sanitizeName($name); |
| 72 |
|
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
public function location(string $location): self |
| 77 |
{ |
| 78 |
$this->location = $this->sanitizePath($location); |
| 79 |
|
| 80 |
return $this; |
| 81 |
} |
| 82 |
|
| 83 |
public function path(string $pathOrFilename = ''): string |
| 84 |
{ |
| 85 |
if (empty($pathOrFilename)) { |
| 86 |
return $this->getFullPath(); |
| 87 |
} |
| 88 |
|
| 89 |
$path = $this->getFullPath().DIRECTORY_SEPARATOR.trim($pathOrFilename, '/'); |
| 90 |
|
| 91 |
$directoryPath = $this->removeFilenameFromPath($path); |
| 92 |
|
| 93 |
if (! file_exists($directoryPath)) { |
| 94 |
mkdir($directoryPath, $this->permission, true); |
| 95 |
} |
| 96 |
|
| 97 |
return $path; |
| 98 |
} |
| 99 |
|
| 100 |
public function empty(): self |
| 101 |
{ |
| 102 |
$this->deleteDirectory($this->getFullPath()); |
| 103 |
|
| 104 |
mkdir($this->getFullPath(), $this->permission, true); |
| 105 |
|
| 106 |
return $this; |
| 107 |
} |
| 108 |
|
| 109 |
public function delete(): bool |
| 110 |
{ |
| 111 |
return $this->deleteDirectory($this->getFullPath()); |
| 112 |
} |
| 113 |
|
| 114 |
public function exists(): bool |
| 115 |
{ |
| 116 |
return file_exists($this->getFullPath()); |
| 117 |
} |
| 118 |
|
| 119 |
public function getName(): string |
| 120 |
{ |
| 121 |
return $this->name; |
| 122 |
} |
| 123 |
|
| 124 |
protected function getFullPath(): string |
| 125 |
{ |
| 126 |
return $this->location.(! empty($this->name) ? DIRECTORY_SEPARATOR.$this->name : ''); |
| 127 |
} |
| 128 |
|
| 129 |
protected function isValidDirectoryName(string $directoryName): bool |
| 130 |
{ |
| 131 |
return strpbrk($directoryName, '\\/?%*:|"<>') === false; |
| 132 |
} |
| 133 |
|
| 134 |
protected function getSystemTemporaryDirectory(): string |
| 135 |
{ |
| 136 |
return rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR); |
| 137 |
} |
| 138 |
|
| 139 |
protected function sanitizePath(string $path): string |
| 140 |
{ |
| 141 |
$path = rtrim($path); |
| 142 |
|
| 143 |
return rtrim($path, DIRECTORY_SEPARATOR); |
| 144 |
} |
| 145 |
|
| 146 |
protected function sanitizeName(string $name): string |
| 147 |
{ |
| 148 |
if (! $this->isValidDirectoryName($name)) { |
| 149 |
throw InvalidDirectoryName::create($name); |
| 150 |
} |
| 151 |
|
| 152 |
return trim($name); |
| 153 |
} |
| 154 |
|
| 155 |
protected function removeFilenameFromPath(string $path): string |
| 156 |
{ |
| 157 |
if (! $this->isFilePath($path)) { |
| 158 |
return $path; |
| 159 |
} |
| 160 |
|
| 161 |
return substr($path, 0, strrpos($path, DIRECTORY_SEPARATOR)); |
| 162 |
} |
| 163 |
|
| 164 |
protected function isFilePath(string $path): bool |
| 165 |
{ |
| 166 |
return str_contains($path, '.'); |
| 167 |
} |
| 168 |
|
| 169 |
protected function deleteDirectory(string $path): bool |
| 170 |
{ |
| 171 |
try { |
| 172 |
if (is_link($path)) { |
| 173 |
return unlink($path); |
| 174 |
} |
| 175 |
|
| 176 |
if (! file_exists($path)) { |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
if (! is_dir($path)) { |
| 181 |
return unlink($path); |
| 182 |
} |
| 183 |
|
| 184 |
foreach (new FilesystemIterator($path) as $item) { |
| 185 |
if (! $this->deleteDirectory((string) $item)) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/* |
| 191 |
* By forcing a php garbage collection cycle using gc_collect_cycles() we can ensure |
| 192 |
* that the rmdir does not fail due to files still being reserved in memory. |
| 193 |
*/ |
| 194 |
gc_collect_cycles(); |
| 195 |
|
| 196 |
return rmdir($path); |
| 197 |
} catch (Throwable) { |
| 198 |
return false; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
public function deleteWhenDestroyed(bool $deleteWhenDestroyed = true): self |
| 203 |
{ |
| 204 |
$this->deleteWhenDestroyed = $deleteWhenDestroyed; |
| 205 |
|
| 206 |
return $this; |
| 207 |
} |
| 208 |
|
| 209 |
public function __destruct() |
| 210 |
{ |
| 211 |
if ($this->deleteWhenDestroyed) { |
| 212 |
$this->delete(); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|