Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Psr6NullModule and deprecate serializable cache component #9

Merged
merged 5 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.