88use Illuminate \Support \ServiceProvider ;
99use Torchlight \Blade \BladeManager ;
1010use Torchlight \Blade \CodeComponent ;
11+ use Torchlight \Blade \EngineDecorator ;
1112use Torchlight \Commands \Install ;
1213use Torchlight \Middleware \RenderTorchlight ;
1314
@@ -20,6 +21,7 @@ public function boot()
2021 $ this ->publishConfig ();
2122 $ this ->registerBladeComponent ();
2223 $ this ->registerLivewire ();
24+ $ this ->decorateGrahamCampbellEngines ();
2325 }
2426
2527 public function bindManagerSingleton ()
@@ -71,6 +73,53 @@ public function registerLivewire()
7173 }
7274 }
7375
76+ /**
77+ * Graham Campbell's Markdown package is a common (and excellent) package that many
78+ * Laravel developers use for markdown. It registers a few view engines so you can
79+ * just return e.g. `view("file.md")` and the markdown will get rendered to HTML.
80+ *
81+ * The markdown file will get parsed *once* and saved to the disk, which could lead
82+ * to data leaks if you're using a post processor that injects some sort of user
83+ * details. The first user that hits the page will have their information saved
84+ * into the compiled views.
85+ *
86+ * We decorate the engines that Graham uses so we can alert our post processors
87+ * not to run when the views are being compiled.
88+ */
89+ public function decorateGrahamCampbellEngines ()
90+ {
91+ if (!class_exists ('\\GrahamCampbell \\Markdown \\MarkdownServiceProvider ' )) {
92+ return ;
93+ }
94+
95+ // The engines won't be registered if this is false.
96+ if (!$ this ->app ->config ->get ('markdown.views ' )) {
97+ return ;
98+ }
99+
100+ // Decorate all the engines that Graham's package registers.
101+ $ this ->decorateEngine ('md ' );
102+ $ this ->decorateEngine ('phpmd ' );
103+ $ this ->decorateEngine ('blademd ' );
104+ }
105+
106+ /**
107+ * Decorate a single view engine.
108+ * @param $name
109+ */
110+ protected function decorateEngine ($ name )
111+ {
112+ // No engine registered.
113+ if (!$ resolved = $ this ->app ->view ->getEngineResolver ()->resolve ($ name )) {
114+ return ;
115+ }
116+
117+ // Wrap the existing engine in our decorator.
118+ $ this ->app ->view ->getEngineResolver ()->register ($ name , function () use ($ resolved ) {
119+ return new EngineDecorator ($ resolved );
120+ });
121+ }
122+
74123 public function register ()
75124 {
76125 $ this ->mergeConfigFrom (__DIR__ . '/../config/torchlight.php ' , 'torchlight ' );
0 commit comments