mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
register resource and pages with livewire
This commit is contained in:
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user