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