Skip to content

Commit 8c8f4f2

Browse files
committed
Added "defaultFileNameHashFunction" option
1 parent 968c704 commit 8c8f4f2

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/Phpfastcache/Config/ConfigurationOption.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class ConfigurationOption extends ArrayObject
4040
*/
4141
protected $defaultKeyHashFunction = 'md5';
4242

43+
/**
44+
* @var string|Callable
45+
*/
46+
protected $defaultFileNameHashFunction = 'md5';
47+
4348
/**
4449
* @var int
4550
*/
@@ -263,6 +268,28 @@ public function setDefaultKeyHashFunction($defaultKeyHashFunction)
263268
return $this;
264269
}
265270

271+
/**
272+
* @return Callable|string
273+
*/
274+
public function getDefaultFileNameHashFunction()
275+
{
276+
return $this->defaultFileNameHashFunction;
277+
}
278+
279+
/**
280+
* @param Callable|string $defaultKeyHashFunction
281+
* @return ConfigurationOption
282+
* @throws PhpfastcacheInvalidConfigurationException
283+
*/
284+
public function setDefaultFileNameHashFunction($defaultFileNameHashFunction)
285+
{
286+
if (!\function_exists($defaultFileNameHashFunction) || !\is_callable($defaultFileNameHashFunction)) {
287+
throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string');
288+
}
289+
$this->defaultFileNameHashFunction = $defaultFileNameHashFunction;
290+
return $this;
291+
}
292+
266293
/**
267294
* @return int
268295
*/
@@ -445,7 +472,7 @@ public function setCacheFileExtension(string $cacheFileExtension): self
445472
}
446473
if (!\in_array($cacheFileExtension, $safeFileExtensions, true)) {
447474
throw new PhpfastcacheInvalidConfigurationException(
448-
"{$cacheFileExtension} is not a safe extension, currently allowed extension: " . \implode(', ', $safeFileExtensions)
475+
"Extension \"{$cacheFileExtension}\" is not safe, currently allowed extension names: " . \implode(', ', $safeFileExtensions)
449476
);
450477
}
451478

lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getPath($readonly = false): string
8585
$path_suffix = $securityKey . \DIRECTORY_SEPARATOR . $this->getDriverName();
8686
$full_path = Directory::getAbsolutePath($path . $path_suffix);
8787
$full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix);
88-
$full_path_hash = \md5($full_path);
88+
$full_path_hash = $this->getConfig()->getDefaultFileNameHashFunction()($full_path);
8989

9090
/**
9191
* In readonly mode we only attempt
@@ -172,7 +172,7 @@ protected function getFilePath($keyword, $skip = false): string
172172
*/
173173
protected function encodeFilename($keyword): string
174174
{
175-
return \md5($keyword);
175+
return $this->getConfig()->getDefaultFileNameHashFunction()($keyword);
176176
}
177177

178178
/**
@@ -289,7 +289,7 @@ protected function writefile($file, $data, $secureFileManipulation = false): boo
289289
$this->eventManager->dispatch('CacheWriteFileOnDisk', $this, $file, $secureFileManipulation);
290290

291291
if ($secureFileManipulation) {
292-
$tmpFilename = Directory::getAbsolutePath(\dirname($file) . '/tmp_' . \md5(
292+
$tmpFilename = Directory::getAbsolutePath(\dirname($file) . '/tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()(
293293
\str_shuffle(\uniqid($this->getDriverName(), false))
294294
. \str_shuffle(\uniqid($this->getDriverName(), false))
295295
));

0 commit comments

Comments
 (0)