generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
auto extract template variables on send test modal
- Loading branch information
1 parent
1356a34
commit cd68603
Showing
3 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace MailCarrier\Helpers; | ||
|
||
use MailCarrier\Models\Template; | ||
use Twig\Environment; | ||
use Twig\Loader\ArrayLoader; | ||
use Twig\Source; | ||
|
||
class TemplateManager | ||
{ | ||
public function __construct(protected Template $template) | ||
{ | ||
// | ||
} | ||
|
||
public static function make(Template $template): static | ||
{ | ||
return new static($template); | ||
Check failure on line 19 in src/Helpers/TemplateManager.php GitHub Actions / PHP 8.1
Check failure on line 19 in src/Helpers/TemplateManager.php GitHub Actions / PHP 8.2
|
||
} | ||
|
||
public function extractVariableNames(): array | ||
{ | ||
$source = $this->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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
use MailCarrier\Helpers\TemplateManager; | ||
use MailCarrier\Models\Layout; | ||
use MailCarrier\Models\Template; | ||
|
||
it('returns empty array if template has no variables', function () { | ||
$template = new Template([ | ||
'content' => '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 }}, | ||
<a href="{{ ctaUrl }}"> | ||
Sign in | ||
</a> | ||
{% 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 }}, | ||
<a href="{{ ctaUrl }}"> | ||
Sign in | ||
</a> | ||
{% if isPremium|default(false) %} | ||
{{ tierLevel|title }} | ||
{% endif %} | ||
TWIG, | ||
]); | ||
|
||
$template->setRelation('layout', new Layout([ | ||
'content' => <<<'TWIG' | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
</head> | ||
<body> | ||
<h1>{{ headline }}</h1> | ||
{% block content %}{% endblock %} | ||
</body> | ||
</html> | ||
TWIG, | ||
])); | ||
|
||
$output = TemplateManager::make($template)->extractVariableNames(); | ||
|
||
expect($output)->toBe(['headline', 'name', 'ctaUrl', 'isPremium', 'tierLevel']); | ||
}); |