Passed
Pull Request — master (#77)
by
unknown
03:17
created

EasyPanelServiceProvider::isDBConnected()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace EasyPanel;
5
6
use EasyPanel\Commands\{Actions\DeleteCRUD,
7
    Actions\Install,
8
    Actions\MakeCRUD,
9
    Actions\MakeCRUDConfig,
10
    Actions\Migration,
11
    Actions\PublishStubs,
12
    Actions\Reinstall,
13
    Actions\Uninstall,
14
    CRUDActions\MakeCreate,
15
    CRUDActions\MakeRead,
16
    CRUDActions\MakeSingle,
17
    CRUDActions\MakeUpdate,
18
    UserActions\DeleteAdmin,
19
    UserActions\GetAdmins,
20
    UserActions\MakeAdmin};
21
use EasyPanel\Http\Middleware\isAdmin;
22
use EasyPanel\Http\Middleware\LangChanger;
23
use EasyPanel\Models\PanelAdmin;
24
use EasyPanel\Support\Contract\{AuthFacade, LangManager, UserProviderFacade};
25
use EasyPanelTest\Dependencies\User;
26
use Exception;
27
use Illuminate\{Routing\Router,
28
    Support\Facades\Blade,
29
    Support\Facades\DB,
30
    Support\Facades\Log,
31
    Support\Facades\Route,
32
    Support\ServiceProvider};
33
use Livewire\Livewire;
34
35
class EasyPanelServiceProvider extends ServiceProvider
36
{
37
    public function register()
38
    {
39
        // Here we merge config with 'easy_panel' key
40
        $this->mergeConfigFrom(__DIR__ . '/../config/easy_panel_config.php', 'easy_panel');
41
42
        // Check the status of module
43
        if (!config('easy_panel.enable')) {
44
            return;
45
        }
46
47
        // Facades will be set
48
        $this->defineFacades();
49
    }
50
51
    private function defineFacades()
52
    {
53
        AuthFacade::shouldProxyTo(config('easy_panel.auth_class'));
54
        UserProviderFacade::shouldProxyTo(config('easy_panel.admin_provider_class'));
55
        LangManager::shouldProxyTo(config('easy_panel.lang_manager_class'));
56
    }
57
58
    public function boot()
59
    {
60
        if (!config('easy_panel.enable')) {
61
            return;
62
        }
63
64
        // Here we register publishes and Commands
65
        $isRunningInConsole = $this->app->runningInConsole();
66
        if ($isRunningInConsole) {
67
            $this->mergePublishes();
68
        }
69
70
        // Bind Artisan commands
71
        $this->bindCommands();
72
73
        // Load Views with 'admin::' prefix
74
        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'admin');
75
76
        // Register Middleware
77
        $this->registerMiddlewareAlias();
78
79
        // Load Livewire components
80
        $this->loadLivewireComponent();
81
82
        // check if database is connected to work with DB
83
        $isDBConnected = $this->isDBConnected();
84
        if ($isDBConnected) {
85
            // Define routes if doesn't cache
86
            $this->defineRoutes();
87
            // Load relationship for administrators
88
            $this->loadRelations();
89
        } else if ($isRunningInConsole) {
90
            echo "\033[31m ** Please check your DB connection \033[0m\n";
91
            echo "\033[31m ** Can not load routes of EasyPanel \033[0m\n";
92
        }
93
94
        Blade::componentNamespace("\\EasyPanel\\ViewComponents", 'easypanel');
95
    }
96
97
    private function mergePublishes()
98
    {
99
        $this->publishes([__DIR__ . '/../config/easy_panel_config.php' => config_path('easy_panel.php')], 'easy-panel-config');
100
101
        $this->publishes([__DIR__ . '/../resources/views' => resource_path('/views/vendor/admin')], 'easy-panel-views');
102
103
        $this->publishes([__DIR__ . '/../resources/assets' => public_path('/assets/admin')], 'easy-panel-styles');
104
105
        $this->publishes([
106
            __DIR__ . '/../database/migrations/cruds_table.php' => base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_cruds_table_easypanel.php'),
107
            __DIR__ . '/../database/migrations/panel_admins_table.php' => base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_panel_admins_table_easypanel.php'),
108
        ], 'easy-panel-migration');
109
110
        $this->publishes([__DIR__ . '/../resources/lang' => app()->langPath()], 'easy-panel-lang');
0 ignored issues
show
introduced by
The method langPath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        $this->publishes([__DIR__ . '/../resources/lang' => app()->/** @scrutinizer ignore-call */ langPath()], 'easy-panel-lang');
Loading history...
111
112
        $this->publishes([__DIR__ . '/Commands/stub' => base_path('/stubs/panel')], 'easy-panel-stubs');
113
    }
114
115
    private function bindCommands()
116
    {
117
        $this->commands([
118
            MakeAdmin::class,
119
            DeleteAdmin::class,
120
            Install::class,
121
            MakeCreate::class,
122
            MakeUpdate::class,
123
            MakeRead::class,
124
            MakeSingle::class,
125
            MakeCRUD::class,
126
            DeleteCRUD::class,
127
            MakeCRUDConfig::class,
128
            GetAdmins::class,
129
            Migration::class,
130
            Uninstall::class,
131
            Reinstall::class,
132
            PublishStubs::class
133
        ]);
134
    }
135
136
    private function registerMiddlewareAlias()
137
    {
138
        $router = $this->app->make(Router::class);
139
        $router->aliasMiddleware('isAdmin', isAdmin::class);
140
        $router->aliasMiddleware('LangChanger', LangChanger::class);
141
    }
142
143
    private function loadLivewireComponent()
144
    {
145
        Livewire::component('admin::livewire.crud.single', Http\Livewire\CRUD\Single::class);
146
        Livewire::component('admin::livewire.crud.create', Http\Livewire\CRUD\Create::class);
147
        Livewire::component('admin::livewire.crud.lists', Http\Livewire\CRUD\Lists::class);
148
149
        Livewire::component('admin::livewire.translation.manage', Http\Livewire\Translation\Manage::class);
150
151
        Livewire::component('admin::livewire.role.single', Http\Livewire\Role\Single::class);
152
        Livewire::component('admin::livewire.role.create', Http\Livewire\Role\Create::class);
153
        Livewire::component('admin::livewire.role.update', Http\Livewire\Role\Update::class);
154
        Livewire::component('admin::livewire.role.lists', Http\Livewire\Role\Lists::class);
155
156
        Livewire::component('admin::livewire.admins.single', Http\Livewire\Admins\Single::class);
157
        Livewire::component('admin::livewire.admins.update', Http\Livewire\Admins\Update::class);
158
    }
159
160
    private function isDBConnected()
161
    {
162
        try {
163
            DB::connection(config('easy_panel.database.connection'))->getPDO();
164
        } catch (Exception $e) {
165
            Log::error('Please check your DB connection \n  Can not load routes of EasyPanel');
166
            Log::error($e->getMessage());
167
            return false;
168
        }
169
        return true;
170
171
    }
172
173
    private function defineRoutes()
174
    {
175
        if (!$this->app->routesAreCached()) {
176
            $middlewares = array_merge(['web', 'isAdmin', 'LangChanger'], config('easy_panel.additional_middlewares'));
177
178
            Route::prefix(config('easy_panel.route_prefix'))
179
                ->middleware($middlewares)
180
                ->name(getRouteName() . '.')
181
                ->group(__DIR__ . '/routes.php');
182
        }
183
    }
184
185
    private function loadRelations()
186
    {
187
        $model = !$this->app->runningUnitTests() ? config('easy_panel.user_model') : User::class;
188
189
        $model::resolveRelationUsing('panelAdmin', function ($userModel) {
190
            return $userModel->hasOne(PanelAdmin::class)->latest();
191
        });
192
    }
193
194
}
195