@@ -12,4 +12,4 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\SimpleCache; |
14 | 14 | |
15 | -class CacheException extends \Exception implements \Psr\SimpleCache\CacheException{} |
|
15 | +class CacheException extends \Exception implements \Psr\SimpleCache\CacheException {} |
@@ -12,4 +12,4 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\SimpleCache; |
14 | 14 | |
15 | -class InvalidArgumentException extends CacheException implements \Psr\SimpleCache\InvalidArgumentException{} |
|
15 | +class InvalidArgumentException extends CacheException implements \Psr\SimpleCache\InvalidArgumentException {} |
@@ -18,6 +18,6 @@ |
||
18 | 18 | * @property string $cacheFilestorage |
19 | 19 | * @property string $cacheSessionkey |
20 | 20 | */ |
21 | -class CacheOptions extends SettingsContainerAbstract{ |
|
21 | +class CacheOptions extends SettingsContainerAbstract { |
|
22 | 22 | use CacheOptionsTrait; |
23 | 23 | } |
@@ -114,8 +114,7 @@ discard block |
||
114 | 114 | |
115 | 115 | if(is_array($data)){ |
116 | 116 | return $data; |
117 | - } |
|
118 | - elseif($data instanceof Traversable){ |
|
117 | + } elseif($data instanceof Traversable){ |
|
119 | 118 | return iterator_to_array($data); // @codeCoverageIgnore |
120 | 119 | } |
121 | 120 | |
@@ -135,8 +134,7 @@ discard block |
||
135 | 134 | |
136 | 135 | if($ttl instanceof DateInterval){ |
137 | 136 | return (new DateTime)->add($ttl)->getTimeStamp() - time(); |
138 | - } |
|
139 | - else if((is_int($ttl) && $ttl > 0) || $ttl === null){ |
|
137 | + } else if((is_int($ttl) && $ttl > 0) || $ttl === null){ |
|
140 | 138 | return $ttl; |
141 | 139 | } |
142 | 140 |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | /** |
33 | 33 | * CacheDriverAbstract constructor. |
34 | 34 | */ |
35 | - public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
|
35 | + public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null) { |
|
36 | 36 | $this->options = $options ?? new CacheOptions; |
37 | 37 | $this->logger = $logger ?? new NullLogger; |
38 | 38 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | public function getMultiple($keys, $default = null):array{ |
47 | 47 | $data = []; |
48 | 48 | |
49 | - foreach($this->getData($keys) as $key){ |
|
49 | + foreach ($this->getData($keys) as $key) { |
|
50 | 50 | $data[$key] = $this->get($key, $default); |
51 | 51 | } |
52 | 52 | |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | public function setMultiple($values, $ttl = null):bool{ |
58 | 58 | $return = []; |
59 | 59 | |
60 | - foreach($this->getData($values) as $key => $value){ |
|
60 | + foreach ($this->getData($values) as $key => $value) { |
|
61 | 61 | $return[] = $this->set($key, $value, $ttl); |
62 | 62 | } |
63 | 63 | |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | public function deleteMultiple($keys):bool{ |
69 | 69 | $return = []; |
70 | 70 | |
71 | - foreach($this->getData($keys) as $key){ |
|
71 | + foreach ($this->getData($keys) as $key) { |
|
72 | 72 | $return[] = $this->delete($key); |
73 | 73 | } |
74 | 74 | |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | */ |
84 | 84 | protected function checkKey($key):string{ |
85 | 85 | |
86 | - if(!is_string($key) || empty($key)){ |
|
86 | + if (!is_string($key) || empty($key)) { |
|
87 | 87 | throw new InvalidArgumentException('invalid cache key: "'.$key.'"'); |
88 | 88 | } |
89 | 89 | |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | */ |
98 | 98 | protected function checkKeyArray(array $keys):void{ |
99 | 99 | |
100 | - foreach($keys as $key){ |
|
100 | + foreach ($keys as $key) { |
|
101 | 101 | $this->checkKey($key); |
102 | 102 | } |
103 | 103 | |
@@ -111,10 +111,10 @@ discard block |
||
111 | 111 | */ |
112 | 112 | protected function getData($data):array{ |
113 | 113 | |
114 | - if(is_array($data)){ |
|
114 | + if (is_array($data)) { |
|
115 | 115 | return $data; |
116 | 116 | } |
117 | - elseif($data instanceof Traversable){ |
|
117 | + elseif ($data instanceof Traversable) { |
|
118 | 118 | return iterator_to_array($data); // @codeCoverageIgnore |
119 | 119 | } |
120 | 120 | |
@@ -127,12 +127,12 @@ discard block |
||
127 | 127 | * @return int|null |
128 | 128 | * @throws \Psr\SimpleCache\InvalidArgumentException |
129 | 129 | */ |
130 | - protected function getTTL($ttl):?int{ |
|
130 | + protected function getTTL($ttl): ?int{ |
|
131 | 131 | |
132 | - if($ttl instanceof DateInterval){ |
|
132 | + if ($ttl instanceof DateInterval) { |
|
133 | 133 | return (new DateTime)->add($ttl)->getTimeStamp() - time(); |
134 | 134 | } |
135 | - else if((is_int($ttl) && $ttl > 0) || $ttl === null){ |
|
135 | + else if ((is_int($ttl) && $ttl > 0) || $ttl === null) { |
|
136 | 136 | return $ttl; |
137 | 137 | } |
138 | 138 | |
@@ -146,9 +146,9 @@ discard block |
||
146 | 146 | */ |
147 | 147 | protected function checkReturn(array $booleans):bool{ |
148 | 148 | |
149 | - foreach($booleans as $boolean){ |
|
149 | + foreach ($booleans as $boolean) { |
|
150 | 150 | |
151 | - if(!(bool)$boolean){ |
|
151 | + if (!(bool)$boolean) { |
|
152 | 152 | return false; // @codeCoverageIgnore |
153 | 153 | } |
154 | 154 |
@@ -12,7 +12,7 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\SimpleCache; |
14 | 14 | |
15 | -trait CacheOptionsTrait{ |
|
15 | +trait CacheOptionsTrait { |
|
16 | 16 | |
17 | 17 | protected string $cacheFilestorage = ''; |
18 | 18 | protected string $cacheSessionkey = '_session_cache'; |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | |
22 | 22 | use const DIRECTORY_SEPARATOR; |
23 | 23 | |
24 | -class FileCache extends CacheDriverAbstract{ |
|
24 | +class FileCache extends CacheDriverAbstract { |
|
25 | 25 | |
26 | 26 | protected string $cachedir; |
27 | 27 | |
@@ -33,32 +33,32 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @throws \chillerlan\SimpleCache\CacheException |
35 | 35 | */ |
36 | - public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
|
36 | + public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null) { |
|
37 | 37 | parent::__construct($options, $logger); |
38 | 38 | |
39 | 39 | $this->cachedir = rtrim($this->options->cacheFilestorage, '/\\').DIRECTORY_SEPARATOR; |
40 | 40 | |
41 | - if(!is_dir($this->cachedir)){ |
|
41 | + if (!is_dir($this->cachedir)) { |
|
42 | 42 | throw new CacheException('invalid cachedir "'.$this->cachedir.'"'); |
43 | 43 | } |
44 | 44 | |
45 | - if(!is_writable($this->cachedir)){ |
|
45 | + if (!is_writable($this->cachedir)) { |
|
46 | 46 | throw new CacheException('cachedir is read-only. permissions?'); |
47 | 47 | } |
48 | 48 | |
49 | 49 | } |
50 | 50 | |
51 | 51 | /** @inheritdoc */ |
52 | - public function get($key, $default = null){ |
|
52 | + public function get($key, $default = null) { |
|
53 | 53 | $filename = $this->getFilepath($this->checkKey($key)); |
54 | 54 | |
55 | - if(is_file($filename)){ |
|
55 | + if (is_file($filename)) { |
|
56 | 56 | $content = file_get_contents($filename); |
57 | 57 | |
58 | - if(!empty($content)){ |
|
58 | + if (!empty($content)) { |
|
59 | 59 | $data = unserialize($content); |
60 | 60 | |
61 | - if($data->ttl === null || $data->ttl > time()){ |
|
61 | + if ($data->ttl === null || $data->ttl > time()) { |
|
62 | 62 | return $data->content; |
63 | 63 | } |
64 | 64 | |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | $file = $this->getFilepath($this->checkKey($key)); |
78 | 78 | $dir = dirname($file); |
79 | 79 | |
80 | - if(!is_dir($dir)){ |
|
80 | + if (!is_dir($dir)) { |
|
81 | 81 | mkdir($dir, 0755, true); |
82 | 82 | } |
83 | 83 | |
@@ -85,13 +85,13 @@ discard block |
||
85 | 85 | $data->ttl = null; |
86 | 86 | $data->content = $value; |
87 | 87 | |
88 | - if($ttl !== null){ |
|
88 | + if ($ttl !== null) { |
|
89 | 89 | $data->ttl = time() + $ttl; |
90 | 90 | } |
91 | 91 | |
92 | 92 | file_put_contents($file, serialize($data)); |
93 | 93 | |
94 | - if(is_file($file)){ |
|
94 | + if (is_file($file)) { |
|
95 | 95 | return true; |
96 | 96 | } |
97 | 97 | |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | public function delete($key):bool{ |
103 | 103 | $filename = $this->getFilepath($this->checkKey($key)); |
104 | 104 | |
105 | - if(is_file($filename)){ |
|
105 | + if (is_file($filename)) { |
|
106 | 106 | return unlink($filename); |
107 | 107 | } |
108 | 108 | |
@@ -111,13 +111,13 @@ discard block |
||
111 | 111 | |
112 | 112 | /** @inheritdoc */ |
113 | 113 | public function clear():bool{ |
114 | - $iterator = new RecursiveDirectoryIterator($this->cachedir, FilesystemIterator::CURRENT_AS_PATHNAME|FilesystemIterator::SKIP_DOTS); |
|
114 | + $iterator = new RecursiveDirectoryIterator($this->cachedir, FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS); |
|
115 | 115 | $return = []; |
116 | 116 | |
117 | - foreach(new RecursiveIteratorIterator($iterator) as $path){ |
|
117 | + foreach (new RecursiveIteratorIterator($iterator) as $path) { |
|
118 | 118 | |
119 | 119 | // skip files in the parent directory - cache files are only under /a/ab/[hash] |
120 | - if(strpos(str_replace($this->cachedir, '', $path), DIRECTORY_SEPARATOR) === false){ |
|
120 | + if (strpos(str_replace($this->cachedir, '', $path), DIRECTORY_SEPARATOR) === false) { |
|
121 | 121 | continue; |
122 | 122 | } |
123 | 123 |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | |
22 | 22 | use function array_keys; |
23 | 23 | |
24 | -class MemcachedCache extends CacheDriverAbstract{ |
|
24 | +class MemcachedCache extends CacheDriverAbstract { |
|
25 | 25 | |
26 | 26 | protected Memcached $memcached; |
27 | 27 | |
@@ -34,22 +34,22 @@ discard block |
||
34 | 34 | * |
35 | 35 | * @throws \chillerlan\SimpleCache\CacheException |
36 | 36 | */ |
37 | - public function __construct(Memcached $memcached, SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
|
37 | + public function __construct(Memcached $memcached, SettingsContainerInterface $options = null, LoggerInterface $logger = null) { |
|
38 | 38 | parent::__construct($options, $logger); |
39 | 39 | |
40 | 40 | $this->memcached = $memcached; |
41 | 41 | |
42 | - if(empty($this->memcached->getServerList())){ |
|
42 | + if (empty($this->memcached->getServerList())) { |
|
43 | 43 | throw new CacheException('no memcache server available'); |
44 | 44 | } |
45 | 45 | |
46 | 46 | } |
47 | 47 | |
48 | 48 | /** @inheritdoc */ |
49 | - public function get($key, $default = null){ |
|
49 | + public function get($key, $default = null) { |
|
50 | 50 | $value = $this->memcached->get($this->checkKey($key)); |
51 | 51 | |
52 | - if($value !== false){ |
|
52 | + if ($value !== false) { |
|
53 | 53 | return $value; |
54 | 54 | } |
55 | 55 | |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | $values = $this->memcached->getMulti($keys); |
81 | 81 | $return = []; |
82 | 82 | |
83 | - foreach($keys as $key){ |
|
83 | + foreach ($keys as $key) { |
|
84 | 84 | $return[$key] = $values[$key] ?? $default; |
85 | 85 | } |
86 | 86 |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | |
18 | 18 | use function time; |
19 | 19 | |
20 | -class SessionCache extends CacheDriverAbstract{ |
|
20 | +class SessionCache extends CacheDriverAbstract { |
|
21 | 21 | |
22 | 22 | protected string $key; |
23 | 23 | |
@@ -26,12 +26,12 @@ discard block |
||
26 | 26 | * |
27 | 27 | * @throws \Psr\SimpleCache\CacheException |
28 | 28 | */ |
29 | - public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
|
29 | + public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null) { |
|
30 | 30 | parent::__construct($options, $logger); |
31 | 31 | |
32 | 32 | $this->key = $this->options->cacheSessionkey; |
33 | 33 | |
34 | - if(!is_string($this->key) || empty($this->key)){ |
|
34 | + if (!is_string($this->key) || empty($this->key)) { |
|
35 | 35 | throw new CacheException('invalid session cache key'); |
36 | 36 | } |
37 | 37 | |
@@ -40,12 +40,12 @@ discard block |
||
40 | 40 | } |
41 | 41 | |
42 | 42 | /** @inheritdoc */ |
43 | - public function get($key, $default = null){ |
|
43 | + public function get($key, $default = null) { |
|
44 | 44 | $key = $this->checkKey($key); |
45 | 45 | |
46 | - if(isset($_SESSION[$this->key][$key])){ |
|
46 | + if (isset($_SESSION[$this->key][$key])) { |
|
47 | 47 | |
48 | - if($_SESSION[$this->key][$key]['ttl'] === null || $_SESSION[$this->key][$key]['ttl'] > time()){ |
|
48 | + if ($_SESSION[$this->key][$key]['ttl'] === null || $_SESSION[$this->key][$key]['ttl'] > time()) { |
|
49 | 49 | return $_SESSION[$this->key][$key]['content']; |
50 | 50 | } |
51 | 51 |
@@ -14,17 +14,17 @@ |
||
14 | 14 | |
15 | 15 | use function time; |
16 | 16 | |
17 | -class MemoryCache extends CacheDriverAbstract{ |
|
17 | +class MemoryCache extends CacheDriverAbstract { |
|
18 | 18 | |
19 | 19 | protected array $cache = []; |
20 | 20 | |
21 | 21 | /** @inheritdoc */ |
22 | - public function get($key, $default = null){ |
|
22 | + public function get($key, $default = null) { |
|
23 | 23 | $key = $this->checkKey($key); |
24 | 24 | |
25 | - if(isset($this->cache[$key])){ |
|
25 | + if (isset($this->cache[$key])) { |
|
26 | 26 | |
27 | - if($this->cache[$key]['ttl'] === null || $this->cache[$key]['ttl'] > time()){ |
|
27 | + if ($this->cache[$key]['ttl'] === null || $this->cache[$key]['ttl'] > time()) { |
|
28 | 28 | return $this->cache[$key]['content']; |
29 | 29 | } |
30 | 30 |