-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApiPlatformDeferredProvider.php
334 lines (297 loc) · 16.9 KB
/
ApiPlatformDeferredProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Laravel;
use ApiPlatform\GraphQl\Resolver\Factory\ResolverFactoryInterface;
use ApiPlatform\GraphQl\State\Provider\DenormalizeProvider as GraphQlDenormalizeProvider;
use ApiPlatform\GraphQl\Type\ContextAwareTypeBuilderInterface;
use ApiPlatform\GraphQl\Type\FieldsBuilder;
use ApiPlatform\GraphQl\Type\FieldsBuilderEnumInterface;
use ApiPlatform\GraphQl\Type\TypeConverterInterface;
use ApiPlatform\GraphQl\Type\TypesContainerInterface;
use ApiPlatform\JsonApi\Filter\SparseFieldset;
use ApiPlatform\JsonApi\Filter\SparseFieldsetParameterProvider;
use ApiPlatform\Laravel\Eloquent\Extension\FilterQueryExtension;
use ApiPlatform\Laravel\Eloquent\Extension\QueryExtensionInterface;
use ApiPlatform\Laravel\Eloquent\Filter\BooleanFilter;
use ApiPlatform\Laravel\Eloquent\Filter\DateFilter;
use ApiPlatform\Laravel\Eloquent\Filter\EndSearchFilter;
use ApiPlatform\Laravel\Eloquent\Filter\EqualsFilter;
use ApiPlatform\Laravel\Eloquent\Filter\FilterInterface as EloquentFilterInterface;
use ApiPlatform\Laravel\Eloquent\Filter\JsonApi\SortFilter;
use ApiPlatform\Laravel\Eloquent\Filter\JsonApi\SortFilterParameterProvider;
use ApiPlatform\Laravel\Eloquent\Filter\OrderFilter;
use ApiPlatform\Laravel\Eloquent\Filter\PartialSearchFilter;
use ApiPlatform\Laravel\Eloquent\Filter\RangeFilter;
use ApiPlatform\Laravel\Eloquent\Filter\StartSearchFilter;
use ApiPlatform\Laravel\Eloquent\Metadata\Factory\Resource\EloquentResourceCollectionMetadataFactory;
use ApiPlatform\Laravel\Eloquent\State\CollectionProvider;
use ApiPlatform\Laravel\Eloquent\State\ItemProvider;
use ApiPlatform\Laravel\Eloquent\State\LinksHandler;
use ApiPlatform\Laravel\Eloquent\State\LinksHandlerInterface;
use ApiPlatform\Laravel\Eloquent\State\PersistProcessor;
use ApiPlatform\Laravel\Eloquent\State\RemoveProcessor;
use ApiPlatform\Laravel\Metadata\CacheResourceCollectionMetadataFactory;
use ApiPlatform\Laravel\Metadata\ParameterValidationResourceMetadataCollectionFactory;
use ApiPlatform\Laravel\State\ParameterValidatorProvider;
use ApiPlatform\Laravel\State\SwaggerUiProcessor;
use ApiPlatform\Metadata\InflectorInterface;
use ApiPlatform\Metadata\Operation\PathSegmentNameGeneratorInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\AlternateUriResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\ConcernsResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\FiltersResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\FormatsResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\InputOutputResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\LinkFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\LinkResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\NotExposedOperationResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\OperationNameResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\ParameterResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\PhpDocResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\UriTemplateResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\Util\ReflectionClassRecursiveIterator;
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use ApiPlatform\Serializer\Parameter\SerializerFilterParameterProvider;
use ApiPlatform\State\CallableProcessor;
use ApiPlatform\State\CallableProvider;
use ApiPlatform\State\ErrorProvider;
use ApiPlatform\State\Pagination\Pagination;
use ApiPlatform\State\ParameterProviderInterface;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\Provider\DeserializeProvider;
use ApiPlatform\State\Provider\ParameterProvider;
use ApiPlatform\State\Provider\SecurityParameterProvider;
use ApiPlatform\State\ProviderInterface;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
class ApiPlatformDeferredProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$directory = app_path();
$classes = ReflectionClassRecursiveIterator::getReflectionClassesFromDirectories([$directory], '(?!.*Test\.php$)');
$this->autoconfigure($classes, QueryExtensionInterface::class, [FilterQueryExtension::class]);
$this->app->singleton(ItemProvider::class, function (Application $app) {
$tagged = iterator_to_array($app->tagged(LinksHandlerInterface::class));
return new ItemProvider(new LinksHandler($app, $app->make(ResourceMetadataCollectionFactoryInterface::class)), new ServiceLocator($tagged), $app->tagged(QueryExtensionInterface::class));
});
$this->app->singleton(CollectionProvider::class, function (Application $app) {
$tagged = iterator_to_array($app->tagged(LinksHandlerInterface::class));
return new CollectionProvider($app->make(Pagination::class), new LinksHandler($app, $app->make(ResourceMetadataCollectionFactoryInterface::class)), $app->tagged(QueryExtensionInterface::class), new ServiceLocator($tagged));
});
$this->app->singleton(SerializerFilterParameterProvider::class, function (Application $app) {
$tagged = iterator_to_array($app->tagged(SerializerFilterInterface::class));
return new SerializerFilterParameterProvider(new ServiceLocator($tagged));
});
$this->app->alias(SerializerFilterParameterProvider::class, 'api_platform.serializer.filter_parameter_provider');
$this->app->singleton('filters', function (Application $app) {
return new ServiceLocator(array_merge(
iterator_to_array($app->tagged(SerializerFilterInterface::class)),
iterator_to_array($app->tagged(EloquentFilterInterface::class))
));
});
$this->autoconfigure($classes, SerializerFilterInterface::class, [PropertyFilter::class]);
$this->app->singleton(ParameterProvider::class, function (Application $app) {
$tagged = iterator_to_array($app->tagged(ParameterProviderInterface::class));
$tagged['api_platform.serializer.filter_parameter_provider'] = $app->make(SerializerFilterParameterProvider::class);
return new ParameterProvider(
new ParameterValidatorProvider(
new SecurityParameterProvider(
$app->make(DeserializeProvider::class),
$app->make(ResourceAccessCheckerInterface::class)
),
),
new ServiceLocator($tagged)
);
});
$this->autoconfigure($classes, ParameterProviderInterface::class, [SerializerFilterParameterProvider::class, SortFilterParameterProvider::class, SparseFieldsetParameterProvider::class]);
$this->app->bind(FilterQueryExtension::class, function (Application $app) {
$tagged = iterator_to_array($app->tagged(EloquentFilterInterface::class));
return new FilterQueryExtension(new ServiceLocator($tagged));
});
$this->autoconfigure($classes, EloquentFilterInterface::class, [
BooleanFilter::class,
DateFilter::class,
EndSearchFilter::class,
EqualsFilter::class,
OrderFilter::class,
PartialSearchFilter::class,
RangeFilter::class,
StartSearchFilter::class,
SortFilter::class,
SparseFieldset::class,
]);
$this->app->singleton(CallableProcessor::class, function (Application $app) {
/** @var ConfigRepository */
$config = $app['config'];
$tagged = iterator_to_array($app->tagged(ProcessorInterface::class));
if ($config->get('api-platform.swagger_ui.enabled', false)) {
// TODO: tag SwaggerUiProcessor instead?
$tagged['api_platform.swagger_ui.processor'] = $app->make(SwaggerUiProcessor::class);
}
return new CallableProcessor(new ServiceLocator($tagged));
});
$this->autoconfigure($classes, ProcessorInterface::class, [RemoveProcessor::class, PersistProcessor::class]);
$this->app->singleton(CallableProvider::class, function (Application $app) {
$tagged = iterator_to_array($app->tagged(ProviderInterface::class));
return new CallableProvider(new ServiceLocator($tagged));
});
$this->autoconfigure($classes, ProviderInterface::class, [ItemProvider::class, CollectionProvider::class, ErrorProvider::class]);
$this->app->singleton(ResourceMetadataCollectionFactoryInterface::class, function (Application $app) {
/** @var ConfigRepository $config */
$config = $app['config'];
$formats = $config->get('api-platform.formats');
if ($config->get('api-platform.swagger_ui.enabled', false) && !isset($formats['html'])) {
$formats['html'] = ['text/html'];
}
return new CacheResourceCollectionMetadataFactory(
new EloquentResourceCollectionMetadataFactory(
new ParameterValidationResourceMetadataCollectionFactory(
new ParameterResourceMetadataCollectionFactory(
$this->app->make(PropertyNameCollectionFactoryInterface::class),
$this->app->make(PropertyMetadataFactoryInterface::class),
new AlternateUriResourceMetadataCollectionFactory(
new FiltersResourceMetadataCollectionFactory(
new FormatsResourceMetadataCollectionFactory(
new InputOutputResourceMetadataCollectionFactory(
new PhpDocResourceMetadataCollectionFactory(
new OperationNameResourceMetadataCollectionFactory(
new LinkResourceMetadataCollectionFactory(
$app->make(LinkFactoryInterface::class),
new UriTemplateResourceMetadataCollectionFactory(
$app->make(LinkFactoryInterface::class),
$app->make(PathSegmentNameGeneratorInterface::class),
new NotExposedOperationResourceMetadataCollectionFactory(
$app->make(LinkFactoryInterface::class),
new AttributesResourceMetadataCollectionFactory(
new ConcernsResourceMetadataCollectionFactory(
null,
$app->make(LoggerInterface::class),
$config->get('api-platform.defaults', []),
$config->get('api-platform.graphql.enabled'),
),
$app->make(LoggerInterface::class),
$config->get('api-platform.defaults', []),
$config->get('api-platform.graphql.enabled'),
),
)
),
$config->get('api-platform.graphql.enabled')
)
)
)
),
$formats,
$config->get('api-platform.patch_formats'),
)
)
),
$app->make('filters'),
$app->make(CamelCaseToSnakeCaseNameConverter::class),
$this->app->make(LoggerInterface::class)
),
$app->make('filters')
)
),
true === $config->get('app.debug') ? 'array' : $config->get('api-platform.cache', 'file')
);
});
if (interface_exists(FieldsBuilderEnumInterface::class)) {
$this->registerGraphQl();
}
}
private function registerGraphQl(): void
{
$this->app->singleton('api_platform.graphql.state_provider.parameter', function (Application $app) {
$tagged = iterator_to_array($app->tagged(ParameterProviderInterface::class));
$tagged['api_platform.serializer.filter_parameter_provider'] = $app->make(SerializerFilterParameterProvider::class);
return new ParameterProvider(
new ParameterValidatorProvider(
new SecurityParameterProvider(
$app->make(GraphQlDenormalizeProvider::class),
$app->make(ResourceAccessCheckerInterface::class)
),
),
new ServiceLocator($tagged)
);
});
$this->app->singleton(FieldsBuilderEnumInterface::class, function (Application $app) {
/** @var ConfigRepository */
$config = $app['config'];
return new FieldsBuilder(
$app->make(PropertyNameCollectionFactoryInterface::class),
$app->make(PropertyMetadataFactoryInterface::class),
$app->make(ResourceMetadataCollectionFactoryInterface::class),
$app->make(ResourceClassResolverInterface::class),
$app->make(TypesContainerInterface::class),
$app->make(ContextAwareTypeBuilderInterface::class),
$app->make(TypeConverterInterface::class),
$app->make(ResolverFactoryInterface::class),
$app->make('filters'),
$app->make(Pagination::class),
$app->make(NameConverterInterface::class),
$config->get('api-platform.graphql.nesting_separator') ?? '__',
$app->make(InflectorInterface::class)
);
});
}
/**
* @param array<class-string, \ReflectionClass> $classes
* @param class-string $interface
* @param array<int, class-string> $apiPlatformProviders
*/
private function autoconfigure(array $classes, string $interface, array $apiPlatformProviders): void
{
$m = $apiPlatformProviders;
foreach ($classes as $className => $refl) {
if ($refl->implementsInterface($interface)) {
$m[] = $className;
}
}
if ($m) {
$this->app->tag($m, $interface);
}
}
/**
* Get the services provided by the provider.
*
* @return array<int, string>
*/
public function provides(): array
{
return [
CallableProvider::class,
CallableProcessor::class,
ItemProvider::class,
CollectionProvider::class,
SerializerFilterParameterProvider::class,
ParameterProvider::class,
FilterQueryExtension::class,
'filters',
ResourceMetadataCollectionFactoryInterface::class,
'api_platform.graphql.state_provider.parameter',
FieldsBuilderEnumInterface::class,
];
}
}