Skip to content

Commit fd47850

Browse files
authored
Merge pull request #789 from Geolim4/master
Fixed #788 // Redundant directory name for Sqlite
2 parents 3c888d9 + 048f826 commit fd47850

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected function getFilePath($keyword, $skip = false): string
100100
);
101101
}
102102

103-
return $path . '/' . $filename . '.' . $this->getConfig()->getCacheFileExtension();
103+
return $path . \DIRECTORY_SEPARATOR . $filename . '.' . $this->getConfig()->getCacheFileExtension();
104104
}
105105

106106
/**
@@ -322,7 +322,7 @@ protected function readFile($file): string
322322
* @return bool
323323
* @throws PhpfastcacheIOException
324324
*/
325-
protected function writefile($file, $data, $secureFileManipulation = false): bool
325+
protected function writefile(string $file, string $data, bool $secureFileManipulation = false): bool
326326
{
327327
/**
328328
* @eventName CacheWriteFileOnDisk
@@ -335,11 +335,10 @@ protected function writefile($file, $data, $secureFileManipulation = false): boo
335335

336336
if ($secureFileManipulation) {
337337
$tmpFilename = Directory::getAbsolutePath(
338-
dirname($file) . '/tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()(
339-
\str_shuffle(\uniqid($this->getDriverName(), false))
340-
. \str_shuffle(\uniqid($this->getDriverName(), false))
338+
dirname($file) . \DIRECTORY_SEPARATOR . 'tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()(
339+
\bin2hex(\random_bytes(16))
341340
)
342-
);
341+
) . '.' . $this->getConfig()->getCacheFileExtension() . \random_int(1000, 9999);
343342

344343
$handle = \fopen($tmpFilename, 'w+b');
345344
if (\is_resource($handle)) {

lib/Phpfastcache/Drivers/Sqlite/Driver.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterfac
3434
{
3535
use DriverBaseTrait, IOHelperTrait;
3636

37-
/**
38-
*
39-
*/
40-
protected const FILE_DIR = 'sqlite';
4137
/**
4238
*
4339
*/
@@ -77,7 +73,7 @@ public function driverCheck(): bool
7773
*/
7874
public function getSqliteDir(): string
7975
{
80-
return $this->SqliteDir ?: $this->getPath() . DIRECTORY_SEPARATOR . self::FILE_DIR;
76+
return $this->SqliteDir ?: $this->getPath();
8177
}
8278

8379
/**
@@ -97,13 +93,13 @@ protected function driverConnect(): bool
9793
if (!file_exists($this->getSqliteDir()) && !@mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)) {
9894
throw new PhpfastcacheIOException(sprintf('Sqlite cannot write in "%s", aborting...', $this->getPath()));
9995
}
100-
if (!file_exists($this->getPath() . '/' . self::FILE_DIR)) {
101-
if (!mkdir($this->getPath() . '/' . self::FILE_DIR, $this->getDefaultChmod(), true)
96+
if (!file_exists($this->getPath())) {
97+
if (!mkdir($this->getPath(), $this->getDefaultChmod(), true)
10298
) {
10399
$this->fallback = true;
104100
}
105101
}
106-
$this->SqliteDir = $this->getPath() . '/' . self::FILE_DIR;
102+
$this->SqliteDir = $this->getPath();
107103

108104
return true;
109105
}

lib/Phpfastcache/Helper/TestHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,4 +482,4 @@ public function printPassText(string $string): self
482482

483483
return $this;
484484
}
485-
}
485+
}

tests/ReadWriteOperations.test.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040

4141
$instances[ $dirIndex ][ $cacheInstanceName ] = CacheManager::getInstance('Files', new FilesConfig([
4242
'path' => $dir . str_pad($i, 3, '0', STR_PAD_LEFT),
43-
'secureFileManipulation' => true
43+
'secureFileManipulation' => true,
44+
'securityKey' => '_cache',
4445
]));
4546

4647
foreach ($keys[ $dirIndex ] as $index => $key) {
@@ -75,4 +76,4 @@
7576
}
7677
}
7778

78-
$testHelper->terminateTest();
79+
$testHelper->terminateTest();

tests/Sqlite.test.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <[email protected]>
6+
*/
7+
8+
use Phpfastcache\CacheManager;
9+
use Phpfastcache\Config\ConfigurationOption;
10+
use Phpfastcache\Helper\TestHelper;
11+
12+
chdir(__DIR__);
13+
require_once __DIR__ . '/../vendor/autoload.php';
14+
$testHelper = new TestHelper('Sqlite');
15+
CacheManager::setDefaultConfig(new ConfigurationOption(['path' => __DIR__ . '/../cache']));
16+
17+
$cacheInstance = CacheManager::getInstance('Sqlite');
18+
19+
$testHelper->runCRUDTests($cacheInstance);
20+
21+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)