Repository navigation
Expand file tree
/
Copy pathMemcacheFactory.php
More file actions
134 lines (113 loc) · 3.79 KB
/
Copy pathMemcacheFactory.php
File metadata and controls
134 lines (113 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Bolt\Session\Handler\Factory;
use Bolt\Session\OptionsBag;
use InvalidArgumentException;
use Memcache;
use RuntimeException;
/**
* Factory for creating Memcache instances from Session options.
*
* @deprecated since 1.0, will be removed in 2.0.
*
* @author Carson Full <carsonfull@gmail.com>
*/
class MemcacheFactory extends AbstractFactory
{
/** @var string */
protected $class;
/**
* Constructor.
*
* @param string|null $class Memcache class name. Mostly for tests.
*/
public function __construct($class = Memcache::class)
{
if (!extension_loaded('memcache')) {
throw new RuntimeException('Unable to use "memcache" session handler as memcache extension is not installed and enabled. Install the extension or try the "memcached" session handler instead.');
}
$this->class = $class ?: Memcache::class;
if (!is_a($this->class, Memcache::class, true)) {
throw new InvalidArgumentException(sprintf('Class name "%s" is not Memcache or a subclass of Memcache.', $this->class));
}
}
/**
* Creates a Memcache instance from the session options.
*
* @param OptionsBag $options
*
* @return Memcache
*/
public function create(OptionsBag $options)
{
$connections = $this->parse($options);
$class = $this->class;
$memcache = new $class();
return $this->configure($memcache, $connections);
}
/**
* @param Memcache $memcache
* @param array $connections
*
* @return Memcache
*/
public function configure(Memcache $memcache, array $connections)
{
foreach ($connections as $conn) {
$memcache->addServer($conn['host'], $conn['port'], $conn['persistent'], $conn['weight'], $conn['timeout'], $conn['retry_interval']);
}
return $memcache;
}
/**
* {@inheritdoc}
*/
protected function parseConnectionItemFromSavePath($path)
{
$uri = $this->parseUri($path);
$query = $this->parseQuery($uri);
$conn = new OptionsBag([
'weight' => $query->getInt('weight', 1),
'persistent' => $query->getBoolean('persistent'),
'timeout' => $query->getInt('timeout', 1),
'retry_interval' => $query->getInt('retry_interval', 0),
]);
if ($uri->getHost()) {
$conn['host'] = $uri->getHost();
$conn['port'] = $uri->getPort() ?: 11211;
} else { // unix path
$conn['host'] = 'unix://' . $uri->getPath();
$conn['port'] = 0;
}
if ((!$conn['host'] && !$conn['path']) || $conn['weight'] <= 0 || $conn['timeout'] <= 0) {
throw new InvalidArgumentException('Failed to parse session save_path');
}
return $conn;
}
/**
* {@inheritdoc}
*/
protected function parseConnectionItemFromOptions($item)
{
if (is_string($item)) {
$uri = $this->parseUri($item);
$item = [
'host' => $uri->getHost(),
'port' => $uri->getPort(),
'path' => $uri->getPath(),
];
}
$item = new OptionsBag($item);
$conn = new OptionsBag([
'host' => $item->get('host') ?: '127.0.0.1',
'port' => $item->getInt('port') ?: 11211,
'weight' => $item->getInt('weight', 1),
'persistent' => $item->getBoolean('persistent', false),
'timeout' => $item->getInt('timeout', 1),
'retry_interval' => $item->getInt('retry_interval', 0),
]);
if ($item['path']) {
$conn['host'] = 'unix://' . $item['path'];
$conn['port'] = 0;
}
return $conn;
}
}