Skip to content

Commit

Permalink
Fixed #14190: Cache\Backend\File -> queryKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmst authored and niden committed Jun 26, 2019
1 parent 30d8a53 commit 925a511
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion phalcon/Helper/Str.zep
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class Str
* ```php
* use Phalcon\Helper\Str;
*
* echo Str::dirFromFile("file1234.jpg"); // /fi/le/12/file1234.jpg
* echo Str::dirFromFile("file1234.jpg"); // fi/le/12/
* ```
*
* @param string $file
Expand Down
37 changes: 23 additions & 14 deletions phalcon/Storage/Adapter/Stream.zep
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ class Stream extends AbstractAdapter
*/
public function delete(string! key) -> bool
{
var directory;
var filepath;

if !this->has(key) {
return false;
}

let directory = this->getDir(key);
let filepath = this->getFilepath(key);

return unlink(directory . key);
return unlink(filepath);
}

/**
Expand All @@ -133,15 +133,15 @@ class Stream extends AbstractAdapter
*/
public function get(string! key, var defaultValue = null) -> var
{
var content, directory, payload;
var content, payload, filepath;

let directory = this->getDir(key);
let filepath = this->getFilepath(key);

if !file_exists(directory . key) {
if !file_exists(filepath) {
return defaultValue;
}

let payload = file_get_contents(directory . key),
let payload = file_get_contents(filepath),
payload = json_decode(payload, true);

if json_last_error() !== JSON_ERROR_NONE {
Expand Down Expand Up @@ -196,16 +196,15 @@ class Stream extends AbstractAdapter
*/
public function has(string! key) -> bool
{
var directory, exists, payload;
var payload, filepath;

let directory = this->getDir(key),
exists = file_exists(directory . key);
let filepath = this->getFilepath(key);

if !exists {
if !file_exists(filepath) {
return false;
}

let payload = file_get_contents(directory . key),
let payload = file_get_contents(filepath),
payload = json_decode(payload);

return !this->isExpired(payload);
Expand Down Expand Up @@ -276,9 +275,19 @@ class Stream extends AbstractAdapter
var dirPrefix, dirFromFile;

let dirPrefix = this->cacheDir . this->prefix,
dirFromFile = Str::dirFromFile(key);
dirFromFile = Str::dirFromFile(
str_replace(this->prefix, "", key, 1)
);

return Str::dirSeparator(dirPrefix) . dirFromFile;
}

return Str::dirSeparator(dirPrefix) . Str::dirSeparator(dirFromFile);
/**
* Returns the full path to the file
*/
private function getFilepath(string! key) -> string
{
return this->getDir(key) . str_replace(this->prefix, "", key, 1);
}

/**
Expand Down
48 changes: 42 additions & 6 deletions tests/unit/Storage/Adapter/Stream/GetKeysCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,20 @@ public function storageAdapterStreamGetKeys(UnitTester $I)

$adapter->clear();



$key = 'key-1';
$adapter->set($key, 'test');

$I->assertTrue(
$adapter->has($key)
);



$key = 'key-2';
$adapter->set($key, 'test');

$I->assertTrue(
$adapter->has($key)
);



$expected = [
'phstrm-key-1',
'phstrm-key-2',
Expand All @@ -69,4 +63,46 @@ public function storageAdapterStreamGetKeys(UnitTester $I)
sort($actual);
$I->assertEquals($expected, $actual);
}

/**
* @author ekmst <https://github.com/ekmst>
* @since 2019-06-26
*
* @dataProvoder keys()
*/
public function storageAdapterStreamGetKeysIssue14190(UnitTester $I)
{
$I->wantToTest('Storage\Adapter\Stream - getKeys() - issue 14190');

$serializer = new SerializerFactory();

$adapter = new Stream(
$serializer,
[
'cacheDir' => outputDir(),
'prefix' => 'basePrefix-',
]
);

$adapter->clear();

$adapter->set('key', 'test');
$adapter->set('key1', 'test');

$expected = [
'basePrefix-key',
'basePrefix-key1',
];

$actual = $adapter->getKeys();
sort($actual);

$I->assertEquals($expected, $actual);

foreach ($expected as $key) {
$I->assertTrue(
$adapter->delete($key)
);
}
}
}

0 comments on commit 925a511

Please sign in to comment.