forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheFactory.php
More file actions
64 lines (52 loc) · 1.56 KB
/
Copy pathCacheFactory.php
File metadata and controls
64 lines (52 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php namespace CodeIgniter\Cache;
/**
* Class Cache
*
* A factory for loading the desired
*
* @package CodeIgniter\Cache
*/
class CacheFactory
{
/**
* Attempts to create the desired cache handler, based upon the
*
* @param $config
* @param string $handler
* @param string $backup
*
* @return mixed
*/
public static function getHandler($config, string $handler = null, string $backup = null)
{
if (! isset($config->validHandlers) || ! is_array($config->validHandlers))
{
throw new \InvalidArgumentException(lang('Cache.cacheInvalidHandlers'));
}
if (! isset($config->handler) || ! isset($config->backupHandler))
{
throw new \InvalidArgumentException(lang('Cache.cacheNoBackup'));
}
$handler = ! empty($handler) ? $handler : $config->handler;
$backup = ! empty($backup) ? $backup : $config->backupHandler;
if (! array_key_exists($handler, $config->validHandlers) || ! array_key_exists($backup, $config->validHandlers))
{
throw new \InvalidArgumentException(lang('Cache.cacheHandlerNotFound'));
}
// Get an instance of our handler.
$adapter = new $config->validHandlers[$handler]($config);
if (! $adapter->isSupported())
{
$adapter = new $config->validHandlers[$backup]($config);
if (! $adapter->isSupported())
{
// Log stuff here, don't throw exception. No need to raise a fuss.
// Fall back to the dummy adapter.
$adapter = new $config->validHandler['dummy']();
}
}
$adapter->initialize();
return $adapter;
}
//--------------------------------------------------------------------
}