-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cache key generator interface and default implementation. (#32)
- Loading branch information
1 parent
506e1c6
commit e484fae
Showing
7 changed files
with
198 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace ScriptFUSION\Porter\Cache; | ||
|
||
interface CacheKeyGenerator | ||
{ | ||
/** | ||
* @param string $source | ||
* @param array $sortedOptions Options sorted by key. | ||
* | ||
* @return string A PSR-6 compatible cache key. | ||
*/ | ||
public function generateCacheKey($source, array $sortedOptions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
namespace ScriptFUSION\Porter\Cache; | ||
|
||
class InvalidCacheKeyException extends \RuntimeException implements \Psr\Cache\InvalidArgumentException | ||
{ | ||
// Intentionally empty. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
namespace ScriptFUSION\Porter\Cache; | ||
|
||
use ScriptFUSION\Porter\Connector\CachingConnector; | ||
|
||
class JsonCacheKeyGenerator implements CacheKeyGenerator | ||
{ | ||
public function generateCacheKey($source, array $sortedOptions) | ||
{ | ||
return str_replace( | ||
str_split(CachingConnector::RESERVED_CHARACTERS), | ||
'.', | ||
json_encode([$source, $sortedOptions], JSON_UNESCAPED_SLASHES) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
namespace ScriptFUSIONTest\Unit\Porter\Cache; | ||
|
||
use ScriptFUSION\Porter\Cache\JsonCacheKeyGenerator; | ||
use ScriptFUSIONTest\Stubs\TestOptions; | ||
|
||
final class JsonCacheKeyGeneratorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testGenerateCacheKey() | ||
{ | ||
$options = new TestOptions; | ||
$options->setFoo('(baz@quz\quux/quuz)'); | ||
|
||
self::assertSame( | ||
'["bar",."foo".".baz.quz..quux.quuz.".]', | ||
(new JsonCacheKeyGenerator)->generateCacheKey('bar', $options->copy()) | ||
); | ||
} | ||
} |