Skip to content

Commit

Permalink
feat: Register filament route resources
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-code-labx committed Apr 4, 2024
1 parent 227807a commit 35a7592
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
50 changes: 50 additions & 0 deletions src/Concerns/WithAutoDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace XtendPackages\RESTPresenter\Concerns;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Symfony\Component\Finder\SplFileInfo;

trait WithAutoDiscovery
{
protected function autoDiscover(string $path, $isKit = false): void
{
$fileSystem = app(Filesystem::class);
if (! $fileSystem->isDirectory($path)) {
return;
}

collect($fileSystem->allFiles($path))
->filter(fn (SplFileInfo $file) => Str::endsWith($file->getFilename(), 'ResourceController.php'))
->mapWithKeys(function (SplFileInfo $file) {
$name = Str::of(basename($file->getRelativePath()))
->replace('/', '.')
->plural()
->lower()
->value();

$controller = Str::of($file->getRealPath())
->replace(app()->path('Api'), config('rest-presenter.generator.namespace'))
->replace('/', '\\')
->replace('.php', '')
->value();

return [
$name => $controller,
];
})
->each(function ($controller, $name) use ($isKit) {
$kit = Str::of($controller)
->remove(config('rest-presenter.generator.namespace') . '\\StarterKits\\')
->before('\\')
->lower()
->value();

return Route::name($isKit ? $kit . '.' : null)
->prefix($isKit ? $kit : null)
->group(fn () => $this->resource($name, $controller));
});
}
}
40 changes: 14 additions & 26 deletions src/Support/XtendRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace XtendPackages\RESTPresenter\Support;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Routing\PendingResourceRegistration;
use Illuminate\Routing\Router;
use Illuminate\Routing\RouteRegistrar;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Symfony\Component\Finder\SplFileInfo;
use XtendPackages\RESTPresenter\Resources\Users\UserResourceController;
use XtendPackages\RESTPresenter\Concerns\WithAutoDiscovery;

class XtendRouter extends Router
{
use WithAutoDiscovery;

public function register(): RouteRegistrar
{
$prefix = config('rest-presenter.api.prefix');
Expand All @@ -38,39 +38,27 @@ public function routes(): void
)->values();
})->name('resources');

Route::middleware('auth:sanctum')->group(function () {
$this->resource('users', UserResourceController::class);
});

Route::name('auth:')
->prefix('auth')
->group(__DIR__ . '/../StarterKits/Auth/Breeze/Routes/auth.php');

$this->autoDiscoverResources();
$this->autoDiscoverStarterKits();
}

public function autoDiscoverResources(): void
{
$fileSystem = app(Filesystem::class);
if (! $fileSystem->isDirectory(app()->basePath(config('rest-presenter.generator.path') . '/Resources'))) {
return;
}
$this->autoDiscover(
path: app()->basePath(config('rest-presenter.generator.path') . '/Resources'),
);
}

collect($fileSystem->allFiles(app()->basePath(config('rest-presenter.generator.path') . '/Resources')))
->filter(fn (SplFileInfo $file) => Str::endsWith($file->getFilename(), 'ResourceController.php'))
->mapWithKeys(function (SplFileInfo $file) {
$name = Str::of($file->getRelativePath())->snake('-')->value();
$controller = Str::of($file->getRealPath())
->replace(app()->path('Api'), config('rest-presenter.generator.namespace'))
->replace('/', '\\')
->replace('.php', '')
->value();

return [
$name => $controller,
];
})
->each(fn ($controller, $name) => $this->resource($name, $controller));
public function autoDiscoverStarterKits(): void
{
$this->autoDiscover(
path: app()->basePath(config('rest-presenter.generator.path') . '/StarterKits'),
isKit: true,
);
}

public function resources(array $resources, array $options = []): void
Expand Down

0 comments on commit 35a7592

Please sign in to comment.