1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace W2w\Lib\Apie\Core; |
4
|
|
|
|
5
|
|
|
use erasys\OpenApi\Spec\v3\Document; |
6
|
|
|
use W2w\Lib\Apie\Apie; |
7
|
|
|
use W2w\Lib\Apie\Core\Resources\ApiResources; |
8
|
|
|
use W2w\Lib\Apie\OpenApiSchema\OpenApiSchemaGenerator; |
9
|
|
|
use W2w\Lib\Apie\OpenApiSchema\OpenApiSpecGenerator; |
10
|
|
|
use W2w\Lib\Apie\OpenApiSchema\SubActions\SubActionContainer; |
11
|
|
|
use W2w\Lib\Apie\OpenApiSchema\SubActions\SubActionFactory; |
12
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ResourceLifeCycleInterface; |
13
|
|
|
use W2w\Lib\Apie\PluginInterfaces\SubActionsProviderInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Used by Apie to create the general Apie classes which you are not supposed to override in a plugin. |
17
|
|
|
* |
18
|
|
|
* @internal |
19
|
|
|
*/ |
20
|
|
|
class ApieCore |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Apie |
24
|
|
|
*/ |
25
|
|
|
private $apie; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var PluginContainer |
29
|
|
|
*/ |
30
|
|
|
private $pluginContainer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ApiResourceRetriever|null |
34
|
|
|
*/ |
35
|
|
|
private $retriever; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ApiResourcePersister|null |
39
|
|
|
*/ |
40
|
|
|
private $persister; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ApiResourceMetadataFactory|null |
44
|
|
|
*/ |
45
|
|
|
private $metadataFactory; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var IdentifierExtractor|null |
49
|
|
|
*/ |
50
|
|
|
private $identifierExtractor; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var OpenApiSchemaGenerator|null |
54
|
|
|
*/ |
55
|
|
|
private $schemaGenerator; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Apie $apie |
59
|
|
|
* @param PluginContainer $pluginContainer |
60
|
|
|
*/ |
61
|
|
|
public function __construct(Apie $apie, PluginContainer $pluginContainer) |
62
|
|
|
{ |
63
|
|
|
$this->apie = $apie; |
64
|
|
|
$this->pluginContainer = $pluginContainer; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the service that generated the complete OpenApi specification, |
69
|
|
|
* |
70
|
|
|
* @return OpenApiSpecGenerator |
71
|
|
|
*/ |
72
|
|
|
public function getOpenApiSpecGenerator(): OpenApiSpecGenerator |
73
|
|
|
{ |
74
|
|
|
return new OpenApiSpecGenerator( |
75
|
|
|
new ApiResources($this->apie->getResources()), |
76
|
|
|
$this->getClassResourceConverter(), |
77
|
|
|
$this->apie->createInfo(), |
78
|
|
|
$this->getSchemaGenerator(), |
79
|
|
|
$this->getApiResourceMetadataFactory(), |
80
|
|
|
$this->getIdentifierExtractor(), |
81
|
|
|
$this->apie->getBaseUrl(), |
82
|
|
|
$this->getSubActionContainer(), |
83
|
|
|
$this->apie->getPropertyConverter(), |
84
|
|
|
function (Document $doc) { |
85
|
|
|
$this->apie->onOpenApiDocGenerated($doc); |
86
|
|
|
} |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getSubActionContainer(): SubActionContainer |
91
|
|
|
{ |
92
|
|
|
$factory = new SubActionFactory($this->apie->getPropertyConverter()); |
93
|
|
|
$types = []; |
94
|
|
|
foreach ($this->pluginContainer->getPluginsWithInterface(SubActionsProviderInterface::class) as $plugin) { |
95
|
|
|
/** @var SubActionsProviderInterface $plugin */ |
96
|
|
|
foreach ($plugin->getSubActions() as $actionName => $actions) { |
97
|
|
|
foreach ($actions as $action) { |
98
|
|
|
$types[$actionName][] = $action; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return new SubActionContainer( |
103
|
|
|
$types, |
104
|
|
|
$factory |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the service that generates the JSON schema of a class. |
110
|
|
|
* |
111
|
|
|
* @return OpenApiSchemaGenerator |
112
|
|
|
*/ |
113
|
|
|
public function getSchemaGenerator(): OpenApiSchemaGenerator |
114
|
|
|
{ |
115
|
|
|
if (!$this->schemaGenerator) { |
116
|
|
|
$this->schemaGenerator = new OpenApiSchemaGenerator( |
117
|
|
|
$this->apie->getDynamicSchemaLogic(), |
118
|
|
|
$this->apie->getObjectAccess(), |
119
|
|
|
$this->apie->getClassMetadataFactory(), |
120
|
|
|
$this->apie->getPropertyConverter() |
121
|
|
|
); |
122
|
|
|
foreach ($this->apie->getDefinedStaticData() as $class => $schema) { |
123
|
|
|
$this->schemaGenerator->defineSchemaForResource($class, $schema); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
return $this->schemaGenerator; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function getResponseFactory(): ApiResourceFacadeResponseFactory |
131
|
|
|
{ |
132
|
|
|
return new ApiResourceFacadeResponseFactory( |
133
|
|
|
$this->apie->getResourceSerializer(), |
134
|
|
|
$this->pluginContainer->getPluginsWithInterface(ResourceLifeCycleInterface::class) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the Apie resource facade to handle REST API responses. |
140
|
|
|
* |
141
|
|
|
* @return ApiResourceFacade |
142
|
|
|
*/ |
143
|
|
|
public function getApiResourceFacade(): ApiResourceFacade |
144
|
|
|
{ |
145
|
|
|
return new ApiResourceFacade( |
146
|
|
|
$this->getResourceRetriever(), |
147
|
|
|
$this->getResourcePersister(), |
148
|
|
|
$this->getClassResourceConverter(), |
149
|
|
|
$this->apie->getResourceSerializer(), |
150
|
|
|
$this->apie->getFormatRetriever(), |
151
|
|
|
$this->getSubActionContainer(), |
152
|
|
|
$this->apie->getPropertyConverter(), |
153
|
|
|
$this->getResponseFactory(), |
154
|
|
|
$this->pluginContainer->getPluginsWithInterface(ResourceLifeCycleInterface::class) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns the service that retrieves Api Resources. |
160
|
|
|
* |
161
|
|
|
* @return ApiResourceRetriever |
162
|
|
|
*/ |
163
|
|
|
public function getResourceRetriever(): ApiResourceRetriever |
164
|
|
|
{ |
165
|
|
|
if (!$this->retriever) { |
166
|
|
|
$this->retriever = new ApiResourceRetriever( |
167
|
|
|
$this->getApiResourceMetadataFactory() |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
return $this->retriever; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns the service that persist Api Resources. |
175
|
|
|
* |
176
|
|
|
* @return ApiResourcePersister |
177
|
|
|
*/ |
178
|
|
|
public function getResourcePersister(): ApiResourcePersister |
179
|
|
|
{ |
180
|
|
|
if (!$this->persister) { |
181
|
|
|
$this->persister = new ApiResourcePersister( |
182
|
|
|
$this->getApiResourceMetadataFactory() |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
return $this->persister; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns the service that gives the metadata of an api resource. |
190
|
|
|
* |
191
|
|
|
* @return ApiResourceMetadataFactory |
192
|
|
|
*/ |
193
|
|
|
public function getApiResourceMetadataFactory(): ApiResourceMetadataFactory |
194
|
|
|
{ |
195
|
|
|
if (!$this->metadataFactory) { |
196
|
|
|
$this->metadataFactory = new ApiResourceMetadataFactory( |
197
|
|
|
$this->apie->getAnnotationReader(), |
198
|
|
|
$this->apie->getApiResourceFactory() |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
return $this->metadataFactory; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns the service that extracts identifiers from an api resource. |
206
|
|
|
* |
207
|
|
|
* @return IdentifierExtractor |
208
|
|
|
*/ |
209
|
|
|
public function getIdentifierExtractor(): IdentifierExtractor |
210
|
|
|
{ |
211
|
|
|
if (!$this->identifierExtractor) { |
212
|
|
|
$this->identifierExtractor = new IdentifierExtractor($this->apie->getObjectAccess()); |
213
|
|
|
} |
214
|
|
|
return $this->identifierExtractor; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Returns the class that converts from URL slug to PHP class and viceversa. |
219
|
|
|
* |
220
|
|
|
* @return ClassResourceConverter |
221
|
|
|
*/ |
222
|
|
|
public function getClassResourceConverter(): ClassResourceConverter |
223
|
|
|
{ |
224
|
|
|
return new ClassResourceConverter( |
225
|
|
|
$this->apie->getPropertyConverter(), |
226
|
|
|
new ApiResources($this->apie->getResources()), |
227
|
|
|
$this->apie->isDebug() |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|