|
1
|
|
|
<?php namespace CMSFactory\Services\Cache; |
|
2
|
|
|
|
|
3
|
|
|
use Doctrine\Common\Cache\Cache; |
|
4
|
|
|
use Memcache; |
|
5
|
|
|
use Memcached; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
|
10
|
|
|
|
|
11
|
|
|
class CacheFactory |
|
12
|
|
|
{ |
|
|
|
|
|
|
13
|
|
|
use ContainerAwareTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @return Cache |
|
|
|
|
|
|
17
|
|
|
* @throws ServiceNotFoundException |
|
|
|
|
|
|
18
|
|
|
* @throws InvalidArgumentException |
|
19
|
|
|
* @throws ServiceCircularReferenceException |
|
20
|
|
|
*/ |
|
21
|
|
|
public function createCacheProvider() { |
|
22
|
|
|
|
|
23
|
|
|
foreach ($this->container->getParameter('cache.provider.priority') as $provider) { |
|
24
|
|
|
if ($this->checkRequirements($provider)) { |
|
25
|
|
|
return $this->container->get($provider); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $provider |
|
32
|
|
|
* @return bool |
|
33
|
|
|
* @throws ServiceNotFoundException |
|
|
|
|
|
|
34
|
|
|
* @throws ServiceCircularReferenceException |
|
35
|
|
|
* @throws InvalidArgumentException |
|
36
|
|
|
*/ |
|
37
|
|
|
private function checkRequirements($provider) { |
|
38
|
|
|
|
|
39
|
|
|
switch ($provider) { |
|
40
|
|
|
case 'cache.provider.memcache': |
|
41
|
|
|
return $this->checkMemcache(); |
|
|
|
|
|
|
42
|
|
|
case 'cache.provider.memcached': |
|
43
|
|
|
return $this->checkMemcached(); |
|
|
|
|
|
|
44
|
|
|
case 'cache.provider.filesystem': |
|
45
|
|
|
return $this->checkFilesystem(); |
|
|
|
|
|
|
46
|
|
|
case 'cache.provider.apc': |
|
47
|
|
|
return $this->checkApc(); |
|
|
|
|
|
|
48
|
|
|
default: |
|
49
|
|
|
return false; |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
private function checkApc() { |
|
58
|
|
|
|
|
59
|
|
|
return extension_loaded('apc') && ini_get('apc.enabled'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return bool |
|
64
|
|
|
* @throws ServiceNotFoundException |
|
|
|
|
|
|
65
|
|
|
* @throws InvalidArgumentException |
|
66
|
|
|
* @throws ServiceCircularReferenceException |
|
67
|
|
|
*/ |
|
68
|
|
|
private function checkMemcache() { |
|
69
|
|
|
|
|
70
|
|
|
/** @var Memcache $memcache */ |
|
71
|
|
|
$memcache = $this->container->get('memcache'); |
|
72
|
|
|
$host = $this->container->getParameter('memcached.host'); |
|
73
|
|
|
return extension_loaded('memcache') && class_exists('Memcache') && $memcache->getserverstatus($host) !== 0; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return bool |
|
78
|
|
|
* @throws ServiceNotFoundException |
|
|
|
|
|
|
79
|
|
|
* @throws ServiceCircularReferenceException |
|
80
|
|
|
* @throws InvalidArgumentException |
|
81
|
|
|
*/ |
|
82
|
|
|
private function checkMemcached() { |
|
83
|
|
|
|
|
84
|
|
|
/** @var Memcached $memcached */ |
|
85
|
|
|
$memcached = $this->container->get('memcached'); |
|
86
|
|
|
$host = $this->container->getParameter('memcached.host'); |
|
87
|
|
|
$port = $this->container->getParameter('memcached.port'); |
|
88
|
|
|
|
|
89
|
|
|
$server = "{$host}:{$port}"; |
|
90
|
|
|
$stats = $memcached->getStats(); |
|
91
|
|
|
$connected = array_key_exists($server, $stats) && $stats[$server]['pid'] > 0; |
|
92
|
|
|
|
|
93
|
|
|
return extension_loaded('memcached') && class_exists('Memcached') && $connected; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return bool |
|
98
|
|
|
* @throws InvalidArgumentException |
|
|
|
|
|
|
99
|
|
|
*/ |
|
100
|
|
|
private function checkFilesystem() { |
|
101
|
|
|
|
|
102
|
|
|
$cacheDirectory = $this->container->getParameter('cache.directory'); |
|
103
|
|
|
return file_exists($cacheDirectory) && is_writable($cacheDirectory); |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |