Passed
Push — master ( 0017b2...5a58d2 )
by Hesham
02:57
created

LumenerServiceProvider::boot()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 29
rs 8.9777
c 0
b 0
f 0
cc 6
nc 16
nop 0
1
<?php
2
3
namespace Lumener;
4
5
use Illuminate\Support\ServiceProvider;
6
use Lumener\Console\UpdateCommand;
7
use Lumener\Console\StylizeCommand;
8
use Lumener\Console\PluginCommand;
9
10
class LumenerServiceProvider extends ServiceProvider
11
{
12
    protected $namespace = 'Lumener\Controllers';
13
    protected $route_options;
14
    protected $route_path;
15
    protected $route_name;
16
    protected $middleware = [
17
        // 'start_session' => \Illuminate\Session\Middleware\StartSession::class
18
    ];
19
    /**
20
     * Bootstrap the application services.
21
     *
22
     * @return void
23
     */
24
    public function boot()
25
    {
26
        define("LUMENER_STORAGE", config('lumener.storage', base_path('storage/lumener')));
27
        if (!is_dir(LUMENER_STORAGE)) {
28
            mkdir(LUMENER_STORAGE);
29
        }
30
31
        $this->route_path = config('lumener.route.path', 'lumener');
32
33
        $route_options = config('lumener.route.options', []);
34
        if (!isset($route_options['as'])) {
35
            $route_options['as'] = 'lumener';
36
        }
37
        if (!isset($route_options['uses'])) {
38
            $route_options['uses'] = 'LumenerController@index';
39
        }
40
        if (isset($route_options['middleware'])) {
41
            $route_options['middleware'] =
42
             array_unique(array_merge(
43
                 array_keys($this->middleware),
44
                 is_array($route_options['middleware']) ?
45
                  $route_options['middleware'] : [$route_options['middleware']]
46
            ));
47
        } else {
48
            $route_options['middleware'] = array_keys($this->middleware);
49
        }
50
        $this->route_options = $route_options;
51
        $this->route_name = $route_options['as'];
52
        $this->map();
53
    }
54
55
    public function map()
56
    {
57
        $this->mapAdminerRoutes();
58
    }
59
60
    public function mapAdminerRoutes()
61
    {
62
        if ($this->app->router instanceof \Laravel\Lumen\Routing\Router) {
2 ignored issues
show
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
The type Laravel\Lumen\Routing\Router was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
            // Lumen
64
            $this->mapAdminerRoutesForLumen();
65
        } else {
66
            // Laravel
67
            $this->mapAdminerRoutesForLaravel();
68
        }
69
    }
70
71
    public function mapAdminerRoutesForLumen()
72
    {
73
        $named = $this->app->router->namedRoutes;
1 ignored issue
show
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
74
        if (isset($named[$this->route_name])) {
75
            $uri = $named[$this->route_name];
76
            $route = $this->app->router->getRoutes()[$uri];
77
            foreach ($route['action'] as $key => $value) {
78
                if ($key == "middleware") {
79
                    $this->route_options['middleware'] =
80
                     array_unique(array_merge(
81
                         $this->route_options['middleware'],
82
                         is_array($value) ? $value : [$value]
83
                    ));
84
                } elseif ($key == "uses") {
85
                    $this->route_options[$key] = "\\{$value}";
86
                } else {
87
                    $this->route_options[$key] = $value;
88
                }
89
            }
90
            $this->route_path = $uri;
91
        }
92
        $this->route_options['namespace'] = $this->namespace;
93
        $this->app->router->group($this->route_options, function ($router) {
94
            $router->addRoute(
95
                ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
96
                $this->route_path,
97
                ['uses' => $this->route_options['uses']]
98
            );
99
            $this->route_options['as'] = "lumener-resources";
100
            $this->route_options['uses'] = 'LumenerController@getResource';
101
            $router->get(
102
                $this->route_path . '/resources',
103
                ['uses' => 'LumenerController@getResource',
104
                 'as' => 'lumener-resources']
105
            );
106
        });
107
    }
108
109
    public function mapAdminerRoutesForLaravel()
110
    {
111
        // TODO: Merge routes for laravel
112
        \Route::namespace($this->namespace)
113
            ->group(function () {
114
                Route::any($this->route_path, $this->route_options);
0 ignored issues
show
Bug introduced by
The type Lumener\Route was not found. Did you mean Route? If so, make sure to prefix the type with \.
Loading history...
115
                $this->route_options['uses'] = "LumenerController@getResource'";
116
                Route::get($this->route_path.'/resources', $this->route_options);
117
            });
118
    }
119
120
    /**
121
     * Register the application services.
122
     *
123
     * @return void
124
     */
125
    public function register()
126
    {
127
        if (method_exists($this->app, 'routeMiddleware')) {
128
            // Lumen
129
            $this->app->routeMiddleware(
130
                $this->middleware
131
            );
132
        } elseif (!method_exists($this->app['router'], 'middleware')) {
133
            // Laravel 5.4 no longer has middleware function on the router class
134
            foreach ($this->middleware as $alias => $class) {
135
                $this->app['router']->aliasMiddleware($alias, $class);
136
            }
137
        } else {
138
            foreach ($this->middleware as $alias => $class) {
139
                $this->app['router']->middleware($alias, $class);
140
            }
141
        }
142
143
144
        $this->app->singleton(
145
            'command.lumener.update',
146
            function (/** @scrutinizer ignore-unused */ $app) {
147
                return new UpdateCommand();
148
            }
149
        );
150
151
        $this->app->singleton(
152
            'command.lumener.stylize',
153
            function (/** @scrutinizer ignore-unused */ $app) {
154
                return new StylizeCommand();
155
            }
156
        );
157
        $this->app->singleton(
158
            'command.lumener.plugin',
159
            function (/** @scrutinizer ignore-unused */ $app) {
160
                return new PluginCommand();
161
            }
162
        );
163
        $this->commands(
164
            ['command.lumener.update',
165
            'command.lumener.stylize',
166
            'command.lumener.plugin']
167
        );
168
    }
169
170
    /**
171
     * Get the services provided by the provider.
172
     *
173
     * @return array
174
     */
175
    public function provides()
176
    {
177
        return [
178
                'command.lumener.update',
179
                'command.lumener.stylize',
180
                'command.lumener.plugin'
181
             ];
182
    }
183
}
184