Completed
Pull Request — master (#259)
by Cristian
03:39 queued 36s
created

BaseServiceProvider::setupRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 12
rs 9.4285
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
        } else {
146
            $this->checkLicenseCodeExists();
147
        }
148
149
        // register the artisan commands
150
        $this->commands($this->commands);
151
    }
152
153
    public function registerMiddlewareGroup(Router $router)
154
    {
155
        $middleware_key = config('backpack.base.middleware_key');
156
        $middleware_class = config('backpack.base.middleware_class');
157
158
        if (!is_array($middleware_class)) {
159
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
160
161
            return;
162
        }
163
164
        foreach ($middleware_class as $middleware_class) {
165
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
166
        }
167
    }
168
169
    public function publishFiles()
170
    {
171
        $error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')];
172
        $backpack_base_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack/base')];
173
        $backpack_public_assets = [__DIR__.'/public' => public_path('vendor/backpack')];
174
        $backpack_lang_files = [__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')];
175
        $backpack_config_files = [__DIR__.'/config' => config_path()];
176
177
        // sidebar_content view, which is the only view most people need to overwrite
178
        $backpack_sidebar_contents_view = [__DIR__.'/resources/views/inc/sidebar_content.blade.php' => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php')];
179
        $backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)];
180
181
        // calculate the path from current directory to get the vendor path
182
        $vendorPath = dirname(__DIR__, 3);
183
        $adminlte_assets = [$vendorPath.'/almasaeed2010/adminlte' => public_path('vendor/adminlte')];
184
        $gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()];
185
186
        // 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
187
        $minimum = array_merge(
188
            $error_views,
189
            // $backpack_base_views,
190
            $backpack_public_assets,
191
            // $backpack_lang_files,
192
            $backpack_config_files,
193
            $backpack_sidebar_contents_view,
194
            $backpack_custom_routes_file,
195
            $adminlte_assets,
196
            $gravatar_assets
197
        );
198
199
        // register all possible publish commands and assign tags to each
200
        $this->publishes($backpack_config_files, 'config');
201
        $this->publishes($backpack_lang_files, 'lang');
202
        $this->publishes($backpack_base_views, 'views');
203
        $this->publishes($backpack_sidebar_contents_view, 'sidebar_content');
204
        $this->publishes($error_views, 'errors');
205
        $this->publishes($backpack_public_assets, 'public');
206
        $this->publishes($backpack_custom_routes_file, 'custom_routes');
207
        $this->publishes($adminlte_assets, 'adminlte');
208
        $this->publishes($gravatar_assets, 'gravatar');
209
        $this->publishes($minimum, 'minimum');
210
    }
211
212
    /**
213
     * Check to to see if a license code exists.
214
     * If it does not, throw a notification bubble.
215
     *
216
     * @return void
217
     */
218
    private function checkLicenseCodeExists()
219
    {
220
        if (!env('BACKPACK_LICENSE')) {
221
            \Alert::add('warning', "<strong>You're using unlicensed software.</strong> Please ask your web developer to <a target='_blank' href='http://backpackforlaravel.com'>purchase a license code</a> to hide this message.");
222
        }
223
    }
224
}
225