Files
pterodactyl-panel/app/Services/FilesystemChanges.php
2026-10-12 05:18:57 -04:00

87 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Services;
use Closure;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use RuntimeException;
use Throwable;
final class FilesystemChanges
{
/**
* Replace or remove paths, retaining originals until the operation succeeds.
*
* @template T
*
* @param array<string, string|null> $replacements Target paths mapped to staged paths, or null for removal.
* @param Closure(): T $callback
* @return T
*/
public function run(array $replacements, Closure $callback): mixed
{
$backups = [];
try {
foreach ($replacements as $target => $staged) {
$backup = File::exists($target) ? dirname($target).'/.previous-'.Str::random(12) : null;
if ($backup !== null) {
$this->move($target, $backup);
}
$backups[$target] = $backup;
if ($staged !== null) {
File::ensureDirectoryExists(dirname($target));
$this->move($staged, $target);
}
}
$result = $callback();
} catch (Throwable $throwable) {
foreach (array_reverse($backups, true) as $target => $backup) {
rescue(fn () => $this->restore($target, $backup));
}
throw $throwable;
}
foreach ($backups as $backup) {
if ($backup !== null) {
rescue(fn () => $this->delete($backup));
}
}
return $result;
}
public function delete(string $path): void
{
// A link (such as an extension folder linked to its source in development) is removed
// itself; deleting it as a directory would empty the folder it points to.
if (File::isDirectory($path) && ! is_link($path)) {
File::deleteDirectory($path);
} else {
File::delete($path);
}
clearstatcache(true, $path);
throw_if(File::exists($path) || is_link($path), RuntimeException::class, "Unable to remove files at {$path}.");
}
private function restore(string $target, ?string $backup): void
{
$this->delete($target);
if ($backup !== null) {
$this->move($backup, $target);
}
}
private function move(string $from, string $to): void
{
throw_unless(File::move($from, $to), RuntimeException::class, "Unable to move {$from} to {$to}; the original remains at {$from}.");
}
}