Completed
Push — master ( 56cad9...ac5209 )
by Cristian
01:53
created

BaseServiceProvider::setupRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 12
rs 9.8666
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
    const VERSION = '1.0.0';
12
13
    protected $commands = [
14
        \Backpack\Base\app\Console\Commands\Install::class,
15
        \Backpack\Base\app\Console\Commands\AddSidebarContent::class,
16
        \Backpack\Base\app\Console\Commands\AddCustomRouteContent::class,
17
        \Backpack\Base\app\Console\Commands\Version::class,
18
        \Backpack\Base\app\Console\Commands\CreateUser::class,
19
        \Backpack\Base\app\Console\Commands\PublishBackpackUserModel::class,
20
        \Backpack\Base\app\Console\Commands\PublishBackpackMiddleware::class,
21
    ];
22
23
    /**
24
     * Indicates if loading of the provider is deferred.
25
     *
26
     * @var bool
27
     */
28
    protected $defer = false;
29
30
    /**
31
     * Where the route file lives, both inside the package and in the app (if overwritten).
32
     *
33
     * @var string
34
     */
35
    public $routeFilePath = '/routes/backpack/base.php';
36
37
    /**
38
     * Where custom routes can be written, and will be registered by Backpack.
39
     *
40
     * @var string
41
     */
42
    public $customRoutesFilePath = '/routes/backpack/custom.php';
43
44
    /**
45
     * Perform post-registration booting of services.
46
     *
47
     * @return void
48
     */
49
    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...
50
    {
51
        $_SERVER['BACKPACK_BASE_VERSION'] = $this::VERSION;
52
        $customViewsFolder = resource_path('views/vendor/backpack/base');
53
54
        // LOAD THE VIEWS
55
        // - first the published views (in case they have any changes)
56
        if (file_exists(resource_path('views/vendor/backpack/base'))) {
57
            $this->loadViewsFrom($customViewsFolder, 'backpack');
58
        }
59
        // - then the stock views that come with the package, in case a published view might be missing
60
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views'), 'backpack');
61
62
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack');
63
64
        // use the vendor configuration file as fallback
65
        $this->mergeConfigFrom(
66
            __DIR__.'/config/backpack/base.php', 'backpack.base'
67
        );
68
69
        // add the root disk to filesystem configuration
70
        app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [
71
            'driver' => 'local',
72
            'root'   => base_path(),
73
        ];
74
75
        $this->addCustomAuthConfigurationValues();
76
        $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...
77
        $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...
78
        $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...
79
        $this->publishFiles();
80
        $this->checkLicenseCodeExists();
81
    }
82
83
    /**
84
     * Load the Backpack helper methods, for convenience.
85
     */
86
    public function loadHelpers()
87
    {
88
        require_once __DIR__.'/helpers.php';
89
    }
90
91
    /**
92
     * Backpack login differs from the standard Laravel login.
93
     * As such, Backpack uses its own authentication provider, password broker and guard.
94
     *
95
     * This method adds those configuration values on top of whatever is in config/auth.php. Developers can overwrite the backpack provider, password broker or guard by adding a provider/broker/guard with the "backpack" name inside their config/auth.php file. Or they can use another provider/broker/guard entirely, by changing the corresponding value inside config/backpack/base.php
96
     */
97
    public function addCustomAuthConfigurationValues()
98
    {
99
        // add the backpack_users authentication provider to the configuration
100
        app()->config['auth.providers'] = app()->config['auth.providers'] +
101
        [
102
            'backpack' => [
103
                'driver'  => 'eloquent',
104
                'model'   => config('backpack.base.user_model_fqn'),
105
            ],
106
        ];
107
108
        // add the backpack_users password broker to the configuration
109
        app()->config['auth.passwords'] = app()->config['auth.passwords'] +
110
        [
111
            'backpack' => [
112
                'provider'  => 'backpack',
113
                'table'     => 'password_resets',
114
                'expire'    => 60,
115
            ],
116
        ];
117
118
        // add the backpack_users guard to the configuration
119
        app()->config['auth.guards'] = app()->config['auth.guards'] +
120
        [
121
            'backpack' => [
122
                'driver'   => 'session',
123
                'provider' => 'backpack',
124
            ],
125
        ];
126
    }
127
128
    /**
129
     * Define the routes for the application.
130
     *
131
     * @param \Illuminate\Routing\Router $router
132
     *
133
     * @return void
134
     */
135
    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...
136
    {
137
        // by default, use the routes file provided in vendor
138
        $routeFilePathInUse = __DIR__.$this->routeFilePath;
139
140
        // but if there's a file with the same name in routes/backpack, use that one
141
        if (file_exists(base_path().$this->routeFilePath)) {
142
            $routeFilePathInUse = base_path().$this->routeFilePath;
143
        }
144
145
        $this->loadRoutesFrom($routeFilePathInUse);
146
    }
