15
15
16
16
namespace phpFastCache \Bundle \Service ;
17
17
18
- use phpFastCache \Cache \ExtendedCacheItemPoolInterface ;
18
+ use phpFastCache \Core \ Pool \ExtendedCacheItemPoolInterface ;
19
19
use phpFastCache \CacheManager ;
20
20
use phpFastCache \Exceptions \phpFastCacheDriverException ;
21
21
use Symfony \Component \Stopwatch \Stopwatch ;
26
26
*/
27
27
class Cache
28
28
{
29
+ /**
30
+ * @var self
31
+ */
32
+ protected static $ selfInstance ;
33
+
29
34
/**
30
35
* @var array
31
36
*/
32
- private $ config = [];
37
+ protected $ config = [];
33
38
34
39
/**
35
40
* @var Stopwatch
@@ -39,9 +44,9 @@ class Cache
39
44
/**
40
45
* Contains all cache instances
41
46
*
42
- * @var \phpFastCache\Cache \ExtendedCacheItemPoolInterface[]
47
+ * @var \phpFastCache\Core\Pool \ExtendedCacheItemPoolInterface[]
43
48
*/
44
- private $ cacheInstances = [];
49
+ protected $ cacheInstances = [];
45
50
46
51
/**
47
52
* Cache constructor.
@@ -51,12 +56,26 @@ class Cache
51
56
*
52
57
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException
53
58
*/
54
- public function __construct ($ config , Stopwatch $ stopwatch = null )
59
+ protected function __construct ($ config , Stopwatch $ stopwatch = null )
55
60
{
56
61
$ this ->config = (array ) $ config ;
57
62
$ this ->stopwatch = $ stopwatch ;
58
63
}
59
64
65
+ /**
66
+ * Factory instance provider
67
+ *
68
+ * @param array $config
69
+ * @param Stopwatch $stopwatch
70
+ *
71
+ * @throws \phpFastCache\Exceptions\phpFastCacheDriverException
72
+ * @return self
73
+ */
74
+ public static function getInstance ($ config , Stopwatch $ stopwatch = null )
75
+ {
76
+ return self ::$ selfInstance ?: self ::$ selfInstance = new self ($ config , $ stopwatch );
77
+ }
78
+
60
79
/**
61
80
* Set a new cache instance
62
81
*
@@ -65,7 +84,7 @@ public function __construct($config, Stopwatch $stopwatch = null)
65
84
*
66
85
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException
67
86
*/
68
- public function createInstance ($ name , ExtendedCacheItemPoolInterface $ instance )
87
+ public function createInstance ($ name , $ instance )
69
88
{
70
89
if (array_key_exists ($ name , $ this ->cacheInstances ) && $ this ->cacheInstances [ $ name ] instanceof ExtendedCacheItemPoolInterface) {
71
90
throw new phpFastCacheDriverException ("Cache instance ' {$ name }' already exists " );
@@ -78,7 +97,7 @@ public function createInstance($name, ExtendedCacheItemPoolInterface $instance)
78
97
*
79
98
* @param string $name Name of configured driver
80
99
*
81
- * @return \phpFastCache\Cache \ExtendedCacheItemPoolInterface
100
+ * @return \phpFastCache\Core\Pool \ExtendedCacheItemPoolInterface
82
101
*
83
102
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException
84
103
*/
@@ -106,7 +125,7 @@ public function get($name)
106
125
}
107
126
108
127
/**
109
- * @return \phpFastCache\Cache \ExtendedCacheItemPoolInterface
128
+ * @return \phpFastCache\Core\Pool \ExtendedCacheItemPoolInterface
110
129
*/
111
130
public function getTwigCacheInstance ()
112
131
{
@@ -126,7 +145,7 @@ public function getConfig()
126
145
/**
127
146
* Return all cache instances
128
147
*
129
- * @return \phpFastCache\Cache \ExtendedCacheItemPoolInterface[]
148
+ * @return \phpFastCache\Core\Pool \ExtendedCacheItemPoolInterface[]
130
149
*/
131
150
public function getInstances ()
132
151
{
0 commit comments