Skip to content

Commit

Permalink
Merge pull request #5 from ray-di/serializable-php-file-adapter
Browse files Browse the repository at this point in the history
Add serializable PhpFileAdapter
  • Loading branch information
koriym authored Jul 17, 2021
2 parents 9dae6ee + 606adc2 commit 9347855
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion php-require-checker.config.json
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"
]
}
41 changes: 41 additions & 0 deletions src/PhpFileAdapter.php
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));
}
}
29 changes: 29 additions & 0 deletions tests/PhpFileAdapterTest.php
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));
}
}

0 comments on commit 9347855

Please sign in to comment.