Completed
Pull Request — master (#259)
by Cristian
02:19
created

BaseServiceProvider::boot()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 27
rs 8.8571
c 0
b 0
f 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
    protected $commands = [
12
        \Backpack\Base\app\Console\Commands\Install::class,
13
        \Backpack\Base\app\Console\Commands\AddSidebarContent::class,
14
        \Backpack\Base\app\Console\Commands\AddCustomRouteContent::class,
15
    ];
16
17
    /**
18
     * Indicates if loading of the provider is deferred.
19
     *
20
     * @var bool
21
     */
22
    protected $defer = false;
23
24
    /**
25
     * Where the route file lives, both inside the package and in the app (if overwritten).
26
     *
27
     * @var string
28
     */
29
    public $routeFilePath = '/routes/backpack/base.php';
30
31
    /**
32
     * Where custom routes can be written, and will be registered by Backpack.
33
     *
34
     * @var string
35
     */
36
    public $customRoutesFilePath = '/routes/backpack/custom.php';
37
38
    /**
39
     * Perform post-registration booting of services.
40
     *
41
     * @return void
42
     */
43
    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...
44
    {
45
        // LOAD THE VIEWS
46
        // - first the published views (in case they have any changes)
47
        $this->loadViewsFrom(resource_path('views/vendor/backpack/base'), 'backpack');
48
        // - then the stock views that come with the package, in case a published view might be missing
49
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views'), 'backpack');
50
51
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack');
52
53
        // use the vendor configuration file as fallback
54
        $this->mergeConfigFrom(
55
            __DIR__.'/config/backpack/base.php', 'backpack.base'
56
        );
57
58
        // add the root disk to filesystem configuration
59
        app()->config['filesystems.disks.root'] = [
60
            'driver' => 'local',
61
            'root'   => base_path(),
62
        ];
63
64
        $this->registerMiddlewareGroup($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...
65
        $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...
66
        $this->setupCustomRoutes($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...
67
        $this->publishFiles();
68
        $this->loadHelpers();
69
    }
70
71
    /**
72
     * Load the Backpack helper methods, for convenience.
73
     */
74
    public function loadHelpers()
75
    {
76
        require_once __DIR__.'/helpers.php';
77
    }
78
79
    /**
80
     * Define the routes for the application.
81
     *
82
     * @param \Illuminate\Routing\Router $router
83
     *
84
     * @return void
85
     */
86
    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...
87
    {
88
        // by default, use the routes file provided in vendor
89
        $routeFilePathInUse = __DIR__.$this->routeFilePath;
90
91
        // but if there's a file with the same name in routes/backpack, use that one
92
        if (file_exists(base_path().$this->routeFilePath)) {
93
            $routeFilePathInUse = base_path().$this->routeFilePath;
94
        }
95
96
        $this->loadRoutesFrom($routeFilePathInUse);
97
    }
98
99
    /**
100
     * Load custom routes file.
101
     *
102
     * @param \Illuminate\Routing\Router $router
103
     *
104
     * @return void
105
     */
106
    public function setupCustomRoutes(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...
107
    {
108
        // if the custom routes file is published, register its routes
109
        if (file_exists(base_path().$this->customRoutesFilePath)) {
110
            $this->loadRoutesFrom(base_path().$this->customRoutesFilePath);
111
        }
112
    }
113
114
    /**
115
     * Register any package services.
116
     *
117
     * @return void
118
     */
119
    public function register()
120
    {
121
        // register the current package
122
        $this->app->bind('base', function ($app) {
123
            return new Base($app);
124
        });
125
126
        // register its dependencies
127
        $this->app->register(\Jenssegers\Date\DateServiceProvider::class);
128
        $this->app->register(\Prologue\Alerts\AlertsServiceProvider::class);
129
        $this->app->register(\Creativeorange\Gravatar\GravatarServiceProvider::class);
130
131
        // register their aliases
132
        $loader = \Illuminate\Foundation\AliasLoader::getInstance();
133
        $loader->alias('Alert', \Prologue\Alerts\Facades\Alert::class);
134
        $loader->alias('Date', \Jenssegers\Date\Date::class);
135
        $loader->alias('Gravatar', \Creativeorange\Gravatar\Facades\Gravatar::class);
136
137
        // register the services that are only used for development
138
        if ($this->app->environment() == 'local') {
139
            if (class_exists('Laracasts\Generators\GeneratorsServiceProvider')) {
140
                $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
141
            }
142
            if (class_exists('Backpack\Generators\GeneratorsServiceProvider')) {
143
                $this->app->register('Backpack\Generators\GeneratorsServiceProvider');
144
            }
145
        }
146
147
        // register the artisan commands
148
        $this->commands($this->commands);
149
    }
150
151
    public function registerMiddlewareGroup(Router $router)
152
    {
153
        $middleware_key = config('backpack.base.middleware_key');
154
        $middleware_class = config('backpack.base.middleware_class');
155
156
        if (!is_array($middleware_class)) {
157
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
158
159
            return;
160
        }
161
162
        foreach ($middleware_class as $middleware_class) {
163
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
164
        }
165
    }
166
167
    public function publishFiles()
168
    {
169
        $error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')];
170
        $backpack_base_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack/base')];
171
        $backpack_public_assets = [__DIR__.'/public' => public_path('vendor/backpack')];
172
        $backpack_lang_files = [__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')];
173
        $backpack_config_files = [__DIR__.'/config' => config_path()];
174
175
        // sidebar_content view, which is the only view most people need to overwrite
176
        $backpack_sidebar_contents_view = [__DIR__.'/resources/views/inc/sidebar_content.blade.php' => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php')];
177
        $backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)];
178
179
        // calculate the path from current directory to get the vendor path
180
        $vendorPath = dirname(__DIR__, 3);
181
        $adminlte_assets = [$vendorPath.'/almasaeed2010/adminlte' => public_path('vendor/adminlte')];
182
        $gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()];
183
184
        // establish the minimum amount of files that need to be published, for Backpack to work; there are the files that will be published by the install command
185
        $minimum = array_merge(
186
            $error_views,
187
            // $backpack_base_views,
188
            $backpack_public_assets,
189
            // $backpack_lang_files,
190
            $backpack_config_files,
191
            $backpack_sidebar_contents_view,
192
            $backpack_custom_routes_file,
193
            $adminlte_assets,
194
            $gravatar_assets
195
        );
196
197
        // register all possible publish commands and assign tags to each
198
        $this->publishes($backpack_config_files, 'config');
199
        $this->publishes($backpack_lang_files, 'lang');
200
        $this->publishes($backpack_base_views, 'views');
201
        $this->publishes($backpack_sidebar_contents_view, 'sidebar_content');
202
        $this->publishes($error_views, 'errors');
203
        $this->publishes($backpack_public_assets, 'public');
204
        $this->publishes($backpack_custom_routes_file, 'custom_routes');
205
        $this->publishes($adminlte_assets, 'adminlte');
206
        $this->publishes($gravatar_assets, 'gravatar');
207
        $this->publishes($minimum, 'minimum');
208
    }
209
}
210