Skip to content

Commit

Permalink
Merge pull request #9 from ray-di/psr6-null-module
Browse files Browse the repository at this point in the history
Add Psr6NullModule and deprecate serializable cache component
  • Loading branch information
koriym authored Nov 6, 2021
2 parents 13a5e8b + a0725f6 commit eaa5ad8
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 25 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,7 +24,7 @@
},
"autoload": {
"psr-4": {
"Ray\\PsrCacheModule\\": "src/"
"Ray\\PsrCacheModule\\": ["src/", "src-deprecated"]
}
},
"autoload-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, mixed> */
private $args;

Expand All @@ -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
}
}
30 changes: 30 additions & 0 deletions src-deprecated/Php73BcSerializableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Ray\PsrCacheModule;

use function serialize;
use function unserialize;

trait Php73BcSerializableTrait
{
/**
* {@inheritDoc}
*/
final public function serialize()
{
return serialize($this->__serialize());
}

/**
* @psalm-suppress all
*
* {@inheritDoc}
*/
final public function unserialize($serializedData)
{
$array = unserialize($serializedData);
$this->__unserialize($array); // @phpstan-ignore-line
}
}
14 changes: 9 additions & 5 deletions src/PhpFileAdapter.php → src-deprecated/PhpFileAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ray\PsrCacheModule;

use JetBrains\PhpStorm\Deprecated;
use Serializable;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter as OriginAdapter;

Expand All @@ -12,8 +13,11 @@
use function serialize;
use function unserialize;

#[Deprecated]
class PhpFileAdapter extends OriginAdapter implements Serializable
{
use Php73BcSerializableTrait;

/** @var array<int, mixed> */
private $args;

Expand All @@ -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
}
}
3 changes: 2 additions & 1 deletion src/LocalCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
]);
}
}
21 changes: 21 additions & 0 deletions src/Psr6NullModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Ray\PsrCacheModule;

use Psr\Cache\CacheItemPoolInterface;
use Ray\Di\AbstractModule;
use Ray\Di\Scope;
use Ray\PsrCacheModule\Annotation\Local;
use Ray\PsrCacheModule\Annotation\Shared;
use Symfony\Component\Cache\Adapter\NullAdapter;

final class Psr6NullModule extends AbstractModule
{
protected function configure(): void
{
$this->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);
}
}
15 changes: 12 additions & 3 deletions tests/Psr6CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions vendor-bin/tools/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eaa5ad8

Please sign in to comment.