1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebStream\Annotation\Attributes; |
4
|
|
|
|
5
|
|
|
use WebStream\Annotation\Base\Annotation; |
6
|
|
|
use WebStream\Annotation\Base\IAnnotatable; |
7
|
|
|
use WebStream\Annotation\Base\IMethod; |
8
|
|
|
use WebStream\Annotation\Base\IRead; |
9
|
|
|
use WebStream\Container\Container; |
|
|
|
|
10
|
|
|
use WebStream\Exception\Extend\AnnotationException; |
|
|
|
|
11
|
|
|
use WebStream\Exception\Extend\InvalidRequestException; |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Header |
15
|
|
|
* @author Ryuichi TANAKA. |
16
|
|
|
* @since 2013/10/20 |
17
|
|
|
* @version 0.4 |
18
|
|
|
* |
19
|
|
|
* @Annotation |
20
|
|
|
* @Target("METHOD") |
21
|
|
|
*/ |
22
|
|
|
class Header extends Annotation implements IMethod, IRead |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array<string> 注入アノテーション情報 |
26
|
|
|
*/ |
27
|
|
|
private array $injectAnnotation; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array<string> 読み込みアノテーション情報 |
31
|
|
|
*/ |
32
|
|
|
private array $readAnnotation; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array<string, string> mimeタイプリスト |
36
|
|
|
*/ |
37
|
|
|
private array $contentTypeList = [ |
38
|
|
|
'txt' => 'text/plain', |
39
|
|
|
'jpeg' => 'image/jpeg', |
40
|
|
|
'jpg' => 'image/jpeg', |
41
|
|
|
'gif' => 'image/gif', |
42
|
|
|
'png' => 'image/png', |
43
|
|
|
'tiff' => 'image/tiff', |
44
|
|
|
'tif' => 'image/tiff', |
45
|
|
|
'bmp' => 'image/bmp', |
46
|
|
|
'ico' => 'image/x-icon', |
47
|
|
|
'svg' => 'image/svg+xml', |
48
|
|
|
'xml' => 'application/xml', |
49
|
|
|
'xsl' => 'application/xml', |
50
|
|
|
'rss' => 'application/rss+xml', |
51
|
|
|
'rdf' => 'application/rdf+xml', |
52
|
|
|
'atom' => 'application/atom+xml', |
53
|
|
|
'zip' => 'application/zip', |
54
|
|
|
'html' => 'text/html', |
55
|
|
|
'htm' => 'text/html', |
56
|
|
|
'css' => 'text/css', |
57
|
|
|
'csv' => 'text/csv', |
58
|
|
|
'js' => 'text/javascript', |
59
|
|
|
'jsonp' => 'text/javascript', |
60
|
|
|
'json' => 'application/json', |
61
|
|
|
'pdf' => 'application/pdf', |
62
|
|
|
'file' => 'application/octet-stream' |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
16 |
|
public function onInject(array $injectAnnotation) |
69
|
|
|
{ |
70
|
16 |
|
$defaultInjectAnnotation = ['allowMethod' => null, 'contentType' => null]; |
71
|
16 |
|
$this->injectAnnotation = array_merge($defaultInjectAnnotation, $injectAnnotation); |
72
|
16 |
|
$this->readAnnotation = []; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
5 |
|
public function getAnnotationInfo(): array |
79
|
|
|
{ |
80
|
5 |
|
return $this->readAnnotation; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
11 |
|
public function onMethodInject(IAnnotatable $instance, \ReflectionMethod $method, Container $container) |
87
|
|
|
{ |
88
|
11 |
|
$allowMethods = $this->injectAnnotation['allowMethod']; |
89
|
11 |
|
$logger = $container->logger; |
90
|
|
|
|
91
|
|
|
// 指定無しの場合はチェックしない(すべてのメソッドを許可する) |
92
|
11 |
|
if ($allowMethods !== null) { |
93
|
8 |
|
if (!is_array($allowMethods)) { |
94
|
6 |
|
$allowMethods = [$allowMethods]; |
95
|
|
|
} |
96
|
|
|
|
97
|
8 |
|
for ($i = 0; $i < count($allowMethods); $i++) { |
|
|
|
|
98
|
8 |
|
if (!preg_match("/^(?:(?:P(?:OS|U)|GE)T|(?:p(?:os|u)|ge)t|DELETE|delete)$/", $allowMethods[$i])) { |
99
|
1 |
|
$errorMsg = "Invalid value '" . $allowMethods[$i] . "' in 'allowMethod' attribute of @Header."; |
100
|
1 |
|
throw new AnnotationException($errorMsg); |
101
|
|
|
} |
102
|
7 |
|
$allowMethods[$i] = strtoupper($allowMethods[$i]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// 複数指定した場合、一つでも許可されていればOK |
106
|
7 |
|
if (!array_key_exists($container->requestMethod, array_flip($allowMethods))) { |
107
|
3 |
|
$errorMsg = "Not allowed request method '" . $container->requestMethod; |
108
|
3 |
|
throw new InvalidRequestException($errorMsg); |
109
|
|
|
} |
110
|
|
|
|
111
|
4 |
|
$logger->debug("Accepted method '" . implode(',', $allowMethods) . "'"); |
112
|
|
|
} |
113
|
|
|
|
114
|
7 |
|
$ext = $this->injectAnnotation['contentType'] ?: 'html'; |
115
|
|
|
|
116
|
7 |
|
if (!is_string($ext)) { |
117
|
1 |
|
$errorMsg = "contentType' attribute of @Header must be string."; |
118
|
1 |
|
throw new AnnotationException($errorMsg); |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
$contentType = null; |
122
|
6 |
|
if (array_key_exists($ext, $this->contentTypeList)) { |
123
|
5 |
|
$contentType = $this->contentTypeList[$ext]; |
124
|
|
|
} |
125
|
6 |
|
if ($contentType === null) { |
126
|
1 |
|
$errorMsg = "Invalid value '$ext' in 'contentType' attribute of @Header."; |
127
|
1 |
|
throw new AnnotationException($errorMsg); |
128
|
|
|
} |
129
|
|
|
|
130
|
5 |
|
$this->readAnnotation['contentType'] = $ext; |
131
|
5 |
|
$logger->debug("Accepted contentType '$ext'"); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths