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
Pull Request — master (#3491)
by
unknown
11:28
created

BackpackServiceProvider::maybeApplySkin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

39
    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...
40
    {
41
        $this->loadViewsWithFallbacks();
42
        $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack');
43
        $this->loadConfigs();
44
        $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?
Loading history...
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...
45
        $this->setupRoutes($this->app->router);
46
        $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...
47
        $this->publishFiles();
48
        $this->checkLicenseCodeExists();
49
        $this->sendUsageStats();
50
        $this->maybeApplySkin();
51
    }
52
53
    /**
54
     * Register any package services.
55
     *
56
     * @return void
57
     */
58
    public function register()
59
    {
60
        // Bind the CrudPanel object to Laravel's service container
61
        $this->app->singleton('crud', function ($app) {
62
            return new CrudPanel($app);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...rudPanel::__construct() has too many arguments starting with $app. ( Ignorable by Annotation )

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

62
            return /** @scrutinizer ignore-call */ new CrudPanel($app);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
63
        });
64
65
        // Bind the widgets collection object to Laravel's service container
66
        $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

66
        $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...
67
            return new Collection();
68
        });
69
70
        // load a macro for Route,
71
        // helps developers load all routes for a CRUD resource in one line
72
        if (! Route::hasMacro('crud')) {
73
            $this->addRouteMacro();
74
        }
75
76
        // register the helper functions
77
        $this->loadHelpers();
78
79
        // register the artisan commands
80
        $this->commands($this->commands);
81
    }
82
83
    public function registerMiddlewareGroup(Router $router)
84
    {
85
        $middleware_key = config('backpack.base.middleware_key');
86
        $middleware_class = config('backpack.base.middleware_class');
87
88
        if (! is_array($middleware_class)) {
89
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
90
91
            return;
92
        }
93
94
        foreach ($middleware_class as $middleware_class) {
95
            $router->pushMiddlewareToGroup($middleware_key, $middleware_class);
96
        }
97
    }
98
99
    public function publishFiles()
