1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace W2w\Laravel\Apie\Plugins\IlluminateDispatcher; |
5
|
|
|
|
6
|
|
|
use Illuminate\Auth\AuthManager; |
7
|
|
|
use Illuminate\Contracts\Auth\Access\Gate; |
8
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
9
|
|
|
use Illuminate\Validation\Factory; |
10
|
|
|
use Psr\Container\ContainerInterface; |
11
|
|
|
use W2w\Lib\Apie\Events\DeleteResourceEvent; |
12
|
|
|
use W2w\Lib\Apie\Events\ModifySingleResourceEvent; |
13
|
|
|
use W2w\Lib\Apie\Events\NormalizeEvent; |
14
|
|
|
use W2w\Lib\Apie\Events\ResponseEvent; |
15
|
|
|
use W2w\Lib\Apie\Events\RetrievePaginatedResourcesEvent; |
16
|
|
|
use W2w\Lib\Apie\Events\RetrieveSingleResourceEvent; |
17
|
|
|
use W2w\Lib\Apie\Events\StoreExistingResourceEvent; |
18
|
|
|
use W2w\Lib\Apie\Events\StoreNewResourceEvent; |
19
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ResourceLifeCycleInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Link the Apie resource life cycle methods Laravel: |
23
|
|
|
* - event dispatcher |
24
|
|
|
* - validation rules with adding 'rules' in context. |
25
|
|
|
* - if the api resource is linked to a policy, check the policy. |
26
|
|
|
*/ |
27
|
|
|
class IlluminateDispatcherPlugin implements ResourceLifeCycleInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Dispatcher |
31
|
|
|
*/ |
32
|
|
|
private $dispatcher; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Factory |
36
|
|
|
*/ |
37
|
|
|
private $validator; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var AuthManager |
41
|
|
|
*/ |
42
|
|
|
private $gate; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Dispatcher $dispatcher |
46
|
|
|
* @param Factory $validator |
47
|
|
|
* @param Gate $gate |
48
|
|
|
*/ |
49
|
|
|
public function __construct(Dispatcher $dispatcher, Factory $validator, Gate $gate) |
50
|
|
|
{ |
51
|
|
|
$this->dispatcher = $dispatcher; |
52
|
|
|
$this->validator = $validator; |
53
|
|
|
$this->gate = $gate; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
public function onPreDeleteResource(DeleteResourceEvent $event) |
60
|
|
|
{ |
61
|
|
|
$this->dispatcher->dispatch($event); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function onPostDeleteResource(DeleteResourceEvent $event) |
68
|
|
|
{ |
69
|
|
|
$this->dispatcher->dispatch($event); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function onPreRetrieveResource(RetrieveSingleResourceEvent $event) |
76
|
|
|
{ |
77
|
|
|
$this->dispatcher->dispatch($event); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
|
|
public function onPostRetrieveResource(RetrieveSingleResourceEvent $event) |
84
|
|
|
{ |
85
|
|
|
if ($this->gate->getPolicyFor($event->getResource())) { |
86
|
|
|
$this->gate->authorize('view', $event->getResource()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->dispatcher->dispatch($event); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function onPreRetrieveAllResources(RetrievePaginatedResourcesEvent $event) |
96
|
|
|
{ |
97
|
|
|
$this->dispatcher->dispatch($event); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
*/ |
103
|
|
|
public function onPostRetrieveAllResources(RetrievePaginatedResourcesEvent $event) |
104
|
|
|
{ |
105
|
|
|
$resources = $event->getResources(); |
106
|
|
|
|
107
|
|
|
$event->setResources($this->iterateList($resources)); |
|
|
|
|
108
|
|
|
$this->dispatcher->dispatch($event); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function iterateList(iterable $resourceList) |
112
|
|
|
{ |
113
|
|
|
foreach ($resourceList as $resource) { |
114
|
|
|
// do policy check for every individual item because of polymorphic item list. |
115
|
|
|
if (!$this->gate->getPolicyFor($resource) || $this->gate->allows('view', $resource)) { |
116
|
|
|
yield $resource; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
|
|
public function onPrePersistExistingResource(StoreExistingResourceEvent $event) |
125
|
|
|
{ |
126
|
|
|
if ($this->gate->getPolicyFor($event->getResource())) { |
127
|
|
|
$this->gate->authorize('update', $event->getResource()); |
128
|
|
|
} |
129
|
|
|
$this->dispatcher->dispatch($event); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
|
|
public function onPostPersistExistingResource(StoreExistingResourceEvent $event) |
136
|
|
|
{ |
137
|
|
|
$this->dispatcher->dispatch($event); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritDoc} |
142
|
|
|
*/ |
143
|
|
|
public function onPreModifyResource(ModifySingleResourceEvent $event) |
144
|
|
|
{ |
145
|
|
|
$this->dispatcher->dispatch($event); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritDoc} |
150
|
|
|
*/ |
151
|
|
|
public function onPostModifyResource(ModifySingleResourceEvent $event) |
152
|
|
|
{ |
153
|
|
|
$this->dispatcher->dispatch($event); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritDoc} |
158
|
|
|
*/ |
159
|
|
|
public function onPreCreateResource(StoreNewResourceEvent $event) |
160
|
|
|
{ |
161
|
|
|
$this->dispatcher->dispatch($event); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
*/ |
167
|
|
|
public function onPostCreateResource(StoreNewResourceEvent $event) |
168
|
|
|
{ |
169
|
|
|
$this->dispatcher->dispatch($event); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
*/ |
175
|
|
|
public function onPrePersistNewResource(StoreExistingResourceEvent $event) |
176
|
|
|
{ |
177
|
|
|
if ($this->gate->getPolicyFor($event->getResource())) { |
178
|
|
|
$this->gate->authorize('create', $event->getResource()); |
179
|
|
|
} |
180
|
|
|
$this->dispatcher->dispatch($event); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritDoc} |
185
|
|
|
*/ |
186
|
|
|
public function onPostPersistNewResource(StoreExistingResourceEvent $event) |
187
|
|
|
{ |
188
|
|
|
$this->dispatcher->dispatch($event); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritDoc} |
193
|
|
|
*/ |
194
|
|
|
public function onPreCreateResponse(ResponseEvent $event) |
195
|
|
|
{ |
196
|
|
|
$this->dispatcher->dispatch($event); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritDoc} |
201
|
|
|
*/ |
202
|
|
|
public function onPostCreateResponse(ResponseEvent $event) |
203
|
|
|
{ |
204
|
|
|
$this->dispatcher->dispatch($event); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritDoc} |
209
|
|
|
*/ |
210
|
|
|
public function onPreCreateNormalizedData(NormalizeEvent $event) |
211
|
|
|
{ |
212
|
|
|
$this->dispatcher->dispatch($event); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritDoc} |
217
|
|
|
*/ |
218
|
|
|
public function onPostCreateNormalizedData(NormalizeEvent $event) |
219
|
|
|
{ |
220
|
|
|
$this->dispatcher->dispatch($event); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..