Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — bassets-removed-bundle-themes ( 54124c )
by
unknown
14:25
created

mergeConfigsFromDirectory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD;
4
5
use Backpack\CRUD\app\Http\Middleware\ThrottlePasswordRecovery;
6
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
7
use Backpack\CRUD\app\Library\Database\DatabaseSchema;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\ServiceProvider;
11
12
class BackpackServiceProvider extends ServiceProvider
13
{
14
    use Stats;
15
16
    protected $commands = [
17
        \Backpack\CRUD\app\Console\Commands\Install::class,
18
        \Backpack\CRUD\app\Console\Commands\AddSidebarContent::class,
19
        \Backpack\CRUD\app\Console\Commands\AddCustomRouteContent::class,
20
        \Backpack\CRUD\app\Console\Commands\Version::class,
21
        \Backpack\CRUD\app\Console\Commands\CreateUser::class,
22
        \Backpack\CRUD\app\Console\Commands\PublishBackpackMiddleware::class,
23
        \Backpack\CRUD\app\Console\Commands\PublishView::class,
24
        \Backpack\CRUD\app\Console\Commands\Addons\RequireDevTools::class,
25
        \Backpack\CRUD\app\Console\Commands\Addons\RequireEditableColumns::class,
26
        \Backpack\CRUD\app\Console\Commands\Addons\RequirePro::class,
27
        \Backpack\CRUD\app\Console\Commands\Fix::class,
28
    ];
29
30
    // Indicates if loading of the provider is deferred.
31
    protected $defer = false;
32
    // Where the route file lives, both inside the package and in the app (if overwritten).
33
    public $routeFilePath = '/routes/backpack/base.php';
34
    // Where custom routes can be written, and will be registered by Backpack.
35
    public $customRoutesFilePath = '/routes/backpack/custom.php';
36
37
    /**
38
     * Perform post-registration booting of services.
39
     *
40
     * @return void
41
     */
42
    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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
    public function boot(/** @scrutinizer ignore-unused */ \Illuminate\Routing\Router$router)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        $this->loadViewsWithFallbacks();
45
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack');
46
        $this->loadConfigs();
47
        $this->registerMiddlewareGroup($this->app->router);
0 ignored issues
show
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Fou...ion\CachesConfiguration suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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?
Loading history...
48
        $this->setupRoutes($this->app->router);
49
        $this->setupCustomRoutes($this->app->router);
0 ignored issues
show
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Foundation\CachesRoutes suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
50
        $this->publishFiles();
51
        $this->sendUsageStats();
52
    }
53
54
    /**
55
     * Register any package services.
56
     *
57
     * @return void
58
     */
59
    public function register()
60
    {
61
        // load the macros
62
        include_once __DIR__.'/macros.php';
63
64
        // Bind the CrudPanel object to Laravel's service container
65
        $this->app->scoped('crud', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

65
        $this->app->scoped('crud', function (/** @scrutinizer ignore-unused */ $app) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
            return new CrudPanel();
67
        });
68
69
        $this->app->scoped('DatabaseSchema', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

69
        $this->app->scoped('DatabaseSchema', function (/** @scrutinizer ignore-unused */ $app) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
            return new DatabaseSchema();
71
        });
72
73
        $this->app->singleton('BackpackViewNamespaces', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
        $this->app->singleton('BackpackViewNamespaces', function (/** @scrutinizer ignore-unused */ $app) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
            return new ViewNamespaces();
75
        });
76
77
        // Bind the widgets collection object to Laravel's service container
78
        $this->app->singleton('widgets', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

78
        $this->app->singleton('widgets', function (/** @scrutinizer ignore-unused */ $app) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
            return new Collection();
80
        });
81
82
        // register the helper functions
83
        $this->loadHelpers();
84
85
        // register the artisan commands
86
        $this->commands($this->commands);
87
    }
88
89
    public function registerMiddlewareGroup(Router $router)
