1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of the Apix Project. |
5
|
|
|
* |
6
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
7
|
|
|
* |
8
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Apix\Cache; |
13
|
|
|
|
14
|
|
|
use Apix\Cache; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Apix Cache Factory class provides a PSR-Cache wrapper. |
18
|
|
|
* |
19
|
|
|
* @author Franck Cassedanne <franck at ouarz.net> |
20
|
|
|
*/ |
21
|
|
|
class Factory |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
const ERROR = '%s requires either a supported client object e.g. "\Redis", "\MongoClient", ...; or an object that implements Apix\Cache\Adapter; or an adapter name (string) e.g. "APC", "array"; or even an a plain array().'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Holds an array of supported cache clients. |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
public static $clients = array( |
31
|
|
|
'Runtime', 'Array', 'ArrayObject', 'Apc', |
32
|
|
|
'Redis', 'MongoClient', 'Memcached', 'PDO', |
33
|
|
|
'Files', 'Directory' |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Holds an associative array of cache adapters. |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
public static $adapters = array( |
41
|
|
|
'MongoClient' => 'Mongo', |
42
|
|
|
'PDO' => 'Pdo', |
43
|
|
|
'ArrayObject' => 'Runtime' |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Factory pattern. |
48
|
|
|
* |
49
|
|
|
* @param mixed $mix Either a supported client object e.g. '\Redis'; |
50
|
|
|
* or one that implements \Apix\Cache\Adapter; |
51
|
|
|
* or an adapter name (string) e.g. "APC", "Runtime"; |
52
|
|
|
* or even a plain array() or \ArrayObject. |
53
|
|
|
* @param array $options An array of options |
54
|
|
|
* @param boolean $taggable Wether to return a taggable pool. |
55
|
|
|
* @return PsrCache\Pool|PsrCache\TaggablePool |
56
|
|
|
* @throws PsrCache\InvalidArgumentException |
57
|
|
|
* @throws Apix\Cache\Exception |
58
|
|
|
*/ |
59
|
289 |
|
public static function getPool($mix, array $options=array(), $taggable=false) |
60
|
|
|
{ |
61
|
289 |
|
switch (true) { |
62
|
|
|
|
63
|
289 |
|
case is_a($mix, 'Apix\Cache\Adapter'): |
64
|
34 |
|
$class = $mix; |
65
|
34 |
|
$mix = null; |
66
|
34 |
|
break; |
67
|
|
|
|
68
|
255 |
|
case is_object($mix) |
|
|
|
|
69
|
255 |
|
&& in_array($name = get_class($mix), self::$clients): |
70
|
|
|
|
71
|
34 |
|
if ($name == 'PDO') { |
72
|
17 |
|
$name = 'Pdo\\' . AbstractPdo::getDriverName($mix); |
73
|
17 |
|
} else { |
74
|
17 |
|
$name = isset(self::$adapters[$name]) |
75
|
17 |
|
? self::$adapters[$name] |
76
|
17 |
|
: $name; |
77
|
|
|
} |
78
|
34 |
|
break; |
79
|
|
|
|
80
|
221 |
|
case is_string($mix) |
81
|
221 |
|
&& in_array( |
82
|
136 |
|
$name = strtolower($mix), |
83
|
136 |
|
$clients = array_map('strtolower', self::$clients) |
84
|
221 |
|
): |
85
|
119 |
|
$key = array_search($name, $clients); |
86
|
119 |
|
$name = self::$clients[$key]; |
87
|
119 |
|
$name = $name == 'Array' || $name == 'ArrayObject' ? 'Runtime' : $name; |
88
|
119 |
|
$mix = null; |
89
|
119 |
|
break; |
90
|
|
|
|
91
|
102 |
|
case is_array($mix): |
92
|
68 |
|
$name = 'Runtime'; |
93
|
68 |
|
break; |
94
|
|
|
|
95
|
34 |
|
default: |
96
|
34 |
|
throw new PsrCache\InvalidArgumentException( |
97
|
34 |
|
sprintf(self::ERROR, __CLASS__) |
98
|
34 |
|
); |
99
|
34 |
|
} |
100
|
|
|
|
101
|
255 |
|
if (!isset($class)) { |
102
|
221 |
|
$class = '\Apix\Cache\\' . $name; |
103
|
221 |
|
} |
104
|
|
|
|
105
|
255 |
|
$cache = null === $mix |
106
|
255 |
|
? new $class($options) |
107
|
255 |
|
: new $class($mix, $options); |
108
|
|
|
|
109
|
|
|
return $taggable |
110
|
255 |
|
? new PsrCache\TaggablePool($cache) |
111
|
255 |
|
: new PsrCache\Pool($cache); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @see self::getPool |
116
|
|
|
* @return PsrCache\TaggablePool |
117
|
|
|
*/ |
118
|
17 |
|
public static function getTaggablePool($mix, array $options=array()) |
119
|
|
|
{ |
120
|
17 |
|
return self::getPool($mix, $options, true); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.