From e7cea642c3b169968baa6ea71a4bc8b34743088c Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 7 Sep 2023 13:57:12 +0100 Subject: [PATCH 1/6] Adds `make:view` Artisan command --- .../Foundation/Console/ViewMakeCommand.php | 236 ++++++++++++++++++ .../Foundation/Console/stubs/view.pest.stub | 9 + .../Foundation/Console/stubs/view.stub | 3 + .../Foundation/Console/stubs/view.test.stub | 20 ++ .../Providers/ArtisanServiceProvider.php | 2 + 5 files changed, 270 insertions(+) create mode 100644 src/Illuminate/Foundation/Console/ViewMakeCommand.php create mode 100644 src/Illuminate/Foundation/Console/stubs/view.pest.stub create mode 100644 src/Illuminate/Foundation/Console/stubs/view.stub create mode 100644 src/Illuminate/Foundation/Console/stubs/view.test.stub diff --git a/src/Illuminate/Foundation/Console/ViewMakeCommand.php b/src/Illuminate/Foundation/Console/ViewMakeCommand.php new file mode 100644 index 000000000000..d58ee15a1d0d --- /dev/null +++ b/src/Illuminate/Foundation/Console/ViewMakeCommand.php @@ -0,0 +1,236 @@ +viewPath( + $this->getNameInput().'.'.$this->option('extension'), + ); + } + + /** + * Get the stub file for the generator. + * + * @return string + */ + protected function getStub() + { + return $this->resolveStubPath( + '/stubs/view.stub', + ); + } + + /** + * Build the class with the given name. + * + * @param string $name + * @return string + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + protected function buildClass($name) + { + $contents = parent::buildClass($name); + + return str_replace( + '{{ quote }}', + Inspiring::quotes()->random(), + $contents, + ); + } + + /** + * Resolve the fully-qualified path to the stub. + * + * @param string $stub + * @return string + */ + protected function resolveStubPath($stub) + { + return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) + ? $customPath + : __DIR__.$stub; + } + + /** + * Get the namespace for the test. + * + * @return string + */ + protected function testNamespace() + { + return Str::of($this->fullyQualifiedTestName()) + ->beforeLast('\\') + ->value(); + } + + /** + * Get the class name for the test. + * + * @return string + */ + protected function testClassName() + { + return Str::of($this->fullyQualifiedTestName()) + ->afterLast('\\') + ->append('Test') + ->value(); + } + + /** + * Get the view name for the test. + * + * @return string + */ + protected function testViewName() + { + return Str::of($this->getNameInput()) + ->replace('/', '.') + ->lower() + ->value(); + } + + /** + * Get the desired view name from the input. + * + * @return string + */ + protected function getNameInput() + { + $name = trim($this->argument('name')); + + $name = str_replace(['\\', '.'], '/', $this->argument('name')); + + return $name; + } + + /** + * Get the test stub file for the generator. + * + * @return string + */ + protected function getTestStub() + { + $stubName = 'view.'.($this->option('pest') ? 'pest' : 'test').'.stub'; + + return file_exists($customPath = $this->laravel->basePath("stubs/$stubName")) + ? $customPath + : __DIR__.'/stubs/'.$stubName; + } + + /** + * Create the matching test case if requested. + * + * @param string $path + */ + protected function handleTestCreation($path): bool + { + if (! $this->option('test') && ! $this->option('pest')) { + return false; + } + + $contents = preg_replace( + ['/\{{ namespace \}}/', '/\{{ class \}}/', '/\{{ name \}}/'], + [$this->testNamespace(), $this->testClassName(), $this->testViewName()], + File::get($this->getTestStub()), + ); + + File::ensureDirectoryExists(dirname($this->getTestPath()), 0755, true); + + return File::put($this->getTestPath(), $contents); + } + + /** + * Get the destination test case path. + * + * @return string + */ + protected function getTestPath() + { + return base_path( + Str::of($this->fullyQualifiedTestName()) + ->replace('\\', '/') + ->replaceFirst('Tests/Feature', 'tests/Feature') + ->append('Test.php') + ->value() + ); + } + + /** + * Get the fully qualified name for the test. + * + * @return string + */ + protected function fullyQualifiedTestName() + { + $name = Str::of(Str::lower($this->getNameInput()))->replace('.'.$this->option('extension'), ''); + + $namespacedName = Str::of( + Str::of($name) + ->replace('/', ' ') + ->explode(' ') + ->map(fn ($part) => Str::of($part)->ucfirst()) + ->implode('\\') + ) + ->replace(['-', '_'], ' ') + ->explode(' ') + ->map(fn ($part) => Str::of($part)->ucfirst()) + ->implode(''); + + return 'Tests\\Feature\\View\\'.$namespacedName; + } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getOptions() + { + return [ + ['extension', null, InputOption::VALUE_OPTIONAL, 'The extension of the generated view.', 'blade.php'], + ['force', 'f', InputOption::VALUE_NONE, 'Create the view even if the view already exists'], + ]; + } +} diff --git a/src/Illuminate/Foundation/Console/stubs/view.pest.stub b/src/Illuminate/Foundation/Console/stubs/view.pest.stub new file mode 100644 index 000000000000..93d07907caa4 --- /dev/null +++ b/src/Illuminate/Foundation/Console/stubs/view.pest.stub @@ -0,0 +1,9 @@ +view('{{ name }}', [ + // + ]); + + $contents->assertSee(''); +}); diff --git a/src/Illuminate/Foundation/Console/stubs/view.stub b/src/Illuminate/Foundation/Console/stubs/view.stub new file mode 100644 index 000000000000..6ecd80c26413 --- /dev/null +++ b/src/Illuminate/Foundation/Console/stubs/view.stub @@ -0,0 +1,3 @@ +
+ +
diff --git a/src/Illuminate/Foundation/Console/stubs/view.test.stub b/src/Illuminate/Foundation/Console/stubs/view.test.stub new file mode 100644 index 000000000000..8b9a9bf0f833 --- /dev/null +++ b/src/Illuminate/Foundation/Console/stubs/view.test.stub @@ -0,0 +1,20 @@ +view('{{ name }}', [ + // + ]); + + $contents->assertSee(''); + } +} diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index f78472a98b32..e68dc81435c3 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -76,6 +76,7 @@ use Illuminate\Foundation\Console\VendorPublishCommand; use Illuminate\Foundation\Console\ViewCacheCommand; use Illuminate\Foundation\Console\ViewClearCommand; +use Illuminate\Foundation\Console\ViewMakeCommand; use Illuminate\Notifications\Console\NotificationTableCommand; use Illuminate\Queue\Console\BatchesTableCommand; use Illuminate\Queue\Console\ClearCommand as QueueClearCommand; @@ -203,6 +204,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid 'Serve' => ServeCommand::class, 'StubPublish' => StubPublishCommand::class, 'TestMake' => TestMakeCommand::class, + 'ViewMake' => ViewMakeCommand::class, 'VendorPublish' => VendorPublishCommand::class, ]; From 2b9bfdf8adddf34aef7f98104bedff995060c31d Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 7 Sep 2023 14:33:16 +0100 Subject: [PATCH 2/6] Style --- src/Illuminate/Foundation/Console/ViewMakeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ViewMakeCommand.php b/src/Illuminate/Foundation/Console/ViewMakeCommand.php index d58ee15a1d0d..ce6dc3b0c28c 100644 --- a/src/Illuminate/Foundation/Console/ViewMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ViewMakeCommand.php @@ -89,8 +89,8 @@ protected function buildClass($name) protected function resolveStubPath($stub) { return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) - ? $customPath - : __DIR__.$stub; + ? $customPath + : __DIR__.$stub; } /** From 4a493032fe2a168f016d3378fef44dd492b114bc Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 7 Sep 2023 14:35:50 +0100 Subject: [PATCH 3/6] Sorts method names --- .../Foundation/Console/ViewMakeCommand.php | 182 +++++++++--------- 1 file changed, 91 insertions(+), 91 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ViewMakeCommand.php b/src/Illuminate/Foundation/Console/ViewMakeCommand.php index ce6dc3b0c28c..10226acc84b0 100644 --- a/src/Illuminate/Foundation/Console/ViewMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ViewMakeCommand.php @@ -16,18 +16,18 @@ class ViewMakeCommand extends GeneratorCommand use CreatesMatchingTest; /** - * The name and signature of the console command. + * The console command description. * * @var string */ - protected $name = 'make:view'; + protected $description = 'Create a new view'; /** - * The console command description. + * The name and signature of the console command. * * @var string */ - protected $description = 'Create a new view'; + protected $name = 'make:view'; /** * The type of file being generated. @@ -36,31 +36,6 @@ class ViewMakeCommand extends GeneratorCommand */ protected $type = 'View'; - /** - * Get the destination view path. - * - * @param string $name - * @return string - */ - protected function getPath($name) - { - return $this->viewPath( - $this->getNameInput().'.'.$this->option('extension'), - ); - } - - /** - * Get the stub file for the generator. - * - * @return string - */ - protected function getStub() - { - return $this->resolveStubPath( - '/stubs/view.stub', - ); - } - /** * Build the class with the given name. * @@ -81,68 +56,95 @@ protected function buildClass($name) } /** - * Resolve the fully-qualified path to the stub. + * Get the fully qualified name for the test. * - * @param string $stub * @return string */ - protected function resolveStubPath($stub) + protected function fullyQualifiedTestName() { - return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) - ? $customPath - : __DIR__.$stub; + $name = Str::of(Str::lower($this->getNameInput()))->replace('.'.$this->option('extension'), ''); + + $namespacedName = Str::of( + Str::of($name) + ->replace('/', ' ') + ->explode(' ') + ->map(fn ($part) => Str::of($part)->ucfirst()) + ->implode('\\') + ) + ->replace(['-', '_'], ' ') + ->explode(' ') + ->map(fn ($part) => Str::of($part)->ucfirst()) + ->implode(''); + + return 'Tests\\Feature\\View\\'.$namespacedName; } /** - * Get the namespace for the test. + * Get the desired view name from the input. * * @return string */ - protected function testNamespace() + protected function getNameInput() { - return Str::of($this->fullyQualifiedTestName()) - ->beforeLast('\\') - ->value(); + $name = trim($this->argument('name')); + + $name = str_replace(['\\', '.'], '/', $this->argument('name')); + + return $name; } /** - * Get the class name for the test. + * Get the console command arguments. * - * @return string + * @return array */ - protected function testClassName() + protected function getOptions() { - return Str::of($this->fullyQualifiedTestName()) - ->afterLast('\\') - ->append('Test') - ->value(); + return [ + ['extension', null, InputOption::VALUE_OPTIONAL, 'The extension of the generated view.', 'blade.php'], + ['force', 'f', InputOption::VALUE_NONE, 'Create the view even if the view already exists'], + ]; } /** - * Get the view name for the test. + * Get the destination view path. * + * @param string $name * @return string */ - protected function testViewName() + protected function getPath($name) { - return Str::of($this->getNameInput()) - ->replace('/', '.') - ->lower() - ->value(); + return $this->viewPath( + $this->getNameInput().'.'.$this->option('extension'), + ); } /** - * Get the desired view name from the input. + * Get the stub file for the generator. * * @return string */ - protected function getNameInput() + protected function getStub() { - $name = trim($this->argument('name')); - - $name = str_replace(['\\', '.'], '/', $this->argument('name')); + return $this->resolveStubPath( + '/stubs/view.stub', + ); + } - return $name; + /** + * Get the destination test case path. + * + * @return string + */ + protected function getTestPath() + { + return base_path( + Str::of($this->fullyQualifiedTestName()) + ->replace('\\', '/') + ->replaceFirst('Tests/Feature', 'tests/Feature') + ->append('Test.php') + ->value() + ); } /** @@ -182,55 +184,53 @@ protected function handleTestCreation($path): bool } /** - * Get the destination test case path. + * Resolve the fully-qualified path to the stub. * + * @param string $stub * @return string */ - protected function getTestPath() + protected function resolveStubPath($stub) { - return base_path( - Str::of($this->fullyQualifiedTestName()) - ->replace('\\', '/') - ->replaceFirst('Tests/Feature', 'tests/Feature') - ->append('Test.php') - ->value() - ); + return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) + ? $customPath + : __DIR__.$stub; } /** - * Get the fully qualified name for the test. + * Get the class name for the test. * * @return string */ - protected function fullyQualifiedTestName() + protected function testClassName() { - $name = Str::of(Str::lower($this->getNameInput()))->replace('.'.$this->option('extension'), ''); - - $namespacedName = Str::of( - Str::of($name) - ->replace('/', ' ') - ->explode(' ') - ->map(fn ($part) => Str::of($part)->ucfirst()) - ->implode('\\') - ) - ->replace(['-', '_'], ' ') - ->explode(' ') - ->map(fn ($part) => Str::of($part)->ucfirst()) - ->implode(''); + return Str::of($this->fullyQualifiedTestName()) + ->afterLast('\\') + ->append('Test') + ->value(); + } - return 'Tests\\Feature\\View\\'.$namespacedName; + /** + * Get the namespace for the test. + * + * @return string + */ + protected function testNamespace() + { + return Str::of($this->fullyQualifiedTestName()) + ->beforeLast('\\') + ->value(); } /** - * Get the console command arguments. + * Get the view name for the test. * - * @return array + * @return string */ - protected function getOptions() + protected function testViewName() { - return [ - ['extension', null, InputOption::VALUE_OPTIONAL, 'The extension of the generated view.', 'blade.php'], - ['force', 'f', InputOption::VALUE_NONE, 'Create the view even if the view already exists'], - ]; + return Str::of($this->getNameInput()) + ->replace('/', '.') + ->lower() + ->value(); } } From 19ce984a11cdcaa0e595ee1213483c339c31e5af Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 7 Sep 2023 14:38:49 +0100 Subject: [PATCH 4/6] Renames --- .../Foundation/Console/ViewMakeCommand.php | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ViewMakeCommand.php b/src/Illuminate/Foundation/Console/ViewMakeCommand.php index 10226acc84b0..6c41c9a591a5 100644 --- a/src/Illuminate/Foundation/Console/ViewMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ViewMakeCommand.php @@ -55,30 +55,6 @@ protected function buildClass($name) ); } - /** - * Get the fully qualified name for the test. - * - * @return string - */ - protected function fullyQualifiedTestName() - { - $name = Str::of(Str::lower($this->getNameInput()))->replace('.'.$this->option('extension'), ''); - - $namespacedName = Str::of( - Str::of($name) - ->replace('/', ' ') - ->explode(' ') - ->map(fn ($part) => Str::of($part)->ucfirst()) - ->implode('\\') - ) - ->replace(['-', '_'], ' ') - ->explode(' ') - ->map(fn ($part) => Str::of($part)->ucfirst()) - ->implode(''); - - return 'Tests\\Feature\\View\\'.$namespacedName; - } - /** * Get the desired view name from the input. * @@ -139,7 +115,7 @@ protected function getStub() protected function getTestPath() { return base_path( - Str::of($this->fullyQualifiedTestName()) + Str::of($this->testClassFullyQualifiedName()) ->replace('\\', '/') ->replaceFirst('Tests/Feature', 'tests/Feature') ->append('Test.php') @@ -196,6 +172,30 @@ protected function resolveStubPath($stub) : __DIR__.$stub; } + /** + * Get the class fully qualified name for the test. + * + * @return string + */ + protected function testClassFullyQualifiedName() + { + $name = Str::of(Str::lower($this->getNameInput()))->replace('.'.$this->option('extension'), ''); + + $namespacedName = Str::of( + Str::of($name) + ->replace('/', ' ') + ->explode(' ') + ->map(fn ($part) => Str::of($part)->ucfirst()) + ->implode('\\') + ) + ->replace(['-', '_'], ' ') + ->explode(' ') + ->map(fn ($part) => Str::of($part)->ucfirst()) + ->implode(''); + + return 'Tests\\Feature\\View\\'.$namespacedName; + } + /** * Get the class name for the test. * @@ -203,7 +203,7 @@ protected function resolveStubPath($stub) */ protected function testClassName() { - return Str::of($this->fullyQualifiedTestName()) + return Str::of($this->testClassFullyQualifiedName()) ->afterLast('\\') ->append('Test') ->value(); @@ -216,7 +216,7 @@ protected function testClassName() */ protected function testNamespace() { - return Str::of($this->fullyQualifiedTestName()) + return Str::of($this->testClassFullyQualifiedName()) ->beforeLast('\\') ->value(); } From 344ade28378be2031923aaa35ad4bc56f2ae074d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Sep 2023 11:27:05 -0500 Subject: [PATCH 5/6] formatting --- .../Foundation/Console/ViewMakeCommand.php | 120 +++++++++--------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ViewMakeCommand.php b/src/Illuminate/Foundation/Console/ViewMakeCommand.php index 6c41c9a591a5..49899312c65d 100644 --- a/src/Illuminate/Foundation/Console/ViewMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ViewMakeCommand.php @@ -55,6 +55,19 @@ protected function buildClass($name) ); } + /** + * Get the destination view path. + * + * @param string $name + * @return string + */ + protected function getPath($name) + { + return $this->viewPath( + $this->getNameInput().'.'.$this->option('extension'), + ); + } + /** * Get the desired view name from the input. * @@ -70,41 +83,28 @@ protected function getNameInput() } /** - * Get the console command arguments. - * - * @return array - */ - protected function getOptions() - { - return [ - ['extension', null, InputOption::VALUE_OPTIONAL, 'The extension of the generated view.', 'blade.php'], - ['force', 'f', InputOption::VALUE_NONE, 'Create the view even if the view already exists'], - ]; - } - - /** - * Get the destination view path. + * Get the stub file for the generator. * - * @param string $name * @return string */ - protected function getPath($name) + protected function getStub() { - return $this->viewPath( - $this->getNameInput().'.'.$this->option('extension'), + return $this->resolveStubPath( + '/stubs/view.stub', ); } /** - * Get the stub file for the generator. + * Resolve the fully-qualified path to the stub. * + * @param string $stub * @return string */ - protected function getStub() + protected function resolveStubPath($stub) { - return $this->resolveStubPath( - '/stubs/view.stub', - ); + return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) + ? $customPath + : __DIR__.$stub; } /** @@ -123,20 +123,6 @@ protected function getTestPath() ); } - /** - * Get the test stub file for the generator. - * - * @return string - */ - protected function getTestStub() - { - $stubName = 'view.'.($this->option('pest') ? 'pest' : 'test').'.stub'; - - return file_exists($customPath = $this->laravel->basePath("stubs/$stubName")) - ? $customPath - : __DIR__.'/stubs/'.$stubName; - } - /** * Create the matching test case if requested. * @@ -160,16 +146,28 @@ protected function handleTestCreation($path): bool } /** - * Resolve the fully-qualified path to the stub. + * Get the namespace for the test. * - * @param string $stub * @return string */ - protected function resolveStubPath($stub) + protected function testNamespace() { - return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) - ? $customPath - : __DIR__.$stub; + return Str::of($this->testClassFullyQualifiedName()) + ->beforeLast('\\') + ->value(); + } + + /** + * Get the class name for the test. + * + * @return string + */ + protected function testClassName() + { + return Str::of($this->testClassFullyQualifiedName()) + ->afterLast('\\') + ->append('Test') + ->value(); } /** @@ -197,28 +195,17 @@ protected function testClassFullyQualifiedName() } /** - * Get the class name for the test. + * Get the test stub file for the generator. * * @return string */ - protected function testClassName() + protected function getTestStub() { - return Str::of($this->testClassFullyQualifiedName()) - ->afterLast('\\') - ->append('Test') - ->value(); - } + $stubName = 'view.'.($this->option('pest') ? 'pest' : 'test').'.stub'; - /** - * Get the namespace for the test. - * - * @return string - */ - protected function testNamespace() - { - return Str::of($this->testClassFullyQualifiedName()) - ->beforeLast('\\') - ->value(); + return file_exists($customPath = $this->laravel->basePath("stubs/$stubName")) + ? $customPath + : __DIR__.'/stubs/'.$stubName; } /** @@ -233,4 +220,17 @@ protected function testViewName() ->lower() ->value(); } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getOptions() + { + return [ + ['extension', null, InputOption::VALUE_OPTIONAL, 'The extension of the generated view', 'blade.php'], + ['force', 'f', InputOption::VALUE_NONE, 'Create the view even if the view already exists'], + ]; + } } From 607aff84f6f6b1a71f247bfd6107dfea1548dac4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Sep 2023 11:29:27 -0500 Subject: [PATCH 6/6] formatting --- src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index e68dc81435c3..b92bd4ac91cb 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -204,8 +204,8 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid 'Serve' => ServeCommand::class, 'StubPublish' => StubPublishCommand::class, 'TestMake' => TestMakeCommand::class, - 'ViewMake' => ViewMakeCommand::class, 'VendorPublish' => VendorPublishCommand::class, + 'ViewMake' => ViewMakeCommand::class, ]; /**