|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) Beau Simensen <[email protected]> (https://github.com/dflydev/dflydev-doctrine-orm-service-provider) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Chubbyphp\ServiceProvider; |
|
10
|
|
|
|
|
11
|
|
|
use Doctrine\Common\Cache\ApcuCache; |
|
12
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
13
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
|
14
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
|
15
|
|
|
use Doctrine\Common\Cache\MemcachedCache; |
|
16
|
|
|
use Doctrine\Common\Cache\XcacheCache; |
|
17
|
|
|
use Doctrine\Common\Cache\RedisCache; |
|
18
|
|
|
use Pimple\Container; |
|
19
|
|
|
use Pimple\ServiceProviderInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class DoctrineCacheServiceProvider implements ServiceProviderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Register ORM service. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Container $container |
|
27
|
|
|
*/ |
|
28
|
|
|
public function register(Container $container) |
|
29
|
|
|
{ |
|
30
|
|
|
$container['doctrine.cache.locator'] = $this->getOrmCacheLocatorDefinition($container); |
|
31
|
|
|
$container['doctrine.cache.factory'] = $this->getOrmCacheFactoryDefinition($container); |
|
32
|
|
|
$container['doctrine.cache.factory.apcu'] = $this->getOrmCacheFactoryApcuDefinition($container); |
|
33
|
|
|
$container['doctrine.cache.factory.array'] = $this->getOrmCacheFactoryArrayDefinition($container); |
|
34
|
|
|
$container['doctrine.cache.factory.filesystem'] = $this->getOrmCacheFactoryFilesystemDefinition($container); |
|
35
|
|
|
$container['doctrine.cache.factory.memcached'] = $this->getOrmCacheFactoryMemcachedDefinition($container); |
|
36
|
|
|
$container['doctrine.cache.factory.redis'] = $this->getOrmCacheFactoryRedisDefinition($container); |
|
37
|
|
|
$container['doctrine.cache.factory.xcache'] = $this->getOrmCacheFactoryXCacheDefinition($container); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Container $container |
|
42
|
|
|
* |
|
43
|
|
|
* @return callable |
|
44
|
|
|
*/ |
|
45
|
|
|
private function getOrmCacheLocatorDefinition(Container $container): callable |
|
46
|
|
|
{ |
|
47
|
|
|
return $container->protect(function (string $key, array $options) use ($container) { |
|
48
|
|
|
$cacheInstanceKey = 'orm.cache.instances.'.$key; |
|
49
|
|
|
if (isset($container[$cacheInstanceKey])) { |
|
50
|
|
|
return $container[$cacheInstanceKey]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (!isset($options['driver'])) { |
|
54
|
|
|
throw new \RuntimeException("No driver specified for '$key'"); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$driver = $options['driver']; |
|
58
|
|
|
|
|
59
|
|
|
/** @var CacheProvider $cache */ |
|
60
|
|
|
$cache = $container['doctrine.cache.factory']($driver, $options); |
|
61
|
|
|
|
|
62
|
|
|
if (isset($options['cache_namespace'])) { |
|
63
|
|
|
$cache->setNamespace($options['cache_namespace']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $container[$cacheInstanceKey] = $cache; |
|
67
|
|
|
}); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param Container $container |
|
72
|
|
|
* |
|
73
|
|
|
* @return callable |
|
74
|
|
|
*/ |
|
75
|
|
|
private function getOrmCacheFactoryDefinition(Container $container): callable |
|
76
|
|
|
{ |
|
77
|
|
|
return $container->protect(function (string $driver, array $options) use ($container) { |
|
78
|
|
|
$cacheFactoryKey = 'doctrine.cache.factory.'.$driver; |
|
79
|
|
|
if (!isset($container[$cacheFactoryKey])) { |
|
80
|
|
|
throw new \RuntimeException( |
|
81
|
|
|
sprintf('Factory "%s" for cache type "%s" not defined', $cacheFactoryKey, $driver) |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $container[$cacheFactoryKey]($options); |
|
86
|
|
|
}); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Container $container |
|
91
|
|
|
* |
|
92
|
|
|
* @return callable |
|
93
|
|
|
*/ |
|
94
|
|
|
private function getOrmCacheFactoryApcuDefinition(Container $container): callable |
|
95
|
|
|
{ |
|
96
|
|
|
return $container->protect(function () use ($container) { |
|
97
|
|
|
return new ApcuCache(); |
|
98
|
|
|
}); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Container $container |
|
103
|
|
|
* |
|
104
|
|
|
* @return callable |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getOrmCacheFactoryArrayDefinition(Container $container): callable |
|
107
|
|
|
{ |
|
108
|
|
|
return $container->protect(function () use ($container) { |
|
109
|
|
|
return new ArrayCache(); |
|
110
|
|
|
}); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param Container $container |
|
115
|
|
|
* |
|
116
|
|
|
* @return callable |
|
117
|
|
|
*/ |
|
118
|
|
|
private function getOrmCacheFactoryFilesystemDefinition(Container $container): callable |
|
119
|
|
|
{ |
|
120
|
|
|
return $container->protect(function (array $options) { |
|
121
|
|
|
if (empty($options['path'])) { |
|
122
|
|
|
throw new \RuntimeException('FilesystemCache path not defined'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$options += [ |
|
126
|
|
|
'extension' => FilesystemCache::EXTENSION, |
|
127
|
|
|
'umask' => 0002, |
|
128
|
|
|
]; |
|
129
|
|
|
|
|
130
|
|
|
return new FilesystemCache($options['path'], $options['extension'], $options['umask']); |
|
131
|
|
|
}); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param Container $container |
|
136
|
|
|
* |
|
137
|
|
|
* @return callable |
|
138
|
|
|
*/ |
|
139
|
|
|
private function getOrmCacheFactoryMemcachedDefinition(Container $container): callable |
|
140
|
|
|
{ |
|
141
|
|
|
return $container->protect(function (array $options) use ($container) { |
|
142
|
|
|
if (empty($options['host']) || empty($options['port'])) { |
|
143
|
|
|
throw new \RuntimeException('Host and port options need to be specified for memcached cache'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$memcached = new \Memcached(); |
|
147
|
|
|
$memcached->addServer($options['host'], $options['port']); |
|
148
|
|
|
|
|
149
|
|
|
$cache = new MemcachedCache(); |
|
150
|
|
|
$cache->setMemcached($memcached); |
|
151
|
|
|
|
|
152
|
|
|
return $cache; |
|
153
|
|
|
}); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param Container $container |
|
158
|
|
|
* |
|
159
|
|
|
* @return callable |
|
160
|
|
|
*/ |
|
161
|
|
|
private function getOrmCacheFactoryRedisDefinition(Container $container): callable |
|
162
|
|
|
{ |
|
163
|
|
|
return $container->protect(function (array $options) use ($container) { |
|
164
|
|
|
if (empty($options['host']) || empty($options['port'])) { |
|
165
|
|
|
throw new \RuntimeException('Host and port options need to be specified for redis cache'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$redis = new \Redis(); |
|
169
|
|
|
$redis->connect($options['host'], $options['port']); |
|
170
|
|
|
|
|
171
|
|
|
if (isset($options['password'])) { |
|
172
|
|
|
$redis->auth($options['password']); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$cache = new RedisCache(); |
|
176
|
|
|
$cache->setRedis($redis); |
|
177
|
|
|
|
|
178
|
|
|
return $cache; |
|
179
|
|
|
}); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param Container $container |
|
184
|
|
|
* |
|
185
|
|
|
* @return callable |
|
186
|
|
|
*/ |
|
187
|
|
|
private function getOrmCacheFactoryXCacheDefinition(Container $container): callable |
|
188
|
|
|
{ |
|
189
|
|
|
return $container->protect(function () use ($container) { |
|
190
|
|
|
return new XcacheCache(); |
|
191
|
|
|
}); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|