|
1
|
|
|
<?php |
|
2
|
|
|
namespace midcom\dependencyInjection; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
5
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
6
|
|
|
use Doctrine\Common\Cache; |
|
7
|
|
|
use SQLite3; |
|
8
|
|
|
use midcom_services_cache_module_memcache; |
|
9
|
|
|
use midcom_config; |
|
10
|
|
|
|
|
11
|
|
|
class cachePass extends configPass |
|
12
|
|
|
{ |
|
13
|
|
|
private $cachedir; |
|
14
|
|
|
|
|
15
|
4 |
|
public function __construct(midcom_config $config, string $cachedir) |
|
16
|
|
|
{ |
|
17
|
4 |
|
parent::__construct($config); |
|
18
|
4 |
|
$this->cachedir = $cachedir; |
|
19
|
4 |
|
} |
|
20
|
|
|
|
|
21
|
4 |
|
public function process(ContainerBuilder $container) |
|
22
|
|
|
{ |
|
23
|
4 |
|
foreach ($this->config->get('cache_autoload_queue') as $name) { |
|
24
|
4 |
|
$container->getDefinition('cache') |
|
25
|
4 |
|
->addMethodCall('add_module', [$name, new Reference('cache.module.' . $name)]); |
|
26
|
|
|
|
|
27
|
4 |
|
if ($name == 'nap' || $name == 'memcache') { |
|
28
|
3 |
|
if ($driver = $this->config->get('cache_module_memcache_backend')) { |
|
29
|
3 |
|
$config = $this->config->get('cache_module_memcache_backend_config'); |
|
30
|
3 |
|
$this->configure_backend($name, $driver, $config, $container); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
} else { |
|
33
|
1 |
|
$config = $this->config->get('cache_module_content_backend'); |
|
34
|
1 |
|
if (!empty($config['driver'])) { |
|
35
|
1 |
|
if (!isset($config['directory'])) { |
|
36
|
1 |
|
$config['directory'] = 'content/'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
$this->configure_backend('content', $config['driver'], $config, $container); |
|
40
|
1 |
|
$this->configure_backend('content_data', $config['driver'], $config, $container); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
4 |
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
private function configure_backend(string $name, string $driver, array $config, ContainerBuilder $container) |
|
47
|
|
|
{ |
|
48
|
4 |
|
$backend = $container->getDefinition('cache.module.' . $name . '.backend'); |
|
49
|
|
|
|
|
50
|
4 |
|
$directory = $this->cachedir; |
|
51
|
4 |
|
if (!empty($config['directory'])) { |
|
52
|
1 |
|
$directory .= '/' . $config['directory']; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
4 |
|
switch ($driver) { |
|
56
|
4 |
|
case 'apc': |
|
57
|
1 |
|
$backend->setClass(Cache\ApcuCache::class); |
|
58
|
1 |
|
break; |
|
59
|
3 |
|
case 'memcached': |
|
60
|
1 |
|
if ($memcached = midcom_services_cache_module_memcache::prepare_memcached($config)) { |
|
61
|
|
|
|
|
62
|
1 |
|
$definition = $container->register('cache.memcached.' . $name, \Memcached::class); |
|
63
|
1 |
|
$server = $memcached->getServerList()[0]; |
|
64
|
1 |
|
$definition->addMethodCall('addServer', [$server['host'], $server['port']]); |
|
65
|
|
|
|
|
66
|
1 |
|
$backend->setClass(Cache\MemcachedCache::class); |
|
67
|
1 |
|
$backend->addMethodCall('setMemcached', [$definition]); |
|
68
|
1 |
|
break; |
|
69
|
|
|
} |
|
70
|
|
|
// fall-through |
|
71
|
2 |
|
case 'dba': |
|
72
|
2 |
|
case 'flatfile': |
|
73
|
1 |
|
$backend->setClass(Cache\FilesystemCache::class); |
|
74
|
1 |
|
$backend->addArgument($directory . '/' . $name); |
|
75
|
1 |
|
break; |
|
76
|
1 |
|
case 'sqlite': |
|
77
|
1 |
|
$definition = $container->register('cache.sqlite.' . $name, \SQLite3::class); |
|
78
|
1 |
|
$definition->setArguments(["{$directory}/sqlite.db"]); |
|
79
|
|
|
|
|
80
|
1 |
|
$backend->setClass(Cache\SQLite3Cache::class); |
|
81
|
1 |
|
$backend->setArguments([$definition, $name]); |
|
82
|
1 |
|
break; |
|
83
|
|
|
} |
|
84
|
4 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |