1
|
|
|
<?php |
2
|
|
|
namespace W2w\Laravel\Apie\Plugins\Illuminate; |
3
|
|
|
|
4
|
|
|
use erasys\OpenApi\Spec\v3\Contact; |
5
|
|
|
use erasys\OpenApi\Spec\v3\Document; |
6
|
|
|
use erasys\OpenApi\Spec\v3\Info; |
7
|
|
|
use erasys\OpenApi\Spec\v3\License; |
8
|
|
|
use erasys\OpenApi\Spec\v3\Schema; |
9
|
|
|
use Illuminate\Container\Container; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
13
|
|
|
use W2w\Laravel\Apie\Events\OpenApiSpecGenerated; |
14
|
|
|
use W2w\Laravel\Apie\Plugins\Illuminate\Encoders\DefaultContentTypeFormatRetriever; |
15
|
|
|
use W2w\Laravel\Apie\Plugins\Illuminate\ResourceFactories\FromIlluminateContainerFactory; |
16
|
|
|
use W2w\Laravel\Apie\Providers\ApieConfigResolver; |
17
|
|
|
use W2w\Lib\Apie\Core\Resources\ApiResourcesInterface; |
18
|
|
|
use W2w\Lib\Apie\Exceptions\InvalidClassTypeException; |
19
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceFactoryInterface; |
20
|
|
|
use W2w\Lib\Apie\Interfaces\FormatRetrieverInterface; |
21
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ApieConfigInterface; |
22
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ApiResourceFactoryProviderInterface; |
23
|
|
|
use W2w\Lib\Apie\PluginInterfaces\EncoderProviderInterface; |
24
|
|
|
use W2w\Lib\Apie\PluginInterfaces\NormalizerProviderInterface; |
25
|
|
|
use W2w\Lib\Apie\PluginInterfaces\OpenApiEventProviderInterface; |
26
|
|
|
use W2w\Lib\Apie\PluginInterfaces\OpenApiInfoProviderInterface; |
27
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ResourceProviderInterface; |
28
|
|
|
use W2w\Lib\Apie\PluginInterfaces\SchemaProviderInterface; |
29
|
|
|
|
30
|
|
|
class IlluminatePlugin implements ResourceProviderInterface, ApieConfigInterface, OpenApiInfoProviderInterface, ApiResourceFactoryProviderInterface, EncoderProviderInterface, NormalizerProviderInterface, OpenApiEventProviderInterface, SchemaProviderInterface |
31
|
|
|
{ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
private $resolvedConfig; |
35
|
|
|
|
36
|
|
|
public function __construct(Container $container, array $config) |
37
|
|
|
{ |
38
|
|
|
$this->container = $container; |
39
|
|
|
$this->resolvedConfig = ApieConfigResolver::resolveConfig($config); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getLaravelConfig(): array |
43
|
|
|
{ |
44
|
|
|
return $this->resolvedConfig; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns a list of Api resources. |
49
|
|
|
* |
50
|
|
|
* @return string[] |
51
|
|
|
*/ |
52
|
|
|
public function getResources(): array |
53
|
|
|
{ |
54
|
|
|
if (!empty($this->resolvedConfig['resources-service'])) { |
55
|
|
|
$resources = $this->container->make($this->resolvedConfig['resources-service']); |
56
|
|
|
if (!($resources instanceof ApiResourcesInterface)) { |
57
|
|
|
throw new InvalidClassTypeException('resources-service', ApiResourcesInterface::class); |
58
|
|
|
} |
59
|
|
|
return $resources->getApiResources(); |
60
|
|
|
} |
61
|
|
|
return $this->resolvedConfig['resources']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function getBaseUrl(): string |
68
|
|
|
{ |
69
|
|
|
$baseUrl = $this->resolvedConfig['base-url'] . $this->resolvedConfig['api-url']; |
70
|
|
|
if ($this->container->has(Request::class)) { |
71
|
|
|
$baseUrl = $this->container->get(Request::class)->getSchemeAndHttpHost() . $baseUrl; |
72
|
|
|
} |
73
|
|
|
return $baseUrl; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
|
|
public function createInfo(): Info |
80
|
|
|
{ |
81
|
|
|
return new Info( |
82
|
|
|
$this->resolvedConfig['metadata']['title'], |
83
|
|
|
$this->resolvedConfig['metadata']['version'], |
84
|
|
|
$this->resolvedConfig['metadata']['description'], |
85
|
|
|
[ |
86
|
|
|
'contact' => new Contact([ |
87
|
|
|
'name' => $this->resolvedConfig['metadata']['contact-name'], |
88
|
|
|
'url' => $this->resolvedConfig['metadata']['contact-url'], |
89
|
|
|
'email' => $this->resolvedConfig['metadata']['contact-email'], |
90
|
|
|
]), |
91
|
|
|
'license' => new License( |
92
|
|
|
$this->resolvedConfig['metadata']['license'], |
93
|
|
|
$this->resolvedConfig['metadata']['license-url'] |
94
|
|
|
), |
95
|
|
|
] |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritDoc} |
101
|
|
|
*/ |
102
|
|
|
public function getApiResourceFactory(): ApiResourceFactoryInterface |
103
|
|
|
{ |
104
|
|
|
return new FromIlluminateContainerFactory($this->container); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritDoc} |
109
|
|
|
*/ |
110
|
|
|
public function getEncoders(): array |
111
|
|
|
{ |
112
|
|
|
return []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritDoc} |
117
|
|
|
*/ |
118
|
|
|
public function getFormatRetriever(): FormatRetrieverInterface |
119
|
|
|
{ |
120
|
|
|
return new DefaultContentTypeFormatRetriever(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
|
|
public function getNormalizers(): array |
127
|
|
|
{ |
128
|
|
|
$res = []; |
129
|
|
|
// container->tagged has hazy return value... |
130
|
|
|
foreach ($this->container->tagged(NormalizerInterface::class) as $normalizer) { |
131
|
|
|
$res[] = $normalizer; |
132
|
|
|
}; |
133
|
|
|
return $res; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
*/ |
139
|
|
|
public function onOpenApiDocGenerated(Document $document): Document |
140
|
|
|
{ |
141
|
|
|
$event = new OpenApiSpecGenerated($document); |
142
|
|
|
$this->container->get('events')->dispatch($event); |
143
|
|
|
return $event->getDocument(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return Schema[] |
148
|
|
|
*/ |
149
|
|
|
public function getDefinedStaticData(): array |
150
|
|
|
{ |
151
|
|
|
return [ |
152
|
|
|
Model::class => new Schema([ |
153
|
|
|
'type' => 'object' |
154
|
|
|
]), |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return callable[] |
160
|
|
|
*/ |
161
|
|
|
public function getDynamicSchemaLogic(): array |
162
|
|
|
{ |
163
|
|
|
return []; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|