Passed
Pull Request — master (#1178)
by Diego
03:31
created

AdminLteServiceProvider::loadComponents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 8
cts 8
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-file-krajee' => Form\InputFileKrajee::class,
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 196
    public function register()
97
    {
98
        // Bind a singleton instance of the AdminLte class into the service
99
        // container.
100
101 196
        $this->app->singleton(AdminLte::class, function (Container $app) {
102 3
            return new AdminLte(
103 3
                $app['config']['adminlte.filters'],
104 3
                $app['events'],
105 3
                $app
106 3
            );
107 196
        });
108
    }
109
110
    /**
111
     * Bootstrap the package's services.
112
     *
113
     * @return void
114
     */
115 196
    public function boot(Factory $view, Dispatcher $events, Repository $config)
116
    {
117 196
        $this->loadViews();
118 196
        $this->loadTranslations();
119 196
        $this->loadConfig();
120 196
        $this->registerCommands();
121 196
        $this->registerViewComposers($view);
122 196
        $this->registerMenu($events, $config);
123 196
        $this->loadComponents();
124 196
        $this->loadRoutes();
125
    }
126
127
    /**
128
     * Load the package views.
129
     *
130
     * @return void
131
     */
132 196
    private function loadViews()
133
    {
134 196
        $viewsPath = $this->packagePath('resources/views');
135 196
        $this->loadViewsFrom($viewsPath, $this->pkgPrefix);
136
    }
137
138
    /**
139
     * Load the package translations.
140
     *
141
     * @return void
142
     */
143 196
    private function loadTranslations()
144
    {
145 196
        $translationsPath = $this->packagePath('resources/lang');
146 196
        $this->loadTranslationsFrom($translationsPath, $this->pkgPrefix);
147
    }
148
149
    /**
150
     * Load the package config.
151
     *
152
     * @return void
153
     */
154 196
    private function loadConfig()
155
    {
156 196
        $configPath = $this->packagePath('config/adminlte.php');
157 196
        $this->mergeConfigFrom($configPath, $this->pkgPrefix);
158
    }
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 196
    private function packagePath($path)
167
    {
168 196
        return __DIR__."/../$path";
169
    }
170
171
    /**
172
     * Register the package's artisan commands.
173
     *
174
     * @return void
175
     */
176 196
    private function registerCommands()
177
    {
178 196
        $this->commands([
179 196
            AdminLteInstallCommand::class,
180 196
            AdminLteStatusCommand::class,
181 196
            AdminLteUpdateCommand::class,
182 196
            AdminLtePluginCommand::class,
183 196
        ]);
184
    }
185
186
    /**
187
     * Register the package's view composers.
188
     *
189
     * @return void
190
     */
191 196
    private function registerViewComposers(Factory $view)
192
    {
193 196
        $view->composer('adminlte::page', AdminLteComposer::class);
194
    }
195
196
    /**
197
     * Register the menu events handlers.
198
     *
199
     * @return void
200
     */
201 196
    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 196
        $events->listen(
207 196
            BuildingMenu::class,
208 196
            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 196
            }
213 196
        );
214
    }
215
216
    /**
217
     * Load the blade view components.
218
     *
219
     * @return void
220
     */
221 196
    private function loadComponents()
222
    {
223
        // Load all the blade-x components.
224
225 196
        $components = array_merge(
226 196
            $this->layoutComponents,
227 196
            $this->formComponents,
228 196
            $this->toolComponents,
229 196
            $this->widgetComponents
230 196
        );
231
232 196
        $this->loadViewComponentsAs($this->pkgPrefix, $components);
233
    }
234
235
    /**
236
     * Load the package web routes.
237
     *
238
     * @return void
239
     */
240 196
    private function loadRoutes()
241
    {
242 196
        $routesCfg = [
243 196
            'as' => "{$this->pkgPrefix}.",
244 196
            'prefix' => $this->pkgPrefix,
245 196
            'middleware' => ['web'],
246 196
        ];
247
248 196
        Route::group($routesCfg, function () {
249 196
            $routesPath = $this->packagePath('routes/web.php');
250 196
            $this->loadRoutesFrom($routesPath);
251 196
        });
252
    }
253
}
254