Skip to content

Commit bb951f8

Browse files
authored
Merge pull request #80 from infocyph/feature/di-update
cache dir
2 parents fc78972 + 9f376f6 commit bb951f8

File tree

2 files changed

+68
-12
lines changed

2 files changed

+68
-12
lines changed

src/Cache/Adapter/FileCacheAdapter.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,57 @@ public function setNamespaceAndDirectory(string $namespace, ?string $baseDir = n
5353
*
5454
* @param string $ns The namespace to use for the directory name, sanitized to allow only
5555
* 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.
5757
* Defaults to the system's temporary directory if null.
5858
*
5959
* @throws RuntimeException If the directory cannot be created or is not writable, or if a
6060
* file with the same name already exists.
6161
*/
62-
private function createDirectory(string $ns, ?string $base): void
62+
private function createDirectory(string $ns, ?string $baseDir): void
6363
{
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+
}
6789

6890
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+
);
7094
}
71-
if (!is_dir($this->dir) && !@mkdir($this->dir, 0770, true)) {
95+
96+
if (!@mkdir($this->dir, 0770, true) && !is_dir($this->dir)) {
7297
$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+
);
74101
}
102+
75103
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+
);
77107
}
78108
}
79109

src/DI/Resolver/ParameterResolver.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,46 @@ public function setClassResolverInstance(ClassResolver $classResolver): void
5858
* of the supplied parameters to create a unique identifier.
5959
*
6060
* @param ReflectionFunctionAbstract $reflector The reflection object representing the function/method.
61-
* @param array $suppliedParameters The parameters supplied for the function/method call.
61+
* @param array $supplied The parameters supplied for the function/method call.
6262
* @param string $type A string indicating the type of operation or context.
6363
* @return string A unique cache key for the given function/method resolution.
6464
*/
6565
private function makeResolutionCacheKey(
6666
ReflectionFunctionAbstract $reflector,
67-
array $suppliedParameters,
67+
array $supplied,
6868
string $type,
6969
): string {
7070
$owner = $reflector->class ?? '';
71-
$argsHash = hash('xxh3', serialize($suppliedParameters));
71+
$norm = array_map([self::class, 'normalise'], $supplied);
72+
$argsHash = hash('xxh3', json_encode($norm, JSON_UNESCAPED_SLASHES));
73+
7274
return "$owner::{$reflector->getName()}|$type|$argsHash";
7375
}
7476

77+
/**
78+
* Normalizes a given value into a string or recursively normalizes arrays.
79+
*
80+
* This function converts different types of values into unique string representations:
81+
* - Closures are represented by their object ID.
82+
* - Objects are represented by their object ID.
83+
* - Resources are represented by their type and ID.
84+
* - Arrays are recursively normalized.
85+
* - Other types returned as-is.
86+
*
87+
* @param mixed $value The value to be normalized.
88+
* @return mixed The normalized value, either as a string or recursively normalized array.
89+
*/
90+
private static function normalise(mixed $value): mixed
91+
{
92+
return match (true) {
93+
$value instanceof \Closure => 'closure#' . spl_object_id($value),
94+
is_object($value) => 'obj#' . spl_object_id($value),
95+
is_resource($value) => 'res#' . get_resource_type($value) . '#' . (int)$value,
96+
is_array($value) => array_map([self::class, 'normalise'], $value),
97+
default => $value,
98+
};
99+
}
100+
75101
/**
76102
* Retrieves the Infuse attributes for the given function/method reflection.
77103
*

0 commit comments

Comments
 (0)