1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW\Core\AppSystems; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
class Memcached extends AbstractSystem |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @const ERR_MEMCACHED_NOT_CLASS_DEFINED Exception code if memcache(d) is |
11
|
|
|
* enabled but the class to use is not defined. |
12
|
|
|
*/ |
13
|
|
|
const ERR_MEMCACHED_NOT_CLASS_DEFINED = 1507001; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @const ERR_MEMCACHED_CLASS_NOT_FOUND Exception code if the memcache(d) |
17
|
|
|
* class is not found. |
18
|
|
|
*/ |
19
|
|
|
const ERR_MEMCACHED_CLASS_NOT_FOUND = 1507002; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @const ERR_MEMCACHED_NOT_IMPLEMENT_INTERFACE Exception code the |
23
|
|
|
* memcache(d) class not implement the interface. |
24
|
|
|
*/ |
25
|
|
|
const ERR_MEMCACHED_NOT_IMPLEMENT_INTERFACE = 1507003; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \BFW\Memcache\MemcacheInterface|null $memcached The class used |
29
|
|
|
* to connect to memcache(d) server. |
30
|
|
|
* The class name should be declared into config file. |
31
|
|
|
*/ |
32
|
|
|
protected $memcached; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function __invoke() |
38
|
|
|
{ |
39
|
|
|
return $this->memcached; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Getter accessor to property memcached |
44
|
|
|
* |
45
|
|
|
* @return \BFW\Memcache\MemcacheInterface|null |
46
|
|
|
*/ |
47
|
|
|
public function getMemcached() |
48
|
|
|
{ |
49
|
|
|
return $this->memcached; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
* Load and initialize le memcached object |
55
|
|
|
*/ |
56
|
|
|
public function init() |
57
|
|
|
{ |
58
|
|
|
$this->loadMemcached(); |
59
|
|
|
$this->initStatus = true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Connect to memcache(d) server with the class declared in config file |
64
|
|
|
* |
65
|
|
|
* @return Object |
66
|
|
|
* |
67
|
|
|
* @throws Exception If memcached is enabled but no class is define. Or if |
68
|
|
|
* The class declared into the config is not found. |
69
|
|
|
*/ |
70
|
|
|
protected function loadMemcached() |
71
|
|
|
{ |
72
|
|
|
$memcachedConfig = \BFW\Application::getInstance() |
|
|
|
|
73
|
|
|
->getConfig() |
74
|
|
|
->getValue('memcached', 'memcached.php') |
75
|
|
|
; |
76
|
|
|
|
77
|
|
|
if ($memcachedConfig['enabled'] === false) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$class = $this->obtainMemcachedClassName($memcachedConfig); |
82
|
|
|
$this->memcached = new $class; |
83
|
|
|
|
84
|
|
|
if (!($this->memcached instanceof \BFW\Memcache\MemcacheInterface)) { |
85
|
|
|
throw new Exception( |
86
|
|
|
'Memcache class '.$class.' not implement the interface.', |
87
|
|
|
$this::ERR_MEMCACHED_NOT_IMPLEMENT_INTERFACE |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->memcached->connectToServers(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Obtain the memcache class name to use |
96
|
|
|
* |
97
|
|
|
* @param array $memcachedConfig |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
* |
101
|
|
|
* @throws Exception If there are some problem with the config declared |
102
|
|
|
*/ |
103
|
|
|
protected function obtainMemcachedClassName($memcachedConfig) |
104
|
|
|
{ |
105
|
|
|
$class = $memcachedConfig['class']; |
106
|
|
|
|
107
|
|
|
if (empty($class)) { |
108
|
|
|
throw new Exception( |
109
|
|
|
'Memcached is active but no class is define', |
110
|
|
|
$this::ERR_MEMCACHED_NOT_CLASS_DEFINED |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (class_exists($class) === false) { |
115
|
|
|
throw new Exception( |
116
|
|
|
'Memcache class '.$class.' not found.', |
117
|
|
|
$this::ERR_MEMCACHED_CLASS_NOT_FOUND |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $class; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: