Completed
Pull Request — master (#252)
by Cristian
04:03 queued 57s
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
    ];
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
        // LOAD THE VIEWS
38
        // - first the published views (in case they have any changes)
39
        $this->loadViewsFrom(resource_path('views/vendor/backpack/base'), 'backpack');
40
        // - then the stock views that come with the package, in case a published view might be missing
41
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views'), 'backpack');
42
43
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack');
44
45
        // use the vendor configuration file as fallback
46
        $this->mergeConfigFrom(
47
            __DIR__.'/config/backpack/base.php', 'backpack.base'
48
        );
49
50
        // add the root disk to filesystem configuration
51
        app()->config['filesystems.disks.root'] = [
52
            'driver' => 'local',
53
            'root'   => base_path(),
54
        ];
55
56
        $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...
57
        $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...
58
        $this->publishFiles();
59
        $this->loadHelpers();
60
    }
61
62
    /**
63
     * Load the Backpack helper methods, for convenience.
64
     */
65
    public function loadHelpers()
66
    {
67
        require_once __DIR__.'/helpers.php';
68
    }
69
70
    /**
71
     * Define the routes for the application.
72
     *
73
     * @param \Illuminate\Routing\Router $router
74
     *
75
     * @return void
76
     */
77
    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...
78
    {
79
        // by default, use the routes file provided in vendor
80
        $routeFilePathInUse = __DIR__.$this->routeFilePath;
81
82
        // but if there's a file with the same name in routes/backpack, use that one
83
        if (file_exists(base_path().$this->routeFilePath)) {
84
            $routeFilePathInUse = base_path().$this->routeFilePath;
85
        }
86
87
        $this->loadRoutesFrom($routeFilePathInUse);
88
    }
89
90
    /**
91
     * Register any package services.
92
     *
93
     * @return void
94
     */
95
    public function register()
96
    {
97
        // register the current package
98
        $this->app->bind('base', function ($app) {
99
            return new Base($app);
100
        });
101
102
        // register its dependencies
103
        $this->app->register(\Jenssegers\Date\DateServiceProvider::class);
104
        $this->app->register(\Prologue\Alerts\AlertsServiceProvider::class);
105
        $this->app->register(\Creativeorange\Gravatar\GravatarServiceProvider::class);
106
107
        // register their aliases
108
        $loader = \Illuminate\Foundation\AliasLoader::getInstance();
109
        $loader->alias('Alert', \Prologue\Alerts\Facades\Alert::class);
110
        $loader->alias('Date', \Jenssegers\Date\Date::class);
111
        $loader->alias('Gravatar', \Creativeorange\Gravatar\Facades\Gravatar::class);
112
113
        // register the services that are only used for development
114
        if ($this->app->environment() == 'local') {
115
            if (class_exists('Laracasts\Generators\GeneratorsServiceProvider')) {
116
                $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
117
            }
118
            if (class_exists('Backpack\Generators\GeneratorsServiceProvider')) {
119
                $this->app->register('Backpack\Generators\GeneratorsServiceProvider');
120
            }
121
        }
122
123
        // register the artisan commands
124
        $this->commands($this->commands);
125
    }
126
127
    public function registerAdminMiddleware(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...
128
    {
129
        Route::aliasMiddleware('admin', \Backpack\Base\app\Http\Middleware\Admin::class);
130
    }
131
132
    public function publishFiles()
133
    {
134
        $error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')];
135
        $backpack_base_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack/base')];
136
        $backpack_public_assets = [__DIR__.'/public' => public_path('vendor/backpack')];
137
        $backpack_lang_files = [__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')];
138
        $backpack_config_files = [__DIR__.'/config' => config_path()];
139
140
        // sidebar_content view, which is the only view most people need to overwrite
141
        $backpack_sidebar_contents_view = [__DIR__.'/resources/views/inc/sidebar_content.blade.php' => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php')];
142
143
        // calculate the path from current directory to get the vendor path
144
        $vendorPath = dirname(__DIR__, 3);
145
        $adminlte_assets = [$vendorPath.'/almasaeed2010/adminlte' => public_path('vendor/adminlte')];
146
        $gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()];
147
148
        // 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
149
        $minimum = array_merge(
150
            $error_views,
151
            // $backpack_base_views,
152
            $backpack_public_assets,
153
            // $backpack_lang_files,
154
            $backpack_config_files,
155
            $backpack_sidebar_contents_view,
156
            $adminlte_assets,
157
            $gravatar_assets
158
        );
159
160
        // register all possible publish commands and assign tags to each
161
        $this->publishes($backpack_config_files, 'config');
162
        $this->publishes($backpack_lang_files, 'lang');
163
        $this->publishes($backpack_base_views, 'views');
164
        $this->publishes($backpack_sidebar_contents_view, 'sidebar_content');
165
        $this->publishes($error_views, 'errors');
166
        $this->publishes($backpack_public_assets, 'public');
167
        $this->publishes($adminlte_assets, 'adminlte');
168
        $this->publishes($gravatar_assets, 'gravatar');
169
        $this->publishes($minimum, 'minimum');
170
    }
171
}
172