From 4f7ff9ea4eb6432f8abd3dc29db3a442acfaa6d6 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Mon, 31 Oct 2022 15:44:37 +0000 Subject: [PATCH] wip --- bootstrap/app.php | 2 - packages/app/src/Context.php | 24 ++++++ packages/app/src/Contracts/Plugin.php | 10 +++ packages/app/src/Facades/Filament.php | 11 --- packages/app/src/FilamentManager.php | 76 ------------------- packages/app/src/Pages/Page.php | 29 +++++++ packages/app/src/Resources/Pages/Page.php | 29 ------- .../support/src/PluginServiceProvider.php | 40 ---------- packages/tables/src/Table.php | 2 +- tests/src/Actions/ActionsServiceProvider.php | 18 ----- tests/src/Actions/TestCase.php | 7 -- tests/src/AdminFilamentProvider.php | 14 ++++ .../GlobalSearchServiceProvider.php | 18 ----- .../src/App/GlobalSearch/GlobalSearchTest.php | 2 +- tests/src/App/GlobalSearch/TestCase.php | 7 -- .../App/Navigation/NavigationBuilderTest.php | 4 +- .../Navigation/NavigationServiceProvider.php | 32 -------- tests/src/App/Navigation/NavigationTest.php | 30 +------- tests/src/App/Navigation/TestCase.php | 7 -- tests/src/App/Pages/PagesServiceProvider.php | 18 ----- tests/src/App/Pages/TestCase.php | 7 -- .../RenderHooksServiceProvider.php | 10 --- tests/src/App/RenderHooks/RenderHooksTest.php | 4 +- tests/src/App/RenderHooks/TestCase.php | 7 -- .../App/Resources/Pages/EditRecordTest.php | 6 -- .../App/Resources/Pages/ViewRecordTest.php | 6 -- .../Resources/ResourcesServiceProvider.php | 20 ----- tests/src/App/Resources/TestCase.php | 7 -- tests/src/TestCase.php | 2 - 29 files changed, 86 insertions(+), 363 deletions(-) create mode 100644 packages/app/src/Contracts/Plugin.php delete mode 100644 tests/src/Actions/ActionsServiceProvider.php delete mode 100644 tests/src/App/GlobalSearch/GlobalSearchServiceProvider.php delete mode 100644 tests/src/App/Navigation/NavigationServiceProvider.php delete mode 100644 tests/src/App/Pages/PagesServiceProvider.php delete mode 100644 tests/src/App/RenderHooks/RenderHooksServiceProvider.php delete mode 100644 tests/src/App/Resources/ResourcesServiceProvider.php diff --git a/bootstrap/app.php b/bootstrap/app.php index af6dd508e4..f6547cfa27 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -8,7 +8,6 @@ use Filament\SpatieLaravelTranslatablePluginServiceProvider; use Filament\Support\SupportServiceProvider; use Filament\Tables\TablesServiceProvider; use Filament\Tests\AdminFilamentProvider; -use Filament\Tests\App\Navigation\NavigationServiceProvider; use Filament\Widgets\WidgetsServiceProvider; use Livewire\LivewireServiceProvider; use Orchestra\Testbench\Concerns\CreatesApplication; @@ -30,7 +29,6 @@ $app->register(LivewireServiceProvider::class); $app->register(FilamentServiceProvider::class); $app->register(ActionsServiceProvider::class); $app->register(FormsServiceProvider::class); -$app->register(NavigationServiceProvider::class); $app->register(NotificationsServiceProvider::class); $app->register(SpatieLaravelTranslatablePluginServiceProvider::class); $app->register(SupportServiceProvider::class); diff --git a/packages/app/src/Context.php b/packages/app/src/Context.php index 98b1dd606a..52d6e05d35 100644 --- a/packages/app/src/Context.php +++ b/packages/app/src/Context.php @@ -5,6 +5,7 @@ namespace Filament; use Closure; use Exception; use Filament\AvatarProviders\UiAvatarsProvider; +use Filament\Contracts\Plugin; use Filament\Facades\Filament; use Filament\GlobalSearch\Contracts\GlobalSearchProvider; use Filament\GlobalSearch\DefaultGlobalSearchProvider; @@ -118,6 +119,8 @@ class Context protected ?string $googleFonts = 'https://fonts.googleapis.com/css2?family=DM+Sans:ital,wght@0,400;0,500;0,700;1,400;1,500;1,700&display=swap'; + protected array $plugins = []; + public function default(bool $condition = true): static { $this->isDefault = $condition; @@ -146,6 +149,10 @@ class Context public function boot(): void { + foreach ($this->plugins as $plugin) { + $plugin->register($this); + } + $this->registerLivewireComponents(); } @@ -406,6 +413,23 @@ class Context return $this; } + public function plugin(Plugin $plugin): static + { + $this->plugins[] = $plugin; + + return $this; + } + + public function plugins(array $plugins): static + { + $this->plugins = array_merge( + $this->plugins, + $plugins, + ); + + return $this; + } + public function hasRoutableTenancy(): bool { /** @var EloquentUserProvider $userProvider */ diff --git a/packages/app/src/Contracts/Plugin.php b/packages/app/src/Contracts/Plugin.php new file mode 100644 index 0000000000..fdfda1d7f4 --- /dev/null +++ b/packages/app/src/Contracts/Plugin.php @@ -0,0 +1,10 @@ +getCurrentContext()->boot(); } - public function navigation(Closure $builder, ?string $context = null): void - { - $this->getContext($context)->navigation($builder); - } - public function buildNavigation(): array { return $this->getCurrentContext()->buildNavigation(); } - public function globalSearchProvider(string $provider, ?string $context = null): void - { - $this->getContext($context)->globalSearchProvider($provider); - } - public function mountNavigation(): void { $this->getCurrentContext()->mountNavigation(); @@ -68,72 +58,6 @@ class FilamentManager } } - public function registerNavigationGroups(array $groups, ?string $context = null): void - { - $this->getContext($context)->navigationGroups($groups); - } - - public function registerNavigationItems(array $items, ?string $context = null): void - { - $this->getContext($context)->navigationItems($items); - } - - public function registerPages(array $pages, ?string $context = null): void - { - $this->getContext($context)->pages($pages); - } - - public function registerRenderHook(string $name, Closure $callback, ?string $context = null): void - { - $this->getContext($context)->renderHook($name, $callback); - } - - public function registerResources(array $resources, ?string $context = null): void - { - $this->getContext($context)->resources($resources); - } - - public function registerTheme(string | Htmlable | null $theme, ?string $context = null): void - { - $this->getContext($context)->theme($theme); - } - - /** - * @deprecated Use `registerTheme()` instead. - */ - public function registerThemeUrl(string | Htmlable | null $theme): void - { - $this->registerTheme($theme); - } - - /** - * @deprecated Use `registerTheme()` instead. - */ - public function registerThemeLink(string | Htmlable | null $theme): void - { - $this->registerTheme($theme); - } - - public function registerTenantMenuItems(array $items, ?string $context = null): void - { - $this->getContext($context)->tenantMenuItems($items); - } - - public function registerUserMenuItems(array $items, ?string $context = null): void - { - $this->getContext($context)->userMenuItems($items); - } - - public function registerWidgets(array $widgets, ?string $context = null): void - { - $this->getContext($context)->widgets($widgets); - } - - public function pushMeta(array $meta, ?string $context = null): void - { - $this->getContext($context)->meta($meta); - } - public function favicon(?string $url): void { $this->favicon = $url; diff --git a/packages/app/src/Pages/Page.php b/packages/app/src/Pages/Page.php index ee3a380af8..363e73618d 100644 --- a/packages/app/src/Pages/Page.php +++ b/packages/app/src/Pages/Page.php @@ -52,6 +52,10 @@ abstract class Page extends Component implements HasActions, RendersFormComponen protected ?string $maxContentWidth = null; + public static string $formActionsAlignment = 'left'; + + public static bool $hasInlineFormLabels = false; + public static function registerNavigationItems(): void { if (! static::shouldRegisterNavigation()) { @@ -279,4 +283,29 @@ abstract class Page extends Component implements HasActions, RendersFormComponen { throw new Halt(); } + + public static function alignFormActionsLeft(): void + { + static::$formActionsAlignment = 'left'; + } + + public static function alignFormActionsCenter(): void + { + static::$formActionsAlignment = 'center'; + } + + public static function alignFormActionsRight(): void + { + static::$formActionsAlignment = 'right'; + } + + public function getFormActionsAlignment(): string + { + return static::$formActionsAlignment; + } + + public function hasInlineFormLabels(): bool + { + return static::$hasInlineFormLabels; + } } diff --git a/packages/app/src/Resources/Pages/Page.php b/packages/app/src/Resources/Pages/Page.php index 91d202f4cd..ef27f55f61 100644 --- a/packages/app/src/Resources/Pages/Page.php +++ b/packages/app/src/Resources/Pages/Page.php @@ -14,10 +14,6 @@ abstract class Page extends BasePage protected static string $resource; - public static string $formActionsAlignment = 'left'; - - public static bool $hasInlineFormLabels = false; - public static function route(string $path): PageRegistration { return new PageRegistration( @@ -80,29 +76,4 @@ abstract class Page extends BasePage $this->{$hook}(); } - - public static function alignFormActionsLeft(): void - { - static::$formActionsAlignment = 'left'; - } - - public static function alignFormActionsCenter(): void - { - static::$formActionsAlignment = 'center'; - } - - public static function alignFormActionsRight(): void - { - static::$formActionsAlignment = 'right'; - } - - public function getFormActionsAlignment(): string - { - return static::$formActionsAlignment; - } - - public function hasInlineFormLabels(): bool - { - return static::$hasInlineFormLabels; - } } diff --git a/packages/support/src/PluginServiceProvider.php b/packages/support/src/PluginServiceProvider.php index f7fb47c036..5690660dc5 100644 --- a/packages/support/src/PluginServiceProvider.php +++ b/packages/support/src/PluginServiceProvider.php @@ -18,12 +18,6 @@ abstract class PluginServiceProvider extends PackageServiceProvider protected string $context = 'default'; - protected array $pages = []; - - protected array $resources = []; - - protected array $widgets = []; - public function configurePackage(Package $package): void { $this->packageConfiguring($package); @@ -66,20 +60,6 @@ abstract class PluginServiceProvider extends PackageServiceProvider } }); - $this->app->afterResolving('filament', function () { - Filament::registerPages($this->getPages(), $this->getContext()); - Filament::registerResources($this->getResources(), $this->getContext()); - Filament::registerWidgets($this->getWidgets(), $this->getContext()); - - Filament::serving(function () { - if (Filament::getCurrentContext()->getId() !== $this->getContext()) { - return; - } - - Filament::registerUserMenuItems($this->getUserMenuItems(), $this->getContext()); - }); - }); - $this->app->resolving(AssetManager::class, function () { FilamentAsset::register($this->getAssets(), $this->getAssetPackage()); FilamentAsset::registerScriptData($this->getScriptData(), $this->getAssetPackage()); @@ -125,16 +105,6 @@ abstract class PluginServiceProvider extends PackageServiceProvider return []; } - protected function getPages(): array - { - return $this->pages; - } - - protected function getResources(): array - { - return $this->resources; - } - protected function getRoutes(): array { return []; @@ -145,16 +115,6 @@ abstract class PluginServiceProvider extends PackageServiceProvider return []; } - protected function getUserMenuItems(): array - { - return []; - } - - protected function getWidgets(): array - { - return $this->widgets; - } - protected function registerMacros(): void { } diff --git a/packages/tables/src/Table.php b/packages/tables/src/Table.php index e4b8d6421f..416745dd78 100644 --- a/packages/tables/src/Table.php +++ b/packages/tables/src/Table.php @@ -73,7 +73,7 @@ class Table extends ViewComponent protected array | Closure | null $contentGrid = null; - protected int | string | Closure | null $defaultPaginationPageOption = null; + protected int | string | Closure | null $defaultPaginationPageOption = 10; protected string | Closure | null $defaultSortColumn = null; diff --git a/tests/src/Actions/ActionsServiceProvider.php b/tests/src/Actions/ActionsServiceProvider.php deleted file mode 100644 index 78cfaf9428..0000000000 --- a/tests/src/Actions/ActionsServiceProvider.php +++ /dev/null @@ -1,18 +0,0 @@ -actingAs(User::factory()->create()); } - - protected function getPackageProviders($app): array - { - return array_merge(parent::getPackageProviders($app), [ - ActionsServiceProvider::class, - ]); - } } diff --git a/tests/src/AdminFilamentProvider.php b/tests/src/AdminFilamentProvider.php index 17a9fa4cf4..89fbcef878 100644 --- a/tests/src/AdminFilamentProvider.php +++ b/tests/src/AdminFilamentProvider.php @@ -8,6 +8,12 @@ use Filament\Http\Middleware\Authenticate; use Filament\Http\Middleware\DispatchServingFilamentEvent; use Filament\Http\Middleware\MirrorConfigToSubpackages; use Filament\Pages; +use Filament\Tests\Actions\Fixtures\Pages\Actions; +use Filament\Tests\App\Fixtures\Pages\Settings; +use Filament\Tests\App\Fixtures\Resources\PostCategoryResource; +use Filament\Tests\App\Fixtures\Resources\PostResource; +use Filament\Tests\App\Fixtures\Resources\ProductResource; +use Filament\Tests\App\Fixtures\Resources\UserResource; use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse; use Illuminate\Cookie\Middleware\EncryptCookies; use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken; @@ -24,8 +30,16 @@ class AdminFilamentProvider extends ContextProvider ->default() ->id('admin') ->loginPage() + ->resources([ + PostResource::class, + PostCategoryResource::class, + ProductResource::class, + UserResource::class, + ]) ->pages([ Pages\Dashboard::class, + Actions::class, + Settings::class, ]) ->middleware([ EncryptCookies::class, diff --git a/tests/src/App/GlobalSearch/GlobalSearchServiceProvider.php b/tests/src/App/GlobalSearch/GlobalSearchServiceProvider.php deleted file mode 100644 index 3a62bb78ad..0000000000 --- a/tests/src/App/GlobalSearch/GlobalSearchServiceProvider.php +++ /dev/null @@ -1,18 +0,0 @@ -globalSearchProvider(CustomSearchProvider::class); livewire(GlobalSearch::class) ->set('search', 'foo') diff --git a/tests/src/App/GlobalSearch/TestCase.php b/tests/src/App/GlobalSearch/TestCase.php index 4f9422b402..90a6637f11 100644 --- a/tests/src/App/GlobalSearch/TestCase.php +++ b/tests/src/App/GlobalSearch/TestCase.php @@ -13,11 +13,4 @@ class TestCase extends BaseTestCase $this->actingAs(User::factory()->create()); } - - protected function getPackageProviders($app): array - { - return array_merge(parent::getPackageProviders($app), [ - GlobalSearchServiceProvider::class, - ]); - } } diff --git a/tests/src/App/Navigation/NavigationBuilderTest.php b/tests/src/App/Navigation/NavigationBuilderTest.php index 8a990f9394..07d8fb1075 100644 --- a/tests/src/App/Navigation/NavigationBuilderTest.php +++ b/tests/src/App/Navigation/NavigationBuilderTest.php @@ -15,7 +15,7 @@ use Filament\Tests\App\Navigation\TestCase; uses(TestCase::class); it('can register navigation', function () { - Filament::navigation(function (NavigationBuilder $navigation): NavigationBuilder { + Filament::getCurrentContext()->navigation(function (NavigationBuilder $navigation): NavigationBuilder { return $navigation ->items([ ...Dashboard::getNavigationItems(), @@ -80,7 +80,7 @@ it('can register navigation', function () { }); it('can register navigation groups individually', function () { - Filament::navigation(function (NavigationBuilder $navigation): NavigationBuilder { + Filament::getCurrentContext()->navigation(function (NavigationBuilder $navigation): NavigationBuilder { return $navigation ->group('Blog', [ ...PostResource::getNavigationItems(), diff --git a/tests/src/App/Navigation/NavigationServiceProvider.php b/tests/src/App/Navigation/NavigationServiceProvider.php deleted file mode 100644 index 6c886f01a8..0000000000 --- a/tests/src/App/Navigation/NavigationServiceProvider.php +++ /dev/null @@ -1,32 +0,0 @@ -toBeInstanceOf(NavigationGroup::class) ->getLabel()->toBeNull() ->getItems() - ->sequence( - fn ($item) => $item - ->getLabel()->toBe('Dashboard') - ->getIcon()->toBe('heroicon-o-home'), - fn ($item) => $item - ->getLabel()->toBe('Users') - ->getIcon()->toBe('heroicon-o-user'), - fn ($item) => $item - ->getLabel()->toBe('Settings') - ->getIcon()->toBe('heroicon-o-cog-6-tooth'), - ) ->each->toBeInstanceOf(NavigationItem::class), fn ($group) => $group ->toBeInstanceOf(NavigationGroup::class) ->getLabel()->toBe('Blog') ->getItems() - ->sequence( - fn ($item) => $item - ->getLabel()->toBe('Posts') - ->getIcon()->toBe('heroicon-o-document-text'), - fn ($item) => $item - ->getLabel()->toBe('Post Categories') - ->getIcon()->toBe('heroicon-o-rectangle-stack'), - ) ->each->toBeInstanceOf(NavigationItem::class), fn ($group) => $group ->toBeInstanceOf(NavigationGroup::class) ->getLabel()->toBe('Shop') ->getItems() - ->sequence( - fn ($item) => $item - ->getLabel()->toBe('Products') - ->getIcon()->toBe('heroicon-o-shopping-bag'), - ) ->each->toBeInstanceOf(NavigationItem::class), ); }); it('can reorder navigation groups by registering them', function () { - Filament::registerNavigationGroups([ + Filament::getCurrentContext()->navigationGroups([ NavigationGroup::make()->label('Shop'), NavigationGroup::make()->label('Blog'), ]); @@ -73,7 +49,7 @@ it('can reorder navigation groups by registering them', function () { }); it('can reorder navigation groups by registering them with different labels', function () { - Filament::registerNavigationGroups([ + Filament::getCurrentContext()->navigationGroups([ 'Shop' => NavigationGroup::make()->label('Store'), 'Blog' => NavigationGroup::make()->label('Posts'), ]); @@ -93,7 +69,7 @@ it('can reorder navigation groups by registering them with different labels', fu }); it('can reorder navigation groups by registering their labels', function () { - Filament::registerNavigationGroups([ + Filament::getCurrentContext()->navigationGroups([ 'Shop', 'Blog', ]); diff --git a/tests/src/App/Navigation/TestCase.php b/tests/src/App/Navigation/TestCase.php index 93d7c677c9..96d20579dc 100644 --- a/tests/src/App/Navigation/TestCase.php +++ b/tests/src/App/Navigation/TestCase.php @@ -13,11 +13,4 @@ class TestCase extends BaseTestCase $this->actingAs(User::factory()->create()); } - - protected function getPackageProviders($app): array - { - return array_merge(parent::getPackageProviders($app), [ - NavigationServiceProvider::class, - ]); - } } diff --git a/tests/src/App/Pages/PagesServiceProvider.php b/tests/src/App/Pages/PagesServiceProvider.php deleted file mode 100644 index 4b02f75b92..0000000000 --- a/tests/src/App/Pages/PagesServiceProvider.php +++ /dev/null @@ -1,18 +0,0 @@ -actingAs(User::factory()->create()); } - - protected function getPackageProviders($app): array - { - return array_merge(parent::getPackageProviders($app), [ - PagesServiceProvider::class, - ]); - } } diff --git a/tests/src/App/RenderHooks/RenderHooksServiceProvider.php b/tests/src/App/RenderHooks/RenderHooksServiceProvider.php deleted file mode 100644 index f4e7689e3f..0000000000 --- a/tests/src/App/RenderHooks/RenderHooksServiceProvider.php +++ /dev/null @@ -1,10 +0,0 @@ -renderHook('foo', function (): string { return Blade::render('bar'); }); @@ -18,7 +18,7 @@ test('render hooks can be registered', function () { }); test('render hooks can render view files', function () { - Filament::registerRenderHook('view-foo', function (): Illuminate\Contracts\View\View { + Filament::getCurrentContext()->renderHook('view-foo', function (): Illuminate\Contracts\View\View { return view('app.fixtures.pages.render-hooks.foo'); }); diff --git a/tests/src/App/RenderHooks/TestCase.php b/tests/src/App/RenderHooks/TestCase.php index 7019c388b4..ca524a3712 100644 --- a/tests/src/App/RenderHooks/TestCase.php +++ b/tests/src/App/RenderHooks/TestCase.php @@ -13,11 +13,4 @@ class TestCase extends BaseTestCase $this->actingAs(User::factory()->create()); } - - protected function getPackageProviders($app): array - { - return array_merge(parent::getPackageProviders($app), [ - RenderHooksServiceProvider::class, - ]); - } } diff --git a/tests/src/App/Resources/Pages/EditRecordTest.php b/tests/src/App/Resources/Pages/EditRecordTest.php index fd209e3e44..df2c84ccff 100644 --- a/tests/src/App/Resources/Pages/EditRecordTest.php +++ b/tests/src/App/Resources/Pages/EditRecordTest.php @@ -10,12 +10,6 @@ use function Pest\Livewire\livewire; uses(TestCase::class); -beforeEach(function () { - Filament::registerResources([ - PostResource::class, - ]); -}); - it('can render page', function () { $this->get(PostResource::getUrl('edit', [ 'record' => Post::factory()->create(), diff --git a/tests/src/App/Resources/Pages/ViewRecordTest.php b/tests/src/App/Resources/Pages/ViewRecordTest.php index 8f8f21bc05..609b74a2b1 100644 --- a/tests/src/App/Resources/Pages/ViewRecordTest.php +++ b/tests/src/App/Resources/Pages/ViewRecordTest.php @@ -9,12 +9,6 @@ use function Pest\Livewire\livewire; uses(TestCase::class); -beforeEach(function () { - Filament::registerResources([ - PostResource::class, - ]); -}); - it('can render page', function () { $this->get(PostResource::getUrl('view', [ 'record' => Post::factory()->create(), diff --git a/tests/src/App/Resources/ResourcesServiceProvider.php b/tests/src/App/Resources/ResourcesServiceProvider.php deleted file mode 100644 index 0112927b4a..0000000000 --- a/tests/src/App/Resources/ResourcesServiceProvider.php +++ /dev/null @@ -1,20 +0,0 @@ -actingAs(User::factory()->create()); } - - protected function getPackageProviders($app): array - { - return array_merge(parent::getPackageProviders($app), [ - ResourcesServiceProvider::class, - ]); - } } diff --git a/tests/src/TestCase.php b/tests/src/TestCase.php index 1de44606bf..e4d2b9eda2 100644 --- a/tests/src/TestCase.php +++ b/tests/src/TestCase.php @@ -12,7 +12,6 @@ use Filament\SpatieLaravelSettingsPluginServiceProvider; use Filament\SpatieLaravelTranslatablePluginServiceProvider; use Filament\Support\SupportServiceProvider; use Filament\Tables\TablesServiceProvider; -use Filament\Tests\App\Navigation\NavigationServiceProvider; use Filament\Tests\Models\User; use Filament\Widgets\WidgetsServiceProvider; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; @@ -34,7 +33,6 @@ abstract class TestCase extends BaseTestCase FilamentServiceProvider::class, FormsServiceProvider::class, LivewireServiceProvider::class, - NavigationServiceProvider::class, NotificationsServiceProvider::class, SpatieLaravelSettingsPluginServiceProvider::class, SpatieLaravelTranslatablePluginServiceProvider::class,