| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace WebStream\Annotation\Attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use WebStream\Annotation\Base\Annotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use WebStream\Annotation\Base\IAnnotatable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use WebStream\Annotation\Base\IMethod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use WebStream\Annotation\Base\IRead; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use WebStream\Container\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use WebStream\Exception\Extend\AnnotationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author Ryuichi TANAKA. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @since 2013/10/10 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @version 0.7 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @Annotation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @Target("METHOD") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class Template extends Annotation implements IMethod, IRead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var array<string> 注入アノテーション情報 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $injectAnnotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var array<string> 読み込みアノテーション情報 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $readAnnotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     public function onInject(array $injectAnnotation) | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $this->injectAnnotation = $injectAnnotation; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $this->readAnnotation = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function getAnnotationInfo(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $this->readAnnotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function onMethodInject(IAnnotatable $instance, \ReflectionMethod $method, Container $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $filename = array_key_exists('value', $this->injectAnnotation) ? $this->injectAnnotation['value'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $engine = array_key_exists('engine', $this->injectAnnotation) ? $this->injectAnnotation['engine'] : "basic"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $debug = array_key_exists('debug', $this->injectAnnotation) ? $this->injectAnnotation['debug'] : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $cacheTime = array_key_exists('cacheTime', $this->injectAnnotation) ? $this->injectAnnotation['cacheTime'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $logger = $container->logger; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (PHP_OS === "WIN32" || PHP_OS === "WINNT") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             if (preg_match("/^.*[. ]|.*[\p{Cntrl}\/:*?\"<>|].*|(?i:CON|PRN|AUX|CLOCK\$|NUL|COM[1-9]|LPT[1-9])(?:[.].+)?$/", $filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 throw new AnnotationException("Invalid string contains in @Template('$filename')"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if (preg_match("/:|\.\.\/|\.\.\\\\/", $filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 throw new AnnotationException("Invalid string contains in @Template('$filename')"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if ($filename === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $errorMsg = "Invalid argument of @Template('$filename'). There is no specification of the base template."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             throw new AnnotationException($errorMsg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if ($engine === "twig") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             if (!is_bool($debug)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 if ($debug !== null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     $errorMsg = "Invalid argument of @Template('$filename'). 'debug' attribute bool only be specified."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     throw new AnnotationException($errorMsg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $debug = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $this->readAnnotation['filename'] = $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->readAnnotation['engine'] = $container->engine['twig']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $this->readAnnotation['debug'] = $debug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             if ($cacheTime !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 $logger->warn("'cacheTime' attribute is not used in Twig template."); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } elseif ($engine === "basic") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             if ($cacheTime !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 // 複数指定は不可 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 if (is_array($cacheTime)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     $errorMsg = "Invalid argument of @Template attribute 'cacheTime' should not be array."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     throw new AnnotationException($errorMsg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 // 数値以外は不可 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 if (!preg_match("/^[1-9]{1}[0-9]{0,}$/", $cacheTime)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     $errorMsg = "Invalid argument of @Template attribute 'cacheTime' should not be integer."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     throw new AnnotationException($errorMsg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $cacheTime = intval($cacheTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 if ($cacheTime <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     $errorMsg = "Expire value is out of integer range: @Template(cacheTime=" . strval($cacheTime) . ")"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     throw new AnnotationException($errorMsg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 } elseif ($cacheTime >= PHP_INT_MAX) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                     $logger->warn("Expire value converted the maximum of PHP Integer."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $this->readAnnotation['cacheTime'] = $cacheTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $this->readAnnotation['filename'] = $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $this->readAnnotation['engine'] = $container->engine['basic']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             if ($debug !== null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $logger->warn("'debug' attribute is not used in Basic template."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $errorMsg = "Invalid 'engine' attribute of @Template('$filename')."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             throw new AnnotationException($errorMsg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  |