| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @link https://github.com/LAV45/yii2-settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Copyright (c) 2016 LAV45 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Alexey Loban <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license http://opensource.org/licenses/BSD-3-Clause | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace lav45\settings\storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use yii\helpers\VarDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class PhpFileStorage extends FileStorage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var string settings file suffix. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public $fileSuffix = '.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 3 |  |     public function getValue($key) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 3 |  |         $fileName = $this->getFile($key); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 3 |  |         if (file_exists($fileName)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 3 |  |             return include $fileName; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 3 |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 3 |  |     public function setValue($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  |         $value = is_string($value) ? "'{$value}'" : VarDumper::export($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |         $value = "<?php\nreturn {$value};\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |         $result = parent::setValue($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |         if ($result === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |             $fileName = $this->getFile($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |             $this->forceScriptCache($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Forcibly caches data of this file in OPCache or APC. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param string $fileName file name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @since 1.0.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 3 |  |     protected function forceScriptCache($fileName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |             (PHP_SAPI !== 'cli' && ini_get('opcache.enable')) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |             ini_get('opcache.enable_cli') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             opcache_invalidate($fileName, true); // @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             opcache_compile_file($fileName); // @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |         if (ini_get('apc.enabled')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             apc_delete_file($fileName); // @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             apc_bin_loadfile($fileName); // @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | } |