1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inspirum\Mcrouter\Providers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Cache\CacheManager; |
6
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository; |
7
|
|
|
use Illuminate\Contracts\Foundation\Application; |
8
|
|
|
use Illuminate\Session\SessionManager; |
9
|
|
|
use Illuminate\Support\ServiceProvider as LaravelServiceProvider; |
10
|
|
|
use Inspirum\Mcrouter\Model\Values\Mcrouter; |
11
|
|
|
use Inspirum\Mcrouter\Services\MemcachedSessionHandler; |
12
|
|
|
use Inspirum\Mcrouter\Services\MemcachedStore; |
13
|
|
|
|
14
|
|
|
class McrouterServiceProvider extends LaravelServiceProvider |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Register the service provider. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function register() |
22
|
|
|
{ |
23
|
|
|
$configFile = __DIR__ . '/../../config/mcrouter.php'; |
24
|
|
|
|
25
|
|
|
$this->mergeConfigFrom($configFile, 'mcrouter'); |
26
|
|
|
$this->publishes([$configFile => config_path('mcrouter.php')], 'config'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Register the service provider. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function boot() |
35
|
|
|
{ |
36
|
|
|
$this->registerCustomMemcachedStore(); |
37
|
|
|
|
38
|
|
|
$this->registerCustomMemcachedSessionHandler(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Register custom memcached cache repository |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
private function registerCustomMemcachedStore(): void |
47
|
|
|
{ |
48
|
|
|
// get Mcrouter configuration |
49
|
|
|
$mcrouter = $this->getMcrouterConfig(); |
50
|
|
|
|
51
|
|
|
// extend to custom memcached store |
52
|
|
|
$this->getCacheManager()->extend('memcached', function (Application $app, array $config) use ($mcrouter) { |
53
|
|
|
// connect memcached |
54
|
|
|
/** @var \Illuminate\Cache\MemcachedConnector $connector */ |
55
|
|
|
$connector = $app['memcached.connector']; |
56
|
|
|
$memcached = $connector->connect( |
57
|
|
|
$config['servers'], |
58
|
|
|
$config['persistent_id'] ?? null, |
59
|
|
|
$config['options'] ?? [], |
60
|
|
|
array_filter($config['sasl'] ?? []) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
// get cache prefix |
64
|
|
|
/** @var \Illuminate\Contracts\Config\Repository $configRepository */ |
65
|
|
|
$configRepository = $app['config']; |
66
|
|
|
$prefix = $config['prefix'] ?? $configRepository->get('cache.prefix'); |
67
|
|
|
|
68
|
|
|
// register memcached store |
69
|
|
|
/** @var \Illuminate\Cache\CacheManager $cacheManager */ |
70
|
|
|
$cacheManager = $app['cache']; |
71
|
|
|
|
72
|
|
|
return $cacheManager->repository(new MemcachedStore($memcached, $prefix, $mcrouter)); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Register custom memcached session driver |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
private function registerCustomMemcachedSessionHandler(): void |
82
|
|
|
{ |
83
|
|
|
// get Mcrouter configuration |
84
|
|
|
$mcrouter = $this->getMcrouterConfig(); |
85
|
|
|
|
86
|
|
|
// extend to custom memcached session handler |
87
|
|
|
$this->getSessionManager()->extend('memcached', function (Application $app) use ($mcrouter) { |
88
|
|
|
/** @var \Illuminate\Cache\CacheManager $cacheManager */ |
89
|
|
|
$cacheManager = $app['cache']; |
90
|
|
|
|
91
|
|
|
/** @var \Illuminate\Contracts\Config\Repository $configRepository */ |
92
|
|
|
$configRepository = $app['config']; |
93
|
|
|
|
94
|
|
|
return new MemcachedSessionHandler( |
95
|
|
|
clone $cacheManager->store('memcached'), |
96
|
|
|
$configRepository->get('session.lifetime'), |
97
|
|
|
$mcrouter |
98
|
|
|
); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get Mcrouter configuration |
104
|
|
|
* |
105
|
|
|
* @return \Inspirum\Mcrouter\Model\Values\Mcrouter |
106
|
|
|
*/ |
107
|
|
|
private function getMcrouterConfig(): Mcrouter |
108
|
|
|
{ |
109
|
|
|
$configRepository = $this->getConfigRepository(); |
110
|
|
|
|
111
|
|
|
return new Mcrouter( |
112
|
|
|
$configRepository->get('mcrouter.shared_prefix') ?? Mcrouter::SHARED_PREFIX, |
113
|
|
|
$configRepository->get('mcrouter.prefixes') ?? [] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get cache manager |
119
|
|
|
* |
120
|
|
|
* @return \Illuminate\Cache\CacheManager |
121
|
|
|
*/ |
122
|
|
|
private function getCacheManager(): CacheManager |
123
|
|
|
{ |
124
|
|
|
return $this->app['cache']; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get config repository |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Contracts\Config\Repository |
131
|
|
|
*/ |
132
|
|
|
private function getConfigRepository(): ConfigRepository |
133
|
|
|
{ |
134
|
|
|
return $this->app['config']; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get Session Manager |
139
|
|
|
* |
140
|
|
|
* @return \Illuminate\Session\SessionManager |
141
|
|
|
*/ |
142
|
|
|
private function getSessionManager(): SessionManager |
143
|
|
|
{ |
144
|
|
|
return $this->app['session']; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|