Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] MailMakeCommand: Add new --view option #51411

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably remove this since you're not actually using {{ quote }} anywhere in the stubs, not to mention an inspiring quote being injected into the mailable template makes no sense.

Copy link
Contributor

@ahinkle ahinkle May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahinkle ah nice I for some reason didn't realize it was the view stub being used, I thought it was one of the ones in the PR. Thanks!

file_get_contents(__DIR__ . '/stubs/view.stub')
Copy link
Contributor

@ahinkle ahinkle May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated blade view stub here has a <div> in it. Wouldn't that be a little strange for emails? We might need a new mailable view stub.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought about adding a new stub for this but kept it simple for now and re-used the existing view.stub file. Will await feedback from others too :^)

);

$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