| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is, guess what, part of WebHelper. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) James <[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 JamesRezo\WebHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Composer\Semver\VersionParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use \Twig_Loader_Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use \Twig_Environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * WebHelper Repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class WebHelperRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var Finder a Finder instance */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var VersionParser a VersionParser instance */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $versionParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var array a structured array of a directives repository */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $memoize = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** @var Twig_Environment a Twig_Environment instance */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $twig = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Base constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string $resDir the Path of a Directives Repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 9 |  |     public function __construct($resDir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 9 |  |         $this->finder = new Finder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 9 |  |         $this->versionParser = new VersionParser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 9 |  |         if ($resDir !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 8 |  |             $this->memoize = $this->memoize($resDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 8 |  |             $this->twig = $this->initialize($resDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 8 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * Initialize the Twig Environment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param  string           $resDir the Path of a Directives Repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return Twig_Environment         the Twig Environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 8 |  |     private function initialize($resDir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 8 |  |             $loader = new Twig_Loader_Filesystem($resDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 8 |  |         } catch (\Twig_Error_Loader $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |         $twig = new Twig_Environment($loader, array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 6 |  |             'cache' => __DIR__.'/../var/cache', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 6 |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 6 |  |         return $twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Initialize the structured array of a directives repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param  string $resDir the Path of a Directives Repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return array          the structured array of a directives repository | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 8 |  |     private function memoize($resDir) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 8 |  |             $this->finder->files()->name('*.twig')->in($resDir); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 8 |  |         } catch (\InvalidArgumentException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 2 |  |             return []; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 6 |  |         $memoize = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 6 |  |         foreach ($this->finder as $file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 6 |  |             $parsedPath = explode('/', $file->getRelativePathname()); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 6 |  |             if (count($parsedPath) == 2) { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 6 |  |                 $parsedPath[2] = $parsedPath[1]; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 6 |  |                 $parsedPath[1] = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 6 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 6 |  |             $parsedPath[2] = str_replace('.twig', '', $parsedPath[2]); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 6 |  |             $memoize[$parsedPath[0]] | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 6 |  |                 [$this->versionParser->normalize($parsedPath[1])] | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 6 |  |                 [$parsedPath[2]] = $file->getRelativePathname(); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 6 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 6 |  |         return $memoize; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Gets the structured array of a directives repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return array the structured array of a directives repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 3 |  |     public function getMemoize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |         return $this->memoize; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Gets the Twig Environment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return Twig_Environment the Twig Environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 3 |  |     public function getTwig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 3 |  |         return $this->twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Tells if the Repository can be used. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @return boolean true if there are some directives in the Path of a Directives Repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 5 |  |     public function okGo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 5 |  |         return !empty($this->memoize) && is_a($this->twig, 'Twig_Environment'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  |