33namespace Asahasrabuddhe \LaravelMJML \Process ;
44
55use Html2Text \Html2Text ;
6+ use Illuminate \View \View ;
67use Illuminate \Support \HtmlString ;
78use Illuminate \Support \Facades \File ;
89use Symfony \Component \Process \Process ;
910use Symfony \Component \Process \Exception \ProcessFailedException ;
1011
1112class 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