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
|
9 |
|
public function onInject(array $injectAnnotation) |
36
|
|
|
{ |
37
|
9 |
|
$this->injectAnnotation = $injectAnnotation; |
38
|
9 |
|
$this->readAnnotation = []; |
39
|
9 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
5 |
|
public function getAnnotationInfo(): array |
45
|
|
|
{ |
46
|
5 |
|
return $this->readAnnotation; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
9 |
|
public function onMethodInject(IAnnotatable $instance, \ReflectionMethod $method, Container $container) |
53
|
|
|
{ |
54
|
9 |
|
$filename = array_key_exists('value', $this->injectAnnotation) ? $this->injectAnnotation['value'] : null; |
55
|
9 |
|
$engine = array_key_exists('engine', $this->injectAnnotation) ? $this->injectAnnotation['engine'] : "basic"; |
56
|
9 |
|
$debug = array_key_exists('debug', $this->injectAnnotation) ? $this->injectAnnotation['debug'] : false; |
57
|
9 |
|
$cacheTime = array_key_exists('cacheTime', $this->injectAnnotation) ? $this->injectAnnotation['cacheTime'] : null; |
58
|
9 |
|
$logger = $container->logger; |
|
|
|
|
59
|
|
|
|
60
|
9 |
|
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
|
9 |
|
if (preg_match("/:|\.\.\/|\.\.\\\\/", $filename)) { |
66
|
|
|
throw new AnnotationException("Invalid string contains in @Template('$filename')"); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
9 |
|
if ($filename === null) { |
71
|
1 |
|
$errorMsg = "Invalid argument of @Template('$filename'). There is no specification of the base template."; |
72
|
1 |
|
throw new AnnotationException($errorMsg); |
73
|
|
|
} |
74
|
|
|
|
75
|
8 |
|
if ($engine === "twig") { |
76
|
3 |
|
if (!is_bool($debug)) { |
77
|
1 |
|
if ($debug !== null) { |
78
|
1 |
|
$errorMsg = "Invalid argument of @Template('$filename'). 'debug' attribute bool only be specified."; |
79
|
1 |
|
throw new AnnotationException($errorMsg); |
80
|
|
|
} |
81
|
|
|
$debug = false; |
82
|
|
|
} |
83
|
2 |
|
$this->readAnnotation['filename'] = $filename; |
84
|
2 |
|
$this->readAnnotation['engine'] = $container->engine['twig']; |
|
|
|
|
85
|
2 |
|
$this->readAnnotation['debug'] = $debug; |
86
|
|
|
|
87
|
2 |
|
if ($cacheTime !== null) { |
88
|
2 |
|
$logger->warn("'cacheTime' attribute is not used in Twig template."); |
|
|
|
|
89
|
|
|
} |
90
|
5 |
|
} elseif ($engine === "basic") { |
91
|
4 |
|
if ($cacheTime !== null) { |
92
|
|
|
// 複数指定は不可 |
93
|
2 |
|
if (is_array($cacheTime)) { |
94
|
|
|
$errorMsg = "Invalid argument of @Template attribute 'cacheTime' should not be array."; |
95
|
|
|
throw new AnnotationException($errorMsg); |
96
|
|
|
} |
97
|
|
|
// 数値以外は不可 |
98
|
2 |
|
if (!preg_match("/^[1-9]{1}[0-9]{0,}$/", $cacheTime)) { |
99
|
1 |
|
$errorMsg = "Invalid argument of @Template attribute 'cacheTime' should not be integer."; |
100
|
1 |
|
throw new AnnotationException($errorMsg); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$cacheTime = intval($cacheTime); |
104
|
1 |
|
if ($cacheTime <= 0) { |
105
|
|
|
$errorMsg = "Expire value is out of integer range: @Template(cacheTime=" . strval($cacheTime) . ")"; |
106
|
|
|
throw new AnnotationException($errorMsg); |
107
|
1 |
|
} elseif ($cacheTime >= PHP_INT_MAX) { |
108
|
|
|
$logger->warn("Expire value converted the maximum of PHP Integer."); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
$this->readAnnotation['cacheTime'] = $cacheTime; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
$this->readAnnotation['filename'] = $filename; |
115
|
3 |
|
$this->readAnnotation['engine'] = $container->engine['basic']; |
116
|
|
|
|
117
|
3 |
|
if ($debug !== null) { |
118
|
3 |
|
$logger->warn("'debug' attribute is not used in Basic template."); |
119
|
|
|
} |
120
|
|
|
} else { |
121
|
1 |
|
$errorMsg = "Invalid 'engine' attribute of @Template('$filename')."; |
122
|
1 |
|
throw new AnnotationException($errorMsg); |
123
|
|
|
} |
124
|
5 |
|
} |
125
|
|
|
} |
126
|
|
|
|