-
Notifications
You must be signed in to change notification settings - Fork 15
/
ArgumentValidator.php
184 lines (155 loc) · 6.76 KB
/
ArgumentValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace Matthias\SymfonyServiceDefinitionValidator;
use Matthias\SymfonyServiceDefinitionValidator\Exception\InvalidExpressionException;
use Matthias\SymfonyServiceDefinitionValidator\Exception\InvalidExpressionSyntaxException;
use Matthias\SymfonyServiceDefinitionValidator\Exception\TypeHintMismatchException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ExpressionLanguage;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\SyntaxError;
class ArgumentValidator implements ArgumentValidatorInterface
{
private $containerBuilder;
private $resultingClassResolver;
private $evaluateExpressions;
public function __construct(
ContainerBuilder $containerBuilder,
ResultingClassResolverInterface $resultingClassResolver,
$evaluateExpressions = false
) {
$this->containerBuilder = $containerBuilder;
$this->resultingClassResolver = $resultingClassResolver;
$this->evaluateExpressions = $evaluateExpressions;
}
public function validate(\ReflectionParameter $parameter, $argument)
{
$parameterType = $parameter->getType();
if ($parameterType === null) {
return;
}
if ($parameterType->getName() === 'array') {
$this->validateArrayArgument($parameter, $argument);
} elseif (class_exists($parameterType->getName())) {
$this->validateObjectArgument($parameterType->getName(), $argument, $parameter->allowsNull());
}
// other arguments don't need to be or can't be validated
}
private function validateArrayArgument(\ReflectionParameter $parameter, $argument)
{
if ($parameter->allowsNull() && is_null($argument)) {
return;
}
if (class_exists('Symfony\Component\ExpressionLanguage\Expression') && $argument instanceof Expression) {
$this->validateExpressionArgument('array', $argument, $parameter->allowsNull());
} else {
if (is_array($argument)) {
return;
}
throw new TypeHintMismatchException(sprintf(
'Argument of type "%s" should have been an array',
gettype($argument)
));
}
}
private function validateObjectArgument($className, $argument, $allowsNull)
{
if ($argument instanceof Reference) {
$this->validateReferenceArgument($className, $argument);
} elseif ($argument instanceof Definition) {
$this->validateDefinitionArgument($className, $argument);
} elseif (class_exists('Symfony\Component\ExpressionLanguage\Expression') && $argument instanceof Expression) {
$this->validateExpressionArgument($className, $argument, $allowsNull);
} elseif ($argument === null && $allowsNull) {
return;
} else {
throw new TypeHintMismatchException(sprintf(
'Type-hint "%s" requires this argument to be a reference to a service or an inline service definition',
$className
));
}
}
private function validateReferenceArgument($className, Reference $reference)
{
// the __toString method of a Reference is the referenced service id
$referencedServiceId = (string)$reference;
if ('service_container' !== $referencedServiceId) {
$definition = $this->containerBuilder->findDefinition($referencedServiceId);
// we don't have to check if the definition exists, since the ContainerBuilder itself does that already
} else {
$definition = new Definition('Symfony\Component\DependencyInjection\Container');
}
$this->validateDefinitionArgument($className, $definition);
}
private function validateDefinitionArgument($className, Definition $definition)
{
$resultingClass = $this->resultingClassResolver->resolve($definition);
if ($resultingClass === null) {
return;
}
$this->validateClass($className, $resultingClass);
}
private function validateExpressionArgument($type, Expression $expression, $allowsNull)
{
$expressionLanguage = new ExpressionLanguage();
$this->validateExpressionSyntax($expression, $expressionLanguage);
if ($this->evaluateExpressions) {
$this->validateExpressionResult($type, $expression, $allowsNull, $expressionLanguage);
}
}
private function validateExpressionSyntax(Expression $expression, ExpressionLanguage $expressionLanguage)
{
try {
$expressionLanguage->parse($expression, array('container'));
} catch (SyntaxError $exception) {
throw new InvalidExpressionSyntaxException($expression, $exception);
}
}
private function validateExpressionResult($expectedType, Expression $expression, $allowsNull, ExpressionLanguage $expressionLanguage)
{
try {
$result = $expressionLanguage->evaluate($expression, array('container' => $this->containerBuilder));
} catch (\Exception $exception) {
throw new InvalidExpressionException($expression, $exception);
}
if ($result === null) {
if ($allowsNull) {
return;
}
throw new TypeHintMismatchException(sprintf(
'Argument for type-hint "%s" is an expression that evaluates to null, which is not allowed',
$expectedType
));
}
if ($expectedType === 'array' && !is_array($result)) {
throw new TypeHintMismatchException(sprintf(
'Argument for type-hint "%s" is an expression that evaluates to a non-array',
$expectedType
));
}
if (class_exists($expectedType)) {
if (!is_object($result)) {
throw new TypeHintMismatchException(sprintf(
'Argument for type-hint "%s" is an expression that evaluates to a non-object',
$expectedType
));
}
$resultingClass = get_class($result);
$this->validateClass($expectedType, $resultingClass);
}
}
private function validateClass($expectedClassName, $actualClassName)
{
if ($expectedClassName === $actualClassName) {
return;
}
if (!is_a($actualClassName, $expectedClassName, true)) {
throw new TypeHintMismatchException(sprintf(
'Argument for type-hint "%s" points to a service of class "%s"',
$expectedClassName,
$actualClassName
));
}
}
}