Skip to content

Commit 437b5eb

Browse files
Merge pull request #12 from aaronschmied/hotfix/fix-caching-with-compiled-views
Enabled caching with content checksums
2 parents 18e5002 + 71dd8ae commit 437b5eb

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

src/Process/MJML.php

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,45 @@
33
namespace Asahasrabuddhe\LaravelMJML\Process;
44

55
use Html2Text\Html2Text;
6+
use Illuminate\View\View;
67
use Illuminate\Support\HtmlString;
78
use Illuminate\Support\Facades\File;
89
use Symfony\Component\Process\Process;
910
use Symfony\Component\Process\Exception\ProcessFailedException;
1011

1112
class MJML
1213
{
14+
/**
15+
* @var Process
16+
*/
1317
protected $process;
1418

19+
/**
20+
* @var View
21+
*/
1522
protected $view;
1623

24+
/**
25+
* @var string
26+
*/
1727
protected $path;
1828

19-
protected $options;
20-
29+
/**
30+
* MJML constructor.
31+
*
32+
* @param View $view
33+
*/
2134
public function __construct($view)
2235
{
23-
$this->view = $view;
24-
$this->path = storage_path('framework/views/' . sha1($this->view->getPath()) . '.php');
25-
$this->compiledPath = storage_path('framework/views/' . sha1($this->view->getPath() . '_compiled') . '.php');
36+
$this->view = $view;
37+
$this->path = storage_path('framework/views/' . sha1($this->view->getPath()) . '.php');
2638
}
2739

40+
/**
41+
* Build the mjml command.
42+
*
43+
* @return string
44+
*/
2845
public function buildCmdLineFromConfig()
2946
{
3047
return implode(' ', [
@@ -35,27 +52,52 @@ public function buildCmdLineFromConfig()
3552
]);
3653
}
3754

55+
/**
56+
* Render the html content.
57+
*
58+
* @return HtmlString
59+
*
60+
* @throws \Throwable
61+
*/
3862
public function renderHTML()
3963
{
4064
$html = $this->view->render();
65+
4166
File::put($this->path, $html);
4267

43-
$this->process = new Process($this->buildCmdLineFromConfig());
44-
$this->process->run();
45-
// executes after the command finishes
46-
File::delete($this->path);
47-
if (! $this->process->isSuccessful()) {
48-
throw new ProcessFailedException($this->process);
68+
$contentChecksum = hash('sha256', $html);
69+
$this->compiledPath = storage_path("framework/views/{$contentChecksum}.php");
70+
71+
if (! File::exists($this->compiledPath)) {
72+
$this->process = new Process($this->buildCmdLineFromConfig());
73+
$this->process->run();
74+
75+
if (! $this->process->isSuccessful()) {
76+
throw new ProcessFailedException($this->process);
77+
}
4978
}
5079

5180
return new HtmlString(File::get($this->compiledPath));
5281
}
5382

83+
/**
84+
* Render the text content.
85+
*
86+
* @return HtmlString
87+
*
88+
* @throws \Html2Text\Html2TextException
89+
* @throws \Throwable
90+
*/
5491
public function renderText()
5592
{
5693
return new HtmlString(html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", Html2Text::convert($this->renderHTML())), ENT_QUOTES, 'UTF-8'));
5794
}
5895

96+
/**
97+
* Detect the path to the mjml executable.
98+
*
99+
* @return string
100+
*/
59101
public function detectBinaryPath()
60102
{
61103
return base_path('node_modules/.bin/mjml');

0 commit comments

Comments
 (0)