Skip to content

Commit 77824bc

Browse files
authored
Merge pull request #379 from Geolim4/final
Introduced partial fix for #373 (without additional option)
2 parents 2578aca + c7734db commit 77824bc

File tree

5 files changed

+136
-61
lines changed

5 files changed

+136
-61
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,12 @@ build/
7070
*.tlb
7171
*.tli
7272
*.tlh
73-
*.tmp
7473
*.tmp_proj
7574
*.log
7675
*.vspscc
7776
*.vssscc
7877
.builds
7978
*.pidb
80-
*.log
8179
*.scc
8280

8381
# Visual C++ cache files
@@ -196,7 +194,6 @@ $RECYCLE.BIN/
196194
*.egg
197195
*.egg-info
198196
dist/
199-
build/
200197
eggs/
201198
parts/
202199
# var/
@@ -233,4 +230,5 @@ nbproject/
233230
#Composer
234231
vendor/
235232
# composer.lock
236-
composer.phar
233+
composer.phar
234+
/cache/

bin/ci/run_tests.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@
44
* @author Georges.L (Geolim4) <[email protected]>
55
*/
66

7+
function glob_recursive($pattern, $flags = 0)
8+
{
9+
$files = glob($pattern, $flags);
10+
11+
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
12+
{
13+
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
14+
}
15+
16+
return $files;
17+
}
18+
719
$status = 0;
820
$driver = (!empty($argv[1]) ? ucfirst($argv[1]) : 'Files');
9-
1021
$testDir = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tests');
1122