90
    {
91
        $middleware_key = config('backpack.base.middleware_key');
92
        $middleware_class = config('backpack.base.middleware_class');
93
94
        if (! is_array($middleware_class)) {
95
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
96
97
            return;
98
        }
99
100
        foreach ($middleware_class as $middleware_class) {
101
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
102
        }
103
104
        // register internal backpack middleware for throttling the password recovery functionality
105
        // but only if functionality is enabled by developer in config
106
        if (config('backpack.base.setup_password_recovery_routes')) {
107
            $router->aliasMiddleware('backpack.throttle.password.recovery', ThrottlePasswordRecovery::class);
108
        }
109
    }
110
111
    public function publishFiles()
112
    {
113
        $error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')];
114
        $backpack_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack')];
115
        $backpack_public_assets = [__DIR__.'/public' => public_path()];
116
        $backpack_lang_files = [__DIR__.'/resources/lang' => app()->langPath().'/vendor/backpack'];
0 ignored issues
show
introduced by
The method langPath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        $backpack_lang_files = [__DIR__.'/resources/lang' => app()->/** @scrutinizer ignore-call */ langPath().'/vendor/backpack'];
Loading history...
117
        $backpack_config_files = [__DIR__.'/config' => config_path()];
118
119
        // sidebar content views, which are the only views most people need to overwrite
120
        $backpack_menu_contents_view = [
121
            __DIR__.'/resources/views/base/inc/sidebar_content.blade.php' => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php'),
122
            __DIR__.'/resources/views/base/inc/topbar_left_content.blade.php' => resource_path('views/vendor/backpack/base/inc/topbar_left_content.blade.php'),
123
            __DIR__.'/resources/views/base/inc/topbar_right_content.blade.php' => resource_path('views/vendor/backpack/base/inc/topbar_right_content.blade.php'),
124
        ];
125
        $backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)];
126
127
        // calculate the path from current directory to get the vendor path
128
        $vendorPath = dirname(__DIR__, 3);
129
        $gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()];
130
131
        // 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
132
        $minimum = array_merge(
133
            // $backpack_views,
134
            // $backpack_lang_files,
135
            $error_views,
136
            $backpack_public_assets,
137
            $backpack_config_files,
138
            $backpack_menu_contents_view,
139
            $backpack_custom_routes_file,
140
            $gravatar_assets
141
        );
142
143
        // register all possible publish commands and assign tags to each
144
        $this->publishes($backpack_config_files, 'config');
145
        $this->publishes($backpack_lang_files, 'lang');
146
        $this->publishes($backpack_views, 'views');
147
        $this->publishes($backpack_menu_contents_view, 'menu_contents');
148
        $this->publishes($error_views, 'errors');
149
        $this->publishes($backpack_public_assets, 'public');
150
        $this->publishes($backpack_custom_routes_file, 'custom_routes');
151
        $this->publishes($gravatar_assets, 'gravatar');
152
        $this->publishes($minimum, 'minimum');
153
    }
154
155
    /**
156
     * Define the routes for the application.
157
     *
158
     * @param  \Illuminate\Routing\Router  $router
159
     * @return void
160
     */
161
    public function setupRoutes(Router $router)
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

161
    public function setupRoutes(/** @scrutinizer ignore-unused */ Router $router)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
162
    {
163
        // by default, use the routes file provided in vendor
164
        $routeFilePathInUse = __DIR__.$this->routeFilePath;
165
166
        // but if there's a file with the same name in routes/backpack, use that one
167
        if (file_exists(base_path().$this->routeFilePath)) {
168
            $routeFilePathInUse = base_path().$this->routeFilePath;
169
        }
170
171
        $this->loadRoutesFrom($routeFilePathInUse);
172
    }
173
174
    /**
175
     * Load custom routes file.
176
     *
177
     * @param  \Illuminate\Routing\Router  $router
178
     * @return void
179
     */
180
    public function setupCustomRoutes(Router $router)
0 ignored issues
show
Unused Code introduced by
The parameter $router is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

180
    public function setupCustomRoutes(/** @scrutinizer ignore-unused */ Router $router)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
