Completed
Pull Request — master (#160)
by
unknown
01:52
created

BaseServiceProvider::register()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 26
rs 8.5806
cc 4
eloc 13
nc 5
nop 0
1
<?php
2
3
namespace Backpack\Base;
4
5
use Illuminate\Routing\Router;
6
use Illuminate\Support\ServiceProvider;
7
use Route;
8
9
class BaseServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Indicates if loading of the provider is deferred.
13
     *
14
     * @var bool
15
     */
16
    protected $defer = false;
17
18
    /**
19
     * Perform post-registration booting of services.
20
     *
21
     * @return void
22
     */
23
    public function boot(\Illuminate\Routing\Router $router)
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        // LOAD THE VIEWS
26
        // - first the published views (in case they have any changes)
27
        $this->loadViewsFrom(resource_path('views/vendor/backpack/base'), 'backpack');
28
        // - then the stock views that come with the package, in case a published view might be missing
29
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views'), 'backpack');
30
31
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack');
32
33
        // use the vendor configuration file as fallback
34
        $this->mergeConfigFrom(
35
            __DIR__.'/config/backpack/base.php',
36
            'backpack.base'
37
        );
38
39
        $this->registerAdminMiddleware($this->app->router);
0 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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
40
        $this->setupRoutes($this->app->router);
0 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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
41
        $this->publishFiles();
42
    }
43
44
    /**
45
     * Define the routes for the application.
46
     *
47
     * @param \Illuminate\Routing\Router $router
48
     *
49
     * @return void
50
     */
51
    public function setupRoutes(Router $router)
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        Route::group(
54
            [
55
            'namespace'  => 'Backpack\Base\app\Http\Controllers',
56
            'middleware' => 'web',
57
            'prefix'     => config('backpack.base.route_prefix'),
58
            ],
59
            function () {
60
            // if not otherwise configured, setup the auth routes
61
                if (config('backpack.base.setup_auth_routes')) {
62
                    // if permission restriction is on disable backpack auth routes
63
                    if (!config('backpack.base.permission_protection')) {
64
                        Route::auth();
65
                    }
66
                    Route::get('logout', 'Auth\LoginController@logout');
67
                }
68
            }
69
        );
70
71
        Route::group(
72
            [
73
            'namespace'  => 'Backpack\Base\app\Http\Controllers',
74
            'middleware' => ['web', 'admin'],
75
            'prefix'     => config('backpack.base.route_prefix'),
76
            ],
77
            function () {
78
            // if not otherwise configured, setup the dashboard routes
79
                if (config('backpack.base.setup_dashboard_routes')) {
80
                    Route::get('dashboard', 'AdminController@dashboard');
81
                    Route::get('/', 'AdminController@redirect');
82
                }
83
            }
84
        );
85
    }
86
87
    /**
88
     * Register any package services.
89
     *
90
     * @return void
91
     */
92
    public function register()
93
    {
94
        // register the current package
95
        $this->app->bind('base', function ($app) {
96
            return new Base($app);
97
        });
98
99
        // register its dependencies
100
        $this->app->register(\Jenssegers\Date\DateServiceProvider::class);
101
        $this->app->register(\Prologue\Alerts\AlertsServiceProvider::class);
102
103
        // register their aliases
104
        $loader = \Illuminate\Foundation\AliasLoader::getInstance();
105
        $loader->alias('Alert', \Prologue\Alerts\Facades\Alert::class);
106
        $loader->alias('Date', \Jenssegers\Date\Date::class);
107
108
        // register the services that are only used for development
109
        if ($this->app->environment() == 'local') {
110
            if (class_exists('Laracasts\Generators\GeneratorsServiceProvider')) {
111
                $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
112
            }
113
            if (class_exists('Backpack\Generators\GeneratorsServiceProvider')) {
114
                $this->app->register('Backpack\Generators\GeneratorsServiceProvider');
115
            }
116
        }
117
    }
118
119
    public function registerAdminMiddleware(Router $router)
120
    {
121
        // in Laravel 5.4
122
        if (method_exists($router, 'aliasMiddleware')) {
123
            Route::aliasMiddleware('admin', \Backpack\Base\app\Http\Middleware\Admin::class);
124
        } // in Laravel 5.3 and below
125
        else {
126
            Route::middleware('admin', \Backpack\Base\app\Http\Middleware\Admin::class);
0 ignored issues
show
Bug introduced by
The method middleware() does not exist on Illuminate\Routing\Router. Did you maybe mean aliasMiddleware()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
127
        }
128
    }
129
130
    public function publishFiles()
131
    {
132
        // publish config file
133
        $this->publishes([__DIR__.'/config' => config_path()], 'config');
134
135
        // publish lang files
136
        // $this->publishes([__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')], 'lang');
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
138
        // publish views
139
        $this->publishes([__DIR__.'/resources/views' => resource_path('views/vendor/backpack/base')], 'views');
140
141
        // publish error views
142
        $this->publishes([__DIR__.'/resources/error_views' => resource_path('views/errors')], 'errors');
143
144
        // publish public Backpack assets
145
        $this->publishes([__DIR__.'/public' => public_path('vendor/backpack')], 'public');
146
147
        // publish public AdminLTE assets
148
        $this->publishes([base_path('vendor/almasaeed2010/adminlte') => public_path('vendor/adminlte')], 'adminlte');
149
    }
150
}
151