56
56
use ApiPlatform \Laravel \Eloquent \State \LinksHandlerInterface ;
57
57
use ApiPlatform \Laravel \Eloquent \State \PersistProcessor ;
58
58
use ApiPlatform \Laravel \Eloquent \State \RemoveProcessor ;
59
- use ApiPlatform \Laravel \Exception \Handler ;
59
+ use ApiPlatform \Laravel \Exception \ErrorHandler ;
60
60
use ApiPlatform \Laravel \Routing \IriConverter ;
61
61
use ApiPlatform \Laravel \Routing \Router as UrlGeneratorRouter ;
62
62
use ApiPlatform \Laravel \Routing \SkolemIriConverter ;
63
+ use ApiPlatform \Laravel \Security \ResourceAccessChecker ;
64
+ use ApiPlatform \Laravel \State \AccessCheckerProvider ;
63
65
use ApiPlatform \Laravel \State \SwaggerUiProcessor ;
64
66
use ApiPlatform \Laravel \State \ValidateProvider ;
65
67
use ApiPlatform \Metadata \Exception \NotExposedHttpException ;
91
93
use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
92
94
use ApiPlatform \Metadata \Resource \Factory \ResourceNameCollectionFactoryInterface ;
93
95
use ApiPlatform \Metadata \Resource \Factory \UriTemplateResourceMetadataCollectionFactory ;
96
+ use ApiPlatform \Metadata \ResourceAccessCheckerInterface ;
94
97
use ApiPlatform \Metadata \ResourceClassResolver ;
95
98
use ApiPlatform \Metadata \ResourceClassResolverInterface ;
96
99
use ApiPlatform \Metadata \UrlGeneratorInterface ;
@@ -262,7 +265,7 @@ public function register(): void
262
265
null ,
263
266
$ app ->make (LoggerInterface::class),
264
267
[
265
- 'routePrefix ' => $ config ->get ('api-platform.prefix ' ) ?? '/ ' ,
268
+ 'routePrefix ' => $ config ->get ('api-platform.routes. prefix ' ) ?? '/ ' ,
266
269
],
267
270
false
268
271
)
@@ -324,8 +327,12 @@ public function register(): void
324
327
return new DeserializeProvider ($ app ->make (JsonApiProvider::class), $ app ->make (SerializerInterface::class), $ app ->make (SerializerContextBuilderInterface::class));
325
328
});
326
329
330
+ $ this ->app ->singleton (AccessCheckerProvider::class, function (Application $ app ) {
331
+ return new AccessCheckerProvider ($ app ->make (DeserializeProvider::class), $ app ->make (ResourceAccessCheckerInterface::class));
332
+ });
333
+
327
334
$ this ->app ->singleton (ContentNegotiationProvider::class, function (Application $ app ) use ($ config ) {
328
- return new ContentNegotiationProvider ($ app ->make (DeserializeProvider ::class), new Negotiator (), $ config ->get ('api-platform.formats ' ), $ config ->get ('api-platform.error_formats ' ));
335
+ return new ContentNegotiationProvider ($ app ->make (AccessCheckerProvider ::class), new Negotiator (), $ config ->get ('api-platform.formats ' ), $ config ->get ('api-platform.error_formats ' ));
329
336
});
330
337
331
338
$ this ->app ->bind (ProviderInterface::class, ContentNegotiationProvider::class);
@@ -443,6 +450,10 @@ public function register(): void
443
450
return new HydraEntrypointNormalizer ($ app ->make (ResourceMetadataCollectionFactoryInterface::class), $ app ->make (IriConverterInterface::class), $ app ->make (UrlGeneratorInterface::class));
444
451
});
445
452
453
+ $ this ->app ->singleton (ResourceAccessCheckerInterface::class, function () {
454
+ return new ResourceAccessChecker ();
455
+ });
456
+
446
457
$ this ->app ->singleton (ItemNormalizer::class, function (Application $ app ) use ($ defaultContext ) {
447
458
return new ItemNormalizer (
448
459
$ app ->make (PropertyNameCollectionFactoryInterface::class),
@@ -454,8 +465,7 @@ public function register(): void
454
465
$ app ->make (ClassMetadataFactoryInterface::class),
455
466
$ app ->make (LoggerInterface::class),
456
467
$ app ->make (ResourceMetadataCollectionFactoryInterface::class),
457
- /* $resourceAccessChecker */
458
- null ,
468
+ $ app ->make (ResourceAccessCheckerInterface::class),
459
469
$ defaultContext
460
470
);
461
471
});
@@ -481,8 +491,7 @@ public function register(): void
481
491
$ app ->make (ClassMetadataFactoryInterface::class),
482
492
$ app ->make (LoggerInterface::class),
483
493
$ app ->make (ResourceMetadataCollectionFactoryInterface::class),
484
- // $app->make(ResourceAccessCheckerInterface::class),
485
- null ,
494
+ $ app ->make (ResourceAccessCheckerInterface::class),
486
495
$ defaultContext
487
496
);
488
497
});
@@ -628,9 +637,8 @@ public function register(): void
628
637
$ app ->make (ClassMetadataFactoryInterface::class),
629
638
$ defaultContext ,
630
639
$ app ->make (ResourceMetadataCollectionFactoryInterface::class),
631
- null ,
640
+ $ app -> make (ResourceAccessCheckerInterface::class) ,
632
641
null
633
- // $app->make(ResourceAccessCheckerInterface::class),
634
642
// $app->make(TagCollectorInterface::class),
635
643
);
636
644
});
@@ -690,15 +698,14 @@ public function register(): void
690
698
$ app ->make (NameConverterInterface::class),
691
699
$ app ->make (ClassMetadataFactoryInterface::class),
692
700
$ defaultContext ,
693
- // $app->make(ResourceAccessCheckerInterface::class),
694
- null
701
+ $ app ->make (ResourceAccessCheckerInterface::class)
695
702
);
696
703
});
697
704
698
705
$ this ->app ->singleton (
699
706
ExceptionHandlerInterface::class,
700
707
function (Application $ app ) {
701
- return new Handler (
708
+ return new ErrorHandler (
702
709
$ app ,
703
710
$ app ->make (ResourceMetadataCollectionFactoryInterface::class),
704
711
$ app ->make (ApiPlatformController::class),
@@ -736,25 +743,28 @@ public function boot(ResourceNameCollectionFactoryInterface $resourceNameCollect
736
743
return ;
737
744
}
738
745
746
+ $ config = $ this ->app ['config ' ];
739
747
$ routeCollection = new RouteCollection ();
740
748
foreach ($ resourceNameCollectionFactory ->create () as $ resourceClass ) {
741
749
foreach ($ resourceMetadataFactory ->create ($ resourceClass ) as $ resourceMetadata ) {
742
750
foreach ($ resourceMetadata ->getOperations () as $ operation ) {
743
751
$ uriTemplate = $ operation ->getUriTemplate ();
744
752
// _format is read by the middleware
745
753
$ uriTemplate = $ operation ->getRoutePrefix ().str_replace ('{._format} ' , '{_format?} ' , $ uriTemplate );
746
- $ route = new Route ([$ operation ->getMethod ()], $ uriTemplate , [ApiPlatformController::class, '__invoke ' ]);
747
- $ route ->name ($ operation ->getName ());
748
- $ route ->setDefaults (['_api_operation_name ' => $ operation ->getName (), '_api_resource_class ' => $ operation ->getClass ()]);
749
- // Another option then to use a middleware, not sure what's best (you then retrieve $request->getRoute() somehow ?)
750
- // $route->??? = ['operation' => $operation];
751
- $ routeCollection ->add ($ route )
752
- ->middleware (ApiPlatformMiddleware::class.': ' .$ operation ->getName ());
754
+ $ route = (new Route ([$ operation ->getMethod ()], $ uriTemplate , [ApiPlatformController::class, '__invoke ' ]))
755
+ ->name ($ operation ->getName ())
756
+ ->setDefaults (['_api_operation_name ' => $ operation ->getName (), '_api_resource_class ' => $ operation ->getClass ()]);
757
+
758
+ $ route ->middleware (ApiPlatformMiddleware::class.': ' .$ operation ->getName ());
759
+ $ route ->middleware ($ config ->get ('api-platform.routes.middleware ' ));
760
+ $ route ->middleware ($ operation ->getMiddleware ());
761
+
762
+ $ routeCollection ->add ($ route );
753
763
}
754
764
}
755
765
}
756
766
757
- $ prefix = $ this -> app [ ' config ' ] ->get ('api-platform.prefix ' ) ?? '' ;
767
+ $ prefix = $ config ->get ('api-platform.routes .prefix ' ) ?? '' ;
758
768
$ route = new Route (['GET ' ], $ prefix .'/contexts/{shortName?}{_format?} ' , [ContextAction::class, '__invoke ' ]);
759
769
$ route ->name ('api_jsonld_context ' )->middleware (ApiPlatformMiddleware::class);
760
770
$ routeCollection ->add ($ route );
@@ -784,10 +794,6 @@ public function boot(ResourceNameCollectionFactoryInterface $resourceNameCollect
784
794
785
795
private function shouldRegisterRoutes (): bool
786
796
{
787
- if (!$ this ->app ['config ' ]->get ('api-platform.register_routes ' )) {
788
- return false ;
789
- }
790
-
791
797
if ($ this ->app instanceof CachesRoutes && $ this ->app ->routesAreCached ()) {
792
798
return false ;
793
799
}
0 commit comments