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 W2w\Laravel\Apie\Plugins\IlluminateTranslation\SubActions\TransChoiceSubAction; |
10
|
|
|
use W2w\Laravel\Apie\Plugins\IlluminateTranslation\ValueObjects\Locale; |
11
|
|
|
use W2w\Lib\Apie\Events\DecodeEvent; |
12
|
|
|
use W2w\Lib\Apie\Events\DeleteResourceEvent; |
13
|
|
|
use W2w\Lib\Apie\Events\ModifySingleResourceEvent; |
14
|
|
|
use W2w\Lib\Apie\Events\NormalizeEvent; |
15
|
|
|
use W2w\Lib\Apie\Events\ResponseEvent; |
16
|
|
|
use W2w\Lib\Apie\Events\RetrievePaginatedResourcesEvent; |
17
|
|
|
use W2w\Lib\Apie\Events\RetrieveSingleResourceEvent; |
18
|
|
|
use W2w\Lib\Apie\Events\StoreExistingResourceEvent; |
19
|
|
|
use W2w\Lib\Apie\Events\StoreNewResourceEvent; |
20
|
|
|
use W2w\Lib\Apie\PluginInterfaces\OpenApiEventProviderInterface; |
21
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ResourceLifeCycleInterface; |
22
|
|
|
use W2w\Lib\Apie\PluginInterfaces\SubActionsProviderInterface; |
23
|
|
|
|
24
|
|
|
class IlluminateTranslationPlugin implements OpenApiEventProviderInterface, ResourceLifeCycleInterface, SubActionsProviderInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $locales; |
30
|
|
|
/** |
31
|
|
|
* @var Translator |
32
|
|
|
*/ |
33
|
|
|
private $translator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string[] $locales |
37
|
|
|
* @param Translator $translator |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $locales, Translator $translator) |
40
|
|
|
{ |
41
|
|
|
$this->locales = $locales; |
42
|
|
|
$this->translator = $translator; |
43
|
|
|
Locale::$locales = $this->locales; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function onOpenApiDocGenerated(Document $document): Document |
47
|
|
|
{ |
48
|
|
|
foreach ($document->paths as $path) { |
49
|
|
|
$this->patchOperation($path->get); |
50
|
|
|
$this->patchOperation($path->put); |
51
|
|
|
$this->patchOperation($path->post); |
52
|
|
|
$this->patchOperation($path->patch); |
53
|
|
|
$this->patchOperation($path->delete); |
54
|
|
|
$this->patchOperation($path->options); |
55
|
|
|
$this->patchOperation($path->head); |
56
|
|
|
$this->patchOperation($path->trace); |
57
|
|
|
} |
58
|
|
|
return $document; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function patchOperation(?Operation $operation) |
62
|
|
|
{ |
63
|
|
|
if ($operation === null) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
if (null === $operation->parameters) { |
67
|
|
|
$operation->parameters = []; |
68
|
|
|
} |
69
|
|
|
Locale::$locales = $this->locales; |
70
|
|
|
$operation->parameters[] = new Parameter( |
71
|
|
|
'Accept-Language', |
72
|
|
|
Parameter::IN_HEADER, |
73
|
|
|
'language', |
74
|
|
|
[ |
75
|
|
|
'schema' => Locale::toSchema(), |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function onPreDeleteResource(DeleteResourceEvent $event) |
81
|
|
|
{ |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function onPostDeleteResource(DeleteResourceEvent $event) |
85
|
|
|
{ |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function onPreRetrieveResource(RetrieveSingleResourceEvent $event) |
89
|
|
|
{ |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function onPostRetrieveResource(RetrieveSingleResourceEvent $event) |
93
|
|
|
{ |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function onPreRetrieveAllResources(RetrievePaginatedResourcesEvent $event) |
97
|
|
|
{ |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function onPostRetrieveAllResources(RetrievePaginatedResourcesEvent $event) |
101
|
|
|
{ |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function onPrePersistExistingResource(StoreExistingResourceEvent $event) |
105
|
|
|
{ |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function onPostPersistExistingResource(StoreExistingResourceEvent $event) |
109
|
|
|
{ |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function onPreDecodeRequestBody(DecodeEvent $event) |
113
|
|
|
{ |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function onPostDecodeRequestBody(DecodeEvent $event) |
117
|
|
|
{ |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function onPreModifyResource(ModifySingleResourceEvent $event) |
121
|
|
|
{ |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function onPostModifyResource(ModifySingleResourceEvent $event) |
125
|
|
|
{ |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function onPreCreateResource(StoreNewResourceEvent $event) |
129
|
|
|
{ |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function onPostCreateResource(StoreNewResourceEvent $event) |
133
|
|
|
{ |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function onPrePersistNewResource(StoreExistingResourceEvent $event) |
137
|
|
|
{ |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function onPostPersistNewResource(StoreExistingResourceEvent $event) |
141
|
|
|
{ |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function onPreCreateResponse(ResponseEvent $event) |
145
|
|
|
{ |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function onPostCreateResponse(ResponseEvent $event) |
149
|
|
|
{ |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function onPreCreateNormalizedData(NormalizeEvent $event) |
153
|
|
|
{ |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function onPostCreateNormalizedData(NormalizeEvent $event) |
157
|
|
|
{ |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getSubActions() |
161
|
|
|
{ |
162
|
|
|
return [ |
163
|
|
|
'withPlaceholders' => [new TransChoiceSubAction($this->translator)] |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|