1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebStream\Annotation\Attributes; |
4
|
|
|
|
5
|
|
|
use WebStream\Annotation\Attributes\Ext\ValidateRule\IValidate; |
6
|
|
|
use WebStream\Annotation\Base\Annotation; |
7
|
|
|
use WebStream\Annotation\Base\IAnnotatable; |
8
|
|
|
use WebStream\Annotation\Base\IMethod; |
9
|
|
|
use WebStream\ClassLoader\ClassLoader; |
|
|
|
|
10
|
|
|
use WebStream\Container\Container; |
|
|
|
|
11
|
|
|
use WebStream\Exception\Extend\AnnotationException; |
|
|
|
|
12
|
|
|
use WebStream\Exception\Extend\InvalidRequestException; |
|
|
|
|
13
|
|
|
use WebStream\Exception\Extend\ValidateException; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Validate |
17
|
|
|
* @author Ryuichi TANAKA. |
18
|
|
|
* @since 2015/03/30 |
19
|
|
|
* @version 0.4 |
20
|
|
|
* |
21
|
|
|
* @Annotation |
22
|
|
|
* @Target("METHOD") |
23
|
|
|
*/ |
24
|
|
|
class Validate extends Annotation implements IMethod |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array<string> 注入アノテーション情報 |
28
|
|
|
*/ |
29
|
|
|
private array $injectAnnotation; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
80 |
|
public function onInject(array $injectAnnotation) |
35
|
|
|
{ |
36
|
80 |
|
$this->injectAnnotation = $injectAnnotation; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
80 |
|
public function onMethodInject(IAnnotatable $instance, \ReflectionMethod $method, Container $container) |
43
|
|
|
{ |
44
|
80 |
|
$key = $this->injectAnnotation['key']; |
45
|
80 |
|
$rule = $this->injectAnnotation['rule']; |
46
|
80 |
|
$method = array_key_exists('method', $this->injectAnnotation) ? |
47
|
80 |
|
$this->injectAnnotation['method'] : "get"; |
48
|
|
|
|
49
|
80 |
|
if ($method !== null && !array_key_exists($method, array_flip(["get", "post", "put", "delete"]))) { |
50
|
|
|
throw new ValidateException("Invalid method attribute is specified: " . $method); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// パラメータの有無にかかわらずルール定義が間違っている場合はエラー |
54
|
80 |
|
if (preg_match('/^([a-zA-Z][a-zA-Z0-9_]+)(?:$|\[(.+?)\]$)/', $rule, $matches)) { |
55
|
80 |
|
$className = ucfirst(preg_replace_callback('/_([a-zA-Z])/', function ($matches) { |
56
|
16 |
|
return ucfirst($matches[1]); |
57
|
80 |
|
}, $matches[1])); |
58
|
80 |
|
$classpath = null; |
59
|
80 |
|
$classLoader = new ClassLoader($container->applicationInfo->applicationRoot); |
60
|
80 |
|
$classLoader->inject('logger', $container->logger); |
61
|
80 |
|
$ignoreDir = $container->applicationInfo->externalLibraryRoot; |
62
|
80 |
|
$fileName = $className . '.php'; |
63
|
80 |
|
$isLoaded = $classLoader->import($fileName, function ($filepath) use ($ignoreDir) { |
64
|
|
|
if ($ignoreDir === null || $ignoreDir === "") { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
$pos = strpos($filepath, $ignoreDir); |
68
|
|
|
return $pos === false || $pos !== 0; |
69
|
80 |
|
}); |
70
|
|
|
|
71
|
|
|
// デフォルトバリデーションルールのパス |
72
|
80 |
|
if (!$isLoaded) { |
73
|
80 |
|
$loadList = $classLoader->load($className); |
74
|
80 |
|
$loadListWithoutIgnorePathList = []; |
75
|
80 |
|
foreach ($loadList as $path) { |
76
|
80 |
|
$pos = strpos($path, $ignoreDir); |
77
|
80 |
|
if ($pos === false || $pos !== 0) { |
78
|
80 |
|
$loadListWithoutIgnorePathList[] = $path; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// バリデーションルールのクラス名が複数指定されている場合は適用判断不可能なのでエラー |
83
|
80 |
|
if (count($loadListWithoutIgnorePathList) >= 2) { |
84
|
|
|
$errorMsg = "Class load failed because the same class name has been identified: " . $className . ""; |
85
|
|
|
throw new ValidateException($errorMsg); |
86
|
|
|
} |
87
|
|
|
|
88
|
80 |
|
if (count($loadListWithoutIgnorePathList) === 0) { |
89
|
|
|
$errorMsg = "Invalid Validate class: " . $className . ""; |
90
|
|
|
throw new ValidateException($errorMsg); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
80 |
|
$namespaces = $classLoader->getNamespaces($fileName); |
95
|
80 |
|
foreach ($namespaces as $namespace) { |
96
|
80 |
|
if (strpos(IValidate::class, $namespace) === 0) { |
97
|
80 |
|
$classpath = $namespace . "\\" . $className; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
80 |
|
if (!class_exists($classpath)) { |
102
|
|
|
$errorMsg = "Invalid Validate class's classpath: " . $classpath; |
103
|
|
|
throw new AnnotationException($errorMsg); |
104
|
|
|
} |
105
|
|
|
|
106
|
80 |
|
$validateInstance = new $classpath(); |
107
|
80 |
|
$params = null; |
108
|
80 |
|
if ($container->request->requestMethod === 'GET') { |
109
|
20 |
|
if ($method === null || "get" === mb_strtolower($method)) { |
110
|
20 |
|
$params = $container->request->get; |
111
|
|
|
} |
112
|
60 |
|
} elseif ($container->request->requestMethod === 'POST') { |
113
|
20 |
|
if ($method === null || "post" === mb_strtolower($method)) { |
114
|
19 |
|
$params = $container->request->post; |
115
|
|
|
} |
116
|
40 |
|
} elseif ($container->request->requestMethod === 'PUT') { |
117
|
20 |
|
if ($method === null || "put" === mb_strtolower($method)) { |
118
|
19 |
|
$params = $container->request->put; |
119
|
|
|
} |
120
|
20 |
|
} elseif ($container->request->requestMethod === 'DELETE') { |
121
|
20 |
|
if ($method === null || "delete" === mb_strtolower($method)) { |
122
|
19 |
|
$params = $container->request->delete; |
123
|
|
|
} |
124
|
|
|
} else { |
125
|
|
|
$errorMsg = "Unsupported method is specified: " . $method; |
126
|
|
|
throw new InvalidRequestException($errorMsg); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// パラメータの指定なしの場合、value=null |
130
|
|
|
// パラメータの指定ありあつ値の指定なしの場合、value="" |
131
|
80 |
|
$value = is_array($params) && array_key_exists($key, $params) ? $params[$key] : null; |
132
|
|
|
|
133
|
80 |
|
if (!$validateInstance->isValid($value, $rule)) { |
134
|
40 |
|
$errorMsg = "Validation rule error. Rule is '$rule', value is " . ($value === null || $value === '' ? "empty" : "'${value}'"); |
135
|
40 |
|
throw new ValidateException($errorMsg); |
136
|
|
|
} |
137
|
|
|
} else { |
138
|
|
|
$errorMsg = "Invalid validation rule definition: " . $rule; |
139
|
|
|
throw new ValidateException($errorMsg); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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