| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Platine Template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Platine Template is a template engine that has taken a lot of inspiration from Django. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This content is released under the MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Copyright (c) 2020 Platine Template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Copyright (c) 2014 Guz Alexander, http://guzalexander.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Copyright (c) 2006 Mateo Murphy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * in the Software without restriction, including without limitation the rights | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * furnished to do so, subject to the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * The above copyright notice and this permission notice shall be included in all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * SOFTWARE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  *  @file FileCache.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  *  The File system Template cache class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  *  @package    Platine\Template\Cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  *  @author Platine Developers Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  *  @copyright  Copyright (c) 2020 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  *  @license    http://opensource.org/licenses/MIT  MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  *  @link   https://www.platine-php.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  *  @version 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  *  @filesource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | namespace Platine\Template\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | use Platine\Template\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | use Platine\Template\Exception\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | use Platine\Template\Util\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  * @class FileCache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  * @package Platine\Template\Cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | class FileCache extends AbstractCache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * The cache directory to use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     protected string $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Create new instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param Configuration|null $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function __construct(?Configuration $config = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         parent::__construct($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $dir = $this->config->get('cache_dir'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $path = Helper::normalizePath($dir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $realPath = realpath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if ($realPath === false || !is_writable($realPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             throw new NotFoundException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 'The cache directory [%s] does not exist or is not writable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->path = $realPath . DIRECTORY_SEPARATOR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function read(string $key, bool $unserialize = true): mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if ($this->exists($key) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $file = $this->getFilePath($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $content = (string) file_get_contents($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if ($unserialize) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             return unserialize($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function exists(string $key): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $file = $this->getFilePath($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         if (file_exists($file) === false || (filemtime($file) + $this->expire) < time()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function write(string $key, mixed $value, bool $serialize = true): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $file = $this->getFilePath($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if ($serialize) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $value = serialize($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $bytes = file_put_contents($file, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $this->clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return $bytes !== false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     public function flush(bool $expired = false): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $list = glob(sprintf('%s%s*', $this->path, $this->prefix)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         if ($list !== false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             foreach ($list as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 if ($expired) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                     if ((filemtime($file) + $this->expire) < time()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                         unlink($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                     unlink($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * Garbage collector of cache file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 163 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |     protected function clean(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |         $this->flush(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * Return the file path for the given key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     private function getFilePath(string $key): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         return sprintf('%s%s%s', $this->path, $this->prefix, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 178 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |  |