Completed
Push — master ( a1970e...6c5d6a )
by Oleg
04:15
created

Cache::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php /** MicroCache */
2
3
namespace Micro\Cache\Drivers;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * Cache class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Cache
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class Cache
20
{
21
    /** @var array $drivers Supported drivers */
22
    protected static $drivers = [
23
        'apc' => '\\Micro\\Cache\\Driver\\ApcDriver',
24
        'array' => '\\Micro\\Cache\\Driver\\ArrayDriver',
25
        'db' => '\\Micro\\Cache\\Driver\\DbDriver',
26
        'file' => '\\Micro\\Cache\\Driver\\FileDriver',
27
        'memcache' => '\\Micro\\Cache\\Driver\\MemcachedDriver',
28
        'memcached' => '\\Micro\\Cache\\Driver\\MemcachedDriver',
29
        'redis' => '\\Micro\\Cache\\Driver\\RedisDriver',
30
        'wincache' => '\\Micro\\Cache\\Driver\\WincacheDriver',
31
        'xcache' => '\\Micro\\Cache\\Driver\\XcacheDriver'
32
    ];
33
34
    /** @var array $servers Activated servers */
35
    protected $servers = [];
36
37
38
    /**
39
     * Constructor is a initialize Caches
40
     *
41
     * @access public
42
     *
43
     * @param array $servers
44
     *
45
     * @result void
46
     * @throws Exception
47
     */
48
    public function __construct(array $servers)
49
    {
50
        foreach ($servers AS $key => $server) {
51
            if (array_key_exists($server['driver'], array_keys(static::$drivers))) {
52
                $this->servers[$key] = new static::$drivers[$server['driver']] ($server);
53
            } else {
54
                throw new Exception('Cache driver `'.$server['driver'].'` not found');
55
            }
56
        }
57
    }
58
59
    /**
60
     * Get cache server by name
61
     *
62
     * @access public
63
     *
64
     * @param string $driver server name
65
     *
66
     * @return mixed
67
     * @throws Exception
68
     */
69
    public function get($driver = null)
70
    {
71
        if (!$driver) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $driver of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
72
            return $this->servers[0];
73
        }
74
75
        if (in_array($driver, $this->servers, true)) {
76
            return $this->servers[$driver];
77
        } else {
78
            throw new Exception('Cache `'.$driver.'` not found.');
79
        }
80
    }
81
}
82