Completed
Pull Request — master (#87)
by Cristian
08:06
created

BaseServiceProvider::boot()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\Base;
4
5
use Config;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\ServiceProvider;
8
use Route;
9
10
class BaseServiceProvider extends ServiceProvider
11
{
12
    protected $commands = [
13
        \Backpack\Base\app\Console\Commands\Install::class,
14
    ];
15
16
    /**
17
     * Indicates if loading of the provider is deferred.
18
     *
19
     * @var bool
20
     */
21
    protected $defer = false;
22
23
    /**
24
     * Where the route file lives, both inside the package and in the app (if overwritten).
25
     *
26
     * @var string
27
     */
28
    public $routeFilePath = '/routes/backpack/base.php';
29
30
    /**
31
     * Perform post-registration booting of services.
32
     *
33
     * @return void
34
     */
35
    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...
36
    {
37
        // -------------
38
        // LOAD THE VIEWS
39
        // -------------
40
        // first the published views (in case they have any changes)
41
        $this->loadViewsFrom(resource_path('views/vendor/backpack/base'), 'backpack');
42
        // then the stock views that come with the package, in case a published view might be missing
43
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views'), 'backpack');
44
45
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack');
46
47
        // use the vendor configuration file as fallback
48
        $this->mergeConfigFrom(
49
            __DIR__.'/config/backpack/base.php',
50
            'backpack.base'
51
        );
52
53
        $this->registerGuards();
54
        $this->registerMiddleware($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...
55
        $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...
56
        $this->publishFiles();
57
        $this->loadHelpers();
58
    }
59
60
    /**
61
     * Load the Backpack helper methods, for convenience.
62
     */
63
    public function loadHelpers()
64
    {
65
        require_once __DIR__.'/helpers.php';
66
    }
67
68
    /**
69
     * Define the routes for the application.
70
     *
71
     * @param \Illuminate\Routing\Router $router
72
     *
73
     * @return void
74
     */
75
    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...
76
    {
77
        // by default, use the routes file provided in vendor
78
        $routeFilePathInUse = __DIR__.$this->routeFilePath;
79
80
        // but if there's a file with the same name in routes/backpack, use that one
81
        if (file_exists(base_path().$this->routeFilePath)) {
82
            $routeFilePathInUse = base_path().$this->routeFilePath;
83
        }
84
85
        $this->loadRoutesFrom($routeFilePathInUse);
86
    }
87
88
    /**
89
     * Register custom backpack authentication guard.
90
     */
91
    public function registerGuards()
92
    {
93
        $backpackAuthGuard = Config::get('backpack.base.admin_guard');
94
        $existingGuards = Config::get('auth.guards');
95
        $existingGuards[$backpackAuthGuard['name']] = $backpackAuthGuard;
96
97
        Config::set('auth.guards', $existingGuards);
98
    }
99
100
    /**
101
     * Register any package services.
102
     *
103
     * @return void
104
     */
105
    public function register()
106
    {
107
        // register the current package
108
        $this->app->bind('base', function ($app) {
109
            return new Base($app);
110
        });
111
112
        // register its dependencies
113
        $this->app->register(\Jenssegers\Date\DateServiceProvider::class);
114
        $this->app->register(\Prologue\Alerts\AlertsServiceProvider::class);
115
        $this->app->register(\Creativeorange\Gravatar\GravatarServiceProvider::class);
116
117
        // register their aliases
118
        $loader = \Illuminate\Foundation\AliasLoader::getInstance();
119
        $loader->alias('Alert', \Prologue\Alerts\Facades\Alert::class);
120
        $loader->alias('Date', \Jenssegers\Date\Date::class);
121
        $loader->alias('Gravatar', \Creativeorange\Gravatar\Facades\Gravatar::class);
122
123
        // register the services that are only used for development
124
        if ($this->app->environment() == 'local') {
125
            if (class_exists('Laracasts\Generators\GeneratorsServiceProvider')) {
126
                $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
127
            }
128
            if (class_exists('Backpack\Generators\GeneratorsServiceProvider')) {
129
                $this->app->register('Backpack\Generators\GeneratorsServiceProvider');
130
            }
131
        }
132
133
        // register the artisan commands
134
        $this->commands($this->commands);
135
    }
136
137
    public function registerMiddleware(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...
138
    {
139
        Route::aliasMiddleware('backpack.auth', \Backpack\Base\app\Http\Middleware\BackpackAuth::class);
140
141
        if (config('backpack.base.separate_admin_session')) {
142
            Route::aliasMiddleware('backpack.auth.guard', \Backpack\Base\app\Http\Middleware\BackpackAuthGuard::class);
143
        }
144
    }
145
146
    public function publishFiles()
147
    {
148
        // publish config file
149
        $this->publishes([__DIR__.'/config' => config_path()], 'config');
150
151
        // publish lang files
152
        // $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...
153
154
        // publish views
155
        $this->publishes([__DIR__.'/resources/views' => resource_path('views/vendor/backpack/base')], 'views');
156
157
        // publish error views
158
        $this->publishes([__DIR__.'/resources/error_views' => resource_path('views/errors')], 'errors');
159
160
        // publish public Backpack assets
161
        $this->publishes([__DIR__.'/public' => public_path('vendor/backpack')], 'public');
162
163
        // calculate the path from current directory to get the vendor path
164
        $vendorPath = dirname(__DIR__, 3);
165
166
        // publish public AdminLTE assets
167
        $this->publishes([$vendorPath.'/almasaeed2010/adminlte' => public_path('vendor/adminlte')], 'adminlte');
168
169
        // publish public Gravatar assets
170
        $this->publishes([$vendorPath.'/creativeorange/gravatar/config' => config_path()], 'gravatar');
171
    }
172
}
173