diff --git a/src/Illuminate/Foundation/Console/MailMakeCommand.php b/src/Illuminate/Foundation/Console/MailMakeCommand.php index af02001c7a2c..2ed4295d007b 100644 --- a/src/Illuminate/Foundation/Console/MailMakeCommand.php +++ b/src/Illuminate/Foundation/Console/MailMakeCommand.php @@ -4,6 +4,7 @@ use Illuminate\Console\Concerns\CreatesMatchingTest; use Illuminate\Console\GeneratorCommand; +use Illuminate\Foundation\Inspiring; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; @@ -48,6 +49,10 @@ public function handle() if ($this->option('markdown') !== false) { $this->writeMarkdownTemplate(); } + + if ($this->option('view') !== false) { + $this->writeView(); + } } /** @@ -61,13 +66,33 @@ protected function writeMarkdownTemplate() str_replace('.', '/', $this->getView()).'.blade.php' ); - if (! $this->files->isDirectory(dirname($path))) { - $this->files->makeDirectory(dirname($path), 0755, true); - } + $this->files->ensureDirectoryExists(dirname($path)); $this->files->put($path, file_get_contents(__DIR__.'/stubs/markdown.stub')); } + /** + * Write the Blade template for the mailable. + * + * @return void + */ + protected function writeView() + { + $path = $this->viewPath( + str_replace('.', '/', $this->getView()) . '.blade.php' + ); + + $this->files->ensureDirectoryExists(dirname($path)); + + $stub = str_replace( + '{{ quote }}', + Inspiring::quotes()->random(), + file_get_contents(__DIR__ . '/stubs/view.stub') + ); + + $this->files->put($path, $stub); + } + /** * Build the class with the given name. * @@ -82,7 +107,7 @@ protected function buildClass($name) parent::buildClass($name) ); - if ($this->option('markdown') !== false) { + if ($this->option('markdown') !== false || $this->option('view') !== false) { $class = str_replace(['DummyView', '{{ view }}'], $this->getView(), $class); } @@ -96,7 +121,7 @@ protected function buildClass($name) */ protected function getView() { - $view = $this->option('markdown'); + $view = $this->option('markdown') ?: $this->option('view'); if (! $view) { $name = str_replace('\\', '/', $this->argument('name')); @@ -116,10 +141,15 @@ protected function getView() */ protected function getStub() { - return $this->resolveStubPath( - $this->option('markdown') !== false - ? '/stubs/markdown-mail.stub' - : '/stubs/mail.stub'); + if ($this->option('markdown') !== false) { + return $this->resolveStubPath('/stubs/markdown-mail.stub'); + } + + if ($this->option('view') !== false) { + return $this->resolveStubPath('/stubs/view-mail.stub'); + } + + return $this->resolveStubPath('/stubs/mail.stub'); } /** @@ -156,6 +186,7 @@ protected function getOptions() return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the mailable already exists'], ['markdown', 'm', InputOption::VALUE_OPTIONAL, 'Create a new Markdown template for the mailable', false], + ['view', null, InputOption::VALUE_OPTIONAL, 'Create a new Blade template for the mailable', false], ]; } } diff --git a/src/Illuminate/Foundation/Console/stubs/view-mail.stub b/src/Illuminate/Foundation/Console/stubs/view-mail.stub new file mode 100644 index 000000000000..8889396d5869 --- /dev/null +++ b/src/Illuminate/Foundation/Console/stubs/view-mail.stub @@ -0,0 +1,53 @@ + + */ + public function attachments(): array + { + return []; + } +} diff --git a/tests/Integration/Generators/MailMakeCommandTest.php b/tests/Integration/Generators/MailMakeCommandTest.php index 8da9c0785ccc..06b418fec164 100644 --- a/tests/Integration/Generators/MailMakeCommandTest.php +++ b/tests/Integration/Generators/MailMakeCommandTest.php @@ -46,6 +46,22 @@ public function testItCanGenerateMailFileWithMarkdownOption() ], 'resources/views/foo-mail.blade.php'); } + public function testItCanGenerateMailFileWithViewOption() + { + $this->artisan('make:mail', ['name' => 'FooMail', '--view' => 'foo-mail']) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Mail;', + 'use Illuminate\Mail\Mailable;', + 'class FooMail extends Mailable', + 'return new Content(', + "view: 'foo-mail',", + ], 'app/Mail/FooMail.php'); + + $this->assertFilenameExists('resources/views/foo-mail.blade.php'); + } + public function testItCanGenerateMailFileWithTest() { $this->artisan('make:mail', ['name' => 'FooMail', '--test' => true])