1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Jira-CLI library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/console-helpers/jira-cli |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\JiraCLI\Cache; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\ApcCache; |
15
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
16
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
17
|
|
|
use Doctrine\Common\Cache\ChainCache; |
18
|
|
|
use Doctrine\Common\Cache\MemcacheCache; |
19
|
|
|
use Doctrine\Common\Cache\MemcachedCache; |
20
|
|
|
|
21
|
|
|
class CacheFactory |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Namespace. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $_namespace; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* CacheFactory constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $namespace Namespace. |
35
|
|
|
*/ |
36
|
|
|
public function __construct($namespace) |
37
|
|
|
{ |
38
|
|
|
$this->_namespace = $namespace; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Creates cache by name. |
43
|
|
|
* |
44
|
|
|
* @param string $name Name. |
45
|
|
|
* @param array $options Options. |
46
|
|
|
* |
47
|
|
|
* @return CacheProvider |
48
|
|
|
* @throws \InvalidArgumentException When cache provider with given name not found. |
49
|
|
|
* @throws \LogicException When no caches provided for "chain" cache. |
50
|
|
|
*/ |
51
|
|
|
public function create($name, array $options = array()) |
52
|
|
|
{ |
53
|
|
|
switch ( $name ) { |
54
|
|
|
case 'chain': |
55
|
|
|
$valid_caches = array(); |
56
|
|
|
|
57
|
|
|
foreach ( array_filter($options) as $cache_name ) { |
58
|
|
|
$valid_caches[] = self::create($cache_name); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ( !$valid_caches ) { |
|
|
|
|
62
|
|
|
throw new \LogicException('No valid caches provided for "chain" cache.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$cache_driver = new ChainCache($valid_caches); |
66
|
|
|
$cache_driver->setNamespace($this->_namespace); |
67
|
|
|
|
68
|
|
|
return $cache_driver; |
69
|
|
|
|
70
|
|
|
case 'array': |
71
|
|
|
$cache_driver = new ArrayCache(); |
72
|
|
|
$cache_driver->setNamespace($this->_namespace); |
73
|
|
|
|
74
|
|
|
return $cache_driver; |
75
|
|
|
|
76
|
|
|
case 'apc': |
77
|
|
|
$cache_driver = new ApcCache(); |
78
|
|
|
$cache_driver->setNamespace($this->_namespace); |
79
|
|
|
|
80
|
|
|
return $cache_driver; |
81
|
|
|
|
82
|
|
|
case 'memcache': |
83
|
|
|
$memcache = new \Memcache(); |
84
|
|
|
$memcache->connect('localhost', 11211); |
85
|
|
|
|
86
|
|
|
$cache_driver = new MemcacheCache(); |
87
|
|
|
$cache_driver->setMemcache($memcache); |
88
|
|
|
$cache_driver->setNamespace($this->_namespace); |
89
|
|
|
|
90
|
|
|
return $cache_driver; |
91
|
|
|
|
92
|
|
|
case 'memcached': |
93
|
|
|
$memcached = new \Memcached(); |
94
|
|
|
$memcached->addServer('memcache_host', 11211); |
95
|
|
|
|
96
|
|
|
$cache_driver = new MemcachedCache(); |
97
|
|
|
$cache_driver->setMemcached($memcached); |
98
|
|
|
$cache_driver->setNamespace($this->_namespace); |
99
|
|
|
|
100
|
|
|
return $cache_driver; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
throw new \InvalidArgumentException('Cache provider "' . $name . '" not found.'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.