| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the CRUDlex package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Philip Lehmann-Böhm <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace CRUDlex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Reads Yaml files and caches them if a writable path is given. The cache is used first on the next read. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * It is a simple PHP class internally. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class YamlReader { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * The path for the cache files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $cachePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Gets an absolute path for a cache file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * the file to cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * the absolute path of the cache file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected function getCacheFile($fileName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $this->cachePath.'/'.basename($fileName).'CRUDlexCache.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Reads the content of the cached file if it exists. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * the cache file to read from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return null|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * the cached data structure or null if the cache file was not available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected function readFromCache($fileName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $cacheFile = $this->getCacheFile($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (file_exists($cacheFile) && is_readable($cacheFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             include($cacheFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             if (isset($crudlexCacheContent)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 return $crudlexCacheContent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Writes the given content to a cached PHP file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * the original filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param array $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * the content to cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     protected function writeToCache($fileName, $content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($this->cachePath === null || !is_dir($this->cachePath) || !is_writable($this->cachePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $encoder    = new \Riimu\Kit\PHPEncoder\PHPEncoder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $contentPHP = $encoder->encode($content, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'whitespace' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             'recursion.detect' => false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $cache = '<?php $crudlexCacheContent = '.$contentPHP.';'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         file_put_contents($this->getCacheFile($fileName), $cache); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * YamlReader constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param string|null $cachePath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * if given, the path for the cache files which should be a writable directory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function __construct($cachePath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->cachePath = $cachePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Reads and returns the contents of the given Yaml file. If | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * it goes wrong, it throws an exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * the file to read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * the file contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * thrown if the file could not be read or parsed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     public function read($fileName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         $parsedYaml = $this->readFromCache($fileName); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         if ($parsedYaml !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             return $parsedYaml; | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             $fileContent = file_get_contents($fileName); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             $parsedYaml  = Yaml::parse($fileContent); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |             if (!is_array($parsedYaml)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 $parsedYaml = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             $this->writeToCache($fileName, $parsedYaml); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             return $parsedYaml; | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         } catch (\Exception $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |             throw new \RuntimeException('Could not read Yaml file '.$fileName, $e->getCode(), $e); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                        
This check looks for calls to
isset(...)orempty()on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.