AdminLteServiceProvider::loadViews()   A
last analyzed

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\Support\Facades\Route;
6
use Illuminate\Support\Facades\View;
7
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
8
use JeroenNoten\LaravelAdminLte\Console\AdminLteInstallCommand;
9
use JeroenNoten\LaravelAdminLte\Console\AdminLtePluginCommand;
10
use JeroenNoten\LaravelAdminLte\Console\AdminLteRemoveCommand;
11
use JeroenNoten\LaravelAdminLte\Console\AdminLteStatusCommand;
12
use JeroenNoten\LaravelAdminLte\Console\AdminLteUpdateCommand;
13
use JeroenNoten\LaravelAdminLte\View\Components\Form;
14
use JeroenNoten\LaravelAdminLte\View\Components\Layout;
15
use JeroenNoten\LaravelAdminLte\View\Components\Tool;
16
use JeroenNoten\LaravelAdminLte\View\Components\Widget;
17
18
class AdminLteServiceProvider extends BaseServiceProvider
19
{
20
    /**
21
     * The prefix to use for register/load the package resources.
22
     *
23
     * @var string
24
     */
25
    protected $pkgPrefix = 'adminlte';
26
27
    /**
28
     * Array with the available layout blade components.
29
     *
30
     * @var array
31
     */
32
    protected $layoutComponents = [
33
        'navbar-darkmode-widget' => Layout\NavbarDarkmodeWidget::class,
34
        'navbar-notification' => Layout\NavbarNotification::class,
35
    ];
36
37
    /**
38
     * Array with the available form blade components.
39
     *
40
     * @var array
41
     */
42
    protected $formComponents = [
43
        'button' => Form\Button::class,
44
        'date-range' => Form\DateRange::class,
45
        'input' => Form\Input::class,
46
        'input-color' => Form\InputColor::class,
47
        'input-date' => Form\InputDate::class,
48
        'input-file' => Form\InputFile::class,
49
        'input-file-krajee' => Form\InputFileKrajee::class,
50
        'input-slider' => Form\InputSlider::class,
51
        'input-switch' => Form\InputSwitch::class,
52
        'options' => Form\Options::class,
53
        'select' => Form\Select::class,
54
        'select2' => Form\Select2::class,
55
        'select-bs' => Form\SelectBs::class,
56
        'textarea' => Form\Textarea::class,
57
        'text-editor' => Form\TextEditor::class,
58
    ];
59
60
    /**
61
     * Array with the available tool blade components.
62
     *
63
     * @var array
64
     */
65
    protected $toolComponents = [
66
        'datatable' => Tool\Datatable::class,
67
        'modal' => Tool\Modal::class,
68
    ];
69
70
    /**
71
     * Array with the available widget blade components.
72
     *
73
     * @var array
74
     */
75
    protected $widgetComponents = [
76
        'alert' => Widget\Alert::class,
77
        'callout' => Widget\Callout::class,
78
        'card' => Widget\Card::class,
79
        'info-box' => Widget\InfoBox::class,
80
        'profile-col-item' => Widget\ProfileColItem::class,
81
        'profile-row-item' => Widget\ProfileRowItem::class,
82
        'profile-widget' => Widget\ProfileWidget::class,
83
        'progress' => Widget\Progress::class,
84
        'small-box' => Widget\SmallBox::class,
85
    ];
86
87
    /**
88
     * Register the package services.
89
     *
90
     * @return void
91
     */
92 215
    public function register()
93
    {
94
        // Bind a singleton instance of the AdminLte class into the service
95
        // container.
96
97 215
        $this->app->singleton(AdminLte::class, function () {
98 3
            return new AdminLte(config('adminlte.filters', []));
99 215
        });
100
    }
101
102
    /**
103
     * Bootstrap the package services.
104
     *
105
     * @return void
106
     */
107 215
    public function boot()
108
    {
109 215
        $this->loadViews();
110 215
        $this->loadTranslations();
111 215
        $this->loadConfig();
112 215
        $this->registerCommands();
113 215
        $this->registerViewComposers();
114 215
        $this->loadComponents();
115 215
        $this->loadRoutes();
116
    }
117
118
    /**
119
     * Load the package views.
120
     *
121
     * @return void
122
     */
123 215
    private function loadViews()
124
    {
125 215
        $viewsPath = $this->packagePath('resources/views');
126 215
        $this->loadViewsFrom($viewsPath, $this->pkgPrefix);
127
    }
128
129
    /**
130
     * Load the package translations.
131
     *
132
     * @return void
133
     */
134 215
    private function loadTranslations()
135
    {
136 215
        $transPath = $this->packagePath('resources/lang');
137 215
        $this->loadTranslationsFrom($transPath, $this->pkgPrefix);
138
    }
139
140
    /**
141
     * Load the package configuration.
142
     *
143
     * @return void
144
     */
145 215
    private function loadConfig()
146
    {
147 215
        $configPath = $this->packagePath('config/adminlte.php');
148 215
        $this->mergeConfigFrom($configPath, $this->pkgPrefix);
149
    }
150
151
    /**
152
     * Register the artisan commands of the package.
153
     *
154
     * @return void
155
     */
156 215
    private function registerCommands()
157
    {
158 215
        if ($this->app->runningInConsole()) {
159 215
            $this->commands([
160 215
                AdminLteInstallCommand::class,
161 215
                AdminLtePluginCommand::class,
162 215
                AdminLteRemoveCommand::class,
163 215
                AdminLteStatusCommand::class,
164 215
                AdminLteUpdateCommand::class,
165 215
            ]);
166
        }
167
    }
168
169
    /**
170
     * Register the view composers of the package. View composers are callbacks
171
     * or class methods that are called when a view is rendered. If you have
172
     * data that you want to be bound to a view each time that view is rendered,
173
     * a view composer can help you organize that logic into a single location.
174
     *
175
     * @return void
176
     */
177 215
    private function registerViewComposers()
178
    {
179
        // Bind the AdminLte singleton instance into each adminlte page view.
180
181 215
        View::composer('adminlte::page', function (\Illuminate\View\View $v) {
182 1
            $v->with('adminlte', $this->app->make(AdminLte::class));
183 215
        });
184
    }
185
186
    /**
187
     * Load the blade view components of the package.
188
     *
189
     * @return void
190
     */
191 215
    private function loadComponents()
192
    {
193
        // Load all the blade-x components.
194
195 215
        $components = array_merge(
196 215
            $this->layoutComponents,
197 215
            $this->formComponents,
198 215
            $this->toolComponents,
199 215
            $this->widgetComponents
200 215
        );
201
202 215
        $this->loadViewComponentsAs($this->pkgPrefix, $components);
203
    }
204
205
    /**
206
     * Load the routes of the package.
207
     *
208
     * @return void
209
     */
210 215
    private function loadRoutes()
211
    {
212 215
        $routesCfg = [
213 215
            'as' => "{$this->pkgPrefix}.",
214 215
            'prefix' => $this->pkgPrefix,
215 215
            'middleware' => ['web'],
216 215
        ];
217
218 215
        Route::group($routesCfg, function () {
219 215
            $routesPath = $this->packagePath('routes/web.php');
220 215
            $this->loadRoutesFrom($routesPath);
221 215
        });
222
    }
223
224
    /**
225
     * Get the absolute path to some package resource.
226
     *
227
     * @param  string  $path  The relative path to the resource
228
     * @return string
229
     */
230 215
    private function packagePath($path)
231
    {
232 215
        return __DIR__."/../$path";
233
    }
234
}
235