Skip to content

Commit d913e2d

Browse files
committed
Init
1 parent 61874d7 commit d913e2d

File tree

5 files changed

+154
-126
lines changed

5 files changed

+154
-126
lines changed

src/Cache/Memcached.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Cache\DoctrineCacheBundle\Cache;
4+
5+
/**
6+
* Class Memcached
7+
*
8+
* @author Aaron Scherer <[email protected]>
9+
*/
10+
class Memcached extends \Memcached
11+
{
12+
/**
13+
* {@inheritDoc}
14+
*/
15+
public function addServer($host, $port, $weight = 0)
16+
{
17+
$serverList = $this->getServerList();
18+
foreach ($serverList as $server) {
19+
if ($server['host'] === $host && $server['port'] === $port) {
20+
return false;
21+
}
22+
}
23+
24+
return parent::addServer($host, $port, $weight);
25+
}
26+
}

src/DependencyInjection/CompilerPass/ServiceBuilderPass.php

Lines changed: 113 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -11,202 +11,201 @@
1111
class ServiceBuilderPass implements CompilerPassInterface
1212
{
1313
/**
14-
* Array of types, and their options
14+
* Array of types, and their options.
1515
*
16-
* @var array $types
16+
* @var array
1717
*/
1818
protected static $types = [
19-
'memcache' => [
20-
'class' => 'Memcache',
21-
'connect' => 'addServer'
19+
'memcache' => [
20+
'class' => 'Memcache',
21+
'connect' => 'addServer',
2222
],
2323
'memcached' => [
24-
'class' => 'Aequasi\Bundle\CacheBundle\Cache\Memcached',
25-
'connect' => 'addServer'
24+
'class' => 'Cache\DoctrineCacheBundle\Cache\Memcached',
25+
'connect' => 'addServer',
26+
],
27+
'redis' => [
28+
'class' => 'Redis',
29+
'connect' => 'connect',
2630
],
27-
'redis' => [
28-
'class' => 'Redis',
29-
'connect' => 'connect'
30-
]
3131
];
3232

3333
/**
34-
*For each configured instance, build a service
34+
* For each configured provider, build a service.
3535
*
3636
* @param ContainerBuilder $container
3737
*/
3838
public function process(ContainerBuilder $container)
3939
{
40-
$instances = $container->getParameter('doctrine_cache.instance');
40+
$providers = $container->getParameter('doctrine_cache.providers');
4141

42-
foreach ($instances as $name => $instance) {
43-
$typeId = 'doctrine_cache.abstract.'.$instance['type'];
44-
if (!$container->findDefinition($typeId)) {
42+
foreach ($providers as $name => $provider) {
43+
$typeServiceId = 'doctrine_cache.abstract.'.$provider['type'];
44+
if (!$container->hasDefinition($typeServiceId)) {
4545
throw new InvalidConfigurationException(
4646
sprintf(
47-
"`%s` is not a valid cache type. If you are using a custom type, make sure to add your service. ",
48-
$instance['type']
47+
'`%s` is not a valid cache type. If you are using a custom type, make sure to add your service. ',
48+
$provider['type']
4949
)
5050
);
5151
}
5252

53-
$service = $this->buildService($container, $typeId, $name, $instance);
54-
$this->prepareCacheClass($container, $service, $name, $instance);
53+
$this->prepareDoctrineCacheClass($container, $typeServiceId, $name, $provider);
54+
$this->createPsr7CompliantService($container, $typeServiceId, $name);
5555
}
5656
}
5757

5858
/**
59+
* Make sure to create a PRS-6 service that wrapps the doctrine service.
60+
*
5961
* @param string $typeId
6062
* @param string $name
61-
* @param array $instance
63+
* @param array $provider
6264
*
6365
* @return Definition
6466
*/
65-
private function buildService(ContainerBuilder $container, $typeId, $name, array $instance)
67+
private function createPsr7CompliantService(ContainerBuilder $container, $typeServiceId, $name)
6668
{
67-
$namespace = is_null($instance['namespace']) ? $name : $instance['namespace'];
68-
$serviceId = 'doctrine_cache.instance.'.$name;
69-
70-
// Modify the core doctrine cache class
71-
$doctrine = $container->getDefinition($typeId);
72-
$doctrine->addMethodCall('setNamespace', [$namespace])
73-
->setPublic(false);
69+
// This is the service id for the PSR6 provider. This is the one that we use.
70+
$serviceId = 'doctrine_cache.provider.'.$name;
7471

7572
// Register the CacheItemPoolInterface definition
7673
$def = $container->setDefinition(
7774
$serviceId,
7875
\Cache\Doctrine\CachePoolItem::class
7976
);
80-
$def->addArgument(0, new Reference($typeId));
77+
$def->addArgument(0, new Reference($typeServiceId));
8178

82-
//TODO add alias
83-
84-
return $doctrine;
79+
//TODO add alias ??
8580
}
8681

8782
/**
88-
* We need to prepare the doctrine cache providers
83+
* We need to prepare the doctrine cache providers.
8984
*
9085
* @param Definition $service
9186
* @param string $name
92-
* @param array $instance
93-
*
94-
* @return Boolean
87+
* @param array $provider
9588
*/
96-
private function prepareCacheClass(ContainerBuilder $container, Definition $service, $name, array $instance)
89+
private function prepareDoctrineCacheClass(ContainerBuilder $container, $typeServiceId, $name, array $provider)
9790
{
98-
$type = $instance['type'];
99-
$id = sprintf("doctrine_cache.instance.%s.cache_instance", $name);
91+
$namespace = is_null($provider['namespace']) ? $name : $provider['namespace'];
92+
93+
// Modify the core doctrine cache class
94+
$service = $container->getDefinition($typeServiceId);
95+
$service->addMethodCall('setNamespace', [$namespace])
96+
->setPublic(false);
97+
98+
$type = $provider['type'];
10099
switch ($type) {
101100
case 'memcache':
102101
case 'memcached':
103102
case 'redis':
104-
return $this->createCacheInstance($container, $service, $type, $id, $instance);
103+
if (!empty($provider['id'])) {
104+
$cacheProviderServiceId = $provider['id'];
105+
} else {
106+
// Create a new cache provider if none is defined
107+
$cacheProviderServiceId = sprintf('doctrine_cache.provider.%s.cache_provider', $name);
108+
$cacheProviderDefinition = $this->createCacheProviderDefinition($type, $provider);
109+
$container->setDefinition($cacheProviderServiceId, $cacheProviderDefinition);
110+
}
111+
112+
$service->addMethodCall(sprintf('set%s', ucwords($type)), [new Reference($cacheProviderServiceId)]);
113+
114+
break;
105115
case 'file_system':
106116
case 'php_file':
107117
$directory = '%kernel.cache_dir%/doctrine/cache';
108-
if (null !== $instance['directory']) {
109-
$directory = $instance['directory'];
118+
if (null !== $provider['directory']) {
119+
$directory = $provider['directory'];
110120
}
111-
$extension = is_null($instance['extension']) ? null : $instance['extension'];
121+
$extension = is_null($provider['extension']) ? null : $provider['extension'];
112122

113123
$service->setArguments([$directory, $extension]);
114124

115-
return true;
125+
break;
116126
case 'mongo':
117127
case 'sqlite3':
118128
case 'sqlite':
119129
case 'riak':
120130
case 'chain':
121-
return false;
122-
default:
123-
return true;
131+
break;
124132
}
125133
}
126134

127135
/**
128-
* Creates a cache instance
136+
* Creates a provider to the Doctrine cache provider.
129137
*
130-
* @param Definition $service
131-
* @param string $type
132-
* @param string $id
133-
* @param array $instance
138+
* @param $type
139+
* @param array $provider
134140
*
135-
* @return Boolean
141+
* @return Definition
136142
*/
137-
public function createCacheInstance(ContainerBuilder $container, Definition $service, $type, $id, array $instance)
143+
public function createCacheProviderDefinition($type, array $provider)
138144
{
139-
if (empty($instance['id'])) {
140-
$cache = new Definition(self::$types[$type]['class']);
141-
142-
// set memcached options first as they need to be set before the servers are added.
143-
if ($type === 'memcached') {
144-
if (!empty($instance['options']['memcached'])) {
145-
foreach ($instance['options']['memcached'] as $option => $value) {
146-
switch ($option) {
147-
case 'serializer':
148-
case 'hash':
149-
case 'distribution':
150-
$value = constant(
151-
sprintf('\Memcached::%s_%s', strtoupper($option), strtoupper($value))
152-
);
153-
break;
154-
}
155-
$cache->addMethodCall(
156-
'setOption',
157-
[constant(sprintf('\Memcached::OPT_%s', strtoupper($option))), $value]
158-
);
145+
$cache = new Definition(self::$types[$type]['class']);
146+
147+
// set memcached options first as they need to be set before the servers are added.
148+
if ($type === 'memcached') {
149+
if (!empty($provider['options']['memcached'])) {
150+
foreach ($provider['options']['memcached'] as $option => $value) {
151+
switch ($option) {
152+
case 'serializer':
153+
case 'hash':
154+
case 'distribution':
155+
$value = constant(
156+
sprintf('\Memcached::%s_%s', strtoupper($option), strtoupper($value))
157+
);
158+
break;
159159
}
160+
$cache->addMethodCall(
161+
'setOption',
162+
[constant(sprintf('\Memcached::OPT_%s', strtoupper($option))), $value]
163+
);
160164
}
161165
}
166+
}
162167

163-
if (isset($instance['persistent']) && $instance['persistent'] !== false) {
164-
if ($instance['persistent'] !== true) {
165-
$persistentId = $instance['persistent'];
166-
} else {
167-
$persistentId = substr(md5(serialize($instance['hosts'])), 0, 5);
168-
}
169-
if ($type === 'memcached') {
170-
$cache->setArguments([$persistentId]);
171-
}
172-
if ($type === 'redis') {
173-
self::$types[$type]['connect'] = 'pconnect';
174-
}
168+
if (isset($provider['persistent']) && $provider['persistent'] !== false) {
169+
if ($provider['persistent'] !== true) {
170+
$persistentId = $provider['persistent'];
171+
} else {
172+
$persistentId = substr(md5(serialize($provider['hosts'])), 0, 5);
173+
}
174+
if ($type === 'memcached') {
175+
$cache->setArguments([$persistentId]);
175176
}
177+
if ($type === 'redis') {
178+
self::$types[$type]['connect'] = 'pconnect';
179+
}
180+
}
176181

177-
foreach ($instance['hosts'] as $config) {
178-
$arguments = [
182+
foreach ($provider['hosts'] as $config) {
183+
$arguments = [
179184
'host' => empty($config['host']) ? 'localhost' : $config['host'],
180-
'port' => empty($config['port']) ? 11211 : $config['port']
185+
'port' => empty($config['port']) ? 11211 : $config['port'],
181186
];
182-
if ($type === 'memcached') {
183-
$arguments[] = is_null($config['weight']) ? 0 : $config['weight'];
184-
} else {
185-
$arguments[] = is_null($config['timeout']) ? 0 : $config['timeout'];
186-
if (isset($persistentId)) {
187-
$arguments[] = $persistentId;
188-
}
187+
if ($type === 'memcached') {
188+
$arguments[] = is_null($config['weight']) ? 0 : $config['weight'];
189+
} else {
190+
$arguments[] = is_null($config['timeout']) ? 0 : $config['timeout'];
191+
if (isset($persistentId)) {
192+
$arguments[] = $persistentId;
189193
}
190-
191-
$cache->addMethodCall(self::$types[$type]['connect'], $arguments);
192194
}
193-
unset($config);
194195

195-
if ($type === 'redis') {
196-
if (isset($instance['auth_password']) && null !== $instance['auth_password']) {
197-
$cache->addMethodCall('auth', [$instance['auth_password']]);
198-
}
199-
if (isset($instance['database'])) {
200-
$cache->addMethodCall('select', [$instance['database']]);
201-
}
202-
}
196+
$cache->addMethodCall(self::$types[$type]['connect'], $arguments);
197+
}
198+
unset($config);
203199

204-
$container->setDefinition($id, $cache);
205-
} else {
206-
$id = $instance['id'];
200+
if ($type === 'redis') {
201+
if (isset($provider['auth_password']) && null !== $provider['auth_password']) {
202+
$cache->addMethodCall('auth', [$provider['auth_password']]);
203+
}
204+
if (isset($provider['database'])) {
205+
$cache->addMethodCall('select', [$provider['database']]);
206+
}
207207
}
208-
$service->addMethodCall(sprintf('set%s', ucwords($type)), [new Reference($id)]);
209208

210-
return true;
209+
return $cache;
211210
}
212-
}
211+
}

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getConfigTreeBuilder()
5252
private function getClustersNode()
5353
{
5454
$treeBuilder = new TreeBuilder();
55-
$node = $treeBuilder->root('instances');
55+
$node = $treeBuilder->root('providers');
5656

5757
$node
5858
->requiresAtLeastOneElement()

src/DependencyInjection/DoctrineCacheExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ class DoctrineCacheExtension extends Extension
2323
public function load(array $configs, ContainerBuilder $container)
2424
{
2525
$configuration = new Configuration($container->getParameter('kernel.debug'));
26-
$this->processConfiguration($configuration, $configs);
26+
$config = $this->processConfiguration($configuration, $configs);
2727

2828
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
2929
$loader->load('services.yml');
30+
31+
$container->setParameter('doctrine_cache.providers', $config['providers']);
32+
3033
}
3134
}

0 commit comments

Comments
 (0)