1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace W2w\Laravel\Apie\Plugins\IlluminateTranslation; |
4
|
|
|
|
5
|
|
|
use erasys\OpenApi\Spec\v3\Document; |
6
|
|
|
use erasys\OpenApi\Spec\v3\Operation; |
7
|
|
|
use erasys\OpenApi\Spec\v3\Parameter; |
8
|
|
|
use Illuminate\Contracts\Translation\Translator; |
9
|
|
|
use Illuminate\Foundation\Application; |
10
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
11
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
12
|
|
|
use W2w\Laravel\Apie\Plugins\IlluminateTranslation\Normalizers\LocationableExceptionNormalizer; |
13
|
|
|
use W2w\Laravel\Apie\Plugins\IlluminateTranslation\SubActions\TransChoiceSubAction; |
14
|
|
|
use W2w\Laravel\Apie\Plugins\IlluminateTranslation\ValueObjects\Locale; |
15
|
|
|
use W2w\Lib\Apie\Events\DecodeEvent; |
16
|
|
|
use W2w\Lib\Apie\Events\DeleteResourceEvent; |
17
|
|
|
use W2w\Lib\Apie\Events\ModifySingleResourceEvent; |
18
|
|
|
use W2w\Lib\Apie\Events\NormalizeEvent; |
19
|
|
|
use W2w\Lib\Apie\Events\ResponseEvent; |
20
|
|
|
use W2w\Lib\Apie\Events\RetrievePaginatedResourcesEvent; |
21
|
|
|
use W2w\Lib\Apie\Events\RetrieveSingleResourceEvent; |
22
|
|
|
use W2w\Lib\Apie\Events\StoreExistingResourceEvent; |
23
|
|
|
use W2w\Lib\Apie\Events\StoreNewResourceEvent; |
24
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ApieAwareInterface; |
25
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ApieAwareTrait; |
26
|
|
|
use W2w\Lib\Apie\PluginInterfaces\NormalizerProviderInterface; |
27
|
|
|
use W2w\Lib\Apie\PluginInterfaces\OpenApiEventProviderInterface; |
28
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ResourceLifeCycleInterface; |
29
|
|
|
use W2w\Lib\Apie\PluginInterfaces\SubActionsProviderInterface; |
30
|
|
|
use W2w\Lib\Apie\Plugins\Core\Normalizers\ExceptionNormalizer; |
31
|
|
|
|
32
|
|
|
class IlluminateTranslationPlugin implements ApieAwareInterface, OpenApiEventProviderInterface, ResourceLifeCycleInterface, SubActionsProviderInterface, NormalizerProviderInterface |
33
|
|
|
{ |
34
|
|
|
use ApieAwareTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $locales; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Translator |
43
|
|
|
*/ |
44
|
|
|
private $translator; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Application |
48
|
|
|
*/ |
49
|
|
|
private $application; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string[] $locales |
53
|
|
|
* @param Translator $translator |
54
|
|
|
*/ |
55
|
|
|
public function __construct(array $locales, Translator $translator, Application $application) |
56
|
|
|
{ |
57
|
|
|
$this->locales = $locales; |
58
|
|
|
$this->translator = $translator; |
59
|
|
|
$this->application = $application; |
60
|
|
|
Locale::$locales = $this->locales; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function onOpenApiDocGenerated(Document $document): Document |
64
|
|
|
{ |
65
|
|
|
foreach ($document->paths as $path) { |
66
|
|
|
$this->patchOperation($path->get); |
67
|
|
|
$this->patchOperation($path->put); |
68
|
|
|
$this->patchOperation($path->post); |
69
|
|
|
$this->patchOperation($path->patch); |
70
|
|
|
$this->patchOperation($path->delete); |
71
|
|
|
$this->patchOperation($path->options); |
72
|
|
|
$this->patchOperation($path->head); |
73
|
|
|
$this->patchOperation($path->trace); |
74
|
|
|
} |
75
|
|
|
return $document; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function patchOperation(?Operation $operation) |
79
|
|
|
{ |
80
|
|
|
if ($operation === null) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
if (null === $operation->parameters) { |
84
|
|
|
$operation->parameters = []; |
85
|
|
|
} |
86
|
|
|
Locale::$locales = $this->locales; |
87
|
|
|
$operation->parameters[] = new Parameter( |
88
|
|
|
'Accept-Language', |
89
|
|
|
Parameter::IN_HEADER, |
90
|
|
|
'language', |
91
|
|
|
[ |
92
|
|
|
'schema' => Locale::toSchema(), |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function onPreDeleteResource(DeleteResourceEvent $event) |
98
|
|
|
{ |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function onPostDeleteResource(DeleteResourceEvent $event) |
102
|
|
|
{ |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function onPreRetrieveResource(RetrieveSingleResourceEvent $event) |
106
|
|
|
{ |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function onPostRetrieveResource(RetrieveSingleResourceEvent $event) |
110
|
|
|
{ |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function onPreRetrieveAllResources(RetrievePaginatedResourcesEvent $event) |
114
|
|
|
{ |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function onPostRetrieveAllResources(RetrievePaginatedResourcesEvent $event) |
118
|
|
|
{ |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function onPrePersistExistingResource(StoreExistingResourceEvent $event) |
122
|
|
|
{ |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function onPostPersistExistingResource(StoreExistingResourceEvent $event) |
126
|
|
|
{ |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function onPreDecodeRequestBody(DecodeEvent $event) |
130
|
|
|
{ |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function onPostDecodeRequestBody(DecodeEvent $event) |
134
|
|
|
{ |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function onPreModifyResource(ModifySingleResourceEvent $event) |
138
|
|
|
{ |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function onPostModifyResource(ModifySingleResourceEvent $event) |
142
|
|
|
{ |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function onPreCreateResource(StoreNewResourceEvent $event) |
146
|
|
|
{ |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function onPostCreateResource(StoreNewResourceEvent $event) |
150
|
|
|
{ |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function onPrePersistNewResource(StoreExistingResourceEvent $event) |
154
|
|
|
{ |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function onPostPersistNewResource(StoreExistingResourceEvent $event) |
158
|
|
|
{ |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function onPreCreateResponse(ResponseEvent $event) |
162
|
|
|
{ |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function onPostCreateResponse(ResponseEvent $event) |
166
|
|
|
{ |
167
|
|
|
$response = $event->getResponse(); |
168
|
|
|
$locale = $this->application->getLocale(); |
169
|
|
|
if ($locale && !$response->hasHeader('Content-Language')) { |
170
|
|
|
$event->setResponse($response->withAddedHeader('Content-Language', $this->application->getLocale())); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function onPreCreateNormalizedData(NormalizeEvent $event) |
176
|
|
|
{ |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function onPostCreateNormalizedData(NormalizeEvent $event) |
180
|
|
|
{ |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getSubActions() |
184
|
|
|
{ |
185
|
|
|
return [ |
186
|
|
|
'withPlaceholders' => [new TransChoiceSubAction($this->translator)] |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getNormalizers(): array |
191
|
|
|
{ |
192
|
|
|
return [ |
|
|
|
|
193
|
|
|
new LocationableExceptionNormalizer( |
194
|
|
|
new ExceptionNormalizer($this->getApie()->isDebug()), |
195
|
|
|
$this->application->make(Translator::class) |
196
|
|
|
), |
197
|
|
|
]; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|