From 75c252da55fc5fb8017048de207cb1fd03ccbef0 Mon Sep 17 00:00:00 2001 From: iantasker Date: Fri, 12 Mar 2021 13:05:01 +0000 Subject: [PATCH] register resource and pages with livewire --- src/PluginServiceProvider.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/PluginServiceProvider.php b/src/PluginServiceProvider.php index 71bd379baa..7c1b7ce02f 100644 --- a/src/PluginServiceProvider.php +++ b/src/PluginServiceProvider.php @@ -3,9 +3,15 @@ namespace Filament; use Illuminate\Support\ServiceProvider; +use Illuminate\Filesystem\Filesystem; +use Livewire\LiveWire; +use ReflectionClass; +use Str; +use Symfony\Component\Finder\SplFileInfo; abstract class PluginServiceProvider extends ServiceProvider { + protected $pages = []; protected $resources = []; @@ -23,10 +29,12 @@ abstract class PluginServiceProvider extends ServiceProvider $this->app->booting(function () { foreach ($this->pages() as $page) { Filament::registerPage($page); + $this->registerWithLivewire($resource); } foreach ($this->resources() as $resource) { Filament::registerResource($resource); + $this->registerWithLivewire($resource); } foreach ($this->roles() as $role) { @@ -85,4 +93,27 @@ abstract class PluginServiceProvider extends ServiceProvider { return $this->widgets; } + + private function registerWithLivewire($resource) + { + $filesystem = new Filesystem(); + $reflector = new \ReflectionClass($resource); + $directory = Str::of($reflector->getFileName())->before('.php'); + $namespace = Str::of($resource)->before('.php'); + + collect($filesystem->allfiles($directory)) + ->map(function (SplFileInfo $file) use ($namespace) { + return (string) Str::of($namespace) + ->append('\\', $file->getRelativePathname()) + ->replace(['/', '.php'], ['\\', '']); + }) + ->each(function ($class) use ($namespace) { + $alias = Str::of($class) + ->replace(['/', '\\'], '.') + ->explode('.') + ->map([Str::class, 'kebab']) + ->implode('.'); + Livewire::component($alias, $class); + }); + } }