diff --git a/src/Helpers/TemplateManager.php b/src/Helpers/TemplateManager.php new file mode 100644 index 0000000..66468f6 --- /dev/null +++ b/src/Helpers/TemplateManager.php @@ -0,0 +1,51 @@ +template->layout?->content . $this->template->content; + + $twig = new Environment(new ArrayLoader()); + $nodes = $twig->parse( + $twig->tokenize(new Source($source, '')) + )->getNode('body')->getNode('0'); + + preg_match_all("|Twig\\\Node\\\Expression\\\NameExpression\(name\: '(.*)'|mi", (string) $nodes, $matches); + + return array_values(array_unique($matches[1])); + } + + protected function getLoader(): ArrayLoader + { + $mainFileName = sprintf('main-%d.html', $this->template->id); + $layoutFileName = !$this->template->layout_id ? null : sprintf('layout-%d.html', $this->template->layout_id); + $mainFileContent = !$this->template->layout_id ? $this->template->content : sprintf( + '{%% extends "%s" %%}{%% block content %%}%s{%% endblock %%}', + $layoutFileName, + $this->template->content, + ); + + return new ArrayLoader([ + $mainFileName => $mainFileContent, + $layoutFileName => $this->template->layout?->content, + ]); + } +} diff --git a/src/Resources/TemplateResource/Actions/SendTestAction.php b/src/Resources/TemplateResource/Actions/SendTestAction.php index 29bbf99..f750389 100644 --- a/src/Resources/TemplateResource/Actions/SendTestAction.php +++ b/src/Resources/TemplateResource/Actions/SendTestAction.php @@ -11,6 +11,7 @@ use Illuminate\Support\HtmlString; use MailCarrier\Actions\SendMail; use MailCarrier\Dto\SendMailDto; +use MailCarrier\Helpers\TemplateManager; class SendTestAction extends Action { @@ -25,6 +26,7 @@ protected function setUp(): void $this->label('Send test'); $this->icon('heroicon-o-paper-airplane'); + $this->modalHeading('Send test email'); $this->modalSubmitActionLabel('Send'); $this->modalFooterActionsAlignment(Alignment::End); $this->extraAttributes([ @@ -37,7 +39,14 @@ protected function setUp(): void ->required(), Forms\Components\KeyValue::make('variables') ->keyLabel('Variable name') - ->valueLabel('Variable value'), + ->valueLabel('Variable value') + ->valuePlaceholder('Fill or delete') + ->default( + Arr::mapWithKeys( + TemplateManager::make($this->getRecord())->extractVariableNames(), + fn (string $value) => [$value => null] + ) + ), Forms\Components\Checkbox::make('enqueue'), ]); diff --git a/tests/Unit/TemplateManager/ExtractVariableNamesTest.php b/tests/Unit/TemplateManager/ExtractVariableNamesTest.php new file mode 100644 index 0000000..38fa7e6 --- /dev/null +++ b/tests/Unit/TemplateManager/ExtractVariableNamesTest.php @@ -0,0 +1,69 @@ + 'Hello', + ]); + + $output = TemplateManager::make($template)->extractVariableNames(); + + expect($output)->toBe([]); +}); + +it('returns all the variables of the template', function () { + $template = new Template([ + 'content' => <<<'TWIG' + Hello {{ name }}, + + Sign in + + + {% if isPremium|default(false) %} + {{ tierLevel|title }} + {% endif %} + TWIG, + ]); + + $output = TemplateManager::make($template)->extractVariableNames(); + + expect($output)->toBe(['name', 'ctaUrl', 'isPremium', 'tierLevel']); +}); + +it('returns all the variables of the template along with its layout', function () { + $template = new Template([ + 'content' => <<<'TWIG' + Hello {{ name }}, + + Sign in + + + {% if isPremium|default(false) %} + {{ tierLevel|title }} + {% endif %} + TWIG, + ]); + + $template->setRelation('layout', new Layout([ + 'content' => <<<'TWIG' + + + + + + + +

{{ headline }}

+ {% block content %}{% endblock %} + + + TWIG, + ])); + + $output = TemplateManager::make($template)->extractVariableNames(); + + expect($output)->toBe(['headline', 'name', 'ctaUrl', 'isPremium', 'tierLevel']); +});