@@ -12,7 +12,7 @@ |
||
| 12 | 12 | |
| 13 | 13 | namespace chillerlan\SimpleCache; |
| 14 | 14 | |
| 15 | -trait CacheOptionsTrait{ |
|
| 15 | +trait CacheOptionsTrait { |
|
| 16 | 16 | |
| 17 | 17 | protected $filestorage = ''; |
| 18 | 18 | protected $cachekey = '_session_cache'; |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | use chillerlan\Traits\ImmutableSettingsInterface; |
| 17 | 17 | use stdClass; |
| 18 | 18 | |
| 19 | -class FileCacheDriver extends CacheDriverAbstract{ |
|
| 19 | +class FileCacheDriver extends CacheDriverAbstract { |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * @var string |
@@ -30,19 +30,19 @@ discard block |
||
| 30 | 30 | * |
| 31 | 31 | * @throws \chillerlan\SimpleCache\CacheException |
| 32 | 32 | */ |
| 33 | - public function __construct(ImmutableSettingsInterface $options = null){ |
|
| 33 | + public function __construct(ImmutableSettingsInterface $options = null) { |
|
| 34 | 34 | parent::__construct($options); |
| 35 | 35 | |
| 36 | 36 | $this->cachedir = $this->options->filestorage; |
| 37 | 37 | |
| 38 | - if(!is_dir($this->cachedir)){ |
|
| 38 | + if (!is_dir($this->cachedir)) { |
|
| 39 | 39 | $msg = 'invalid cachedir "'.$this->cachedir.'"'; |
| 40 | 40 | |
| 41 | 41 | $this->logger->error($msg); |
| 42 | 42 | throw new CacheException($msg); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - if(!is_writable($this->cachedir)){ |
|
| 45 | + if (!is_writable($this->cachedir)) { |
|
| 46 | 46 | $msg = 'cachedir is read-only. permissions?'; |
| 47 | 47 | |
| 48 | 48 | $this->logger->error($msg); |
@@ -52,16 +52,16 @@ discard block |
||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** @inheritdoc */ |
| 55 | - public function get(string $key, $default = null){ |
|
| 55 | + public function get(string $key, $default = null) { |
|
| 56 | 56 | $filename = $this->filename($key); |
| 57 | 57 | |
| 58 | - if(is_file($filename)){ |
|
| 58 | + if (is_file($filename)) { |
|
| 59 | 59 | $content = file_get_contents($filename); |
| 60 | 60 | |
| 61 | - if(!empty($content)){ |
|
| 61 | + if (!empty($content)) { |
|
| 62 | 62 | $data = unserialize($content); |
| 63 | 63 | |
| 64 | - if($data->ttl === null || $data->ttl > time()){ |
|
| 64 | + if ($data->ttl === null || $data->ttl > time()) { |
|
| 65 | 65 | return $data->content; |
| 66 | 66 | } |
| 67 | 67 | |
@@ -80,13 +80,13 @@ discard block |
||
| 80 | 80 | $data->ttl = null; |
| 81 | 81 | $data->content = $value; |
| 82 | 82 | |
| 83 | - if($ttl !== null){ |
|
| 83 | + if ($ttl !== null) { |
|
| 84 | 84 | $data->ttl = time() + $ttl; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | file_put_contents($filename, serialize($data)); |
| 88 | 88 | |
| 89 | - if(is_file($filename)){ |
|
| 89 | + if (is_file($filename)) { |
|
| 90 | 90 | return true; |
| 91 | 91 | } |
| 92 | 92 | |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | public function delete(string $key):bool{ |
| 98 | 98 | $filename = $this->filename($key); |
| 99 | 99 | |
| 100 | - if(is_file($filename)){ |
|
| 100 | + if (is_file($filename)) { |
|
| 101 | 101 | return unlink($filename); |
| 102 | 102 | } |
| 103 | 103 | |
@@ -108,13 +108,13 @@ discard block |
||
| 108 | 108 | public function clear():bool{ |
| 109 | 109 | $dir = scandir($this->cachedir); |
| 110 | 110 | |
| 111 | - if(is_array($dir) && !empty($dir)){ |
|
| 111 | + if (is_array($dir) && !empty($dir)) { |
|
| 112 | 112 | $return = []; |
| 113 | 113 | |
| 114 | - foreach($dir as $file){ |
|
| 114 | + foreach ($dir as $file) { |
|
| 115 | 115 | $path = $this->cachedir.DIRECTORY_SEPARATOR.$file; |
| 116 | 116 | |
| 117 | - if(is_file($path) && strlen($file) === 64){ |
|
| 117 | + if (is_file($path) && strlen($file) === 64) { |
|
| 118 | 118 | $return[] = unlink($path); |
| 119 | 119 | } |
| 120 | 120 | |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | use chillerlan\Traits\ImmutableSettingsInterface; |
| 16 | 16 | use Redis; |
| 17 | 17 | |
| 18 | -class RedisDriver extends CacheDriverAbstract{ |
|
| 18 | +class RedisDriver extends CacheDriverAbstract { |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * @var \Redis |
@@ -28,14 +28,14 @@ discard block |
||
| 28 | 28 | * @param \Redis $redis |
| 29 | 29 | * @param \chillerlan\Traits\ImmutableSettingsInterface|null $options |
| 30 | 30 | */ |
| 31 | - public function __construct(Redis $redis, ImmutableSettingsInterface $options = null){ |
|
| 31 | + public function __construct(Redis $redis, ImmutableSettingsInterface $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(string $key, $default = null){ |
|
| 38 | + public function get(string $key, $default = null) { |
|
| 39 | 39 | $value = $this->redis->get($key); |
| 40 | 40 | |
| 41 | 41 | return $value ? $value : $default; |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | /** @inheritdoc */ |
| 45 | 45 | public function set(string $key, $value, int $ttl = null):bool{ |
| 46 | 46 | |
| 47 | - if($ttl === null){ |
|
| 47 | + if ($ttl === null) { |
|
| 48 | 48 | return $this->redis->set($key, $value); |
| 49 | 49 | } |
| 50 | 50 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | $return = []; |
| 70 | 70 | |
| 71 | - foreach($keys as $key){ |
|
| 71 | + foreach ($keys as $key) { |
|
| 72 | 72 | $return[$key] = $values[$key] !== false ? $values[$key] : $default; |
| 73 | 73 | } |
| 74 | 74 | |
@@ -78,13 +78,13 @@ discard block |
||
| 78 | 78 | /** @inheritdoc */ |
| 79 | 79 | public function setMultiple(array $values, int $ttl = null):bool{ |
| 80 | 80 | |
| 81 | - if($ttl === null){ |
|
| 81 | + if ($ttl === null) { |
|
| 82 | 82 | return $this->redis->msetnx($values); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $return = []; |
| 86 | 86 | |
| 87 | - foreach($values as $key => $value){ |
|
| 87 | + foreach ($values as $key => $value) { |
|
| 88 | 88 | $return[] = $this->set($key, $value, $ttl); |
| 89 | 89 | } |
| 90 | 90 | |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | * @param \chillerlan\Traits\ImmutableSettingsInterface|null $options |
| 31 | 31 | */ |
| 32 | - public function __construct(ImmutableSettingsInterface $options = null){ |
|
| 32 | + public function __construct(ImmutableSettingsInterface $options = null) { |
|
| 33 | 33 | $this->options = $options ?? new CacheOptions; |
| 34 | 34 | $this->logger = new NullLogger; // logger will be set from the Cache instance |
| 35 | 35 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | public function getMultiple(array $keys, $default = null):array{ |
| 44 | 44 | $data = []; |
| 45 | 45 | |
| 46 | - foreach($keys as $key){ |
|
| 46 | + foreach ($keys as $key) { |
|
| 47 | 47 | $data[$key] = $this->get($key, $default); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | public function setMultiple(array $values, int $ttl = null):bool{ |
| 55 | 55 | $return = []; |
| 56 | 56 | |
| 57 | - foreach($values as $key => $value){ |
|
| 57 | + foreach ($values as $key => $value) { |
|
| 58 | 58 | $return[] = $this->set($key, $value, $ttl); |
| 59 | 59 | } |
| 60 | 60 | |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | public function deleteMultiple(array $keys):bool{ |
| 66 | 66 | $return = []; |
| 67 | 67 | |
| 68 | - foreach($keys as $key){ |
|
| 68 | + foreach ($keys as $key) { |
|
| 69 | 69 | $return[] = $this->delete($key); |
| 70 | 70 | } |
| 71 | 71 | |
@@ -79,9 +79,9 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | protected function checkReturn(array $booleans):bool{ |
| 81 | 81 | |
| 82 | - foreach($booleans as $bool){ |
|
| 82 | + foreach ($booleans as $bool) { |
|
| 83 | 83 | |
| 84 | - if(!(bool)$bool){ |
|
| 84 | + if (!(bool)$bool) { |
|
| 85 | 85 | return false; // @codeCoverageIgnore |
| 86 | 86 | } |
| 87 | 87 | |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | use chillerlan\SimpleCache\CacheException; |
| 16 | 16 | use chillerlan\Traits\ImmutableSettingsInterface; |
| 17 | 17 | |
| 18 | -class SessionCacheDriver extends CacheDriverAbstract{ |
|
| 18 | +class SessionCacheDriver extends CacheDriverAbstract { |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * @var string |
@@ -29,12 +29,12 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | * @throws \chillerlan\SimpleCache\CacheException |
| 31 | 31 | */ |
| 32 | - public function __construct(ImmutableSettingsInterface $options = null){ |
|
| 32 | + public function __construct(ImmutableSettingsInterface $options = null) { |
|
| 33 | 33 | parent::__construct($options); |
| 34 | 34 | |
| 35 | 35 | $this->key = $this->options->cachekey; |
| 36 | 36 | |
| 37 | - if(!is_string($this->key) || empty($this->key)){ |
|
| 37 | + if (!is_string($this->key) || empty($this->key)) { |
|
| 38 | 38 | $msg = 'invalid session cache key'; |
| 39 | 39 | |
| 40 | 40 | $this->logger->error($msg); |
@@ -46,11 +46,11 @@ discard block |
||
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** @inheritdoc */ |
| 49 | - public function get(string $key, $default = null){ |
|
| 49 | + public function get(string $key, $default = null) { |
|
| 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 | |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | use chillerlan\Traits\ImmutableSettingsInterface; |
| 17 | 17 | use Memcached; |
| 18 | 18 | |
| 19 | -class MemcachedDriver extends CacheDriverAbstract{ |
|
| 19 | +class MemcachedDriver extends CacheDriverAbstract { |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * @var \Memcached |
@@ -31,12 +31,12 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @throws \chillerlan\SimpleCache\CacheException |
| 33 | 33 | */ |
| 34 | - public function __construct(Memcached $memcached, ImmutableSettingsInterface $options = null){ |
|
| 34 | + public function __construct(Memcached $memcached, ImmutableSettingsInterface $options = null) { |
|
| 35 | 35 | parent::__construct($options); |
| 36 | 36 | |
| 37 | 37 | $this->memcached = $memcached; |
| 38 | 38 | |
| 39 | - if(empty($this->memcached->getServerList())){ |
|
| 39 | + if (empty($this->memcached->getServerList())) { |
|
| 40 | 40 | $msg = 'no memcache server available'; |
| 41 | 41 | |
| 42 | 42 | $this->logger->error($msg); |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** @inheritdoc */ |
| 49 | - public function get(string $key, $default = null){ |
|
| 49 | + public function get(string $key, $default = null) { |
|
| 50 | 50 | $value = $this->memcached->get($key); |
| 51 | 51 | |
| 52 | 52 | return $value ?: $default; |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | $values = $this->memcached->getMulti($keys); |
| 73 | 73 | $return = []; |
| 74 | 74 | |
| 75 | - foreach($keys as $key){ |
|
| 75 | + foreach ($keys as $key) { |
|
| 76 | 76 | $return[$key] = $values[$key] ?? $default; |
| 77 | 77 | } |
| 78 | 78 | |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | /** |
| 16 | 16 | * @method setLogger(\Psr\Log\LoggerInterface $logger) |
| 17 | 17 | */ |
| 18 | -interface CacheDriverInterface{ |
|
| 18 | +interface CacheDriverInterface { |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * @param string $key |
@@ -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 {} |
|
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * @param \chillerlan\SimpleCache\Drivers\CacheDriverInterface $cacheDriver |
| 38 | 38 | * @param \Psr\Log\LoggerInterface $logger |
| 39 | 39 | */ |
| 40 | - public function __construct(CacheDriverInterface $cacheDriver, LoggerInterface $logger = null){ |
|
| 40 | + public function __construct(CacheDriverInterface $cacheDriver, LoggerInterface $logger = null) { |
|
| 41 | 41 | $this->cacheDriver = $cacheDriver; |
| 42 | 42 | $this->setLogger($logger ?? new NullLogger); |
| 43 | 43 | } |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** @inheritdoc */ |
| 58 | - public function get($key, $default = null){ |
|
| 58 | + public function get($key, $default = null) { |
|
| 59 | 59 | $this->checkKey($key); |
| 60 | 60 | |
| 61 | 61 | return $this->cacheDriver->get($key, $default); |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | public function setMultiple($values, $ttl = null):bool{ |
| 93 | 93 | $values = $this->getData($values); |
| 94 | 94 | |
| 95 | - foreach($values as $key => $value){ |
|
| 95 | + foreach ($values as $key => $value) { |
|
| 96 | 96 | $this->checkKey($key); |
| 97 | 97 | } |
| 98 | 98 | |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | protected function checkKey($key):void{ |
| 124 | 124 | |
| 125 | - if(!is_string($key) || empty($key)){ |
|
| 125 | + if (!is_string($key) || empty($key)) { |
|
| 126 | 126 | $msg = 'invalid cache key: "'.$key.'"'; |
| 127 | 127 | $this->logger->error($msg); |
| 128 | 128 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | */ |
| 139 | 139 | protected function checkKeyArray(array $keys):void{ |
| 140 | 140 | |
| 141 | - foreach($keys as $key){ |
|
| 141 | + foreach ($keys as $key) { |
|
| 142 | 142 | $this->checkKey($key); |
| 143 | 143 | } |
| 144 | 144 | |
@@ -152,10 +152,10 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | protected function getData($data):array{ |
| 154 | 154 | |
| 155 | - if($data instanceof Traversable){ |
|
| 155 | + if ($data instanceof Traversable) { |
|
| 156 | 156 | return iterator_to_array($data); // @codeCoverageIgnore |
| 157 | 157 | } |
| 158 | - else if(is_array($data)){ |
|
| 158 | + else if (is_array($data)) { |
|
| 159 | 159 | return $data; |
| 160 | 160 | } |
| 161 | 161 | |
@@ -171,12 +171,12 @@ discard block |
||
| 171 | 171 | * @return int|null |
| 172 | 172 | * @throws \chillerlan\SimpleCache\InvalidArgumentException |
| 173 | 173 | */ |
| 174 | - protected function getTTL($ttl):?int{ |
|
| 174 | + protected function getTTL($ttl): ?int{ |
|
| 175 | 175 | |
| 176 | - if($ttl instanceof DateInterval){ |
|
| 176 | + if ($ttl instanceof DateInterval) { |
|
| 177 | 177 | return (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
| 178 | 178 | } |
| 179 | - else if(is_int($ttl) || $ttl === null){ |
|
| 179 | + else if (is_int($ttl) || $ttl === null) { |
|
| 180 | 180 | return $ttl; |
| 181 | 181 | } |
| 182 | 182 | |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | * |
| 192 | 192 | * @throws \chillerlan\SimpleCache\CacheException |
| 193 | 193 | */ |
| 194 | - protected function throwException(string $message){ |
|
| 194 | + protected function throwException(string $message) { |
|
| 195 | 195 | $this->logger->error($message); |
| 196 | 196 | |
| 197 | 197 | throw new InvalidArgumentException($message); |
@@ -154,8 +154,7 @@ discard block |
||
| 154 | 154 | |
| 155 | 155 | if($data instanceof Traversable){ |
| 156 | 156 | return iterator_to_array($data); // @codeCoverageIgnore |
| 157 | - } |
|
| 158 | - else if(is_array($data)){ |
|
| 157 | + } else if(is_array($data)){ |
|
| 159 | 158 | return $data; |
| 160 | 159 | } |
| 161 | 160 | |
@@ -175,8 +174,7 @@ discard block |
||
| 175 | 174 | |
| 176 | 175 | if($ttl instanceof DateInterval){ |
| 177 | 176 | return (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
| 178 | - } |
|
| 179 | - else if(is_int($ttl) || $ttl === null){ |
|
| 177 | + } else if(is_int($ttl) || $ttl === null){ |
|
| 180 | 178 | return $ttl; |
| 181 | 179 | } |
| 182 | 180 | |