Completed
Pull Request — master (#294)
by
unknown
02:21
created

BaseServiceProvider::loadHelpers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
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.'.config('backpack.base.root_disk_name')] = [
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->checkLicenseCodeExists();
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 the helper functions
127
        $this->loadHelpers();
128
129
        // register its dependencies
130
        $this->app->register(\Jenssegers\Date\DateServiceProvider::class);
131
        $this->app->register(\Prologue\Alerts\AlertsServiceProvider::class);
132
        $this->app->register(\Creativeorange\Gravatar\GravatarServiceProvider::class);
133
134
        // register their aliases
135
        $loader = \Illuminate\Foundation\AliasLoader::getInstance();
136
        $loader->alias('Alert', \Prologue\Alerts\Facades\Alert::class);
137
        $loader->alias('Date', \Jenssegers\Date\Date::class);
138
        $loader->alias('Gravatar', \Creativeorange\Gravatar\Facades\Gravatar::class);
139
140
        // register the services that are only used for development
141
        if ($this->app->environment() == 'local') {
142
            if (class_exists('Laracasts\Generators\GeneratorsServiceProvider')) {
143
                $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
144
            }
145
            if (class_exists('Backpack\Generators\GeneratorsServiceProvider')) {
146
                $this->app->register('Backpack\Generators\GeneratorsServiceProvider');
147
            }
148
        }
149
150
        // register the artisan commands
151
        $this->commands($this->commands);
152
    }
153
154
    public function registerMiddlewareGroup(Router $router)
155
    {
156
        $middleware_key = config('backpack.base.middleware_key');
157
        $middleware_class = config('backpack.base.middleware_class');
158
159
        if (!is_array($middleware_class)) {
160
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
161
162
            return;
163
        }
164
165
        foreach ($middleware_class as $middleware_class) {
166
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
167
        }
168
    }
169
170
    public function publishFiles()
171
    {
172
        $error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')];
173
        $backpack_base_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack/base')];
174
        $backpack_public_assets = [__DIR__.'/public' => public_path('vendor/backpack')];
175
        $backpack_lang_files = [__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')];
176
        $backpack_config_files = [__DIR__.'/config' => config_path()];
177
178
        // sidebar_content view, which is the only view most people need to overwrite
179
        $backpack_sidebar_contents_view = [__DIR__.'/resources/views/inc/sidebar_content.blade.php' => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php')];
180
        $backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)];
181
182
        // calculate the path from current directory to get the vendor path
183
        $vendorPath = dirname(__DIR__, 3);
184
        $adminlte_assets = [$vendorPath.'/almasaeed2010/adminlte' => public_path('vendor/adminlte')];
185
        $gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()];
186
187
        // 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
188
        $minimum = array_merge(
189
            $error_views,
190
            // $backpack_base_views,
191
            $backpack_public_assets,
192
            // $backpack_lang_files,
193
            $backpack_config_files,
194
            $backpack_sidebar_contents_view,
195
            $backpack_custom_routes_file,
196
            $adminlte_assets,
197
            $gravatar_assets
198
        );
199
200
        // register all possible publish commands and assign tags to each
201
        $this->publishes($backpack_config_files, 'config');
202
        $this->publishes($backpack_lang_files, 'lang');
203
        $this->publishes($backpack_base_views, 'views');
204
        $this->publishes($backpack_sidebar_contents_view, 'sidebar_content');
205
        $this->publishes($error_views, 'errors');
206
        $this->publishes($backpack_public_assets, 'public');
207
        $this->publishes($backpack_custom_routes_file, 'custom_routes');
208
        $this->publishes($adminlte_assets, 'adminlte');
209
        $this->publishes($gravatar_assets, 'gravatar');
210
        $this->publishes($minimum, 'minimum');
211
    }
212
213
    /**
214
     * Check to to see if a license code exists.
215
     * If it does not, throw a notification bubble.
216
     *
217
     * @return void
218
     */
219
    private function checkLicenseCodeExists()
220
    {
221
        if ($this->app->environment() != 'local' && !config('backpack.base.license_code')) {
222
            \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.");
223
        }
224
    }
225
}
226