| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace suda\application\template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use function constant; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use function is_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use function is_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use suda\application\template\compiler\Compiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use suda\framework\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use suda\application\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use suda\framework\filesystem\FileSystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * 模块模板 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class ModuleTemplate extends ModuleTemplateBase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * 获取模板源路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function getSourcePath():?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $subfix = $this->config['subfix'] ?? '.tpl.html'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         return $this->getResource($this->module)->getResourcePath($this->getTemplatePath().'/'.$this->name.$subfix); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * 获取模板编译后的路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function getPath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $output = $this->config['output'] ?? $this->application->getDataPath().'/template/'. $this->uriName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         FileSystem::make($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return $output .'/'. $this->name.'.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * 包含模板 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function include(string $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $included = new self($name, $this->application, $this->request, $this->module); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $included->parent = $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $included->value = $this->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         echo $included->getRenderedString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function getRenderedString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->application->debug()->time('render '.$this->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $code =  parent::getRenderedString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->application->debug()->timeEnd('render '.$this->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     protected function compile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if ($this->isCompiled() === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->application->debug()->time('compile '.$this->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $result = parent::compile(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $this->application->debug()->timeEnd('compile '.$this->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     protected function createCompiler(): Compiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $compiler = parent::createCompiler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->application->event()->exec( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 'application:template:compile::create', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 [$compiler, $this->config, $this->application] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } catch (\ReflectionException $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return $compiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     public function getUrl($name = null, $values = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $defaultName = $this->request->getAttribute('route'); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         if (is_string($name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             if (!is_array($values)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 $args = func_get_args(); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                 array_shift($args); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                 $values = $args; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             return $this->application->getUrl($this->request, $name, $values ?? [], true, $this->module, $this->group); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         } elseif (is_array($name) && is_string($defaultName)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             return $this->application->getUrl( | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |                 $this->request, | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                 $defaultName, | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 array_merge($this->request->get() ?? [], $name), | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                 true, | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                 $this->module, | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 $this->group | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         } elseif (is_string($defaultName)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             return $this->application->getUrl( | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                 $this->request, | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                 $defaultName, | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 $this->request->get() ?? [], | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 true, | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 $this->module, | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |                 $this->group | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         return '#'.$defaultName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * 判断是否是某路由 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param array $parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function is(string $name, array $parameter = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $full = $this->application->getRouteName($name, $this->module, $this->group); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         if ($this->request->getAttribute('route') === $full) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             if (is_array($parameter)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 foreach ($parameter as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     if ($this->request->getQuery($key) != $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  |