Passed
Pull Request — master (#888)
by Diego
03:20
created

AdminLteServiceProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Test Coverage

Coverage 98.21%

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 212
ccs 55
cts 56
cp 0.9821
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

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