Skip to content

Commit

Permalink
[11.x] MailMakeCommand: Add new --view option (#51411)
Browse files Browse the repository at this point in the history
* MailMakeCommand: Add new --view option

* MailMakeCommand: Add tests for new --view option

* Update src/Illuminate/Foundation/Console/MailMakeCommand.php

Co-authored-by: Mior Muhammad Zaki <[email protected]>

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
3 people authored May 20, 2024
1 parent 93658ff commit 422d196
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 9 deletions.
49 changes: 40 additions & 9 deletions src/Illuminate/Foundation/Console/MailMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,10 @@ public function handle()
if ($this->option('markdown') !== false) {
$this->writeMarkdownTemplate();
}

if ($this->option('view') !== false) {
$this->writeView();
}
}

/**
Expand All @@ -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.
*
Expand All @@ -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);
}

Expand All @@ -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'));
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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],
];
}
}
53 changes: 53 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/view-mail.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace {{ namespace }};

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class {{ class }} extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct()
{
//
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: '{{ subject }}',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: '{{ view }}',
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
16 changes: 16 additions & 0 deletions tests/Integration/Generators/MailMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 422d196

Please sign in to comment.