1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File was created 08.10.2015 17:55 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Component\Slumber\Annotation; |
7
|
|
|
|
8
|
|
|
use Doctrine\Common\Annotations\Annotation; |
9
|
|
|
use PeekAndPoke\Component\Psi\Psi\IsInstanceOf; |
10
|
|
|
use PeekAndPoke\Component\Slumber\Core\Exception\SlumberException; |
11
|
|
|
use PeekAndPoke\Component\Slumber\Core\Validation\ValidationContext; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Karsten J. Gerber <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ServiceInjectingSlumberAnnotation extends SlumberAnnotation |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The ID of the request service |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
* |
24
|
|
|
* @Annotation\Required() |
25
|
|
|
*/ |
26
|
|
|
public $service; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The class that the service must be an instance of |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
* |
33
|
|
|
* @Annotation\Required() |
34
|
|
|
*/ |
35
|
|
|
public $ofClass; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getServiceDefinition() |
41
|
|
|
{ |
42
|
|
|
return $this->service ?: $this->getServiceDefinitionDefault(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return null |
47
|
|
|
*/ |
48
|
|
|
public function getServiceDefinitionDefault() |
49
|
|
|
{ |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getServiceClassDefinition() |
57
|
|
|
{ |
58
|
|
|
return $this->ofClass ?: $this->getServiceClassDefinitionDefault(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getServiceClassDefinitionDefault() |
65
|
|
|
{ |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* |
71
|
|
|
* @param ValidationContext $context |
72
|
|
|
* |
73
|
|
|
* @throws SlumberException |
74
|
|
|
*/ |
75
|
|
|
public function validate(ValidationContext $context) |
76
|
|
|
{ |
77
|
|
|
$this->validateParamAreSet($context); |
78
|
|
|
|
79
|
|
|
$this->validateServiceClassExists($context); |
80
|
|
|
|
81
|
|
|
$this->validateServiceIsPresentInContainer($context); |
82
|
|
|
|
83
|
|
|
$this->validateServiceHasCorrectType($context); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param ContainerInterface $provider |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
protected function getService(ContainerInterface $provider) |
92
|
|
|
{ |
93
|
|
|
return $provider->get( |
94
|
|
|
$this->getServiceDefinition() |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function validateParamAreSet(ValidationContext $context) |
99
|
|
|
{ |
100
|
|
|
$service = $this->getServiceDefinition(); |
101
|
|
|
$ofClass = $this->getServiceClassDefinition(); |
102
|
|
|
|
103
|
|
|
if (empty($service) || empty($ofClass)) { |
104
|
|
|
throw $this->createValidationException( |
105
|
|
|
$context, |
106
|
|
|
"you must set the 'service' and the 'ofClass'" |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function validateServiceIsPresentInContainer(ValidationContext $context) |
112
|
|
|
{ |
113
|
|
|
$service = $this->getServiceDefinition(); |
114
|
|
|
|
115
|
|
|
if (! $context->getProvider()->has($service)) { |
116
|
|
|
throw $this->createValidationException( |
117
|
|
|
$context, |
118
|
|
|
"you requested the non-existing service '$service'" |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function validateServiceClassExists(ValidationContext $context) |
124
|
|
|
{ |
125
|
|
|
$ofClass = $this->getServiceClassDefinition(); |
126
|
|
|
|
127
|
|
|
if (! class_exists($ofClass) && ! interface_exists($ofClass)) { |
128
|
|
|
throw $this->createValidationException( |
129
|
|
|
$context, |
130
|
|
|
"you requested a service instance of the non-existing class or interface '$ofClass'" |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function validateServiceHasCorrectType(ValidationContext $context) |
136
|
|
|
{ |
137
|
|
|
$service = $this->getServiceDefinition(); |
138
|
|
|
$ofClass = $this->getServiceClassDefinition(); |
139
|
|
|
|
140
|
|
|
$instance = $this->getService($context->getProvider()); |
141
|
|
|
$check = new IsInstanceOf($ofClass); |
142
|
|
|
|
143
|
|
|
if (! $check->__invoke($instance)) { |
144
|
|
|
throw $this->createValidationException( |
145
|
|
|
$context, |
146
|
|
|
"The service '$service' is not an instance of '$ofClass' but is '" . |
147
|
|
|
(\is_object($instance) ? \get_class($instance) : \gettype($instance)) . "'" |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|