|
1
|
|
|
<?php |
|
2
|
|
|
namespace midcom\bundle\dependencyInjection; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
5
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
6
|
|
|
use Memcached; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
8
|
|
|
use Symfony\Component\Cache\Adapter\ApcuAdapter; |
|
9
|
|
|
use Symfony\Component\Cache\Adapter\MemcachedAdapter; |
|
10
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
|
11
|
|
|
use Symfony\Component\Cache\Adapter\PdoAdapter; |
|
12
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
|
13
|
|
|
use Symfony\Component\Cache\Adapter\NullAdapter; |
|
14
|
|
|
|
|
15
|
|
|
class cachePass implements CompilerPassInterface |
|
16
|
|
|
{ |
|
17
|
|
|
const NS_PLACEHOLDER = '__NAMESPACE__'; |
|
18
|
|
|
|
|
19
|
|
|
public static function factory(string $name, string $classname, ...$args) : AdapterInterface |
|
20
|
|
|
{ |
|
21
|
|
|
foreach ($args as &$arg) { |
|
22
|
|
|
if ($arg === self::NS_PLACEHOLDER) { |
|
23
|
|
|
$arg = $name . $_SERVER['SERVER_NAME']; |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
return new $classname(...$args); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
public function process(ContainerBuilder $container) |
|
30
|
|
|
{ |
|
31
|
4 |
|
foreach ($container->getParameter('midcom.cache_autoload_queue') as $name) { |
|
32
|
4 |
|
$container->getDefinition('cache') |
|
33
|
4 |
|
->addMethodCall('add_module', [$name, new Reference('cache.module.' . $name)]); |
|
34
|
|
|
|
|
35
|
4 |
|
if (in_array($name, ['nap', 'memcache'])) { |
|
36
|
3 |
|
if ($driver = $container->getParameter('midcom.cache_module_memcache_backend')) { |
|
37
|
3 |
|
$config = $container->getParameter('midcom.cache_module_memcache_backend_config'); |
|
38
|
3 |
|
$this->configure_backend($name, $driver, $config, $container); |
|
39
|
|
|
} |
|
40
|
|
|
} else { |
|
41
|
1 |
|
$config = $container->getParameter('midcom.cache_module_content_backend'); |
|
42
|
1 |
|
if (!empty($config['driver'])) { |
|
43
|
1 |
|
if (!isset($config['directory'])) { |
|
44
|
1 |
|
$config['directory'] = 'content/'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
$this->configure_backend('content', $config['driver'], $config, $container); |
|
48
|
1 |
|
$this->configure_backend('content_data', $config['driver'], $config, $container); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
4 |
|
private function configure_backend(string $name, string $driver, array $config, ContainerBuilder $container) |
|
55
|
|
|
{ |
|
56
|
4 |
|
$backend = $container->getDefinition('cache.module.' . $name . '.backend'); |
|
57
|
4 |
|
$directory = $container->getParameter('kernel.cache_dir'); |
|
58
|
|
|
|
|
59
|
4 |
|
if (!empty($config['directory'])) { |
|
60
|
1 |
|
$directory .= '/' . $config['directory']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
switch ($driver) { |
|
64
|
4 |
|
case 'apc': |
|
65
|
1 |
|
$backend->setArguments([$name, ApcuAdapter::class, self::NS_PLACEHOLDER]); |
|
66
|
1 |
|
break; |
|
67
|
3 |
|
case 'memcached': |
|
68
|
1 |
|
if ($memcached = self::prepare_memcached($config)) { |
|
69
|
|
|
|
|
70
|
1 |
|
$definition = $container->register('cache.memcached.' . $name, \Memcached::class); |
|
71
|
1 |
|
$server = $memcached->getServerList()[0]; |
|
72
|
1 |
|
$definition->addMethodCall('addServer', [$server['host'], $server['port']]); |
|
73
|
|
|
|
|
74
|
1 |
|
$backend->setArguments([$name, MemcachedAdapter::class, $definition, self::NS_PLACEHOLDER]); |
|
75
|
1 |
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
// fall-through |
|
78
|
2 |
|
case 'dba': |
|
79
|
2 |
|
case 'flatfile': |
|
80
|
1 |
|
$backend->setArguments([$name, FilesystemAdapter::class, self::NS_PLACEHOLDER, 0, $directory . '/' . $name]); |
|
81
|
1 |
|
break; |
|
82
|
1 |
|
case 'sqlite': |
|
83
|
1 |
|
$backend->setArguments([$name, PdoAdapter::class, "{$directory}/sqlite.db", self::NS_PLACEHOLDER]); |
|
84
|
1 |
|
break; |
|
85
|
|
|
default: |
|
86
|
|
|
$backend->setArguments([$name, NullAdapter::class]); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public static function prepare_memcached(array $config) : ?Memcached |
|
91
|
|
|
{ |
|
92
|
2 |
|
if (!MemcachedAdapter::isSupported()) { |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
2 |
|
$host = $config['host'] ?? 'localhost'; |
|
96
|
2 |
|
$port = $config['port'] ?? 11211; |
|
97
|
2 |
|
$memcached = new Memcached; |
|
98
|
2 |
|
if (!$memcached->addServer($host, $port)) { |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
return $memcached; |
|
103
|
|
|
} |
|
104
|
|
|
} |