Skip to content
Closed
Next Next commit
make FileProfilerStorage more lazy
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Sep 16, 2023
commit 3908ff2703ae7796caddb82ae81ec2a727f82540
12 changes: 12 additions & 0 deletions lib/private/Profiler/FileProfilerStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
class FileProfilerStorage {
// Folder where profiler data are stored.
private string $folder;
private bool $folderPrepared = false;

/**
* Constructs the file storage using a "dsn-like" path.
Expand All @@ -44,13 +45,21 @@ class FileProfilerStorage {
*/
public function __construct(string $folder) {
$this->folder = $folder;
}

private function prepareFolder() {
if ($this->folderPrepared) {
return;
}
$this->folderPrepared = true;

if (!is_dir($this->folder) && false === @mkdir($this->folder, 0777, true) && !is_dir($this->folder)) {
throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $this->folder));
}
}

public function find(?string $url, ?int $limit, ?string $method, int $start = null, int $end = null, string $statusCode = null): array {
$this->prepareFolder();
$file = $this->getIndexFilename();

if (!file_exists($file)) {
Expand Down Expand Up @@ -94,6 +103,7 @@ public function find(?string $url, ?int $limit, ?string $method, int $start = nu
}

public function purge(): void {
$this->prepareFolder();
$flags = \FilesystemIterator::SKIP_DOTS;
$iterator = new \RecursiveDirectoryIterator($this->folder, $flags);
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
Expand All @@ -109,6 +119,7 @@ public function purge(): void {
}

public function read(string $token): ?IProfile {
$this->prepareFolder();
if (!$token || !file_exists($file = $this->getFilename($token))) {
return null;
}
Expand All @@ -124,6 +135,7 @@ public function read(string $token): ?IProfile {
* @throws \RuntimeException
*/
public function write(IProfile $profile): bool {
$this->prepareFolder();
$file = $this->getFilename($profile->getToken());

$profileIndexed = is_file($file);
Expand Down