12
12
use function array_keys ;
13
13
use function array_map ;
14
14
use function closedir ;
15
- use function dirname ;
16
15
use function error_get_last ;
17
16
use function filemtime ;
18
17
use function fileowner ;
@@ -91,9 +90,6 @@ public function __construct(
91
90
private string $ cachePath ,
92
91
private int $ directoryMode = 0775 ,
93
92
) {
94
- if (!$ this ->createDirectoryIfNotExists ($ cachePath )) {
95
- throw new CacheException ("Failed to create cache directory \"$ cachePath \". " );
96
- }
97
93
}
98
94
99
95
public function get (string $ key , mixed $ default = null ): mixed
@@ -123,14 +119,7 @@ public function set(string $key, mixed $value, null|int|DateInterval $ttl = null
123
119
return $ this ->delete ($ key );
124
120
}
125
121
126
- $ file = $ this ->getCacheFile ($ key );
127
- $ cacheDirectory = dirname ($ file );
128
-
129
- if (!is_dir ($ this ->cachePath )
130
- || $ this ->directoryLevel > 0 && !$ this ->createDirectoryIfNotExists ($ cacheDirectory )
131
- ) {
132
- throw new CacheException ("Failed to create cache directory \"$ cacheDirectory \". " );
133
- }
122
+ $ file = $ this ->getCacheFile ($ key , ensureDirectory: true );
134
123
135
124
// If ownership differs, the touch call will fail, so we try to
136
125
// rebuild the file from scratch by deleting it first
@@ -325,25 +314,27 @@ private function normalizeTtl(null|int|string|DateInterval $ttl = null): ?int
325
314
}
326
315
327
316
/**
328
- * Ensures that the directory is created .
317
+ * Ensures that the directory exists .
329
318
*
330
319
* @param string $path The path to the directory.
331
- *
332
- * @return bool Whether the directory was created.
333
320
*/
334
- private function createDirectoryIfNotExists (string $ path ): bool
321
+ private function ensureDirectory (string $ path ): void
335
322
{
336
323
if (is_dir ($ path )) {
337
- return true ;
324
+ return ;
325
+ }
326
+
327
+ if (is_file ($ path )) {
328
+ throw new CacheException ("Failed to create cache directory, file with the same name exists: \"$ path \". " );
338
329
}
339
330
340
- $ result = ! is_file ( $ path ) && mkdir (directory: $ path , recursive: true ) && is_dir ( $ path );
331
+ mkdir ($ path , recursive: true );
341
332
342
- if ($ result ) {
343
- chmod ( $ path , $ this -> directoryMode );
333
+ if (! is_dir ( $ path ) ) {
334
+ throw new CacheException ( " Failed to create cache directory \" $ path \" . " );
344
335
}
345
336
346
- return $ result ;
337
+ chmod ( $ path , $ this -> directoryMode ) ;
347
338
}
348
339
349
340
/**
@@ -353,8 +344,12 @@ private function createDirectoryIfNotExists(string $path): bool
353
344
*
354
345
* @return string The cache file path.
355
346
*/
356
- private function getCacheFile (string $ key ): string
347
+ private function getCacheFile (string $ key, bool $ ensureDirectory = false ): string
357
348
{
349
+ if ($ ensureDirectory ) {
350
+ $ this ->ensureDirectory ($ this ->cachePath );
351
+ }
352
+
358
353
if ($ this ->directoryLevel < 1 ) {
359
354
return $ this ->cachePath . DIRECTORY_SEPARATOR . $ key . $ this ->fileSuffix ;
360
355
}
@@ -364,6 +359,9 @@ private function getCacheFile(string $key): string
364
359
for ($ i = 0 ; $ i < $ this ->directoryLevel ; ++$ i ) {
365
360
if (($ prefix = substr ($ key , $ i + $ i , 2 )) !== '' ) {
366
361
$ base .= DIRECTORY_SEPARATOR . $ prefix ;
362
+ if ($ ensureDirectory ) {
363
+ $ this ->ensureDirectory ($ base );
364
+ }
367
365
}
368
366
}
369
367
0 commit comments