1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\QueryRepository; |
6
|
|
|
|
7
|
|
|
use BEAR\RepositoryModule\Annotation\RedisDsn; |
8
|
|
|
use BEAR\RepositoryModule\Annotation\RedisDsnOptions; |
9
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
10
|
|
|
use Ray\Di\AbstractModule; |
11
|
|
|
use Ray\Di\ProviderInterface; |
12
|
|
|
use Ray\Di\Scope; |
13
|
|
|
use Ray\PsrCacheModule\Annotation\CacheNamespace; |
14
|
|
|
use Ray\PsrCacheModule\Annotation\Local; |
15
|
|
|
use Ray\PsrCacheModule\Annotation\Shared; |
16
|
|
|
use Ray\PsrCacheModule\ApcuAdapter; |
17
|
|
|
use Ray\PsrCacheModule\RedisAdapter; |
18
|
|
|
use ReflectionException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Provides ResourceStorageInterface and derived bindings |
22
|
|
|
* * |
23
|
|
|
* * The following bindings are provided: |
24
|
|
|
* * |
25
|
|
|
* * CacheItemPoolInterface-EtagPool::class |
26
|
|
|
* * |
27
|
|
|
* * The following module are installed: |
28
|
|
|
* * |
29
|
|
|
* * Psr6RedisModule |
30
|
|
|
* Provides CacheItemPool and derived bindings |
31
|
|
|
* |
32
|
|
|
* [...] |
33
|
|
|
* |
34
|
|
|
* The following bindings are provided: |
35
|
|
|
* |
36
|
|
|
* ::RedisDsn |
37
|
|
|
* CacheItemPoolInterface::Local |
38
|
|
|
* CacheItemPoolInterface::Shared |
39
|
|
|
*/ |
40
|
|
|
final class StorageRedisDsnModule extends AbstractModule |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* Redis configuration DSN |
44
|
|
|
* |
45
|
|
|
* @param string $dsn Redis DSN |
46
|
|
|
* @param array<string, bool|int|string|array<string, mixed>|null> $options Redis DSN Options |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
private readonly string $dsn, |
50
|
|
|
private readonly array $options = [], |
51
|
|
|
AbstractModule|null $module = null, |
52
|
|
|
) { |
53
|
|
|
parent::__construct($module); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @throws ReflectionException */ |
57
|
|
|
protected function configure(): void |
58
|
|
|
{ |
59
|
|
|
$this->bind()->annotatedWith(RedisDsn::class)->toInstance($this->dsn); |
60
|
|
|
$this->bind()->annotatedWith(RedisDsnOptions::class)->toInstance($this->options); |
61
|
|
|
$this->bind(CacheItemPoolInterface::class)->annotatedWith(Local::class)->toConstructor(ApcuAdapter::class, ['namespace' => CacheNamespace::class])->in(Scope::SINGLETON); |
62
|
|
|
$this->bind(CacheItemPoolInterface::class)->annotatedWith(Shared::class)->toConstructor(RedisAdapter::class, [ |
63
|
|
|
'redisProvider' => 'redis', |
64
|
|
|
'namespace' => CacheNamespace::class, |
65
|
|
|
]); |
66
|
|
|
$this->bind(ProviderInterface::class)->annotatedWith('redis')->to(RedisDsnProvider::class); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|