100
    {
101
        $error_views = [__DIR__.'/resources/error_views' => resource_path('views/errors')];
102
        $backpack_views = [__DIR__.'/resources/views' => resource_path('views/vendor/backpack')];
103
        $backpack_public_assets = [__DIR__.'/public' => public_path()];
104
        $backpack_lang_files = [__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')];
105
        $backpack_config_files = [__DIR__.'/config' => config_path()];
106
107
        // sidebar content views, which are the only views most people need to overwrite
108
        $backpack_menu_contents_view = [
109
            __DIR__.'/resources/views/base/inc/sidebar_content.blade.php'      => resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php'),
110
            __DIR__.'/resources/views/base/inc/topbar_left_content.blade.php'  => resource_path('views/vendor/backpack/base/inc/topbar_left_content.blade.php'),
111
            __DIR__.'/resources/views/base/inc/topbar_right_content.blade.php' => resource_path('views/vendor/backpack/base/inc/topbar_right_content.blade.php'),
112
        ];
113
        $backpack_custom_routes_file = [__DIR__.$this->customRoutesFilePath => base_path($this->customRoutesFilePath)];
114
115
        // calculate the path from current directory to get the vendor path
116
        $vendorPath = dirname(__DIR__, 3);
117
        $gravatar_assets = [$vendorPath.'/creativeorange/gravatar/config' => config_path()];
118
119
        // 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
120
        $minimum = array_merge(
121
            // $backpack_views,
122
            // $backpack_lang_files,
123
            $error_views,
124
            $backpack_public_assets,
125
            $backpack_config_files,
126
            $backpack_menu_contents_view,
127
            $backpack_custom_routes_file,
128
            $gravatar_assets
129
        );
130
131
        // register all possible publish commands and assign tags to each
132
        $this->publishes($backpack_config_files, 'config');
133
        $this->publishes($backpack_lang_files, 'lang');
134
        $this->publishes($backpack_views, 'views');
135
        $this->publishes($backpack_menu_contents_view, 'menu_contents');
136
        $this->publishes($error_views, 'errors');
137
        $this->publishes($backpack_public_assets, 'public');
138
        $this->publishes($backpack_custom_routes_file, 'custom_routes');
139
        $this->publishes($gravatar_assets, 'gravatar');
140
        $this->publishes($minimum, 'minimum');
141
    }
142
143
    /**
144
     * Define the routes for the application.
145
     *
146
     * @param \Illuminate\Routing\Router $router
147
     *
148
     * @return void
149
     */
150
    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

150
    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...
151
    {
152
        // by default, use the routes file provided in vendor
153
        $routeFilePathInUse = __DIR__.$this->routeFilePath;
154
155
        // but if there's a file with the same name in routes/backpack, use that one
156
        if (file_exists(base_path().$this->routeFilePath)) {
157
            $routeFilePathInUse = base_path().$this->routeFilePath;
158
        }
159
160
        $this->loadRoutesFrom($routeFilePathInUse);
161
    }
162
163
    /**
164
     * Load custom routes file.
165
     *
166
     * @param \Illuminate\Routing\Router $router
167
     *
168
     * @return void
169
     */
170
    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

170
    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...
171
    {
172
        // if the custom routes file is published, register its routes
173
        if (file_exists(base_path().$this->customRoutesFilePath)) {
174
            $this->loadRoutesFrom(base_path().$this->customRoutesFilePath);
175
        }
176
    }
177
178
    /**
179
     * The route macro allows developers to generate the routes for a CrudController,
180
     * for all operations, using a simple syntax: Route::crud().
181
     *
182
     * It will go to the given CrudController and get the setupRoutes() method on it.
183
     */
184
    private function addRouteMacro()
185
    {
186
        Route::macro('crud', function ($name, $controller) {
187
            // put together the route name prefix,
188
            // as passed to the Route::group() statements
189
            $routeName = '';
190
            if ($this->hasGroupStack()) {
0 ignored issues
show
Bug introduced by
The method hasGroupStack() does not exist on Backpack\CRUD\BackpackServiceProvider. ( Ignorable by Annotation )

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

190
            if ($this->/** @scrutinizer ignore-call */ hasGroupStack()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
                foreach ($this->getGroupStack() as $key => $groupStack) {
0 ignored issues
show
Bug introduced by
The method getGroupStack() does not exist on Backpack\CRUD\BackpackServiceProvider. ( Ignorable by Annotation )

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

191
                foreach ($this->/** @scrutinizer ignore-call */ getGroupStack() as $key => $groupStack) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
192
                    if (isset($groupStack['name'])) {
193
                        if (is_array($groupStack['name'])) {
194
                            $routeName = implode('', $groupStack['name']);
195
                        } else {
196
                            $routeName = $groupStack['name'];
197
                        }
198
                    }
199
                }
200
            }
201
            // add the name of the current entity to the route name prefix
202
            // the result will be the current route name (not ending in dot)
203
            $routeName .= $name;
204
205
            // get an instance of the controller
206
            if ($this->hasGroupStack()) {
207
                $groupStack = $this->getGroupStack();
208
                $groupNamespace = $groupStack && isset(end($groupStack)['namespace']) ? end($groupStack)['namespace'].'\\' : '';
209
            } else {
210
                $groupNamespace = '';
211
            }
212
            $namespacedController = $groupNamespace.$controller;
213
            $controllerInstance = App::make($namespacedController);
214
215
            return $controllerInstance->setupRoutes($name, $routeName, $controller);
216
        });
217
    }
218
219
    public function loadViewsWithFallbacks()
220
    {
221
        $customBaseFolder = resource_path('views/vendor/backpack/base');
222
        $customCrudFolder = resource_path('views/vendor/backpack/crud');
223
224
        // - first the published/overwritten views (in case they have any changes)
225
        if (file_exists($customBaseFolder)) {
226
            $this->loadViewsFrom($customBaseFolder, 'backpack');
227
        }
228
        if (file_exists($customCrudFolder)) {
229
            $this->loadViewsFrom($customCrudFolder, 'crud');
230
        }
231
        // - then the stock views that come with the package, in case a published view might be missing
232
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views/base'), 'backpack');
233
        $this->loadViewsFrom(realpath(__DIR__.'/resources/views/crud'), 'crud');
234
    }
235
236
    public function loadConfigs()
237
    {
238
        // use the vendor configuration file as fallback
239
        $this->mergeConfigFrom(__DIR__.'/config/backpack/crud.php', 'backpack.crud');
240
        $this->mergeConfigFrom(__DIR__.'/config/backpack/base.php', 'backpack.base');
241
242
        // add the root disk to filesystem configuration
243
        app()->config['filesystems.disks.'.config('backpack.base.root_disk_name')] = [
244
            'driver' => 'local',
245
            'root'   => base_path(),
246
        ];
247
248
        /*
249
         * Backpack login differs from the standard Laravel login.
250
         * As such, Backpack uses its own authentication provider, password broker and guard.
251
         *
252
         * THe process below adds those configuration values on top of whatever is in config/auth.php.
253
         * Developers can overwrite the backpack provider, password broker or guard by adding a
254
         * provider/broker/guard with the "backpack" name inside their config/auth.php file.
255
         * Or they can use another provider/broker/guard entirely, by changing the corresponding
256
         * value inside config/backpack/base.php
257
         */
258
259
        // add the backpack_users authentication provider to the configuration
260
        app()->config['auth.providers'] = app()->config['auth.providers'] +
261
        [
262
            'backpack' => [
263
                'driver'  => 'eloquent',
264
                'model'   => config('backpack.base.user_model_fqn'),
265
            ],
266
        ];
267
268
        // add the backpack_users password broker to the configuration
269
        app()->config['auth.passwords'] = app()->config['auth.passwords'] +
270
        [
271
            'backpack' => [
272
                'provider'  => 'backpack',
273
                'table'     => 'password_resets',
274
                'expire'    => 60,
275
            ],
276
        ];
277
278
        // add the backpack_users guard to the configuration
279
        app()->config['auth.guards'] = app()->config['auth.guards'] +
280
        [
281
            'backpack' => [
282
                'driver'   => 'session',
283
                'provider' => 'backpack',
284
            ],
285
        ];
286
    }
287
288
    /**
289
     * Load the Backpack helper methods, for convenience.
290
     */
291
    public function loadHelpers()
292
    {
293
        require_once __DIR__.'/helpers.php';
294
    }
295
    /**
296
     * Listens to Illuminate\Routing\Events\RouteMatched event and loads any skin into the config.
297
     * @return void
298
     */
299
    public function maybeApplySkin()
300
    {
301
        if($skin = config('backpack.base.skin')){
302
            backpack_apply_skin($skin);
303
        }
304
    }
305
306
    /**
307
     * Get the services provided by the provider.
308
     *
309
     * @return array
310
     */
311
    public function provides()
312
    {
313
        return ['crud', 'widgets'];
314
    }
315
}
316