Skip to content

Commit

Permalink
Merge pull request #77 from reziamini/fix/after-install-issue
Browse files Browse the repository at this point in the history
[FIX] after installation issues
  • Loading branch information
reziamini authored Jun 16, 2024
2 parents 271c019 + 962738d commit de90ec6
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 132 deletions.
18 changes: 11 additions & 7 deletions config/easy_panel_config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use EasyPanel\Services\LangService;
use EasyPanel\Support\Auth\AdminIdentifier;
use EasyPanel\Support\User\UserProvider;

return [

// Enable whole module
Expand All @@ -19,13 +23,13 @@

// How to authenticate admin
// You may use other ways to authenticate a admin (tables or ..) you can manage it with this class
'auth_class' => \EasyPanel\Support\Auth\AdminIdentifier::class,
'auth_class' => AdminIdentifier::class,

// With this class you can manage how to create a admin or remove it.
'admin_provider_class' => \EasyPanel\Support\User\UserProvider::class,
'admin_provider_class' => UserProvider::class,

//The namespace of lang manager class
'lang_manager_class' => \EasyPanel\Services\LangService::class,
'lang_manager_class' => LangService::class,

// it's a place where a user if not authenticated will be redirected
'redirect_unauthorized' => '/',
Expand All @@ -43,9 +47,9 @@
'lazy_mode' => true,

// database configure
'database'=>[
'connection'=> env('EZ_PANEL_DB_CONNECTION'),
'panel_admin_table'=>'panel_admins',
'crud_table'=> 'cruds'
'database' => [
'connection' => env('EASY_PANEL_DB_CONNECTION', env('DB_CONNECTION', 'mysql')),
'panel_admin_table' => 'panel_admins',
'crud_table' => 'cruds'
]
];
6 changes: 3 additions & 3 deletions database/migrations/cruds_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CreateCrudsTableEasypanel extends Migration
*/
public function getConnection()
{
return config('easy_panel_config.database.connection') ?: config('database.default');
return config('easy_panel.database.connection');
}

/**
Expand All @@ -21,7 +21,7 @@ public function getConnection()
*/
public function up()
{
Schema::create(config('easy_panel_config.database.crud_table'), function (Blueprint $table) {
Schema::create(config('easy_panel.database.crud_table'), function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('model')->unique();
Expand All @@ -42,6 +42,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists(config('easy_panel_config.database.crud_table'));
Schema::dropIfExists(config('easy_panel.database.crud_table'));
}
}
7 changes: 4 additions & 3 deletions database/migrations/panel_admins_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ class CreatePanelAdminsTableEasypanel extends Migration
*/
public function getConnection()
{
return config('easy_panel_config.database.connection') ?: config('database.default');
return config('easy_panel.database.connection');
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(config('easy_panel_config.database.panel_admin_table'), function (Blueprint $table) {
Schema::create(config('easy_panel.database.panel_admin_table'), function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->boolean('is_superuser');
Expand All @@ -37,6 +38,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists(config('easy_panel_config.database.panel_admin_table'));
Schema::dropIfExists(config('easy_panel.database.panel_admin_table'));
}
}
204 changes: 140 additions & 64 deletions src/EasyPanelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,34 @@

namespace EasyPanel;

use EasyPanel\Commands\{Actions\PublishStubs,
use EasyPanel\Commands\{Actions\DeleteCRUD,
Actions\Install,
Actions\MakeCRUD,
Actions\MakeCRUDConfig,
Actions\Migration,
Actions\PublishStubs,
Actions\Reinstall,
Actions\Uninstall,
CRUDActions\MakeCreate,
UserActions\GetAdmins,
Actions\MakeCRUDConfig,
CRUDActions\MakeRead,
CRUDActions\MakeSingle,
CRUDActions\MakeUpdate,
Actions\DeleteCRUD,
Actions\MakeCRUD,
UserActions\DeleteAdmin,
Actions\Install,
UserActions\MakeAdmin,
Actions\Migration,
Actions\Uninstall};
UserActions\GetAdmins,
UserActions\MakeAdmin};
use EasyPanel\Http\Middleware\isAdmin;
use EasyPanel\Http\Middleware\LangChanger;
use EasyPanel\Support\Contract\{LangManager, UserProviderFacade, AuthFacade};
use Illuminate\{Routing\Router, Support\Facades\Blade, Support\Facades\Route, Support\ServiceProvider};
use Livewire\Livewire;
use EasyPanel\Models\PanelAdmin;
use EasyPanel\Support\Contract\{AuthFacade, LangManager, UserProviderFacade};
use EasyPanelTest\Dependencies\User;
use Exception;
use Illuminate\{Routing\Router,
Support\Facades\Blade,
Support\Facades\DB,
Support\Facades\Log,
Support\Facades\Route,
Support\ServiceProvider};
use Livewire\Livewire;

class EasyPanelServiceProvider extends ServiceProvider
{
Expand All @@ -34,22 +40,30 @@ public function register()
$this->mergeConfigFrom(__DIR__ . '/../config/easy_panel_config.php', 'easy_panel');

// Check the status of module
if(!config('easy_panel.enable')) {
if (!config('easy_panel.enable')) {
return;
}

// Facades will be set
$this->defineFacades();
}

private function defineFacades()
{
AuthFacade::shouldProxyTo(config('easy_panel.auth_class'));
UserProviderFacade::shouldProxyTo(config('easy_panel.admin_provider_class'));
LangManager::shouldProxyTo(config('easy_panel.lang_manager_class'));
}

public function boot()
{
if(!config('easy_panel.enable')) {
if (!config('easy_panel.enable')) {
return;
}

// Here we register publishes and Commands
if ($this->app->runningInConsole()) {
$isRunningInConsole = $this->app->runningInConsole();
if ($isRunningInConsole) {
$this->mergePublishes();
}

Expand All @@ -62,35 +76,111 @@ public function boot()
// Register Middleware
$this->registerMiddlewareAlias();

// Define routes if doesn't cached
$this->defineRoutes();

// Load Livewire components
$this->loadLivewireComponent();

// Load relationship for administrators
$this->loadRelations();
// check if database is connected to work with DB
$isDBConnected = $this->isDBConnected();
if ($isDBConnected) {
// Define routes if doesn't cache
$this->defineRoutes();
// Load relationship for administrators
$this->loadRelations();
} else if ($isRunningInConsole) {
echo "\033[31m ** Please check your DB connection \033[0m\n";
echo "\033[31m ** Can not load routes of EasyPanel \033[0m\n";
}

Blade::componentNamespace("\\EasyPanel\\ViewComponents", 'easypanel');
}

private function defineRoutes()
private function mergePublishes()
{
if(!$this->app->routesAreCached()) {
$middlewares = array_merge(['web', 'isAdmin', 'LangChanger'], config('easy_panel.additional_middlewares'));
$this->publishes([__DIR__ . '/../config/easy_panel_config.php' => config_path('easy_panel.php')], 'easy-panel-config');

Route::prefix(config('easy_panel.route_prefix'))
->middleware($middlewares)
->name(getRouteName() . '.')
->group(__DIR__ . '/routes.php');
$this->publishes([__DIR__ . '/../resources/views' => resource_path('/views/vendor/admin')], 'easy-panel-views');

$this->publishes([__DIR__ . '/../resources/assets' => public_path('/assets/admin')], 'easy-panel-styles');

$migrationForCopy = $this->publishMigration();
if ($migrationForCopy) {
$this->publishes($migrationForCopy, 'easy-panel-migration');
}

$this->publishes([__DIR__ . '/../resources/lang' => app()->langPath()], 'easy-panel-lang');

$this->publishes([__DIR__ . '/Commands/stub' => base_path('/stubs/panel')], 'easy-panel-stubs');
}

private function defineFacades()
/**
* Scans the target migration directory and copies migrations from the package that do not exist in the target project.
*
* This method performs several key operations:
* 1. It fetches the current migration files from the target project's migration directory.
* 2. It identifies migrations related to this package within the target project by looking for files with a specific naming convention (i.e., containing '_999999_' in the filename).
* 3. It then scans the package's migration directory for all available migrations.
* 4. For each migration in the package, it checks if a corresponding migration (with a modified naming convention indicating it belongs to the EasyPanel package) does not already exist in the target project.
* 5. If such a migration does not exist, it prepares to copy the migration file from the package to the target project, renaming it according to the target project's naming convention and appending '_easypanel' to indicate its origin.
*
* The method ultimately returns an associative array where the keys are the full paths to the source migration files in the package, and the values are the intended full paths for these migrations once copied to the target project. This array can then be used to perform the actual file copy operations.
*
* @return array An associative array mapping source migration file paths to target file paths for migrations that need to be copied. The structure is ['sourceFilePath' => 'targetFilePath'].
*/
private function publishMigration(): array
{
AuthFacade::shouldProxyTo(config('easy_panel.auth_class'));
UserProviderFacade::shouldProxyTo(config('easy_panel.admin_provider_class'));
LangManager::shouldProxyTo(config('easy_panel.lang_manager_class'));
// 1- fetch current migration files
// 1-1- scan project
$projectMigrations = array_slice(scandir(base_path('/database/migrations/')), 2);
$packageMigrationInProject = [];
foreach ($projectMigrations as $migrationName) {
if (str_contains($migrationName, '_999999_')) {
// 2024_03_28_999999_migration_name.php
// convert to migration_name
$candidateMigrationName = substr(explode('_999999_', $migrationName)[1], 0, -4);
$packageMigrationInProject[] = $candidateMigrationName;

}
}

// 1-2- current migration
$packageMigration = [];
foreach (array_slice(scandir(__DIR__ . '/../database/migrations'), 2) as $migrationName) {
// migration_name.php
// convert to migration_name
$packageMigration[] = substr($migrationName, 0, -4);
}
// copy not existence files:
$migrationForCopy = [];
foreach ($packageMigration as $migrationName) {
$targetMigrationName = 'create_' . $migrationName . '_easypanel';
if (!in_array($targetMigrationName, $packageMigrationInProject)) {
$migrationForCopy[dirname(__DIR__) . '/database/migrations/' . $migrationName . '.php'] = base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_' . $migrationName . '_easypanel.php');
}
}
return $migrationForCopy;


}

private function bindCommands()
{
$this->commands([
MakeAdmin::class,
DeleteAdmin::class,
Install::class,
MakeCreate::class,
MakeUpdate::class,
MakeRead::class,
MakeSingle::class,
MakeCRUD::class,
DeleteCRUD::class,
MakeCRUDConfig::class,
GetAdmins::class,
Migration::class,
Uninstall::class,
Reinstall::class,
PublishStubs::class
]);
}

private function registerMiddlewareAlias()
Expand All @@ -117,50 +207,36 @@ private function loadLivewireComponent()
Livewire::component('admin::livewire.admins.update', Http\Livewire\Admins\Update::class);
}

private function mergePublishes()
private function isDBConnected()
{
$this->publishes([__DIR__ . '/../config/easy_panel_config.php' => config_path('easy_panel.php')], 'easy-panel-config');

$this->publishes([__DIR__ . '/../resources/views' => resource_path('/views/vendor/admin')], 'easy-panel-views');

$this->publishes([__DIR__ . '/../resources/assets' => public_path('/assets/admin')], 'easy-panel-styles');

$this->publishes([
__DIR__ . '/../database/migrations/cruds_table.php' => base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_cruds_table_easypanel.php'),
__DIR__ . '/../database/migrations/panel_admins_table.php' => base_path('/database/migrations/' . date('Y_m_d') . '_999999_create_panel_admins_table_easypanel.php'),
], 'easy-panel-migration');

$this->publishes([__DIR__.'/../resources/lang' => app()->langPath()], 'easy-panel-lang');
try {
DB::connection(config('easy_panel.database.connection'))->getPDO();
} catch (Exception $e) {
Log::error('Please check your DB connection \n Can not load routes of EasyPanel');
Log::error($e->getMessage());
return false;
}
return true;

$this->publishes([__DIR__.'/Commands/stub' => base_path('/stubs/panel')], 'easy-panel-stubs');
}

private function bindCommands()
private function defineRoutes()
{
$this->commands([
MakeAdmin::class,
DeleteAdmin::class,
Install::class,
MakeCreate::class,
MakeUpdate::class,
MakeRead::class,
MakeSingle::class,
MakeCRUD::class,
DeleteCRUD::class,
MakeCRUDConfig::class,
GetAdmins::class,
Migration::class,
Uninstall::class,
Reinstall::class,
PublishStubs::class
]);
if (!$this->app->routesAreCached()) {
$middlewares = array_merge(['web', 'isAdmin', 'LangChanger'], config('easy_panel.additional_middlewares'));

Route::prefix(config('easy_panel.route_prefix'))
->middleware($middlewares)
->name(getRouteName() . '.')
->group(__DIR__ . '/routes.php');
}
}

private function loadRelations()
{
$model = !$this->app->runningUnitTests() ? config('easy_panel.user_model') : User::class;

$model::resolveRelationUsing('panelAdmin', function ($userModel){
$model::resolveRelationUsing('panelAdmin', function ($userModel) {
return $userModel->hasOne(PanelAdmin::class)->latest();
});
}
Expand Down
Loading

0 comments on commit de90ec6

Please sign in to comment.