1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jellyfish\LockSymfony; |
4
|
|
|
|
5
|
|
|
use Jellyfish\Lock\LockIdentifierGeneratorInterface; |
6
|
|
|
use Pimple\Container; |
7
|
|
|
use Pimple\ServiceProviderInterface; |
8
|
|
|
use Predis\Client; |
9
|
|
|
use Symfony\Component\Lock\Factory as SymfonyLockFactory; |
10
|
|
|
use Symfony\Component\Lock\Store\RedisStore; |
11
|
|
|
use Symfony\Component\Lock\StoreInterface; |
12
|
|
|
|
13
|
|
|
class LockSymfonyServiceProvider implements ServiceProviderInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param \Pimple\Container $pimple |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function register(Container $pimple): void |
21
|
|
|
{ |
22
|
|
|
$this->createLockFactory($pimple); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param \Pimple\Container $container |
27
|
|
|
* |
28
|
|
|
* @return \Pimple\ServiceProviderInterface |
29
|
|
|
*/ |
30
|
|
|
protected function createLockFactory(Container $container): ServiceProviderInterface |
31
|
|
|
{ |
32
|
|
|
$self = $this; |
33
|
|
|
|
34
|
|
|
$container->offsetSet('lock_factory', function (Container $container) use ($self) { |
35
|
|
|
return new LockFactory( |
36
|
|
|
$self->createSymfonyLockFactory($container), |
37
|
|
|
$self->createLockIdentifierGenerator() |
38
|
|
|
); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \Pimple\Container $container |
46
|
|
|
* |
47
|
|
|
* @return \Symfony\Component\Lock\Factory |
48
|
|
|
*/ |
49
|
|
|
protected function createSymfonyLockFactory(Container $container): SymfonyLockFactory |
50
|
|
|
{ |
51
|
|
|
$redisClient = $this->createRedisClient($container); |
52
|
|
|
$redisStore = $this->createRedisStore($redisClient); |
53
|
|
|
|
54
|
|
|
return new SymfonyLockFactory($redisStore); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \Predis\Client $redisClient |
59
|
|
|
* |
60
|
|
|
* @return \Symfony\Component\Lock\StoreInterface |
61
|
|
|
*/ |
62
|
|
|
protected function createRedisStore(Client $redisClient): StoreInterface |
63
|
|
|
{ |
64
|
|
|
return new RedisStore($redisClient); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param \Pimple\Container $container |
69
|
|
|
* |
70
|
|
|
* @return \Predis\Client |
71
|
|
|
*/ |
72
|
|
|
protected function createRedisClient(Container $container): Client |
73
|
|
|
{ |
74
|
|
|
$config = $container->offsetGet('config'); |
75
|
|
|
|
76
|
|
|
return new Client([ |
77
|
|
|
'scheme' => 'tcp', |
78
|
|
|
'host' => $config->get(LockConstants::REDIS_STORE_HOST, LockConstants::DEFAULT_REDIS_STORE_HOST), |
79
|
|
|
'port' => $config->get(LockConstants::REDIS_STORE_PORT, LockConstants::DEFAULT_REDIS_STORE_PORT), |
80
|
|
|
'database' => $config->get(LockConstants::REDIS_STORE_DB, LockConstants::DEFAULT_REDIS_STORE_DB), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return \Jellyfish\Lock\LockIdentifierGeneratorInterface |
86
|
|
|
*/ |
87
|
|
|
protected function createLockIdentifierGenerator(): LockIdentifierGeneratorInterface |
88
|
|
|
{ |
89
|
|
|
return new LockIdentifierGenerator(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|