Passed
Push — master ( 43342b...911e23 )
by Reza
14:51
created

EasyPanelServiceProvider::loadRelations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
4
namespace EasyPanel;
5
6
use EasyPanel\Commands\{Actions\PublishStubs,
7
    Actions\Reinstall,
8
    CRUDActions\MakeCreate,
9
    UserActions\GetAdmins,
10
    Actions\MakeCRUDConfig,
11
    CRUDActions\MakeRead,
12
    CRUDActions\MakeSingle,
13
    CRUDActions\MakeUpdate,
14
    Actions\DeleteCRUD,
15
    Actions\MakeCRUD,
16
    UserActions\DeleteAdmin,
17
    Actions\Install,
18
    UserActions\MakeAdmin,
19
    Actions\Migration,
20
    Actions\Uninstall};
21
use EasyPanel\Http\Livewire\Todo\Create;
22
use EasyPanel\Http\Livewire\Todo\Lists;
23
use EasyPanel\Http\Livewire\Todo\Single;
24
use EasyPanel\Http\Middleware\isAdmin;
25
use EasyPanel\Http\Middleware\LangChanger;
26
use EasyPanel\Support\Contract\{UserProviderFacade, AuthFacade};
27
use Illuminate\{
28
    Routing\Router,
29
    Support\Facades\Route,
30
    Support\ServiceProvider
31
};
32
use Livewire\Livewire;
33
use EasyPanel\Models\PanelAdmin;
34
use EasyPanelTest\Dependencies\User;
35
36
class EasyPanelServiceProvider extends ServiceProvider
37
{
38
    public function register()
39
    {
40
        // Here we merge config with 'easy_panel' key
41
        $this->mergeConfigFrom(__DIR__ . '/../config/easy_panel_config.php', 'easy_panel');
42
43
        // Check the status of module
44
        if(!config('easy_panel.enable')) {
45
            return;
46
        }
47
48
        // Facades will be set
49
        $this->defineFacades();
50
    }
51
52
    public function boot()
53
    {
54
        if(!config('easy_panel.enable')) {
55
            return;
56
        }
57
58
        // Here we register publishes and Commands
59
        if ($this->app->runningInConsole()) {
60
            $this->mergePublishes();
61
        }
62
63
        // Bind Artisan commands
64
        $this->bindCommands();
65
66
        // Load Views with 'admin::' prefix
67
        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'admin');
68
69
        // Register Middleware
70
        $this->registerMiddlewareAlias();
71
72
        // Define routes if doesn't cached
73
        $this->defineRoutes();
74
75
        // Load Livewire TODOs components
76
        $this->loadLivewireComponent();
77
78
        // Load relationship for administrators
79
        $this->loadRelations();
80
    }
81
82
    private function defineRoutes()
83
    {
84
        if(!$this->app->routesAreCached()) {
85
            $middlewares = array_merge(['web', 'isAdmin', 'LangChanger'], config('easy_panel.additional_middlewares'));
86
87
            Route::prefix(config('easy_panel.route_prefix'))
88
                ->middleware($middlewares)
89
                ->name(getRouteName() . '.')
90
                ->group(__DIR__ . '/routes.php');
91
        }
92
    }
93
94
    private function defineFacades()
95
    {
96
        AuthFacade::shouldProxyTo(config('easy_panel.auth_class'));
97
        UserProviderFacade::shouldProxyTo(config('easy_panel.admin_provider_class'));
98
    }
99
100
    private function registerMiddlewareAlias()
101
    {
102
        $router = $this->app->make(Router::class);
103
        $router->aliasMiddleware('isAdmin', isAdmin::class);
104
        $router->aliasMiddleware('LangChanger', LangChanger::class);
105
    }
106
107
    private function loadLivewireComponent()
108
    {
109
        Livewire::component('admin::livewire.todo.single', Single::class);
110
        Livewire::component('admin::livewire.todo.create', Create::class);
111
        Livewire::component('admin::livewire.todo.lists', Lists::class);
112
113
        Livewire::component('admin::livewire.crud.single', Http\Livewire\CRUD\Single::class);
114
        Livewire::component('admin::livewire.crud.create', Http\Livewire\CRUD\Create::class);
115
        Livewire::component('admin::livewire.crud.lists', Http\Livewire\CRUD\Lists::class);
116
    }
117
118
    private function mergePublishes()
119
    {
120
        $this->publishes([__DIR__ . '/../config/easy_panel_config.php' => config_path('easy_panel.php')], 'easy-panel-config');
121
122
        $this->publishes([__DIR__ . '/../resources/views' => resource_path('/views/vendor/admin')], 'easy-panel-views');
123
124
        $this->publishes([__DIR__ . '/../resources/assets' => public_path('/assets/admin')], 'easy-panel-styles');
125
126
        $this->publishes([__DIR__ . '/../database/migrations/2020_09_05_999999_create_todos_table.php' => base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_admin_todos_table.php')], 'easy-panel-todo');
127
128
        $this->publishes([
129
            __DIR__ . '/../database/migrations/2021_07_17_999999_create_cruds_table.php' => base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_cruds_table.php'),
130
            __DIR__ . '/../database/migrations/2021_12_17_999999_create_user_admins_table.php' => base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_user_admins_table.php')
131
        ], 'easy-panel-migration');
132
133
        $this->publishes([__DIR__.'/../resources/lang' => resource_path('/lang')], 'easy-panel-lang');
134
135
        $this->publishes([__DIR__.'/Commands/stub' => base_path('/stubs/panel')], 'easy-panel-stubs');
136
    }
137
138
    private function bindCommands()
139
    {
140
        $this->commands([
141
            MakeAdmin::class,
142
            DeleteAdmin::class,
143
            Install::class,
144
            MakeCreate::class,
145
            MakeUpdate::class,
146
            MakeRead::class,
147
            MakeSingle::class,
148
            MakeCRUD::class,
149
            DeleteCRUD::class,
150
            MakeCRUDConfig::class,
151
            GetAdmins::class,
152
            Migration::class,
153
            Uninstall::class,
154
            Reinstall::class,
155
            PublishStubs::class
156
        ]);
157
    }
158
159
    private function loadRelations()
160
    {
161
        $model = !$this->app->runningUnitTests() ? config('easy_panel.user_model') : User::class;
162
163
        $model::resolveRelationUsing('panelAdmin', function ($userModel){
164
            return $userModel->hasOne(PanelAdmin::class)->latest();
165
        });
166
    }
167
168
}
169