Skip to content

Commit 422d196

Browse files
ryangjchandlertaylorotwellcrynobone
authored
[11.x] MailMakeCommand: Add new --view option (#51411)
* 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 <crynobone@gmail.com> * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent 93658ff commit 422d196

File tree

3 files changed

+109
-9
lines changed

3 files changed

+109
-9
lines changed

src/Illuminate/Foundation/Console/MailMakeCommand.php

+40-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Concerns\CreatesMatchingTest;
66
use Illuminate\Console\GeneratorCommand;
7+
use Illuminate\Foundation\Inspiring;
78
use Illuminate\Support\Str;
89
use Symfony\Component\Console\Attribute\AsCommand;
910
use Symfony\Component\Console\Input\InputOption;
@@ -48,6 +49,10 @@ public function handle()
4849
if ($this->option('markdown') !== false) {
4950
$this->writeMarkdownTemplate();
5051
}
52+
53+
if ($this->option('view') !== false) {
54+
$this->writeView();
55+
}
5156
}
5257

5358
/**
@@ -61,13 +66,33 @@ protected function writeMarkdownTemplate()
6166
str_replace('.', '/', $this->getView()).'.blade.php'
6267
);
6368

64-
if (! $this->files->isDirectory(dirname($path))) {
65-
$this->files->makeDirectory(dirname($path), 0755, true);
66-
}
69+
$this->files->ensureDirectoryExists(dirname($path));
6770

6871
$this->files->put($path, file_get_contents(__DIR__.'/stubs/markdown.stub'));
6972
}
7073

74+
/**
75+
* Write the Blade template for the mailable.
76+
*
77+
* @return void
78+
*/
79+
protected function writeView()
80+
{
81+
$path = $this->viewPath(
82+
str_replace('.', '/', $this->getView()) . '.blade.php'
83+
);
84+
85+
$this->files->ensureDirectoryExists(dirname($path));
86+
87+
$stub = str_replace(
88+
'{{ quote }}',
89+
Inspiring::quotes()->random(),
90+
file_get_contents(__DIR__ . '/stubs/view.stub')
91+
);
92+
93+
$this->files->put($path, $stub);
94+
}
95+
7196
/**
7297
* Build the class with the given name.
7398
*
@@ -82,7 +107,7 @@ protected function buildClass($name)
82107
parent::buildClass($name)
83108
);
84109

85-
if ($this->option('markdown') !== false) {
110+
if ($this->option('markdown') !== false || $this->option('view') !== false) {
86111
$class = str_replace(['DummyView', '{{ view }}'], $this->getView(), $class);
87112
}
88113

@@ -96,7 +121,7 @@ protected function buildClass($name)
96121
*/
97122
protected function getView()
98123
{
99-
$view = $this->option('markdown');
124+
$view = $this->option('markdown') ?: $this->option('view');
100125

101126
if (! $view) {
102127
$name = str_replace('\\', '/', $this->argument('name'));
@@ -116,10 +141,15 @@ protected function getView()
116141
*/
117142
protected function getStub()
118143
{
119-
return $this->resolveStubPath(
120-
$this->option('markdown') !== false
121-
? '/stubs/markdown-mail.stub'
122-
: '/stubs/mail.stub');
144+
if ($this->option('markdown') !== false) {
145+
return $this->resolveStubPath('/stubs/markdown-mail.stub');
146+
}
147+
148+
if ($this->option('view') !== false) {
149+
return $this->resolveStubPath('/stubs/view-mail.stub');
150+
}
151+
152+
return $this->resolveStubPath('/stubs/mail.stub');
123153
}
124154

125155
/**
@@ -156,6 +186,7 @@ protected function getOptions()
156186
return [
157187
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the mailable already exists'],
158188
['markdown', 'm', InputOption::VALUE_OPTIONAL, 'Create a new Markdown template for the mailable', false],
189+
['view', null, InputOption::VALUE_OPTIONAL, 'Create a new Blade template for the mailable', false],
159190
];
160191
}
161192
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Mail\Mailable;
8+
use Illuminate\Mail\Mailables\Content;
9+
use Illuminate\Mail\Mailables\Envelope;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
class {{ class }} extends Mailable
13+
{
14+
use Queueable, SerializesModels;
15+
16+
/**
17+
* Create a new message instance.
18+
*/
19+
public function __construct()
20+
{
21+
//
22+
}
23+
24+
/**
25+
* Get the message envelope.
26+
*/
27+
public function envelope(): Envelope
28+
{
29+
return new Envelope(
30+
subject: '{{ subject }}',
31+
);
32+
}
33+
34+
/**
35+
* Get the message content definition.
36+
*/
37+
public function content(): Content
38+
{
39+
return new Content(
40+
view: '{{ view }}',
41+
);
42+
}
43+
44+
/**
45+
* Get the attachments for the message.
46+
*
47+
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
48+
*/
49+
public function attachments(): array
50+
{
51+
return [];
52+
}
53+
}

tests/Integration/Generators/MailMakeCommandTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ public function testItCanGenerateMailFileWithMarkdownOption()
4646
], 'resources/views/foo-mail.blade.php');
4747
}
4848

49+
public function testItCanGenerateMailFileWithViewOption()
50+
{
51+
$this->artisan('make:mail', ['name' => 'FooMail', '--view' => 'foo-mail'])
52+
->assertExitCode(0);
53+
54+
$this->assertFileContains([
55+
'namespace App\Mail;',
56+
'use Illuminate\Mail\Mailable;',
57+
'class FooMail extends Mailable',
58+
'return new Content(',
59+
"view: 'foo-mail',",
60+
], 'app/Mail/FooMail.php');
61+
62+
$this->assertFilenameExists('resources/views/foo-mail.blade.php');
63+
}
64+
4965
public function testItCanGenerateMailFileWithTest()
5066
{
5167
$this->artisan('make:mail', ['name' => 'FooMail', '--test' => true])

0 commit comments

Comments
 (0)