Completed
Pull Request — master (#223)
by
unknown
01:48
created

BaseServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 10
c 6
b 2
f 0
lcom 1
cbo 4
dl 0
loc 143
rs 10

6 Methods

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