| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace kalanis\kw_cache_psr\Adapters\SimpleCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DateInterval; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use kalanis\kw_cache\CacheException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use kalanis\kw_cache\Interfaces\IFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use kalanis\kw_cache_psr\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use kalanis\kw_cache_psr\Traits\TCheckKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use kalanis\kw_files\Access\CompositeAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use kalanis\kw_files\FilesException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use kalanis\kw_files\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use kalanis\kw_paths\ArrayPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use kalanis\kw_paths\PathsException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Psr\SimpleCache\CacheInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Class FilesAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @package kalanis\kw_cache_psr\Adapters\SimpleCache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Files adapter for PSR Cache Interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class FilesAdapter implements CacheInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     use TCheckKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected CompositeAdapter $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected IFormat $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected ArrayPath $arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** @var string[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected array $initialPath = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param CompositeAdapter $files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param IFormat $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string[] $initialPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 9 |  |     public function __construct(CompositeAdapter $files, IFormat $format, array $initialPath = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 9 |  |         $this->files = $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 9 |  |         $this->format = $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 9 |  |         $this->initialPath = $initialPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 9 |  |         $this->arr = new ArrayPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param mixed $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return mixed|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 4 |  |     public function get($key, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |             if ($this->has($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |                 return $this->format->unpack($this->files->readFile($this->fullKey($key))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |             return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         } catch (CacheException | FilesException | PathsException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |             return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param DateInterval|int|null $ttl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 5 |  |     public function set($key, $value, $ttl = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 5 |  |             return $this->files->saveFile($this->fullKey($key), strval($this->format->pack($value))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         } catch (CacheException | FilesException | PathsException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |     public function delete($key): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 3 |  |             return $this->files->deleteFile($this->fullKey($key)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         } catch (FilesException | PathsException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 3 |  |     public function clear(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |             $result = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 3 |  |             foreach ($this->files->readDir($this->initialPath) as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 /** @var Node $item */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |                 $result = $result && $this->files->deleteFile($item->getPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         } catch (FilesException | PathsException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param iterable<string|int, string> $keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param mixed $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return iterable<string, mixed> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |     public function getMultiple($keys, $default = null): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |         foreach ($keys as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |             $result[$key] = $this->get($key, $default); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param iterable<string, mixed> $values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param null|int|DateInterval $ttl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |     public function setMultiple($values, $ttl = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |         $result = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |         foreach ($values as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |             $result = $result && $this->set($key, $value, $ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param iterable<string|int, string> $keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 144 | 1 |  |     public function deleteMultiple($keys): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 146 | 1 |  |         $result = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 147 | 1 |  |         foreach ($keys as $item) { | 
            
                                                                        
                            
            
                                    
            
            
                | 148 | 1 |  |             $result = $result && $this->delete($item); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 150 | 1 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 6 |  |     public function has($key): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 6 |  |             $useKey = $this->fullKey($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 6 |  |             return $this->files->exists($useKey) && $this->files->isFile($useKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |         } catch (FilesException | PathsException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @param string $initialKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @throws PathsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 8 |  |     protected function fullKey(string $initialKey): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 8 |  |         return array_values($this->initialPath + $this->arr->setString($this->checkKey($initialKey))->getArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 178 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |  |