Skip to content

Commit

Permalink
[BUG] Fix the Krait Table Command
Browse files Browse the repository at this point in the history
  • Loading branch information
cvetty committed Dec 17, 2024
1 parent af2a594 commit b8ead0c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 24 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
}
],
"require": {
"php": "^8.2"
"php": "^8.2",
"composer-runtime-api": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
17 changes: 15 additions & 2 deletions krait/src/Console/KraitTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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 🚀");

Expand Down
5 changes: 1 addition & 4 deletions krait/src/Console/RefreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion krait/src/DTO/TableResourceDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
10 changes: 8 additions & 2 deletions krait/src/Services/PreviewConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions krait/src/Services/TablesOrchestrator/TableCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
35 changes: 30 additions & 5 deletions krait/src/Services/TablesOrchestrator/TablesOrchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MtrDesign\Krait\Services\TablesOrchestrator;

use Exception;
use FilesystemIterator;
use MtrDesign\Krait\Services\PreviewConfigService;
use MtrDesign\Krait\Tables\BaseTable;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -77,7 +96,7 @@ public function getTableByRoute(string $tableName): ?BaseTable
}
}

return null; // Return null if no table matches the name
return null;
}

/**
Expand All @@ -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
);
}

/**
Expand Down
22 changes: 15 additions & 7 deletions krait/src/TablesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,33 @@ 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.
*
* @throws BindingResolutionException
*/
public function boot(): void
{
// Registering the tables to the orchestrator
$tablesOrchestrator = $this->app->make(TablesOrchestrator::class);
foreach ($this->tables as $table) {
$tablesOrchestrator->registerTable($table);
Expand Down

0 comments on commit b8ead0c

Please sign in to comment.