Completed
Pull Request — master (#324)
by Cristian
08:34
created

addCustomAuthConfigurationValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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