This commit is contained in:
baodev0
2026-10-05 09:30:24 +07:00
parent a1aeb88061
commit 1544629a1f
53 changed files with 1292 additions and 963 deletions
+52
View File
@@ -0,0 +1,52 @@
<?php
namespace App\Mail;
use App\Mail\Concerns\BuildsTemplateEnvelope;
use App\Models\Template;
use App\Services\Email\EmailTemplateRenderer;
use App\Services\Email\EmailTemplateService;
use App\Support\MailGlobals;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
abstract class TemplateMailable extends Mailable
{
use BuildsTemplateEnvelope;
private ?array $rendered = null;
abstract protected function scenario(): string;
/**
* @return array<string, mixed>
*/
abstract protected function variables(): array;
public function envelope(): Envelope
{
return $this->templateEnvelope($this->rendered()['subject'], $this->template()->mailConfig());
}
public function content(): Content
{
return new Content(htmlString: $this->rendered()['html']);
}
/**
* @return array{subject: string, html: string}
*/
private function rendered(): array
{
return $this->rendered ??= app(EmailTemplateRenderer::class)->render(
$this->template(),
[...MailGlobals::values(), ...$this->variables()],
);
}
private function template(): Template
{
return app(EmailTemplateService::class)->loadTemplate($this->scenario());
}
}