Passed
Pull Request — master (#941)
by Diego
07:10
created

AdminLteServiceProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Test Coverage

Coverage 98.25%

Importance

Changes 0
Metric Value
eloc 81
dl 0
loc 232
ccs 56
cts 57
cp 0.9825
rs 10
c 0
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 10 1
A loadComponents() 0 24 2
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 loadTranslations() 0 4 1
A loadConfig() 0 4 1
A loadViews() 0 4 1
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
18
class AdminLteServiceProvider extends BaseServiceProvider
19
{
20
    /**
21
     * Array with the available layout components.
22
     *
23
     * @var array
24
     */
25
    protected $layoutComponents = [
26
        Components\Layout\NavbarDarkmodeWidget::class,
27
        Components\Layout\NavbarNotification::class,
28
    ];
29
30
    /**
31
     * Array with the available form components.
32
     *
33
     * @var array
34
     */
35
    protected $formComponents = [
36
        Components\Form\Button::class,
37
        Components\Form\DateRange::class,
38
        Components\Form\Input::class,
39
        Components\Form\InputColor::class,
40
        Components\Form\InputDate::class,
41
        Components\Form\InputFile::class,
42
        Components\Form\InputSlider::class,
43
        Components\Form\InputSwitch::class,
44
        Components\Form\Select::class,
45
        Components\Form\Select2::class,
46
        Components\Form\SelectBs::class,
47
        Components\Form\Textarea::class,
48
        Components\Form\TextEditor::class,
49
    ];
50
51
    /**
52
     * Array with the available tool components.
53
     *
54
     * @var array
55
     */
56
    protected $toolComponents = [
57
        Components\Tool\Datatable::class,
58
        Components\Tool\Modal::class,
59
    ];
60
61
    /**
62
     * Array with the available widget components.
63
     *
64
     * @var array
65
     */
66
    protected $widgetComponents = [
67
        Components\Widget\Alert::class,
68
        Components\Widget\Callout::class,
69
        Components\Widget\Card::class,
70
        Components\Widget\InfoBox::class,
71
        Components\Widget\ProfileColItem::class,
72
        Components\Widget\ProfileRowItem::class,
73
        Components\Widget\ProfileWidget::class,
74
        Components\Widget\Progress::class,
75
        Components\Widget\SmallBox::class,
76
    ];
77
78
    /**
79
     * Register the package services.
80
     *
81 56
     * @return void
82
     */
83
    public function register()
84
    {
85
        // Bind a singleton instance of the AdminLte class into the service
86
        // container.
87 3
88 3
        $this->app->singleton(AdminLte::class, function (Container $app) {
89 3
            return new AdminLte(
90
                $app['config']['adminlte.filters'],
91
                $app['events'],
92 56
                $app
93 56
            );
94
        });
95
    }
96
97
    /**
98
     * Bootstrap the package's services.
99
     *
100 56
     * @return void
101
     */
102 56
    public function boot(Factory $view, Dispatcher $events, Repository $config)
103 56
    {
104 56
        $this->loadViews();
105 56
        $this->loadTranslations();
106 56
        $this->loadConfig();
107 56
        $this->registerCommands();
108 56
        $this->registerViewComposers($view);
109 56
        $this->registerMenu($events, $config);
110
        $this->loadComponents();
111
        $this->loadRoutes();
112
    }
113
114
    /**
115
     * Load the package views.
116 56
     *
117
     * @return void
118 56
     */
119 56
    private function loadViews()
120 56
    {
121
        $viewsPath = $this->packagePath('resources/views');
122
        $this->loadViewsFrom($viewsPath, 'adminlte');
123
    }
124
125
    /**
126
     * Load the package translations.
127 56
     *
128
     * @return void
129 56
     */
130 56
    private function loadTranslations()
131 56
    {
132
        $translationsPath = $this->packagePath('resources/lang');
133
        $this->loadTranslationsFrom($translationsPath, 'adminlte');
134
    }
135
136
    /**
137
     * Load the package config.
138 56
     *
139
     * @return void
140 56
     */
141 56
    private function loadConfig()
142 56
    {
143
        $configPath = $this->packagePath('config/adminlte.php');
144
        $this->mergeConfigFrom($configPath, 'adminlte');
145
    }
146
147
    /**
148
     * Get the absolute path to some package resource.
149
     *
150 56
     * @param string $path The relative path to the resource
151
     * @return string
152 56
     */
153
    private function packagePath($path)
154
    {
155
        return __DIR__."/../$path";
156
    }
157
158
    /**
159
     * Register the package's artisan commands.
160 56
     *
161
     * @return void
162 56
     */
163 56
    private function registerCommands()
164
    {
165
        $this->commands([
166
            AdminLteInstallCommand::class,
167
            AdminLteStatusCommand::class,
168 56
            AdminLteUpdateCommand::class,
169
            AdminLtePluginCommand::class,
170
        ]);
171
    }
172
173
    /**
174
     * Register the package's view composers.
175 56
     *
176
     * @return void
177 56
     */
178 56
    private function registerViewComposers(Factory $view)
179
    {
180
        $view->composer('adminlte::page', AdminLteComposer::class);
181
    }
182
183
    /**
184
     * Register the menu events handlers.
185 56
     *
186
     * @return void
187
     */
188
    private static function registerMenu(Dispatcher $events, Repository $config)
189
    {
190 56
        // Register a handler for the BuildingMenu event, this handler will add
191 56
        // the menu defined on the config file to the menu builder instance.
192
193 1
        $events->listen(
194 1
            BuildingMenu::class,
195 1
            function (BuildingMenu $event) use ($config) {
196 56
                $menu = $config->get('adminlte.menu', []);
197
                $menu = is_array($menu) ? $menu : [];
198 56
                $event->menu->add(...$menu);
199
            }
200
        );
201
    }
202
203
    /**
204
     * Load the blade view components.
205 56
     *
206
     * @return void
207
     */
208
    private function loadComponents()
209
    {
210 56
        // Support of x-components is only available for Laravel >= 7.x
211 56
        // versions. So, we check if we can load components.
212 56
213
        $canLoadComponents = method_exists(
214
            'Illuminate\Support\ServiceProvider',
215 56
            'loadViewComponentsAs'
216
        );
217
218
        if (! $canLoadComponents) {
219
            return;
220
        }
221 56
222 56
        // Load all the blade-x components.
223 56
224 56
        $components = array_merge(
225 56
            $this->layoutComponents,
226
            $this->formComponents,
227
            $this->toolComponents,
228 56
            $this->widgetComponents
229 56
        );
230
231
        $this->loadViewComponentsAs('adminlte', $components);
232
    }
233
234
    /**
235
     * Load the package web routes.
236
     *
237
     * @return void
238
     */
239
    private function loadRoutes()
240
    {
241
        $routesCfg = [
242
            'as' => 'adminlte.',
243
            'prefix' => 'adminlte',
244
            'middleware' => ['web'],
245
        ];
246
247
        Route::group($routesCfg, function () {
248
            $routesPath = $this->packagePath('routes/web.php');
249
            $this->loadRoutesFrom($routesPath);
250
        });
251
    }
252
}
253