From b8ead0c5f3046898937102c05a68f3c9f0f0445d Mon Sep 17 00:00:00 2001 From: cvetty Date: Tue, 17 Dec 2024 12:28:57 +0200 Subject: [PATCH] [BUG] Fix the Krait Table Command --- composer.json | 3 +- krait/src/Console/KraitTableCommand.php | 17 +++++++-- krait/src/Console/RefreshCommand.php | 5 +-- krait/src/DTO/TableResourceDTO.php | 2 +- krait/src/Services/PreviewConfigService.php | 10 ++++-- .../TablesOrchestrator/TableCluster.php | 2 -- .../TablesOrchestrator/TablesOrchestrator.php | 35 ++++++++++++++++--- krait/src/TablesProvider.php | 22 ++++++++---- 8 files changed, 72 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index 55dcb19..0b2b067 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ } ], "require": { - "php": "^8.2" + "php": "^8.2", + "composer-runtime-api": "^2.0" }, "autoload": { "psr-4": { diff --git a/krait/src/Console/KraitTableCommand.php b/krait/src/Console/KraitTableCommand.php index 0ea5ecd..05e2e85 100644 --- a/krait/src/Console/KraitTableCommand.php +++ b/krait/src/Console/KraitTableCommand.php @@ -6,6 +6,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\File; use MtrDesign\Krait\Services\TablesOrchestrator\TableCluster; +use MtrDesign\Krait\Services\TablesOrchestrator\TablesOrchestrator; use Symfony\Component\Console\Attribute\AsCommand; use Throwable; @@ -41,7 +42,7 @@ class KraitTableCommand extends Command * * @throws Exception|Throwable */ - public function handle(): int + public function handle(TablesOrchestrator $tablesOrchestrator): int { $this->tableClass = $this->argument('table-class'); $this->validateClass(); @@ -54,7 +55,19 @@ public function handle(): int $this->createTableDefinitionClass(); $this->createController(); $this->createVueComponent(); - $this->callSilent('krait:refresh'); + + // Clear any cached files first + $this->call('route:clear'); + $this->call('cache:clear'); + + // Make sure all files are written before continuing + clearstatcache(true); + + // Register the new table cluster + $tablesOrchestrator->registerTableCluster($this->tableCluster); + + // Now refresh after everything is in place + $this->call('krait:refresh'); $this->components->info("The $this->tableClass table has been created successfully 🚀"); diff --git a/krait/src/Console/RefreshCommand.php b/krait/src/Console/RefreshCommand.php index 9bb84e1..24554c9 100644 --- a/krait/src/Console/RefreshCommand.php +++ b/krait/src/Console/RefreshCommand.php @@ -24,11 +24,8 @@ class RefreshCommand extends Command */ protected $description = 'Exposes all table vue components components.'; - public function handle() + public function handle(TablesOrchestrator $tablesOrchestrator) { - $tableComponentsPath = resource_path('js/components/tables'); - - $tablesOrchestrator = app(TablesOrchestrator::class); $tables = $tablesOrchestrator->getTables(); $components = []; foreach ($tables as $table) { diff --git a/krait/src/DTO/TableResourceDTO.php b/krait/src/DTO/TableResourceDTO.php index 5374185..d78d8d2 100644 --- a/krait/src/DTO/TableResourceDTO.php +++ b/krait/src/DTO/TableResourceDTO.php @@ -7,7 +7,7 @@ /** * DTO Object for handling TableResource data - * like VueJS components, Controllers, Definition Classes, etc. + * like Vue.js components, Controllers, Definition Classes, etc. */ readonly class TableResourceDTO { diff --git a/krait/src/Services/PreviewConfigService.php b/krait/src/Services/PreviewConfigService.php index 7ed2066..d2e03c7 100644 --- a/krait/src/Services/PreviewConfigService.php +++ b/krait/src/Services/PreviewConfigService.php @@ -3,7 +3,6 @@ namespace MtrDesign\Krait\Services; use Exception; -use Illuminate\Support\Collection; use Illuminate\Support\Facades\Request; use MtrDesign\Krait\Models\KraitPreviewConfiguration; use MtrDesign\Krait\Tables\BaseTable; @@ -17,6 +16,10 @@ class PreviewConfigService { /** * Returns the user preview configuration. + * + * @param mixed $user - the target user object + * @param string $tableName - the table name + * @return KraitPreviewConfiguration - the preview configuration */ public function getConfiguration(mixed $user, string $tableName): KraitPreviewConfiguration { @@ -50,7 +53,10 @@ public function sort( BaseTable $table, ): mixed { if ( - in_array(null, [$previewConfiguration->sort_column, $previewConfiguration->sort_direction]) || + in_array(null, [ + $previewConfiguration->sort_column, + $previewConfiguration->sort_direction, + ]) || ! $table->hasColumn($previewConfiguration->sort_column) ) { return $records; diff --git a/krait/src/Services/TablesOrchestrator/TableCluster.php b/krait/src/Services/TablesOrchestrator/TableCluster.php index 67363f4..e8b42e4 100644 --- a/krait/src/Services/TablesOrchestrator/TableCluster.php +++ b/krait/src/Services/TablesOrchestrator/TableCluster.php @@ -115,8 +115,6 @@ public function getController(): TableResourceDTO * Returns the VueJS component resource. * * @return TableResourceDTO - the VueJS component resource - * - * @throws Exception */ public function getVue(): TableResourceDTO { diff --git a/krait/src/Services/TablesOrchestrator/TablesOrchestrator.php b/krait/src/Services/TablesOrchestrator/TablesOrchestrator.php index 413f597..cc595d2 100644 --- a/krait/src/Services/TablesOrchestrator/TablesOrchestrator.php +++ b/krait/src/Services/TablesOrchestrator/TablesOrchestrator.php @@ -2,6 +2,7 @@ namespace MtrDesign\Krait\Services\TablesOrchestrator; +use Exception; use FilesystemIterator; use MtrDesign\Krait\Services\PreviewConfigService; use MtrDesign\Krait\Tables\BaseTable; @@ -33,13 +34,31 @@ public function __construct( * * @param SplFileInfo $table - the table definition class file * @return $this + * + * @throws Exception */ public function registerTable(SplFileInfo $table): TablesOrchestrator { $definitionClass = PathUtils::dirToNamespace($table->getPathname()); + $tableCluster = new TableCluster($definitionClass); + $this->registerTableCluster($tableCluster); - $this->tables[$definitionClass] = new TableCluster($definitionClass); - $this->tables[$definitionClass]->instantiate($this->previewConfigService); + return $this; + } + + /** + * Registers a table cluster. + * + * @param TableCluster $cluster - the already built cluster + * @return $this + * + * @throws Exception + */ + public function registerTableCluster(TableCluster $cluster): TablesOrchestrator + { + $namespace = $cluster->getDefinitionClass()->namespace; + $this->tables[$namespace] = $cluster; + $this->tables[$namespace]->instantiate($this->previewConfigService); return $this; } @@ -77,7 +96,7 @@ public function getTableByRoute(string $tableName): ?BaseTable } } - return null; // Return null if no table matches the name + return null; } /** @@ -92,9 +111,15 @@ public static function getTablesDirectoryIterator(): ?RecursiveIteratorIterator return null; } - $directoryIterator = new RecursiveDirectoryIterator($tablesDirectory, FilesystemIterator::SKIP_DOTS); + $directoryIterator = new RecursiveDirectoryIterator( + $tablesDirectory, + FilesystemIterator::SKIP_DOTS + ); - return new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST); + return new RecursiveIteratorIterator( + $directoryIterator, + RecursiveIteratorIterator::SELF_FIRST + ); } /** diff --git a/krait/src/TablesProvider.php b/krait/src/TablesProvider.php index de10a3d..2dbdff2 100644 --- a/krait/src/TablesProvider.php +++ b/krait/src/TablesProvider.php @@ -41,18 +41,25 @@ public function register(): void if (! $file->isFile()) { continue; } - $this->tables[] = $file; - $tableClass = TablesOrchestrator::getTableDefinitionClass($file->getPathname()); - $this->app->singleton($tableClass, function ($app) use ($tableClass) { - $tablesOrchestrator = $app->make(TablesOrchestrator::class); - - return $tablesOrchestrator->getTable($tableClass); - }); + $this->registerTable($file); } } } + /** + * Registers a single table to the container + */ + private function registerTable(SplFileInfo $file): void + { + $tableClass = TablesOrchestrator::getTableDefinitionClass($file->getPathname()); + $this->app->singleton($tableClass, function ($app) use ($tableClass) { + $tablesOrchestrator = $app->make(TablesOrchestrator::class); + + return $tablesOrchestrator->getTable($tableClass); + }); + } + /** * Configures the package resources. * @@ -60,6 +67,7 @@ public function register(): void */ public function boot(): void { + // Registering the tables to the orchestrator $tablesOrchestrator = $this->app->make(TablesOrchestrator::class); foreach ($this->tables as $table) { $tablesOrchestrator->registerTable($table);