Skip to content

Commit 02e5524

Browse files
committed
Merge pull request #8 from Nyholm/patch-1
Make sure to use good default ports
2 parents 91b8d5e + bfe7c7d commit 02e5524

File tree

1 file changed

+73
-30
lines changed

1 file changed

+73
-30
lines changed

src/DependencyInjection/DoctrineAdapterExtension.php

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ class DoctrineAdapterExtension extends Extension
3535
'memcache' => [
3636
'class' => 'Memcache',
3737
'connect' => 'addServer',
38+
'port' => 11211,
3839
],
3940
'memcached' => [
4041
'class' => 'Cache\Adapter\DoctrineAdapterBundle\ProviderHelper\Memcached',
4142
'connect' => 'addServer',
43+
'port' => 11211,
4244
],
4345
'redis' => [
4446
'class' => 'Redis',
4547
'connect' => 'connect',
48+
'port' => 6379,
4649
],
4750
];
4851

@@ -186,25 +189,10 @@ private function createProviderHelperDefinition($type, array $provider)
186189

187190
// set memcached options first as they need to be set before the servers are added.
188191
if ($type === 'memcached') {
189-
if (!empty($provider['options']['memcached'])) {
190-
foreach ($provider['options']['memcached'] as $option => $value) {
191-
switch ($option) {
192-
case 'serializer':
193-
case 'hash':
194-
case 'distribution':
195-
$value = constant(
196-
sprintf('\Memcached::%s_%s', strtoupper($option), strtoupper($value))
197-
);
198-
break;
199-
}
200-
$helperDefinition->addMethodCall(
201-
'setOption',
202-
[constant(sprintf('\Memcached::OPT_%s', strtoupper($option))), $value]
203-
);
204-
}
205-
}
192+
$provider = $this->setMemcachedOptions($provider, $helperDefinition);
206193
}
207194

195+
$persistentId = null;
208196
if (isset($provider['persistent']) && $provider['persistent'] !== false) {
209197
if ($provider['persistent'] !== true) {
210198
$persistentId = $provider['persistent'];
@@ -219,23 +207,21 @@ private function createProviderHelperDefinition($type, array $provider)
219207
}
220208
}
221209

222-
foreach ($provider['hosts'] as $config) {
210+
// If no host is configured, use localhost and default port
211+
if (empty($provider['hosts'])) {
223212
$arguments = [
224-
'host' => empty($config['host']) ? 'localhost' : $config['host'],
225-
'port' => empty($config['port']) ? 11211 : $config['port'],
213+
'host' => 'localhost',
214+
'port' => self::$types[$type]['port'],
226215
];
227-
if ($type === 'memcached') {
228-
$arguments[] = is_null($config['weight']) ? 0 : $config['weight'];
229-
} else {
230-
$arguments[] = is_null($config['timeout']) ? 0 : $config['timeout'];
231-
if (isset($persistentId)) {
232-
$arguments[] = $persistentId;
233-
}
234-
}
235-
236216
$helperDefinition->addMethodCall(self::$types[$type]['connect'], $arguments);
217+
} else {
218+
// If one or more hosts are configured
219+
foreach ($provider['hosts'] as $config) {
220+
$arguments = $this->getHelperArguments($type, $config, $persistentId);
221+
222+
$helperDefinition->addMethodCall(self::$types[$type]['connect'], $arguments);
223+
}
237224
}
238-
unset($config);
239225

240226
if ($type === 'redis') {
241227
if (isset($provider['auth_password']) && null !== $provider['auth_password']) {
@@ -256,4 +242,61 @@ public function getAlias()
256242
{
257243
return 'cache_adapter_doctrine';
258244
}
245+
246+
/**
247+
* @param array $provider
248+
* @param $helperDefinition
249+
*
250+
* @return array
251+
*/
252+
private function setMemcachedOptions(array $provider, $helperDefinition)
253+
{
254+
if (!empty($provider['options']['memcached'])) {
255+
foreach ($provider['options']['memcached'] as $option => $value) {
256+
switch ($option) {
257+
case 'serializer':
258+
case 'hash':
259+
case 'distribution':
260+
$value = constant(
261+
sprintf('\Memcached::%s_%s', strtoupper($option), strtoupper($value))
262+
);
263+
break;
264+
}
265+
$helperDefinition->addMethodCall(
266+
'setOption',
267+
[constant(sprintf('\Memcached::OPT_%s', strtoupper($option))), $value]
268+
);
269+
}
270+
271+
return $provider;
272+
}
273+
274+
return $provider;
275+
}
276+
277+
/**
278+
* @param $type
279+
* @param $config
280+
* @param $persistentId
281+
*
282+
* @return array
283+
*/
284+
private function getHelperArguments($type, $config, $persistentId = null)
285+
{
286+
$arguments = [
287+
'host' => empty($config['host']) ? 'localhost' : $config['host'],
288+
'port' => empty($config['port']) ? self::$types[$type]['port'] : $config['port'],
289+
];
290+
291+
if ($type === 'memcached') {
292+
$arguments[] = is_null($config['weight']) ? 0 : $config['weight'];
293+
} else {
294+
$arguments[] = is_null($config['timeout']) ? 0 : $config['timeout'];
295+
if ($persistentId !== null) {
296+
$arguments[] = $persistentId;
297+
}
298+
}
299+
300+
return $arguments;
301+
}
259302
}

0 commit comments

Comments
 (0)