Passed
Push — master ( bfee8d...851a31 )
by Florian
07:30
created

AdminLteServiceProvider::loadViews()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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