now with browser tests
This commit is contained in:
@@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace mpba\Modules\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\File;
|
||||||
|
use Modules\DevelopmentSupport\Entities\ModuleStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class SyncModuleStatusesCommand
|
||||||
|
*
|
||||||
|
* Scans the Modules directory and ensures each module has a record
|
||||||
|
* in the module_statuses table.
|
||||||
|
*/
|
||||||
|
class SyncModuleStatusesCommand extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The console command name and signature.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'modules:sync-statuses';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Scan the Modules directory and add missing modules to the module_statuses table';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$modulesPath = base_path('Modules');
|
||||||
|
|
||||||
|
if (! File::exists($modulesPath)) {
|
||||||
|
$this->error('Modules directory does not exist: ' . $modulesPath);
|
||||||
|
|
||||||
|
return self::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var Collection<int, string> $moduleNames */
|
||||||
|
$moduleNames = $this->getModuleNames($modulesPath);
|
||||||
|
|
||||||
|
if ($moduleNames->isEmpty()) {
|
||||||
|
$this->warn('No modules were found in the Modules directory.');
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
$createdCount = 0;
|
||||||
|
|
||||||
|
foreach ($moduleNames as $moduleName) {
|
||||||
|
$moduleStatus = ModuleStatus::firstOrCreate(
|
||||||
|
['module' => $moduleName],
|
||||||
|
['enabled' => true]
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($moduleStatus->wasRecentlyCreated) {
|
||||||
|
$createdCount++;
|
||||||
|
|
||||||
|
$this->line(sprintf('Added module: %s', $moduleName));
|
||||||
|
} else {
|
||||||
|
$this->line(sprintf('Already exists: %s', $moduleName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->info(sprintf(
|
||||||
|
'Module sync complete. %d module(s) found, %d new record(s) added.',
|
||||||
|
$moduleNames->count(),
|
||||||
|
$createdCount
|
||||||
|
));
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all module directory names from the Modules path.
|
||||||
|
*
|
||||||
|
* @param string $modulesPath
|
||||||
|
* @return Collection<int, string>
|
||||||
|
*/
|
||||||
|
protected function getModuleNames(string $modulesPath): Collection
|
||||||
|
{
|
||||||
|
return collect(File::directories($modulesPath))
|
||||||
|
->map(static function (string $directory): string {
|
||||||
|
return basename($directory);
|
||||||
|
})
|
||||||
|
->filter(static function (string $moduleName): bool {
|
||||||
|
return $moduleName !== '';
|
||||||
|
})
|
||||||
|
->sort()
|
||||||
|
->values();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace mpba\Modules\DevelopmentSupport\Entities;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ModuleStatus
|
||||||
|
*/
|
||||||
|
class ModuleStatus extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The table associated with the model.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $table = 'module_statuses';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array<int, string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'module',
|
||||||
|
'enabled',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that should be cast.
|
||||||
|
*
|
||||||
|
* @var array<string, string>
|
||||||
|
*/
|
||||||
|
protected $casts = [
|
||||||
|
'enabled' => 'boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -42,6 +42,7 @@ use mpba\Modules\Commands\SeedCommand;
|
|||||||
use mpba\Modules\Commands\SeedMakeCommand;
|
use mpba\Modules\Commands\SeedMakeCommand;
|
||||||
use mpba\Modules\Commands\SetupCommand;
|
use mpba\Modules\Commands\SetupCommand;
|
||||||
use mpba\Modules\Commands\SupportMakeCommand;
|
use mpba\Modules\Commands\SupportMakeCommand;
|
||||||
|
use mpba\Modules\Commands\SyncModuleStatusesCommand;
|
||||||
use mpba\Modules\Commands\TestMakeCommand;
|
use mpba\Modules\Commands\TestMakeCommand;
|
||||||
use mpba\Modules\Commands\TraitMakeCommand;
|
use mpba\Modules\Commands\TraitMakeCommand;
|
||||||
use mpba\Modules\Commands\UnUseCommand;
|
use mpba\Modules\Commands\UnUseCommand;
|
||||||
@@ -100,6 +101,7 @@ class ConsoleServiceProvider extends ServiceProvider
|
|||||||
TestMakeCommand::class,
|
TestMakeCommand::class,
|
||||||
ComponentMakeCommand::class,
|
ComponentMakeCommand::class,
|
||||||
TraitMakeCommand::class,
|
TraitMakeCommand::class,
|
||||||
|
SyncModuleStatusesCommand::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user