From 5f140ce34d8f18761381b84518c9877aedaf4911 Mon Sep 17 00:00:00 2001 From: LT Date: Tue, 7 Jan 2025 19:16:25 +0300 Subject: [PATCH 1/3] fix: Import MenuItem on MakeResource --- src/Laravel/src/Commands/MoonShineCommand.php | 6 +++++- .../src/DependencyInjection/MoonShineConfigurator.php | 4 ++-- tests/Feature/Commands/ResourceCommandTest.php | 9 +++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Laravel/src/Commands/MoonShineCommand.php b/src/Laravel/src/Commands/MoonShineCommand.php index 0e9f5769e..642c9fe0a 100644 --- a/src/Laravel/src/Commands/MoonShineCommand.php +++ b/src/Laravel/src/Commands/MoonShineCommand.php @@ -7,6 +7,7 @@ use Closure; use Illuminate\Support\Stringable; +use MoonShine\MenuManager\MenuItem; use function Laravel\Prompts\{outro, text}; use Leeto\PackageCommand\Command; @@ -43,11 +44,14 @@ public static function addResourceOrPageToMenu(string $class, string $title, str { $namespace = rtrim($namespace, '\\'); + $reflector = new \ReflectionClass(moonshineConfig()->getLayout()); + self::addResourceOrPageTo( class: "$namespace\\$class", - to: app_path('MoonShine/Layouts/MoonShineLayout.php'), + to: $reflector->getFileName(), between: static fn (Stringable $content): Stringable => $content->betweenFirst("protected function menu(): array", '}'), replace: static fn (Stringable $content, Closure $tab): Stringable => $content->replace("];", "{$tab()}MenuItem::make('$title', $class::class),\n{$tab(2)}];"), + use: MenuItem::class ); } diff --git a/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php b/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php index 2cf481965..94b098f9b 100644 --- a/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php +++ b/src/Laravel/src/DependencyInjection/MoonShineConfigurator.php @@ -29,8 +29,8 @@ public function __construct(Repository $repository) $this->items = $repository->get('moonshine', []); $this->authorizationRules = Collection::make(); $this - ->set('dir', 'app/MoonShine') - ->set('namespace', 'App\MoonShine'); + ->set('dir', $this->items['dir'] ?? 'app/MoonShine') + ->set('namespace', $this->items['namespace'] ?? 'App\MoonShine'); } public function dir(string $dir, string $namespace): self diff --git a/tests/Feature/Commands/ResourceCommandTest.php b/tests/Feature/Commands/ResourceCommandTest.php index 574cc4cee..59c23f5e4 100644 --- a/tests/Feature/Commands/ResourceCommandTest.php +++ b/tests/Feature/Commands/ResourceCommandTest.php @@ -5,6 +5,7 @@ namespace MoonShine\Tests\Feature\Commands; use MoonShine\Laravel\Commands\MakeResourceCommand; +use MoonShine\MenuManager\MenuItem; use MoonShine\Tests\TestCase; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; @@ -19,9 +20,12 @@ final class ResourceCommandTest extends TestCase #[TestDox('it successful file created')] public function successfulCreated(): void { + $reflector = new \ReflectionClass(moonshineConfig()->getLayout()); + $name = 'DeleteMeResource'; $file = "$name.php"; $path = __DIR__ . "/../../../app/MoonShine/Resources/$file"; + $layoutPath = $reflector->getFileName(); @unlink($path); @@ -37,6 +41,11 @@ public function successfulCreated(): void ->assertSuccessful(); $this->assertFileExists($path); + + $layoutContent = file_get_contents($layoutPath); + + $this->assertStringContainsString(MenuItem::class, $layoutContent); + $this->assertStringContainsString('DeleteMeResource', $layoutContent); } #[Test] From 245cdffe0a7346512ae6ebab874f56e49b40cbe1 Mon Sep 17 00:00:00 2001 From: lee-to Date: Tue, 7 Jan 2025 16:17:19 +0000 Subject: [PATCH 2/3] [rector] Rector fixes --- src/Laravel/src/Commands/MoonShineCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Laravel/src/Commands/MoonShineCommand.php b/src/Laravel/src/Commands/MoonShineCommand.php index 642c9fe0a..a8b5a6bdc 100644 --- a/src/Laravel/src/Commands/MoonShineCommand.php +++ b/src/Laravel/src/Commands/MoonShineCommand.php @@ -4,6 +4,7 @@ namespace MoonShine\Laravel\Commands; +use ReflectionClass; use Closure; use Illuminate\Support\Stringable; @@ -44,7 +45,7 @@ public static function addResourceOrPageToMenu(string $class, string $title, str { $namespace = rtrim($namespace, '\\'); - $reflector = new \ReflectionClass(moonshineConfig()->getLayout()); + $reflector = new ReflectionClass(moonshineConfig()->getLayout()); self::addResourceOrPageTo( class: "$namespace\\$class", From 80d456720ebc9f795cdcd60c0f227ebc5da62705 Mon Sep 17 00:00:00 2001 From: lee-to Date: Tue, 7 Jan 2025 16:17:47 +0000 Subject: [PATCH 3/3] Fix styling --- src/Laravel/src/Commands/MoonShineCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Laravel/src/Commands/MoonShineCommand.php b/src/Laravel/src/Commands/MoonShineCommand.php index a8b5a6bdc..625031ef6 100644 --- a/src/Laravel/src/Commands/MoonShineCommand.php +++ b/src/Laravel/src/Commands/MoonShineCommand.php @@ -4,15 +4,15 @@ namespace MoonShine\Laravel\Commands; -use ReflectionClass; use Closure; use Illuminate\Support\Stringable; -use MoonShine\MenuManager\MenuItem; use function Laravel\Prompts\{outro, text}; use Leeto\PackageCommand\Command; use MoonShine\Laravel\Support\StubsPath; +use MoonShine\MenuManager\MenuItem; +use ReflectionClass; abstract class MoonShineCommand extends Command {