diff --git a/src/Laravel/src/Commands/InstallCommand.php b/src/Laravel/src/Commands/InstallCommand.php index be9f9c665..6bd569b01 100644 --- a/src/Laravel/src/Commands/InstallCommand.php +++ b/src/Laravel/src/Commands/InstallCommand.php @@ -72,7 +72,7 @@ public function handle(): int } if (! $this->testsMode) { - confirm('Can you quickly star our GitHub repository? 🙏🏻', true); + confirm('Can you quickly star our GitHub repository? 🙏🏻'); $this->components->bulletList([ 'Star or contribute to MoonShine: https://github.com/moonshine-software/moonshine', @@ -259,7 +259,7 @@ protected function initDirectories(): void ); } - $this->makeDir($this->getDirectory() . '/Resources'); + $this->makeDir($this->getDirectory('/Resources')); $this->components->task('Resources directory created'); } @@ -283,10 +283,14 @@ protected function initDashboard(): void $this->components->task('Dashboard created'); } - protected function initLayout(): void { - $compact = $this->confirmAction('Want to use a minimalist theme?', skipOption: 'default-layout', autoEnable: $this->testsMode, default: false); + $compact = $this->confirmAction( + 'Want to use a minimalist theme?', + skipOption: 'default-layout', + autoEnable: $this->testsMode, + default: false, + ); $this->call(MakeLayoutCommand::class, [ 'className' => 'MoonShineLayout', @@ -323,7 +327,10 @@ private function confirmAction( private function registerServiceProvider(): void { - if (method_exists(ServiceProvider::class, 'addProviderToBootstrapFile') && file_exists(base_path('bootstrap/app.php'))) { + if ( + method_exists(ServiceProvider::class, 'addProviderToBootstrapFile') + && file_exists(base_path('bootstrap/app.php')) + ) { // @phpstan-ignore-next-line ServiceProvider::addProviderToBootstrapFile(\App\Providers\MoonShineServiceProvider::class); diff --git a/src/Laravel/src/Commands/MakeApplyCommand.php b/src/Laravel/src/Commands/MakeApplyCommand.php index ba65fdc02..00289f80a 100644 --- a/src/Laravel/src/Commands/MakeApplyCommand.php +++ b/src/Laravel/src/Commands/MakeApplyCommand.php @@ -6,8 +6,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; -use function Laravel\Prompts\outro; -use function Laravel\Prompts\text; +use function Laravel\Prompts\{outro, text}; use Symfony\Component\Console\Attribute\AsCommand; @@ -28,11 +27,10 @@ public function handle(): int required: true ); - $apply = $this->getDirectory() . "/Applies/$className.php"; + $appliesDir = $this->getDirectory('/Applies'); + $apply = "$appliesDir/$className.php"; - if (! is_dir($this->getDirectory() . '/Applies')) { - $this->makeDir($this->getDirectory() . '/Applies'); - } + $this->makeDirectory($appliesDir); $this->copyStub('Apply', $apply, [ '{namespace}' => moonshineConfig()->getNamespace('\Applies'), @@ -40,11 +38,7 @@ public function handle(): int ]); outro( - "$className was created: " . str_replace( - base_path(), - '', - $apply - ) + "$className was created: " . $this->getRelativePath($apply) ); return self::SUCCESS; diff --git a/src/Laravel/src/Commands/MakeComponentCommand.php b/src/Laravel/src/Commands/MakeComponentCommand.php index 5dc988764..6fd70133b 100644 --- a/src/Laravel/src/Commands/MakeComponentCommand.php +++ b/src/Laravel/src/Commands/MakeComponentCommand.php @@ -6,8 +6,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; -use function Laravel\Prompts\outro; -use function Laravel\Prompts\text; +use function Laravel\Prompts\{outro, text}; use Symfony\Component\Console\Attribute\AsCommand; @@ -41,42 +40,33 @@ public function handle(): int required: true ); - $component = $this->getDirectory() . "/Components/$className.php"; + $componentsDir = $this->getDirectory('/Components'); + $componentPath = "$componentsDir/$className.php"; - if (! is_dir($this->getDirectory() . '/Components')) { - $this->makeDir($this->getDirectory() . '/Components'); - } + $this->makeDirectory($componentsDir); $view = str_replace('.blade.php', '', $view); $viewPath = resource_path('views/' . str_replace('.', DIRECTORY_SEPARATOR, $view)); $viewPath .= '.blade.php'; - if (! is_dir(\dirname($viewPath))) { - $this->makeDir(\dirname($viewPath)); - } + $this->makeDirectory( + \dirname($viewPath) + ); $this->copyStub('view', $viewPath); - $this->copyStub('Component', $component, [ + $this->copyStub('Component', $componentPath, [ '{namespace}' => moonshineConfig()->getNamespace('\Components'), '{view}' => $view, 'DummyClass' => $className, ]); outro( - "$className was created: " . str_replace( - base_path(), - '', - $component - ) + "$className was created: " . $this->getRelativePath($componentPath) ); outro( - "View was created: " . str_replace( - base_path(), - '', - $viewPath - ) + "View was created: " . $this->getRelativePath($viewPath) ); return self::SUCCESS; diff --git a/src/Laravel/src/Commands/MakeControllerCommand.php b/src/Laravel/src/Commands/MakeControllerCommand.php index 08400eb2b..0709254f6 100644 --- a/src/Laravel/src/Commands/MakeControllerCommand.php +++ b/src/Laravel/src/Commands/MakeControllerCommand.php @@ -6,15 +6,14 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; -use function Laravel\Prompts\outro; -use function Laravel\Prompts\text; +use function Laravel\Prompts\{outro, text}; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'moonshine:controller')] class MakeControllerCommand extends MoonShineCommand { - protected $signature = 'moonshine:controller {className?}'; + protected $signature = 'moonshine:controller {name?}'; protected $description = 'Create controller'; @@ -23,28 +22,23 @@ class MakeControllerCommand extends MoonShineCommand */ public function handle(): int { - $className = $this->argument('className') ?? text( + $name = $this->argument('name') ?? text( 'Class name', required: true ); - $controller = $this->getDirectory() . "/Controllers/$className.php"; + $controllersDir = $this->getDirectory('/Controllers'); + $controllerPath = "$controllersDir/$name.php"; - if (! is_dir($this->getDirectory() . '/Controllers')) { - $this->makeDir($this->getDirectory() . '/Controllers'); - } + $this->makeDirectory($controllersDir); - $this->copyStub('Controller', $controller, [ + $this->copyStub('Controller', $controllerPath, [ '{namespace}' => moonshineConfig()->getNamespace('\Controllers'), - 'DummyClass' => $className, + 'DummyClass' => $name, ]); outro( - "$className was created: " . str_replace( - base_path(), - '', - $controller - ) + "$name was created: " . $this->getRelativePath($controllerPath) ); return self::SUCCESS; diff --git a/src/Laravel/src/Commands/MakeFieldCommand.php b/src/Laravel/src/Commands/MakeFieldCommand.php index 6f61c9c6d..bd7dc0d94 100644 --- a/src/Laravel/src/Commands/MakeFieldCommand.php +++ b/src/Laravel/src/Commands/MakeFieldCommand.php @@ -7,9 +7,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Support\Facades\File; -use function Laravel\Prompts\outro; -use function Laravel\Prompts\select; -use function Laravel\Prompts\text; +use function Laravel\Prompts\{outro, select, text}; use MoonShine\UI\Fields\Field; use Symfony\Component\Console\Attribute\AsCommand; @@ -61,13 +59,12 @@ public function handle(): int Field::class ); - $field = $this->getDirectory() . "/Fields/$className.php"; + $fieldsDir = $this->getDirectory('/Fields'); + $fieldPath = "$fieldsDir/$className.php"; - if (! is_dir($this->getDirectory() . '/Fields')) { - $this->makeDir($this->getDirectory() . '/Fields'); - } + $this->makeDirectory($fieldsDir); - $this->copyStub('Field', $field, [ + $this->copyStub('Field', $fieldPath, [ '{namespace}' => moonshineConfig()->getNamespace('\Fields'), '{view}' => $view, '{extend}' => $extends, @@ -79,26 +76,18 @@ public function handle(): int $viewPath = resource_path('views/' . str_replace('.', DIRECTORY_SEPARATOR, $view)); $viewPath .= '.blade.php'; - if (! is_dir(\dirname($viewPath))) { - $this->makeDir(\dirname($viewPath)); - } + $this->makeDirectory( + \dirname($viewPath) + ); $this->copyStub('view', $viewPath); outro( - "$className was created: " . str_replace( - base_path(), - '', - $field - ) + "$className was created: " . $this->getRelativePath($fieldPath) ); outro( - "View was created: " . str_replace( - base_path(), - '', - $viewPath - ) + "View was created: " . $this->getRelativePath($viewPath) ); return self::SUCCESS; diff --git a/src/Laravel/src/Commands/MakeHandlerCommand.php b/src/Laravel/src/Commands/MakeHandlerCommand.php index 83a52aa76..e62ae8409 100644 --- a/src/Laravel/src/Commands/MakeHandlerCommand.php +++ b/src/Laravel/src/Commands/MakeHandlerCommand.php @@ -27,23 +27,18 @@ public function handle(): int required: true ); - $path = $this->getDirectory() . "/Handlers/$className.php"; + $handlersDir = $this->getDirectory() . '/Handlers'; + $handlerPath = "$handlersDir/$className.php"; - if (! is_dir($this->getDirectory() . '/Handlers')) { - $this->makeDir($this->getDirectory() . '/Handlers'); - } + $this->makeDirectory($handlersDir); - $this->copyStub('Handler', $path, [ + $this->copyStub('Handler', $handlerPath, [ '{namespace}' => moonshineConfig()->getNamespace('\Handlers'), 'DummyHandler' => $className, ]); outro( - "$className was created: " . str_replace( - base_path(), - '', - $path - ) + "$className was created: " . $this->getRelativePath($handlerPath) ); return self::SUCCESS; diff --git a/src/Laravel/src/Commands/MakeLayoutCommand.php b/src/Laravel/src/Commands/MakeLayoutCommand.php index 592a26e05..988d4673f 100644 --- a/src/Laravel/src/Commands/MakeLayoutCommand.php +++ b/src/Laravel/src/Commands/MakeLayoutCommand.php @@ -6,9 +6,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; -use function Laravel\Prompts\confirm; -use function Laravel\Prompts\outro; -use function Laravel\Prompts\text; +use function Laravel\Prompts\{confirm, outro, text}; use Symfony\Component\Console\Attribute\AsCommand; @@ -36,18 +34,17 @@ public function handle(): int $dir = 'Layouts'; } - $layout = $this->getDirectory() . "/$dir/$className.php"; + $layoutsDir = $this->getDirectory() . "/$dir"; + $layoutPath = "$layoutsDir/$className.php"; - if (! is_dir($this->getDirectory() . "/$dir")) { - $this->makeDir($this->getDirectory() . "/$dir"); - } + $this->makeDirectory($layoutsDir); $compact = ! $this->option('full') && ($this->option('compact') || confirm('Want to use a minimalist theme?')); $extendClassName = $compact ? 'CompactLayout' : 'AppLayout'; $extends = "MoonShine\Laravel\Layouts\\$extendClassName"; - $this->copyStub('Layout', $layout, [ + $this->copyStub('Layout', $layoutPath, [ '{namespace}' => moonshineConfig()->getNamespace('\\' . str_replace('/', '\\', $dir)), '{extend}' => $extends, '{extendShort}' => class_basename($extends), @@ -55,11 +52,7 @@ public function handle(): int ]); outro( - "$className was created: " . str_replace( - base_path(), - '', - $layout - ) + "$className was created: " . $this->getRelativePath($layoutPath) ); if ($this->option('default') || confirm('Use the default template in the system?')) { diff --git a/src/Laravel/src/Commands/MakePageCommand.php b/src/Laravel/src/Commands/MakePageCommand.php index ea66c7537..c57a5154a 100644 --- a/src/Laravel/src/Commands/MakePageCommand.php +++ b/src/Laravel/src/Commands/MakePageCommand.php @@ -6,9 +6,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; -use function Laravel\Prompts\outro; -use function Laravel\Prompts\select; -use function Laravel\Prompts\text; +use function Laravel\Prompts\{outro, select, text}; use Symfony\Component\Console\Attribute\AsCommand; @@ -85,13 +83,12 @@ private function makePage( $dir = \is_null($dir) ? 'Pages' : $dir; $extends = $extends === null || $extends === '' || $extends === '0' ? 'Page' : $extends; - $page = $this->getDirectory() . "/$dir/$className.php"; + $pagesDir = $this->getDirectory("/$dir"); + $pagePath = "$pagesDir/$className.php"; - if (! is_dir($this->getDirectory() . "/$dir")) { - $this->makeDir($this->getDirectory() . "/$dir"); - } + $this->makeDirectory($pagesDir); - $this->copyStub($stub, $page, [ + $this->copyStub($stub, $pagePath, [ '{namespace}' => moonshineConfig()->getNamespace('\\' . str_replace('/', '\\', $dir)), 'DummyPage' => $className, 'DummyTitle' => $className, @@ -99,11 +96,7 @@ private function makePage( ]); outro( - "$className was created: " . str_replace( - base_path(), - '', - $page - ) + "$className was created: " . $this->getRelativePath($pagePath) ); if (! $this->option('without-register')) { diff --git a/src/Laravel/src/Commands/MakePolicyCommand.php b/src/Laravel/src/Commands/MakePolicyCommand.php index 741ddb2ba..ec1986c09 100644 --- a/src/Laravel/src/Commands/MakePolicyCommand.php +++ b/src/Laravel/src/Commands/MakePolicyCommand.php @@ -6,8 +6,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; -use function Laravel\Prompts\outro; -use function Laravel\Prompts\suggest; +use function Laravel\Prompts\{outro, suggest}; use MoonShine\Laravel\MoonShineAuth; use Symfony\Component\Console\Attribute\AsCommand; @@ -27,27 +26,29 @@ public function handle(): int { $modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path(); - if (! $className = $this->argument('className')) { - $className = suggest( - label: 'Model', - options: collect((new Finder())->files()->depth(0)->in($modelPath)) - ->map(static fn ($file) => $file->getBasename('.php')) - ->values() - ->all(), - required: true, - ); - } + $className = $this->argument('className') ?? suggest( + label: 'Model', + options: collect((new Finder())->files()->depth(0)->in($modelPath)) + ->map(static fn ($file) => $file->getBasename('.php')) + ->values() + ->all(), + required: true, + ); + + $className = str($className) + ->ucfirst() + ->remove('policy', false) + ->value(); $model = $this->qualifyModel($className); - $className = class_basename($model) . "Policy"; + $className = class_basename($model) . 'Policy'; - $path = app_path("/Policies/$className.php"); + $policiesDir = app_path('/Policies'); + $policyPath = "$policiesDir/$className.php"; - if (! is_dir(app_path('/Policies'))) { - $this->makeDir(app_path('/Policies')); - } + $this->makeDirectory($policiesDir); - $this->copyStub('Policy', $path, [ + $this->copyStub('Policy', $policyPath, [ 'DummyClass' => $className, '{model-namespace}' => $model, '{model}' => class_basename($model), @@ -56,11 +57,7 @@ public function handle(): int ]); outro( - "$className was created: " . str_replace( - base_path(), - '', - $path - ) + "$className was created: " . $this->getRelativePath($policyPath) ); return self::SUCCESS; diff --git a/src/Laravel/src/Commands/MakeResourceCommand.php b/src/Laravel/src/Commands/MakeResourceCommand.php index 9b6b06e62..655757744 100644 --- a/src/Laravel/src/Commands/MakeResourceCommand.php +++ b/src/Laravel/src/Commands/MakeResourceCommand.php @@ -22,28 +22,24 @@ class MakeResourceCommand extends MoonShineCommand */ public function handle(): int { - $name = str( - text( - 'Name', - 'ArticleResource', - $this->argument('name') ?? '', - required: true, - ) + $name = $this->argument('name') ?? text( + 'Resource name', + 'ArticleResource', + required: true, ); - $name = $name->ucfirst() + $name = str($name) + ->ucfirst() ->remove('resource', false) ->value(); $model = $this->qualifyModel($this->option('model') ?? $name); $title = $this->option('title') ?? str($name)->singular()->plural()->value(); - $moonshineDir = $this->getDirectory(); + $resourcesDir = $this->getDirectory('/Resources'); - $resource = "$moonshineDir/Resources/{$name}Resource.php"; + $resource = "$resourcesDir/{$name}Resource.php"; - if (! is_dir("$moonshineDir/Resources")) { - $this->makeDir("$moonshineDir/Resources"); - } + $this->makeDirectory($resourcesDir); $stub = select('Resource type', [ 'ModelResourceDefault' => 'Default model resource', @@ -61,7 +57,7 @@ public function handle(): int if ($this->option('test') || $this->option('pest')) { $testStub = $this->option('pest') ? 'pest' : 'test'; - $testPath = base_path('tests/Feature/') . $name . 'ResourceTest.php'; + $testPath = base_path("tests/Feature/{$name}ResourceTest.php"); $this->copyStub($testStub, $testPath, $replace); @@ -90,11 +86,7 @@ public function handle(): int $this->copyStub($stub, $resource, $replace); info( - "{$name}Resource file was created: " . str_replace( - base_path(), - '', - $resource - ) + "{$name}Resource file was created: " . $this->getRelativePath($resource) ); self::addResourceOrPageToProviderFile( diff --git a/src/Laravel/src/Commands/MakeTypeCastCommand.php b/src/Laravel/src/Commands/MakeTypeCastCommand.php index 74e8d79cd..36b0c731e 100644 --- a/src/Laravel/src/Commands/MakeTypeCastCommand.php +++ b/src/Laravel/src/Commands/MakeTypeCastCommand.php @@ -27,23 +27,18 @@ public function handle(): int required: true ); - $path = $this->getDirectory() . "/TypeCasts/$className.php"; + $typeCastsDir = $this->getDirectory('/TypeCasts'); + $typeCastPath = "$typeCastsDir/$className.php"; - if (! is_dir($this->getDirectory() . '/TypeCasts')) { - $this->makeDir($this->getDirectory() . '/TypeCasts'); - } + $this->makeDirectory($typeCastsDir); - $this->copyStub('TypeCast', $path, [ + $this->copyStub('TypeCast', $typeCastPath, [ '{namespace}' => moonshineConfig()->getNamespace('\TypeCasts'), 'DummyCast' => $className, ]); outro( - "$className was created: " . str_replace( - base_path(), - '', - $path - ) + "$className was created: " . $this->getRelativePath($typeCastPath) ); return self::SUCCESS; diff --git a/src/Laravel/src/Commands/MakeUserCommand.php b/src/Laravel/src/Commands/MakeUserCommand.php index e6ddab100..95df9efdd 100644 --- a/src/Laravel/src/Commands/MakeUserCommand.php +++ b/src/Laravel/src/Commands/MakeUserCommand.php @@ -26,7 +26,7 @@ public function handle(): int $password = $this->option('password') ?? password('Password'); if ($username && $name && $password) { - MoonShineAuth::getModel()->query()->create([ + MoonShineAuth::getModel()::query()->create([ moonshineConfig()->getUserField('username', 'email') => $username, moonshineConfig()->getUserField('name') => $name, moonshineConfig()->getUserField('password') => Hash::make($password), @@ -53,8 +53,7 @@ private function uniqueUsername(): string required: true ); - $exists = MoonShineAuth::getModel() - ->query() + $exists = MoonShineAuth::getModel()::query() ->where( moonshineConfig()->getUserField('username', 'email'), $username, diff --git a/src/Laravel/src/Commands/MoonShineCommand.php b/src/Laravel/src/Commands/MoonShineCommand.php index 899884af4..0d7a3b6a3 100644 --- a/src/Laravel/src/Commands/MoonShineCommand.php +++ b/src/Laravel/src/Commands/MoonShineCommand.php @@ -13,9 +13,21 @@ abstract class MoonShineCommand extends Command { protected string $stubsDir = __DIR__ . '/../../stubs'; - protected function getDirectory(): string + protected function getDirectory(string $path = ''): string { - return moonshineConfig()->getDir(); + return moonshineConfig()->getDir($path); + } + + protected function makeDirectory(string $path): void + { + if (! is_dir($path)) { + $this->makeDir($path); + } + } + + protected function getRelativePath(string $path): string + { + return str_replace(base_path(), '', $path); } public static function addResourceOrPageToProviderFile(string $class, bool $page = false, string $prefix = ''): void @@ -38,7 +50,7 @@ class: $prefix . $class, to: app_path('MoonShine/Layouts/MoonShineLayout.php'), isPage: $page, 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)}];"), + 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/Commands/PublishCommand.php b/src/Laravel/src/Commands/PublishCommand.php index af33abd26..3c93043a2 100644 --- a/src/Laravel/src/Commands/PublishCommand.php +++ b/src/Laravel/src/Commands/PublishCommand.php @@ -151,9 +151,7 @@ private function publishForms(): void private function publishSystemForm(string $className, string $configKey): void { - if (! is_dir($this->getDirectory() . "/Forms")) { - $this->makeDir($this->getDirectory() . "/Forms"); - } + $this->makeDirectory($this->getDirectory('/Forms')); $this->copySystemClass($className, 'Forms'); @@ -192,9 +190,7 @@ private function publishPages(): void private function publishSystemPage(string $className, string $configKey): void { - if (! is_dir($this->getDirectory() . "/Pages")) { - $this->makeDir($this->getDirectory() . "/Pages"); - } + $this->makeDirectory($this->getDirectory('/Pages')); $copyInfo = $this->copySystemClass($className, 'Pages');