pjordaan /
laravel-apie
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace W2w\Laravel\Apie\Providers; |
||
| 5 | |||
| 6 | use Illuminate\Support\ServiceProvider; |
||
| 7 | use Psr\Http\Message\ServerRequestInterface; |
||
| 8 | use W2w\Laravel\Apie\Services\RequestToFacadeResponseConverter; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Service provider that allows you to add a REST api from a service provider. |
||
| 12 | */ |
||
| 13 | abstract class AbstractRestApiServiceProvider extends ServiceProvider |
||
| 14 | { |
||
| 15 | abstract protected function getApiName(): string; |
||
| 16 | |||
| 17 | abstract protected function getApiConfig(): array; |
||
| 18 | |||
| 19 | public function register() |
||
| 20 | { |
||
| 21 | $apiName = $this->getApiName(); |
||
| 22 | $apiConfig = $this->getApiConfig(); |
||
| 23 | $this->app->get('config')->set('apie.contexts.' . $apiName, $apiConfig); |
||
| 24 | if ($apiConfig['bind-api-resource-facade-response'] ?? true) { |
||
| 25 | foreach ($apiConfig['resources'] as $resourceClass) { |
||
| 26 | $this->registerResourceClass($apiName, $resourceClass); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | private function registerResourceClass(string $apiName, string $resourceClass) |
||
| 32 | { |
||
| 33 | $this->app->bind($resourceClass, function () use ($resourceClass, $apiName) { |
||
|
0 ignored issues
–
show
|
|||
| 34 | /** @var ServerRequestInterface $request */ |
||
| 35 | $request = $this->app->get(ServerRequestInterface::class); |
||
| 36 | |||
| 37 | /** @var RequestToFacadeResponseConverter $converter */ |
||
| 38 | $converter = $this->app->get(RequestToFacadeResponseConverter::class); |
||
| 39 | |||
| 40 | return $converter->convertRequestToResponse($resourceClass, $request)->getResource(); |
||
| 41 | }); |
||
| 42 | } |
||
| 43 | } |
||
| 44 |
This check looks for imports that have been defined, but are not used in the scope.