12-
foreach (glob($testDir . DIRECTORY_SEPARATOR . '*.test.php') as $filename) {
23+
foreach (glob_recursive($testDir . DIRECTORY_SEPARATOR . '*.test.php') as $filename) {
1324
echo "\e[97m--\n";
1425
$command = "php -f {$filename} {$driver}";
1526
echo "\e[33mphpfastcache@test \e[34m~ \e[92m# \e[91m" . $command . "\n";

src/phpFastCache/Core/PathSeekerTrait.php

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use phpFastCache\Exceptions\phpFastCacheCoreException;
1818
use phpFastCache\Exceptions\phpFastCacheDriverException;
19+
use phpFastCache\Util\Directory;
1920

2021
trait PathSeekerTrait
2122
{
@@ -25,85 +26,91 @@ trait PathSeekerTrait
2526
public $tmp = [];
2627

2728
/**
28-
* @param bool $skip_create_path
29-
* @param $config
29+
* @param bool $readonly
3030
* @return string
31-
* @throws \Exception
31+
* @throws phpFastCacheDriverException
3232
*/
33-
public function getPath($getBasePath = false)
33+
public function getPath($readonly = false)
3434
{
35-
$tmp_dir = rtrim(ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache';
36-
37-
if (!isset($this->config[ 'path' ]) || $this->config[ 'path' ] == '') {
38-
if (self::isPHPModule()) {
39-
$path = $tmp_dir;
40-
} else {
41-
$document_root_path = rtrim($_SERVER[ 'DOCUMENT_ROOT' ], '/') . '/../';
42-
$path = isset($_SERVER[ 'DOCUMENT_ROOT' ]) && is_writable($document_root_path) ? $document_root_path : rtrim(__DIR__, '/') . '/';
35+
/**
36+
* Get the base system temporary directory
37+
*/
38+
$tmp_dir = rtrim(ini_get('upload_tmp_dir') ?: sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache';
39+
/**
40+
* Calculate the security key
41+
*/
42+
{
43+
$securityKey = array_key_exists('securityKey', $this->config) ? $this->config[ 'securityKey' ] : '';
44+
if (!$securityKey || $securityKey === 'auto') {
45+
if (isset($_SERVER[ 'HTTP_HOST' ])) {
46+
$securityKey = preg_replace('/^www./', '', strtolower(str_replace(':', '_', $_SERVER[ 'HTTP_HOST' ])));
47+
} else {
48+
$securityKey = ($this->isPHPModule() ? 'web' : 'cli');
49+
}
4350
}
44-
45-
if ($this->config[ 'path' ] != '') {
46-
$path = $this->config[ 'path' ];
51+
if ($securityKey !== '') {
52+
$securityKey .= '/';
4753
}
48-
49-
} else {
50-
$path = $this->config[ 'path' ];
54+
$securityKey = static::cleanFileName($securityKey);
5155
}
52-
53-
if ($getBasePath === true) {
54-
return $path;
56+
/**
57+
* Extends the temporary directory
58+
* with the security key and the driver name
59+
*/
60+
$tmp_dir = rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR;
61+
if (empty($this->config[ 'path' ]) || !is_string($this->config[ 'path' ])) {
62+
$path = $tmp_dir;
63+
} else {
64+
$path = rtrim($this->config[ 'path' ], '/') . DIRECTORY_SEPARATOR;
5565
}
56-
57-
$securityKey = array_key_exists('securityKey', $this->config) ? $this->config[ 'securityKey' ] : '';
58-
if (!$securityKey || $securityKey === 'auto') {
59-
if (isset($_SERVER[ 'HTTP_HOST' ])) {
60-
$securityKey = preg_replace('/^www./', '', strtolower(str_replace(':', '_', $_SERVER[ 'HTTP_HOST' ])));
61-
} else {
62-
$securityKey = ($this->isPHPModule() ? 'web' : 'cli');
66+
$path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName();
67+
$full_path = Directory::getAbsolutePath($path . $path_suffix);
68+
$full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix);
69+
$full_path_hash = md5($full_path);
70+
/**
71+
* In readonly mode we only attempt
72+
* to verify if the directory exists
73+
* or not, if it does not then we
74+
* return the temp dir
75+
*/
76+
if ($readonly === true) {
77+
if(!@file_exists($full_path) || !@is_writable($full_path)){
78+
return $full_path_tmp;
6379
}
64-
}
65-
66-
if ($securityKey !== '') {
67-
$securityKey .= '/';
68-
}
69-
70-
$securityKey = static::cleanFileName($securityKey);
71-
72-
$full_path = rtrim($path, '/') . '/' . $securityKey;
73-
$full_pathx = md5($full_path);
74-
75-
76-
if (!isset($this->tmp[ $full_pathx ])) {
77-
78-
if (!@file_exists($full_path) || !@is_writable($full_path)) {
80+
return $full_path;
81+
}else{
82+
if (!isset($this->tmp[ $full_path_hash ]) || (!@file_exists($full_path) || !@is_writable($full_path))) {
7983
if (!@file_exists($full_path)) {
8084
@mkdir($full_path, $this->setChmodAuto(), true);
81-
}
82-
if (!@is_writable($full_path)) {
85+
}else if (!@is_writable($full_path)) {
8386
@chmod($full_path, $this->setChmodAuto());
8487
}
8588
if (!@is_writable($full_path)) {
86-
// switch back to tmp dir again if the path is not writeable
87-
$full_path = rtrim($tmp_dir, '/') . '/' . $securityKey;
89+
/**
90+
* Switch back to tmp dir
91+
* again if the path is not writable
92+
*/
93+
$full_path = $full_path_tmp;
8894
if (!@file_exists($full_path)) {
8995
@mkdir($full_path, $this->setChmodAuto(), true);
9096
}
91-
if (!@is_writable($full_path)) {
92-
@chmod($full_path, $this->setChmodAuto());
93-
}
9497
}
98+
/**
99+
* In case there is no directory
100+
* writable including tye temporary
101+
* one, we must throw an exception
102+
*/
95103
if (!@file_exists($full_path) || !@is_writable($full_path)) {
96-
throw new phpFastCacheCoreException('PLEASE CREATE OR CHMOD ' . $full_path . ' - 0777 OR ANY WRITABLE PERMISSION!', 92);
104+
throw new phpFastCacheDriverException('PLEASE CREATE OR CHMOD ' . $full_path . ' - 0777 OR ANY WRITABLE PERMISSION!');
97105
}
106+
$this->tmp[ $full_path_hash ] = $full_path;
107+
$this->htaccessGen($full_path, array_key_exists('htaccess', $this->config) ? $this->config[ 'htaccess' ] : false);
98108
}
99-
100-
$this->tmp[ $full_pathx ] = true;
101-
$this->htaccessGen($full_path, array_key_exists('htaccess', $this->config) ? $this->config[ 'htaccess' ] : false);
102109
}
103-
104110
return realpath($full_path);
105111
}
106112

113+
107114
/**
108115
* @param $keyword
109116
* @return string

src/phpFastCache/Util/Directory.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,29 @@ public static function rrmdir($source, $removeOnlyChildren = false)
110110

111111
return true;
112112
}
113+
114+
/**
115+
* Alias of realpath() but work
116+
* on non-existing files
117+
*
118+
* @param $path
119+
* @return string
120+
*/
121+
public static function getAbsolutePath($path)
122+
{
123+
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
124+
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
125+
$absolutes = [];
126+
foreach ($parts as $part) {
127+
if ('.' === $part) {
128+
continue;
129+
}
130+
if ('..' === $part) {
131+
array_pop($absolutes);
132+
} else {
133+
$absolutes[] = $part;
134+
}
135+
}
136+
return implode(DIRECTORY_SEPARATOR, $absolutes);
137+
}
113138
}

tests/issues/Github-373.test.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <[email protected]>
6+
*/
7+
use phpFastCache\CacheManager;
8+
9+
chdir(__DIR__);
10+
require_once __DIR__ . '/../../src/autoload.php';
11+
12+
$status = 0;
13+
echo "Testing Github issue #373 - Files driver issue after clearing cache\n";
14+
15+
CacheManager::setDefaultConfig(array('path' => __DIR__ . '/../../cache'));
16+
$cacheInstance = CacheManager::getInstance('Files');
17+
18+
$key = 'test';
19+
$cacheItem = $cacheInstance->getItem($key);
20+
$cacheItem->set('value');
21+
22+
$cacheInstance->save($cacheItem);
23+
$cacheInstance->deleteItem($key);
24+
$cacheInstance->clear();
25+
26+
try {
27+
$has = $cacheInstance->hasItem($key);
28+
echo "[PASS] No error thrown while trying to test if an item exists after clearing\n";
29+
} catch (Exception $e) {
30+
$status = 255;
31+
echo "[FAIL] An error has been thrown while trying to test if an item exists after clearing\n";
32+
}
33+
34+
exit($status);

0 commit comments

Comments
 (0)