147
148
    /**
149
     * Load custom routes file.
150
     *
151
     * @param \Illuminate\Routing\Router $router
152
     *
153
     * @return void
154
     */
155
    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...
156
    {
157
        // if the custom routes file is published, register its routes
158
        if (file_exists(base_path().$this->customRoutesFilePath)) {
159
            $this->loadRoutesFrom(base_path().$this->customRoutesFilePath);
160
        }
161
    }
162
163
    /**
164
     * Register any package services.
165
     *
166
     * @return void
167
     */
168
    public function register()
169
    {
170
        // register the current package
171
        $this->app->bind('base', function ($app) {
172
            return new Base($app);
173
        });
174
175
        // register the helper functions
176
        $this->loadHelpers();
177
178
        // register its dependencies
179
        $this->app->register(\Jenssegers\Date\DateServiceProvider::class);
180
        $this->app->register(\Prologue\Alerts\AlertsServiceProvider::class);
181
        $this->app->register(\Creativeorange\Gravatar\GravatarServiceProvider::class);
182
183
        // register their aliases
184
        $loader = \Illuminate\Foundation\AliasLoader::getInstance();
185
        $loader->alias('Alert', \Prologue\Alerts\Facades\Alert::class);
186
        $loader->alias('Date', \Jenssegers\Date\Date::class);
187
        $loader->alias('Gravatar', \Creativeorange\Gravatar\Facades\Gravatar::class);
188
189
        // register the services that are only used for development
190
        if ($this->app->environment() == 'local') {
191
            if (class_exists('Laracasts\Generators\GeneratorsServiceProvider')) {
192
                $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
193
            }
194
            if (class_exists('Backpack\Generators\GeneratorsServiceProvider')) {
195
                $this->app->register('Backpack\Generators\GeneratorsServiceProvider');
196
            }
197
        }
198
199
        // register the artisan commands
200
        $this->commands($this->commands);
201
    }
202
203
    public function registerMiddlewareGroup(Router $router)
204
    {
205
        $middleware_key = config('backpack.base.middleware_key');
206
        $middleware_class = config('backpack.base.middleware_class');
207
208
        if (!is_array($middleware_class)) {
209
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
210
211
            return;
212
        }
213
214
        foreach ($middleware_class as $middleware_class) {
215
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
216
        }
217
    }
218
219
    public function publishFiles()
220
    {
221
        $error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')];
222
        $backpack_base_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack/base')];
223
        $backpack_public_assets = [__DIR__.'/public' => public_path('vendor/backpack')];
224
        $backpack_lang_files = [__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')];
225
        $backpack_config_files = [__DIR__.'/config' => config_path()];
226
227
        // sidebar_content view, which is the only view most people need to overwrite
228
        $backpack_menu_contents_view = [
229
            __DIR__.'/resources/views/inc/sidebar_content.blade.php'      => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php'),
230
            __DIR__.'/resources/views/inc/topbar_left_content.blade.php'  => resource_path('views/vendor/backpack/base/inc/topbar_left_content.blade.php'),
231
            __DIR__.'/resources/views/inc/topbar_right_content.blade.php' => resource_path('views/vendor/backpack/base/inc/topbar_right_content.blade.php'),
232
        ];
233
        $backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)];
234
235
        // calculate the path from current directory to get the vendor path
236
        $vendorPath = dirname(__DIR__, 3);
237
        $adminlte_assets = [$vendorPath.'/almasaeed2010/adminlte' => public_path('vendor/adminlte')];
238
        $gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()];
239
240
        // 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
241
        $minimum = array_merge(
242
            $error_views,
243
            // $backpack_base_views,
244
            $backpack_public_assets,
245
            // $backpack_lang_files,
246
            $backpack_config_files,
247
            $backpack_menu_contents_view,
248
            $backpack_custom_routes_file,
249
            $adminlte_assets,
250
            $gravatar_assets
251
        );
252
253
        // register all possible publish commands and assign tags to each
254
        $this->publishes($backpack_config_files, 'config');
255
        $this->publishes($backpack_lang_files, 'lang');
256
        $this->publishes($backpack_base_views, 'views');
257
        $this->publishes($backpack_menu_contents_view, 'menu_contents');
258
        $this->publishes($error_views, 'errors');
259
        $this->publishes($backpack_public_assets, 'public');
260
        $this->publishes($backpack_custom_routes_file, 'custom_routes');
261
        $this->publishes($adminlte_assets, 'adminlte');
262
        $this->publishes($gravatar_assets, 'gravatar');
263
        $this->publishes($minimum, 'minimum');
264
    }
265
266
    /**
267
     * Check to to see if a license code exists.
268
     * If it does not, throw a notification bubble.
269
     *
270
     * @return void
271
     */
272
    private function checkLicenseCodeExists()
273
    {
274
        if ($this->app->environment() != 'local' && !config('backpack.base.license_code')) {
275
            \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.");
276
        }
277
    }
278
}
279