Passed
Branch split-service-provider-lumen-l... (a6b330)
by Pieter
05:01
created

ApieLaravelServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 10 3
A register() 0 20 3
1
<?php
2
namespace W2w\Laravel\Apie\Providers;
3
4
use Illuminate\Contracts\Routing\UrlGenerator;
5
use Illuminate\Support\ServiceProvider;
6
use Psr\Http\Message\ServerRequestInterface;
7
use W2w\Laravel\Apie\Controllers\SwaggerUiController;
8
9
/**
10
 * Service provider for Apie to link to Laravel (and that do not work in Lumen)
11
 */
12
class ApieLaravelServiceProvider extends ServiceProvider
13
{
14
    public function boot()
15
    {
16
        $config = $this->app->get('apie.config');
17
        if ($config['disable-routes']) {
18
            return;
19
        }
20
        if ($config['swagger-ui-test-page']) {
21
            $this->loadRoutesFrom(__DIR__ . '/../../config/routes-openapi.php');
22
        }
23
        $this->loadRoutesFrom(__DIR__ . '/../../config/routes.php');
24
    }
25
26
    public function register()
27
    {
28
        // fix for https://github.com/laravel/framework/issues/30415
29
        $this->app->extend(
30
            ServerRequestInterface::class, function (ServerRequestInterface $psrRequest) {
31
                $route = $this->app->make('request')->route();
32
                if ($route) {
33
                    $parameters = $route->parameters();
34
                    foreach ($parameters as $key => $value) {
35
                        $psrRequest = $psrRequest->withAttribute($key, $value);
36
                    }
37
                }
38
                return $psrRequest;
39
            }
40
        );
41
42
        $this->app->bind(
43
            SwaggerUiController::class, function () {
44
                $urlGenerator = $this->app->get(UrlGenerator::class);
45
                return new SwaggerUiController($urlGenerator, __DIR__ . '/../../resources/open-api.html');
46
            }
47
        );
48
    }
49
}
50