-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ray-di/serializable-php-file-adapter
Add serializable PhpFileAdapter
- Loading branch information
Showing
3 changed files
with
71 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"symbol-whitelist": [ | ||
"int", "array", "null", "parent", "string", "void", | ||
"int", "array", "null", "parent", "string", "void", "bool", "false", | ||
"Redis", "Memcached" | ||
] | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\PsrCacheModule; | ||
|
||
use Serializable; | ||
use Symfony\Component\Cache\Adapter\PhpFilesAdapter as OriginAdapter; | ||
|
||
use function call_user_func_array; | ||
use function func_get_args; | ||
use function serialize; | ||
use function unserialize; | ||
|
||
class PhpFileAdapter extends OriginAdapter implements Serializable | ||
{ | ||
/** @var array<int, mixed> */ | ||
private $args; | ||
|
||
public function __construct(string $namespace = '', int $defaultLifetime = 0, ?string $directory = null, bool $appendOnly = false) | ||
{ | ||
$this->args = func_get_args(); | ||
parent::__construct($namespace, $defaultLifetime, $directory, $appendOnly); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function serialize() | ||
{ | ||
return serialize($this->args); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function unserialize($data) | ||
{ | ||
call_user_func_array([$this, '__construct'], unserialize($data)); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ray\PsrCacheModule; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
use function serialize; | ||
use function unserialize; | ||
|
||
class PhpFileAdapterTest extends TestCase | ||
{ | ||
public function testSerialize(): string | ||
{ | ||
$string = serialize(new PhpFileAdapter()); | ||
$this->assertIsString($string); | ||
|
||
return $string; | ||
} | ||
|
||
/** | ||
* @depends testSerialize | ||
*/ | ||
public function testUnserialize(string $string): void | ||
{ | ||
$this->assertInstanceOf(PhpFileAdapter::class, unserialize($string)); | ||
} | ||
} |