Completed
Push — v5 ( 0b505a...012ddd )
by Georges
02:41
created

CacheManager::getStaticSystemDrivers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 17
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 20
rs 9.4285
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache;
16
17
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
18
use phpFastCache\Core\DriverAbstract;
19
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
20
use CouchbaseCluster;
21
22
/**
23
 * Class CacheManager
24
 * @package phpFastCache
25
 *
26
 * @method static DriverAbstract Apc() Apc($config = []) Return a driver "apc" instance
27
 * @method static DriverAbstract Cookie() Cookie($config = []) Return a driver "cookie" instance
28
 * @method static DriverAbstract Files() Files($config = []) Return a driver "files" instance
29
 * @method static DriverAbstract Memcache() Memcache($config = []) Return a driver "memcache" instance
30
 * @method static DriverAbstract Memcached() Memcached($config = []) Return a driver "memcached" instance
31
 * @method static DriverAbstract Predis() Predis($config = []) Return a driver "predis" instance
32
 * @method static DriverAbstract Redis() Redis($config = []) Return a driver "redis" instance
33
 * @method static DriverAbstract Sqlite() Sqlite($config = []) Return a driver "sqlite" instance
34
 * @method static DriverAbstract Ssdb() Ssdb($config = []) Return a driver "ssdb" instance
35
 * @method static DriverAbstract Wincache() Wincache($config = []) Return a driver "wincache" instance
36
 * @method static DriverAbstract Xcache() Xcache($config = []) Return a driver "xcache" instance
37
 *
38
 */
39
class CacheManager
40
{
41
    /**
42
     * @var int
43
     */
44
    public static $ReadHits = 0;
45
46
    /**
47
     * @var int
48
     */
49
    public static $WriteHits = 0;
50
51
    /**
52
     * @var array
53
     */
54
    public static $config = [
55
      'default_chmod' => 0777, // 0777 recommended
56
      'fallback' => 'files', //Fall back when old driver is not support
57
      'securityKey' => 'auto',
58
      'htaccess' => true,
59
      'path' => '',// if not set will be the value of sys_get_temp_dir()
60
      "limited_memory_each_object" => 4096, // maximum size (bytes) of object store in memory
61
      "compress_data" => false, // compress stored data, if the backend supports it
62
    ];
63
64
    /**
65
     * @var array
66
     */
67
68
    protected static $namespacePath;
69
    protected static $instances = [];
70
71
    /**
72
     * @param string $driver
73
     * @param array $config
74
     * @return ExtendedCacheItemPoolInterface
75
     */
76
    public static function getInstance($driver = 'auto', $config = [])
77
    {
78
        $driver = ucfirst(strtolower($driver));
79
        $config = array_merge(self::$config, $config);
80
        if ($driver === 'Auto') {
81
            $driver = self::getAutoClass($config);
82
        }
83
84
        $instance = crc32($driver . serialize($config));
85
        if (!isset(self::$instances[ $instance ])) {
86
            $class = self::getNamespacePath() . $driver . '\Driver';
87
            self::$instances[ $instance ] = new $class($config);
88
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
89
           // trigger_error('[' . $driver . '] Calling CacheManager::getInstance for already instanced drivers is a bad practice and have a significant impact on performances.
90
           // See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F');
91
        }
92
93
        return self::$instances[ $instance ];
94
    }
95
96
    /**
97
     * @param $config
98
     * @return string
99
     * @throws \Exception
100
     */
101
    public static function getAutoClass($config)
102
    {
103
        static $autoDriver;
104
105
        if ($autoDriver === null) {
106
            foreach (self::getStaticSystemDrivers() as $driver) {
107
                try {
108
                    self::getInstance($driver, $config);
109
                    $autoDriver = $driver;
110
                } catch (phpFastCacheDriverCheckException $e) {
111
                    continue;
112
                }
113
            }
114
        }
115
116
        return $autoDriver;
117
    }
118
119
    /**
120
     * @param string $name
121
     * @param array $arguments
122
     * @return \Psr\Cache\CacheItemPoolInterface
123
     */
124
    public static function __callStatic($name, $arguments)
125
    {
126
        $options = (array_key_exists(0, $arguments) && is_array($arguments) ? $arguments[ 0 ] : []);
127
128
        return self::getInstance($name, $options);
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public static function clearInstances()
135
    {
136
        foreach (self::$instances as &$instance) {
137
            unset($instance);
138
        }
139
140
        return !count(self::$instances);
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public static function getNamespacePath()
147
    {
148
        return self::$namespacePath ?: __NAMESPACE__ . '\Drivers\\';
149
    }
150
151
    /**
152
     * @param string $path
153
     */
154
    public static function setNamespacePath($path)
155
    {
156
        self::$namespacePath = $path;
0 ignored issues
show
Documentation Bug introduced by
It seems like $path of type string is incompatible with the declared type array of property $namespacePath.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
157
    }
158
159
    /**
160
     * @param $name
161
     * @param string $value
162
     */
163
    public static function setup($name, $value = '')
164
    {
165
        if (is_array($name)) {
166
            self::$config = array_merge(self::$config, $name);
167
        } else {
168
            self::$config[ $name ] = $value;
169
        }
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    public static function getStaticSystemDrivers()
176
    {
177
        return [
178
          'Sqlite',
179
          'Files',
180
          'Apc',
181
          'Apcu',
182
          'Memcache',
183
          'Memcached',
184
          'Couchbase',
185
          'Mongodb',
186
          'Predis',
187
          'Redis',
188
          'Ssdb',
189
          'Leveldb',
190
          'Wincache',
191
          'Xcache',
192
          'Devnull',
193
        ];
194
    }
195
196
    /**
197
     * @return array
198
     */
199
    public static function getStaticAllDrivers()
200
    {
201
        return array_merge(self::getStaticSystemDrivers(), [
202
            'Devtrue',
203
            'Devfalse',
204
            'Cookie',
205
        ]);
206
    }
207
}
208