Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
22 / 22
CRAP
100.00% covered (success)
100.00%
1 / 1
Cache
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
22 / 22
27
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAvailableAdapters
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 isAvailable
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 adapter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTtl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getItemTtl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 saveItem
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 saveItems
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getItem
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasItem
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deleteItem
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 deleteItems
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 clear
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 destroy
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __isset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __unset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetExists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetUnset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Cache;
15
16use Pop\Cache\Adapter\AdapterInterface;
17use ReturnTypeWillChange;
18
19/**
20 * Cache class
21 *
22 * @category   Pop
23 * @package    Pop\Cache
24 * @author     Nick Sagona, III <dev@nolainteractive.com>
25 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
26 * @license    http://www.popphp.org/license     New BSD License
27 * @version    3.4.0
28 */
29class Cache implements \ArrayAccess
30{
31
32    /**
33     * Cache adapter
34     * @var AdapterInterface
35     */
36    protected $adapter = null;
37
38    /**
39     * Constructor
40     *
41     * Instantiate the cache object
42     *
43     * @param  Adapter\AdapterInterface $adapter
44     */
45    public function __construct(Adapter\AdapterInterface $adapter)
46    {
47        $this->adapter = $adapter;
48    }
49
50    /**
51     * Determine available adapters
52     *
53     * @return array
54     */
55    public static function getAvailableAdapters()
56    {
57        $pdoDrivers = (class_exists('Pdo', false)) ? \PDO::getAvailableDrivers() : [];
58
59        return [
60            'apc'       => (function_exists('apc_cache_info')),
61            'file'      => true,
62            'memcached' => (class_exists('Memcache', false)),
63            'redis'     => (class_exists('Redis', false)),
64            'session'   => (function_exists('session_start')),
65            'sqlite'    => (class_exists('Sqlite3') || in_array('sqlite', $pdoDrivers))
66        ];
67    }
68
69    /**
70     * Determine if an adapter is available
71     *
72     * @param  string $adapter
73     * @return boolean
74     */
75    public static function isAvailable($adapter)
76    {
77        $adapter  = strtolower($adapter);
78        $adapters = self::getAvailableAdapters();
79        return (isset($adapters[$adapter]) && ($adapters[$adapter]));
80    }
81
82    /**
83     * Get the adapter
84     *
85     * @return mixed
86     */
87    public function adapter()
88    {
89        return $this->adapter;
90    }
91
92    /**
93     * Get global cache TTL
94     *
95     * @return int
96     */
97    public function getTtl()
98    {
99        return $this->adapter->getTtl();
100    }
101
102    /**
103     * Get item cache TTL
104     *
105     * @param  string $id
106     * @return int
107     */
108    public function getItemTtl($id)
109    {
110        return $this->adapter->getItemTtl($id);
111    }
112
113    /**
114     * Save an item to cache
115     *
116     * @param  string $id
117     * @param  mixed  $value
118     * @param  int    $ttl
119     * @return Cache
120     */
121    public function saveItem($id, $value, $ttl = null)
122    {
123        $this->adapter->saveItem($id, $value, $ttl);
124        return $this;
125    }
126
127    /**
128     * Save items to cache
129     *
130     * @param  array $items
131     * @return Cache
132     */
133    public function saveItems(array $items)
134    {
135        foreach ($items as $id => $value) {
136            $this->adapter->saveItem($id, $value);
137        }
138        return $this;
139    }
140
141    /**
142     * Get an item from cache
143     *
144     * @param  string $id
145     * @return mixed
146     */
147    public function getItem($id)
148    {
149        return $this->adapter->getItem($id);
150    }
151
152    /**
153     * Determine if the item is in cache
154     *
155     * @param  string $id
156     * @return mixed
157     */
158    public function hasItem($id)
159    {
160        return $this->adapter->hasItem($id);
161    }
162
163    /**
164     * Delete an item in cache
165     *
166     * @param  string $id
167     * @return Cache
168     */
169    public function deleteItem($id)
170    {
171        $this->adapter->deleteItem($id);
172        return $this;
173    }
174
175    /**
176     * Delete items in cache
177     *
178     * @param  array $ids
179     * @return Cache
180     */
181    public function deleteItems(array $ids)
182    {
183        foreach ($ids as $id) {
184            $this->adapter->deleteItem($id);
185        }
186        return $this;
187    }
188
189    /**
190     * Clear all stored values from cache
191     *
192     * @return Cache
193     */
194    public function clear()
195    {
196        $this->adapter->clear();
197        return $this;
198    }
199
200    /**
201     * Destroy cache resource
202     *
203     * @return Cache
204     */
205    public function destroy()
206    {
207        $this->adapter->destroy();
208        return $this;
209    }
210
211    /**
212     * Magic get method to return an item from cache
213     *
214     * @param  string $name
215     * @return mixed
216     */
217    public function __get($name)
218    {
219        return $this->adapter->getItem($name);
220    }
221
222    /**
223     * Magic set method to save an item in the cache
224     *
225     * @param  string $name
226     * @param  mixed $value
227     * @throws Exception
228     * @return void
229     */
230    public function __set($name, $value)
231    {
232        $this->adapter->saveItem($name, $value);
233    }
234
235    /**
236     * Determine if the item is in cache
237     *
238     * @param  string $name
239     * @return boolean
240     */
241    public function __isset($name)
242    {
243        return $this->adapter->hasItem($name);
244    }
245
246    /**
247     * Delete value from cache
248     *
249     * @param  string $name
250     * @throws Exception
251     * @return void
252     */
253    public function __unset($name)
254    {
255        $this->adapter->deleteItem($name);
256    }
257
258    /**
259     * ArrayAccess offsetExists
260     *
261     * @param  mixed $offset
262     * @return boolean
263     */
264    public function offsetExists($offset): bool
265    {
266        return $this->__isset($offset);
267    }
268
269    /**
270     * ArrayAccess offsetGet
271     *
272     * @param  mixed $offset
273     * @return mixed
274     */
275    #[ReturnTypeWillChange]
276    public function offsetGet($offset)
277    {
278        return $this->__get($offset);
279    }
280
281    /**
282     * ArrayAccess offsetSet
283     *
284     * @param  mixed $offset
285     * @param  mixed $value
286     * @return void
287     */
288    #[ReturnTypeWillChange]
289    public function offsetSet($offset, $value)
290    {
291        $this->__set($offset, $value);
292    }
293
294    /**
295     * ArrayAccess offsetUnset
296     *
297     * @param  mixed $offset
298     * @return void
299     */
300    #[ReturnTypeWillChange]
301    public function offsetUnset($offset)
302    {
303        $this->__unset($offset);
304    }
305
306}