Dynamic livewire resource components

This commit is contained in:
Ryan Scherler
2020-11-04 16:48:20 -08:00
parent 02648e43fa
commit e7ae77b704
3 changed files with 27 additions and 19 deletions
+5 -3
View File
@@ -142,9 +142,9 @@ class Filament
return $manifest[$key];
}
public function getResourceModels(string $path): Collection
public function getResourceModels(): Collection
{
$models = collect(File::allFiles($path))
$models = collect(File::allFiles(app_path()))
->map(function ($item) {
$path = $item->getRelativePathName();
$class = sprintf('\%s%s',
@@ -162,8 +162,10 @@ class Filament
return $reflection->isSubclassOf(Model::class) &&
in_array(FilamentResource::class, class_implements($class)) &&
!$reflection->isAbstract();
})->mapWithKeys(function ($class) {
return [class_basename($class) => $class];
});
return $models->values();
return $models;
}
}
+9 -8
View File
@@ -118,17 +118,18 @@ class FilamentServiceProvider extends ServiceProvider
{
if (Features::hasResourceModels()) {
$this->app->booted(function () {
$models = $this->app->filament->getResourceModels(app_path());
$models = $this->app->filament->getResourceModels();
$models->each(function ($class) {
$model = $this->app->make($class);
$actions = $model->actions() ?? [];
$models->each(function ($item, $key) {
$model = $this->app->make($item);
if (array_key_exists('index', $model->actions())) {
$route = route('filament.resource', ['model' => $key]);
if (array_key_exists('index', $actions)) {
Navigation::create([
'path' => route('filament.resource', ['model' => class_basename($model)]),
'active' => $model->active ?? [],
'label' => $model->label ?? Str::plural(class_basename($model)),
'path' => $route,
'active' => $route.'*',
'label' => $model->label ?? Str::plural($key),
'icon' => $model->icon ?? 'heroicon-o-database',
]);
}
@@ -2,16 +2,21 @@
namespace Filament\Http\Controllers;
use Illuminate\Container\Container;
use Illuminate\Routing\Route;
use Illuminate\View\View;
use Filament;
class ResourceModelController extends Controller
{
/**
* Show the resource for the given model.
*
* @param int $id
* @return \Illuminate\View\View
*/
public function __invoke(string $model, string $action = 'index')
public function __invoke(Container $container, Route $route, string $model, string $action = 'index'): View
{
return 'Model: '.$model.' action: '.$action;
$model = Filament::getResourceModels()->get($model);
abort_unless($model, 404);
$component = collect(app($model)->actions())->get($action);
abort_unless($component, 400, __("`$action` is not a valid resource action in `$model`"));
return call_user_func((new $component), $container, $route);
}
}