register resource and pages with livewire

This commit is contained in:
iantasker
2021-03-12 13:05:01 +00:00
parent 6daf8097a7
commit 75c252da55
+31
View File
@@ -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);
});
}
}