Skip to content

Commit 52efb24

Browse files
committed
fix storage errors
1 parent afac64e commit 52efb24

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

Engine/Error/StorgeError.php renamed to Engine/Error/StorageError.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ class StorageError extends ShiftError {
2424
* @param Throwable|null $previous
2525
*/
2626
public function __construct(string $message = "", int $code = 0, Throwable $previous = null) {
27-
parent::__construct($message, $code, $previous);
27+
parent::__construct('Storage Error: ' . $message, $code, $previous);
2828
}
29-
}
29+
}

Engine/Utils/Storage.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,18 @@
99
namespace Engine\Utils;
1010

1111

12-
use Engine\Error\ShiftError;
12+
use Engine\Error\StorageError;
1313

1414
class Storage {
1515

1616
public $storageDir;
1717
public $storageViewsDir;
1818

1919
public function __construct() {
20-
$this->storageDir = realpath(__DIR__ . '/../storage/');
20+
$storageParentDir = realpath(__DIR__ . '/../');
21+
$this->storageDir = $storageParentDir . '/storage/';
2122
$this->storageViewsDir = $this->storageDir . '/views/';
22-
23-
if (!file_exists($this->storageDir)) {
24-
throw new ShiftError('Storage directory ' . $this->storageDir . ' does not exists');
25-
}
26-
if (!is_writable($this->storageDir)) {
27-
throw new ShiftError('Storage directory ' . $this->storageDir . ' is not writable');
28-
}
23+
$this->checkDir($this->storageViewsDir);
2924
}
3025

3126
public function saveView(string $name, string $content): void {
@@ -35,4 +30,16 @@ public function saveView(string $name, string $content): void {
3530

3631
file_put_contents($this->storageViewsDir . $name, $content);
3732
}
33+
34+
private function checkDir(string $dir): void {
35+
if (!file_exists($dir)) {
36+
mkdir($dir, 0777, true);
37+
}
38+
if (!file_exists($dir)) {
39+
throw new StorageError('Directory ' . $dir . ' does not exists');
40+
}
41+
if (!is_writable($dir)) {
42+
throw new StorageError('Directory ' . $dir . ' is not writable');
43+
}
44+
}
3845
}

0 commit comments

Comments
 (0)