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
|
4 |
|
public function __construct($namespace) |
37
|
|
|
{ |
38
|
4 |
|
$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
|
4 |
|
public function create($name, array $options = array()) |
52
|
|
|
{ |
53
|
|
|
switch ( $name ) { |
54
|
4 |
|
case 'chain': |
55
|
4 |
|
$valid_caches = array(); |
56
|
|
|
|
57
|
4 |
|
foreach ( array_filter($options) as $cache_name ) { |
58
|
4 |
|
$valid_caches[] = self::create($cache_name); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
if ( !$valid_caches ) { |
62
|
|
|
throw new \LogicException('No valid caches provided for "chain" cache.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
$cache_driver = new ChainCache($valid_caches); |
66
|
4 |
|
$cache_driver->setNamespace($this->_namespace); |
67
|
|
|
|
68
|
4 |
|
return $cache_driver; |
69
|
|
|
|
70
|
4 |
|
case 'array': |
71
|
4 |
|
$cache_driver = new ArrayCache(); |
72
|
4 |
|
$cache_driver->setNamespace($this->_namespace); |
73
|
|
|
|
74
|
4 |
|
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('localhost', 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
|
|
|
|