Passed
Pull Request — master (#1022)
by Diego
03:15
created

AdminLteServiceProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Test Coverage

Coverage 98.41%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 84
c 1
b 0
f 0
dl 0
loc 241
ccs 62
cts 63
cp 0.9841
rs 10
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMenu() 0 11 2
A registerCommands() 0 7 1
A registerViewComposers() 0 3 1
A loadRoutes() 0 11 1
A boot() 0 10 1
A packagePath() 0 3 1
A register() 0 10 1
A loadTranslations() 0 4 1
A loadConfig() 0 4 1
A loadViews() 0 4 1
A loadComponents() 0 24 2
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Illuminate\Contracts\View\Factory;
9
use Illuminate\Support\Facades\Route;
10
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
11
use JeroenNoten\LaravelAdminLte\Console\AdminLteInstallCommand;
12
use JeroenNoten\LaravelAdminLte\Console\AdminLtePluginCommand;
13
use JeroenNoten\LaravelAdminLte\Console\AdminLteStatusCommand;
14
use JeroenNoten\LaravelAdminLte\Console\AdminLteUpdateCommand;
15
use JeroenNoten\LaravelAdminLte\Events\BuildingMenu;
16
use JeroenNoten\LaravelAdminLte\Http\ViewComposers\AdminLteComposer;
17
use JeroenNoten\LaravelAdminLte\View\Components\Form;
18
use JeroenNoten\LaravelAdminLte\View\Components\Layout;
19
use JeroenNoten\LaravelAdminLte\View\Components\Tool;
20
use JeroenNoten\LaravelAdminLte\View\Components\Widget;
21
22
class AdminLteServiceProvider extends BaseServiceProvider
23
{
24
    /**
25
     * The prefix to use for register/load the package resources.
26
     *
27
     * @var string
28
     */
29
    protected $pkgPrefix = 'adminlte';
30
31
    /**
32
     * Array with the available layout components.
33
     *
34
     * @var array
35
     */
36
    protected $layoutComponents = [
37
        'navbar-darkmode-widget' => Layout\NavbarDarkmodeWidget::class,
38
        'navbar-notification' => Layout\NavbarNotification::class,
39
    ];
40
41
    /**
42
     * Array with the available form components.
43
     *
44
     * @var array
45
     */
46
    protected $formComponents = [
47
        'button' => Form\Button::class,
48
        'date-range' => Form\DateRange::class,
49
        'input' => Form\Input::class,
50
        'input-color' => Form\InputColor::class,
51
        'input-date' => Form\InputDate::class,
52
        'input-file' => Form\InputFile::class,
53
        'input-file-bs' => Form\InputFileBs::class,
0 ignored issues
show
Bug introduced by
The type JeroenNoten\LaravelAdmin...onents\Form\InputFileBs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
        'input-slider' => Form\InputSlider::class,
55
        'input-switch' => Form\InputSwitch::class,
56
        'options' => Form\Options::class,
57
        'select' => Form\Select::class,
58
        'select2' => Form\Select2::class,
59
        'select-bs' => Form\SelectBs::class,
60
        'textarea' => Form\Textarea::class,
61
        'text-editor' => Form\TextEditor::class,
62
    ];
63
64
    /**
65
     * Array with the available tool components.
66
     *
67
     * @var array
68
     */
69
    protected $toolComponents = [
70
        'datatable' => Tool\Datatable::class,
71
        'modal' => Tool\Modal::class,
72
    ];
73
74
    /**
75
     * Array with the available widget components.
76
     *
77
     * @var array
78
     */
79
    protected $widgetComponents = [
80
        'alert' => Widget\Alert::class,
81
        'callout' => Widget\Callout::class,
82
        'card' => Widget\Card::class,
83
        'info-box' => Widget\InfoBox::class,
84
        'profile-col-item' => Widget\ProfileColItem::class,
85
        'profile-row-item' => Widget\ProfileRowItem::class,
86
        'profile-widget' => Widget\ProfileWidget::class,
87
        'progress' => Widget\Progress::class,
88
        'small-box' => Widget\SmallBox::class,
89
    ];
90
91
    /**
92
     * Register the package services.
93
     *
94
     * @return void
95
     */
96 172
    public function register()
97
    {
98
        // Bind a singleton instance of the AdminLte class into the service
99
        // container.
100
101
        $this->app->singleton(AdminLte::class, function (Container $app) {
102 3
            return new AdminLte(
103 3
                $app['config']['adminlte.filters'],
104 3
                $app['events'],
105
                $app
106
            );
107 172
        });
108 172
    }
109
110
    /**
111
     * Bootstrap the package's services.
112
     *
113
     * @return void
114
     */
115 172
    public function boot(Factory $view, Dispatcher $events, Repository $config)
116
    {
117 172
        $this->loadViews();
118 172
        $this->loadTranslations();
119 172
        $this->loadConfig();
120 172
        $this->registerCommands();
121 172
        $this->registerViewComposers($view);
122 172
        $this->registerMenu($events, $config);
123 172
        $this->loadComponents();
124 172
        $this->loadRoutes();
125 172
    }
126
127
    /**
128
     * Load the package views.
129
     *
130
     * @return void
131
     */
132 172
    private function loadViews()
133
    {
134 172
        $viewsPath = $this->packagePath('resources/views');
135 172
        $this->loadViewsFrom($viewsPath, $this->pkgPrefix);
136 172
    }
137
138
    /**
139
     * Load the package translations.
140
     *
141
     * @return void
142
     */
143 172
    private function loadTranslations()
144
    {
145 172
        $translationsPath = $this->packagePath('resources/lang');
146 172
        $this->loadTranslationsFrom($translationsPath, $this->pkgPrefix);
147 172
    }
148
149
    /**
150
     * Load the package config.
151
     *
152
     * @return void
153
     */
154 172
    private function loadConfig()
155
    {
156 172
        $configPath = $this->packagePath('config/adminlte.php');
157 172
        $this->mergeConfigFrom($configPath, $this->pkgPrefix);
158 172
    }
159
160
    /**
161
     * Get the absolute path to some package resource.
162
     *
163
     * @param  string  $path  The relative path to the resource
164
     * @return string
165
     */
166 172
    private function packagePath($path)
167
    {
168 172
        return __DIR__."/../$path";
169
    }
170
171
    /**
172
     * Register the package's artisan commands.
173
     *
174
     * @return void
175
     */
176 172
    private function registerCommands()
177
    {
178 172
        $this->commands([
179 172
            AdminLteInstallCommand::class,
180
            AdminLteStatusCommand::class,
181
            AdminLteUpdateCommand::class,
182
            AdminLtePluginCommand::class,
183
        ]);
184 172
    }
185
186
    /**
187
     * Register the package's view composers.
188
     *
189
     * @return void
190
     */
191 172
    private function registerViewComposers(Factory $view)
192
    {
193 172
        $view->composer('adminlte::page', AdminLteComposer::class);
194 172
    }
195
196
    /**
197
     * Register the menu events handlers.
198
     *
199
     * @return void
200
     */
201 172
    private static function registerMenu(Dispatcher $events, Repository $config)
202
    {
203
        // Register a handler for the BuildingMenu event, this handler will add
204
        // the menu defined on the config file to the menu builder instance.
205
206 172
        $events->listen(
207 172
            BuildingMenu::class,
208
            function (BuildingMenu $event) use ($config) {
209 1
                $menu = $config->get('adminlte.menu', []);
210 1
                $menu = is_array($menu) ? $menu : [];
211 1
                $event->menu->add(...$menu);
212 172
            }
213
        );
214 172
    }
215
216
    /**
217
     * Load the blade view components.
218
     *
219
     * @return void
220
     */
221 172
    private function loadComponents()
222
    {
223
        // Support of x-components is only available for Laravel >= 7.x
224
        // versions. So, we check if we can load components.
225
226 172
        $canLoadComponents = method_exists(
227 172
            'Illuminate\Support\ServiceProvider',
228 172
            'loadViewComponentsAs'
229
        );
230
231 172
        if (! $canLoadComponents) {
232
            return;
233
        }
234
235
        // Load all the blade-x components.
236
237 172
        $components = array_merge(
238 172
            $this->layoutComponents,
239 172
            $this->formComponents,
240 172
            $this->toolComponents,
241 172
            $this->widgetComponents
242
        );
243
244 172
        $this->loadViewComponentsAs($this->pkgPrefix, $components);
245 172
    }
246
247
    /**
248
     * Load the package web routes.
249
     *
250
     * @return void
251
     */
252 172
    private function loadRoutes()
253
    {
254
        $routesCfg = [
255 172
            'as' => "{$this->pkgPrefix}.",
256 172
            'prefix' => $this->pkgPrefix,
257
            'middleware' => ['web'],
258
        ];
259
260
        Route::group($routesCfg, function () {
261 172
            $routesPath = $this->packagePath('routes/web.php');
262 172
            $this->loadRoutesFrom($routesPath);
263 172
        });
264 172
    }
265
}
266