diff --git a/README.md b/README.md index bde8cd0..0bebb3f 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,19 @@ assert($foo instanceof Foo); ## PSR-6 +### Psr6NullModule + +This module is for the development. + +* Local: Null +* Shared: Null + +```php +use Ray\PsrCacheModule\Psr6NullModule; + +new Psr6NullModule(); +``` + ### Psr6ArrayModule This module is for the development. diff --git a/composer.json b/composer.json index a7a805e..5090ab6 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "require": { "php": "^7.3 || ^8.0", "psr/cache": "^1.0.1", - "ray/di": "^2.12.0", + "ray/di": "^2.13.2", "symfony/cache": "^5.3.4", "doctrine/annotations": "^1.13", "psr/simple-cache": "^1.0" @@ -24,7 +24,7 @@ }, "autoload": { "psr-4": { - "Ray\\PsrCacheModule\\": "src/" + "Ray\\PsrCacheModule\\": ["src/", "src-deprecated"] } }, "autoload-dev": { diff --git a/src/FilesystemAdapter.php b/src-deprecated/FilesystemAdapter.php similarity index 71% rename from src/FilesystemAdapter.php rename to src-deprecated/FilesystemAdapter.php index 44356cb..58b6725 100644 --- a/src/FilesystemAdapter.php +++ b/src-deprecated/FilesystemAdapter.php @@ -4,17 +4,19 @@ namespace Ray\PsrCacheModule; +use JetBrains\PhpStorm\Deprecated; use Serializable; use Symfony\Component\Cache\Adapter\FilesystemAdapter as OriginAdapter; use Symfony\Component\Cache\Marshaller\MarshallerInterface; use function call_user_func_array; use function func_get_args; -use function serialize; -use function unserialize; +#[Deprecated] class FilesystemAdapter extends OriginAdapter implements Serializable { + use Php73BcSerializableTrait; + /** @var array */ private $args; @@ -27,16 +29,16 @@ public function __construct(string $namespace = '', int $defaultLifetime = 0, ?s /** * @inheritDoc */ - public function serialize() + public function __serialize(): array { - return serialize($this->args); + return $this->args; } /** - * @inheritDoc + * {@inheritDoc} */ - public function unserialize($data) + public function __unserialize($data) { - call_user_func_array([$this, '__construct'], unserialize($data)); // @phpstan-ignore-line + call_user_func_array([$this, '__construct'], $data); // @phpstan-ignore-line } } diff --git a/src-deprecated/Php73BcSerializableTrait.php b/src-deprecated/Php73BcSerializableTrait.php new file mode 100644 index 0000000..b0887f0 --- /dev/null +++ b/src-deprecated/Php73BcSerializableTrait.php @@ -0,0 +1,30 @@ +__serialize()); + } + + /** + * @psalm-suppress all + * + * {@inheritDoc} + */ + final public function unserialize($serializedData) + { + $array = unserialize($serializedData); + $this->__unserialize($array); // @phpstan-ignore-line + } +} \ No newline at end of file diff --git a/src/PhpFileAdapter.php b/src-deprecated/PhpFileAdapter.php similarity index 71% rename from src/PhpFileAdapter.php rename to src-deprecated/PhpFileAdapter.php index afa6d9b..c8fe3e2 100644 --- a/src/PhpFileAdapter.php +++ b/src-deprecated/PhpFileAdapter.php @@ -4,6 +4,7 @@ namespace Ray\PsrCacheModule; +use JetBrains\PhpStorm\Deprecated; use Serializable; use Symfony\Component\Cache\Adapter\PhpFilesAdapter as OriginAdapter; @@ -12,8 +13,11 @@ use function serialize; use function unserialize; +#[Deprecated] class PhpFileAdapter extends OriginAdapter implements Serializable { + use Php73BcSerializableTrait; + /** @var array */ private $args; @@ -26,16 +30,16 @@ public function __construct(string $namespace = '', int $defaultLifetime = 0, ?s /** * @inheritDoc */ - public function serialize() + public function __serialize(): array { - return serialize($this->args); + return $this->args; } /** - * @inheritDoc + * {@inheritDoc} */ - public function unserialize($data) + public function __unserialize($data) { - call_user_func_array([$this, '__construct'], unserialize($data)); // @phpstan-ignore-line + call_user_func_array([$this, '__construct'], $data); // @phpstan-ignore-line } } diff --git a/src/LocalCacheProvider.php b/src/LocalCacheProvider.php index b67cfb8..73fc78f 100644 --- a/src/LocalCacheProvider.php +++ b/src/LocalCacheProvider.php @@ -9,6 +9,7 @@ use Ray\PsrCacheModule\Annotation\CacheNamespace; use Symfony\Component\Cache\Adapter\ApcuAdapter; use Symfony\Component\Cache\Adapter\ChainAdapter; +use Symfony\Component\Cache\Adapter\FilesystemAdapter as SymfonyFilesystemAdapter; use function sys_get_temp_dir; @@ -35,7 +36,7 @@ public function get(): ChainAdapter { return new ChainAdapter([ new ApcuAdapter($this->namespace), - new FilesystemAdapter($this->namespace, 0, $this->cacheDir), + new SymfonyFilesystemAdapter($this->namespace, 0, $this->cacheDir), ]); } } diff --git a/src/Psr6NullModule.php b/src/Psr6NullModule.php new file mode 100644 index 0000000..a10a19b --- /dev/null +++ b/src/Psr6NullModule.php @@ -0,0 +1,21 @@ +bind(CacheItemPoolInterface::class)->annotatedWith(Local::class)->to(NullAdapter::class)->in(Scope::SINGLETON); + $this->bind(CacheItemPoolInterface::class)->annotatedWith(Shared::class)->to(NullAdapter::class)->in(Scope::SINGLETON); + } +} diff --git a/tests/Psr6CacheTest.php b/tests/Psr6CacheTest.php index dca32d7..c56e827 100644 --- a/tests/Psr6CacheTest.php +++ b/tests/Psr6CacheTest.php @@ -5,16 +5,25 @@ namespace Ray\PsrCacheModule; use PHPUnit\Framework\TestCase; -use Ray\Di\AbstractModule; +use Psr\Cache\CacheItemPoolInterface; use Ray\Di\Injector; use Ray\PsrCacheModule\Annotation\CacheDir; +use Ray\PsrCacheModule\Annotation\Shared; +use Symfony\Component\Cache\Adapter\ArrayAdapter; +use Symfony\Component\Cache\Adapter\NullAdapter; class Psr6CacheTest extends TestCase { public function testDevPsrCacheModule(): void { - $module = new Psr6ArrayModule(); - $this->assertInstanceOf(AbstractModule::class, $module); + $cache = (new Injector(new Psr6NullModule()))->getInstance(CacheItemPoolInterface::class, Shared::class); + $this->assertInstanceOf(NullAdapter::class, $cache); + } + + public function testArrayCacheModule(): void + { + $cache = (new Injector(new Psr6ArrayModule()))->getInstance(CacheItemPoolInterface::class, Shared::class); + $this->assertInstanceOf(ArrayAdapter::class, $cache); } public function testCacheDirModule(): void diff --git a/vendor-bin/tools/composer.lock b/vendor-bin/tools/composer.lock index 7757c24..dc46718 100644 --- a/vendor-bin/tools/composer.lock +++ b/vendor-bin/tools/composer.lock @@ -2077,20 +2077,20 @@ }, { "name": "psr/container", - "version": "1.1.1", + "version": "1.1.2", "source": { "type": "git", "url": "https://github.com/php-fig/container.git", - "reference": "8622567409010282b7aeebe4bb841fe98b58dcaf" + "reference": "513e0666f7216c7459170d56df27dfcefe1689ea" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/container/zipball/8622567409010282b7aeebe4bb841fe98b58dcaf", - "reference": "8622567409010282b7aeebe4bb841fe98b58dcaf", + "url": "https://api.github.com/repos/php-fig/container/zipball/513e0666f7216c7459170d56df27dfcefe1689ea", + "reference": "513e0666f7216c7459170d56df27dfcefe1689ea", "shasum": "" }, "require": { - "php": ">=7.2.0" + "php": ">=7.4.0" }, "type": "library", "autoload": { @@ -2119,9 +2119,9 @@ ], "support": { "issues": "https://github.com/php-fig/container/issues", - "source": "https://github.com/php-fig/container/tree/1.1.1" + "source": "https://github.com/php-fig/container/tree/1.1.2" }, - "time": "2021-03-05T17:36:06+00:00" + "time": "2021-11-05T16:50:12+00:00" }, { "name": "psr/log",