Issues (17)

src/EasyPanelServiceProvider.php (1 issue)

Severity
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\Middleware\isAdmin;
22
use EasyPanel\Http\Middleware\LangChanger;
23
use EasyPanel\Support\Contract\{LangManager, UserProviderFacade, AuthFacade};
24
use Illuminate\{Routing\Router, Support\Facades\Blade, Support\Facades\Route, Support\ServiceProvider};
25
use Livewire\Livewire;
26
use EasyPanel\Models\PanelAdmin;
27
use EasyPanelTest\Dependencies\User;
28
29
class EasyPanelServiceProvider extends ServiceProvider
30
{
31
    public function register()
32
    {
33
        // Here we merge config with 'easy_panel' key
34
        $this->mergeConfigFrom(__DIR__ . '/../config/easy_panel_config.php', 'easy_panel');
35
36
        // Check the status of module
37
        if(!config('easy_panel.enable')) {
38
            return;
39
        }
40
41
        // Facades will be set
42
        $this->defineFacades();
43
    }
44
45
    public function boot()
46
    {
47
        if(!config('easy_panel.enable')) {
48
            return;
49
        }
50
51
        // Here we register publishes and Commands
52
        if ($this->app->runningInConsole()) {
53
            $this->mergePublishes();
54
        }
55
56
        // Bind Artisan commands
57
        $this->bindCommands();
58
59
        // Load Views with 'admin::' prefix
60
        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'admin');
61
62
        // Register Middleware
63
        $this->registerMiddlewareAlias();
64
65
        // Define routes if doesn't cached
66
        $this->defineRoutes();
67
68
        // Load Livewire components
69
        $this->loadLivewireComponent();
70
71
        // Load relationship for administrators
72
        $this->loadRelations();
73
74
        Blade::componentNamespace("\\EasyPanel\\ViewComponents", 'easypanel');
75
    }
76
77
    private function defineRoutes()
78
    {
79
        if(!$this->app->routesAreCached()) {
80
            $middlewares = array_merge(['web', 'isAdmin', 'LangChanger'], config('easy_panel.additional_middlewares'));
81
82
            Route::prefix(config('easy_panel.route_prefix'))
83
                ->middleware($middlewares)
84
                ->name(getRouteName() . '.')
85
                ->group(__DIR__ . '/routes.php');
86
        }
87
    }
88
89
    private function defineFacades()
90
    {
91
        AuthFacade::shouldProxyTo(config('easy_panel.auth_class'));
92
        UserProviderFacade::shouldProxyTo(config('easy_panel.admin_provider_class'));
93
        LangManager::shouldProxyTo(config('easy_panel.lang_manager_class'));
94
    }
95
96
    private function registerMiddlewareAlias()
97
    {
98
        $router = $this->app->make(Router::class);
99
        $router->aliasMiddleware('isAdmin', isAdmin::class);
100
        $router->aliasMiddleware('LangChanger', LangChanger::class);
101
    }
102
103
    private function loadLivewireComponent()
104
    {
105
        Livewire::component('admin::livewire.crud.single', Http\Livewire\CRUD\Single::class);
106
        Livewire::component('admin::livewire.crud.create', Http\Livewire\CRUD\Create::class);
107
        Livewire::component('admin::livewire.crud.lists', Http\Livewire\CRUD\Lists::class);
108
109
        Livewire::component('admin::livewire.translation.manage', Http\Livewire\Translation\Manage::class);
110
111
        Livewire::component('admin::livewire.role.single', Http\Livewire\Role\Single::class);
112
        Livewire::component('admin::livewire.role.create', Http\Livewire\Role\Create::class);
113
        Livewire::component('admin::livewire.role.update', Http\Livewire\Role\Update::class);
114
        Livewire::component('admin::livewire.role.lists', Http\Livewire\Role\Lists::class);
115
116
        Livewire::component('admin::livewire.admins.single', Http\Livewire\Admins\Single::class);
117
        Livewire::component('admin::livewire.admins.update', Http\Livewire\Admins\Update::class);
118
    }
119
120
    private function mergePublishes()
121
    {
122
        $this->publishes([__DIR__ . '/../config/easy_panel_config.php' => config_path('easy_panel.php')], 'easy-panel-config');
123
124
        $this->publishes([__DIR__ . '/../resources/views' => resource_path('/views/vendor/admin')], 'easy-panel-views');
125
126
        $this->publishes([__DIR__ . '/../resources/assets' => public_path('/assets/admin')], 'easy-panel-styles');
127
128
        $this->publishes([
129
            __DIR__ . '/../database/migrations/cruds_table.php' => base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_cruds_table_easypanel.php'),
130
            __DIR__ . '/../database/migrations/panel_admins_table.php' => base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_panel_admins_table_easypanel.php'),
131
        ], 'easy-panel-migration');
132
133
        $this->publishes([__DIR__.'/../resources/lang' => app()->langPath()], 'easy-panel-lang');
0 ignored issues
show
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

133
        $this->publishes([__DIR__.'/../resources/lang' => app()->/** @scrutinizer ignore-call */ langPath()], 'easy-panel-lang');
Loading history...
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