-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The generated blade view stub here has a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
); | ||
|
||
$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], | ||
]; | ||
} | ||
} |
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 []; | ||
} | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joshmanders View stub has a quote:
https://github.com/laravel/framework/blob/11.x/src/Illuminate/Foundation/Console/stubs/view.stub
There was a problem hiding this comment.
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!