181
    {
182
        // if the custom routes file is published, register its routes
183
        if (file_exists(base_path().$this->customRoutesFilePath)) {
184
            $this->loadRoutesFrom(base_path().$this->customRoutesFilePath);
185
        }
186
    }
187
188
    public function loadViewsWithFallbacks()
189
    {
190
        $customBaseFolder = resource_path('views/vendor/backpack/base');
191
        $customCrudFolder = resource_path('views/vendor/backpack/crud');
192
193
        // - first the published/overwritten views (in case they have any changes)
194
        if (file_exists($customBaseFolder)) {
195
            $this->loadViewsFrom($customBaseFolder, 'backpack');
196
        }
197
        if (file_exists($customCrudFolder)) {
198
            $this->loadViewsFrom($customCrudFolder, 'crud');
199
        }
200
        // - then the stock views that come with the package, in case a published view might be missing
201
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views/base'), 'backpack');
202
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views/crud'), 'crud');
203
    }
204
205
    protected function mergeConfigsFromDirectory($dir)
206
    {
207
        $configs = scandir(__DIR__."/config/backpack/$dir/");
208
        $configs = array_diff($configs, ['.', '..']);
209
210
        if (! count($configs)) {
211
            return;
212
        }
213
214
        foreach ($configs as $configFile) {
215
            $this->mergeConfigFrom(
216
                __DIR__."/config/backpack/$dir/$configFile",
217
                "backpack.$dir.".substr($configFile, 0, strrpos($configFile, '.'))
218
            );
219
        }
220
    }
221
222
    public function loadConfigs()
223
    {
224
        // use the vendor configuration file as fallback
225
        $this->mergeConfigFrom(__DIR__.'/config/backpack/crud.php', 'backpack.crud');
226
        $this->mergeConfigFrom(__DIR__.'/config/backpack/base.php', 'backpack.base');
227
        $this->mergeConfigsFromDirectory('operations');
228
        $this->mergeConfigsFromDirectory('themes');
229
230
        // add the root disk to filesystem configuration
231
        app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [
232
            'driver' => 'local',
233
            'root' => base_path(),
234
        ];
235
236
        /*
237
         * Backpack login differs from the standard Laravel login.
238
         * As such, Backpack uses its own authentication provider, password broker and guard.
239
         *
240
         * THe process below adds those configuration values on top of whatever is in config/auth.php.
241
         * Developers can overwrite the backpack provider, password broker or guard by adding a
242
         * provider/broker/guard with the "backpack" name inside their config/auth.php file.
243
         * Or they can use another provider/broker/guard entirely, by changing the corresponding
244
         * value inside config/backpack/base.php
245
         */
246
247
        // add the backpack_users authentication provider to the configuration
248
        app()->config['auth.providers'] = app()->config['auth.providers'] +
249
            [
250
            'backpack' => [
251
                'driver' => 'eloquent',
252
                'model' => config('backpack.base.user_model_fqn'),
253
            ],
254
        ];
255
256
        // add the backpack_users password broker to the configuration
257
        app()->config['auth.passwords'] = app()->config['auth.passwords'] +
258
            [
259
            'backpack' => [
260
                'provider' => 'backpack',
261
                'table' => 'password_resets',
262
                'expire' => 60,
263
                'throttle' => config('backpack.base.password_recovery_throttle_notifications'),
264
            ],
265
        ];
266
267
        // add the backpack_users guard to the configuration
268
        app()->config['auth.guards'] = app()->config['auth.guards'] +
269
            [
270
            'backpack' => [
271
                'driver' => 'session',
272
                'provider' => 'backpack',
273
            ],
274
        ];
275
    }
276
277
    /**
278
     * Load the Backpack helper methods, for convenience.
279
     */
280
    public function loadHelpers()
281
    {
282
        require_once __DIR__.'/helpers.php';
283
    }
284
285
    /**
286
     * Get the services provided by the provider.
287
     *
288
     * @return array
289
     */
290
    public function provides()
291
    {
292
        return ['crud', 'widgets', 'BackpackViewNamespaces', 'DatabaseSchema'];
293
    }
294
}
295