@@ -53,27 +53,57 @@ public function setNamespaceAndDirectory(string $namespace, ?string $baseDir = n
53
53
*
54
54
* @param string $ns The namespace to use for the directory name, sanitized to allow only
55
55
* alphanumeric characters, underscores, and hyphens.
56
- * @param string|null $base The base directory where the cache directory will be created.
56
+ * @param string|null $baseDir The base directory where the cache directory will be created.
57
57
* Defaults to the system's temporary directory if null.
58
58
*
59
59
* @throws RuntimeException If the directory cannot be created or is not writable, or if a
60
60
* file with the same name already exists.
61
61
*/
62
- private function createDirectory (string $ ns , ?string $ base ): void
62
+ private function createDirectory (string $ ns , ?string $ baseDir ): void
63
63
{
64
- $ base = rtrim ($ base ?? sys_get_temp_dir (), DIRECTORY_SEPARATOR );
65
- $ ns = preg_replace ('/[^A-Za-z0-9_\-]/ ' , '_ ' , $ ns );
66
- $ this ->dir = "$ base/cache_ $ ns/ " ;
64
+ $ baseDir = rtrim ($ baseDir ?? sys_get_temp_dir (), DIRECTORY_SEPARATOR );
65
+ $ ns = preg_replace ('/[^A-Za-z0-9_\-]/ ' , '_ ' , $ ns );
66
+ $ this ->dir = $ baseDir . DIRECTORY_SEPARATOR . 'cache_ ' . $ ns . DIRECTORY_SEPARATOR ;
67
+
68
+ if (is_dir ($ this ->dir )) {
69
+ if (!is_writable ($ this ->dir )) {
70
+ throw new RuntimeException (
71
+ "Cache directory ' $ this ->dir ' exists but is not writable "
72
+ );
73
+ }
74
+ return ;
75
+ }
76
+
77
+ if (file_exists ($ baseDir ) && !is_dir ($ baseDir )) {
78
+ throw new RuntimeException (
79
+ 'Cache base path ' . realpath ($ baseDir ) . ' exists and is *not* a directory '
80
+ );
81
+ }
82
+
83
+ if (!is_dir ($ baseDir ) && !@mkdir ($ baseDir , 0770 , true ) && !is_dir ($ baseDir )) {
84
+ $ err = error_get_last ()['message ' ] ?? 'unknown error ' ;
85
+ throw new RuntimeException (
86
+ 'Failed to create base directory ' . $ baseDir . ": $ err "
87
+ );
88
+ }
67
89
68
90
if (file_exists ($ this ->dir ) && !is_dir ($ this ->dir )) {
69
- throw new RuntimeException ("' $ this ->dir ' exists and is not a directory " );
91
+ throw new RuntimeException (
92
+ realpath ($ this ->dir ) . ' exists and is not a directory '
93
+ );
70
94
}
71
- if (!is_dir ($ this ->dir ) && !@mkdir ($ this ->dir , 0770 , true )) {
95
+
96
+ if (!@mkdir ($ this ->dir , 0770 , true ) && !is_dir ($ this ->dir )) {
72
97
$ err = error_get_last ()['message ' ] ?? 'unknown error ' ;
73
- throw new RuntimeException ("Failed to create ' {$ this ->dir }': {$ err }" );
98
+ throw new RuntimeException (
99
+ 'Failed to create cache directory ' . $ this ->dir . ": $ err "
100
+ );
74
101
}
102
+
75
103
if (!is_writable ($ this ->dir )) {
76
- throw new RuntimeException ("Cache directory ' {$ this ->dir }' is not writable " );
104
+ throw new RuntimeException (
105
+ 'Cache directory ' . $ this ->dir . ' is not writable '
106
+ );
77
107
}
78
108
}
79
109
0 commit comments