File tree Expand file tree Collapse file tree 4 files changed +50
-1
lines changed
Expand file tree Collapse file tree 4 files changed +50
-1
lines changed Original file line number Diff line number Diff line change 11{
22 "name" : " putyourlightson/craft-datastar-module" ,
33 "description" : " A template-driven, reactive hypermedia framework for Craft." ,
4- "version" : " 1.0.0-RC.8 " ,
4+ "version" : " 1.0.0-RC.9 " ,
55 "type" : " yii-module" ,
66 "license" : " mit" ,
77 "require" : {
Original file line number Diff line number Diff line change 88use Craft ;
99use craft \web \Response ;
1010use putyourlightson \datastar \assets \DatastarAssetBundle ;
11+ use putyourlightson \datastar \dumpers \SseDumper ;
1112use putyourlightson \datastar \models \Settings ;
1213use putyourlightson \datastar \services \SseService ;
1314use putyourlightson \datastar \twigextensions \DatastarTwigExtension ;
@@ -66,6 +67,7 @@ public function init(): void
6667
6768 $ this ->registerComponents ();
6869 $ this ->registerTwigExtension ();
70+ $ this ->registerSseDumper ();
6971 $ this ->registerScript ();
7072 }
7173
@@ -90,6 +92,15 @@ private function registerTwigExtension(): void
9092 Craft::$ app ->getView ()->registerTwigExtension (new DatastarTwigExtension ());
9193 }
9294
95+ private function registerSseDumper (): void
96+ {
97+ if (empty (Craft::$ app ->getRequest ()->getHeaders ()->get ('Datastar-Request ' ))) {
98+ return ;
99+ }
100+
101+ Craft::$ app ->set ('dumper ' , new SseDumper ());
102+ }
103+
93104 private function registerScript (): void
94105 {
95106 if (!$ this ->settings ->registerScript ) {
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * @copyright Copyright (c) PutYourLightsOn
4+ */
5+
6+ namespace putyourlightson \datastar \dumpers ;
7+
8+ use putyourlightson \datastar \Datastar ;
9+ use Symfony \Component \VarDumper \Cloner \Data ;
10+ use Symfony \Component \VarDumper \Dumper \HtmlDumper ;
11+
12+ class SseDumper extends HtmlDumper
13+ {
14+ public function dump (Data $ data , $ output = null , array $ extraDisplayOptions = []): ?string
15+ {
16+ $ result = parent ::dump ($ data , true , $ extraDisplayOptions );
17+
18+ Datastar::getInstance ()->sse ->dump ($ result );
19+
20+ return $ result ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1212use Exception ;
1313use putyourlightson \datastar \Datastar ;
1414use putyourlightson \datastar \helpers \Request ;
15+ use starfederation \datastar \enums \ElementPatchMode ;
1516use starfederation \datastar \events \EventInterface ;
1617use starfederation \datastar \events \ExecuteScript ;
1718use starfederation \datastar \events \Location ;
@@ -304,6 +305,21 @@ public function throwException(Throwable $exception): void
304305 exit (1 );
305306 }
306307
308+ /**
309+ * Prepends dumped content to the `<body>` tag.
310+ */
311+ public function dump (string $ output ): void
312+ {
313+ $ this ->patchElements ($ output , [
314+ 'selector ' => 'body ' ,
315+ 'mode ' => ElementPatchMode::Prepend,
316+ ]);
317+
318+ if (!$ this ->isStreamedResponse ) {
319+ $ this ->getEventStream ()->send ();
320+ }
321+ }
322+
307323 /**
308324 * Returns patch event options with null values removed.
309325 */
You can’t perform that action at this time.
0 commit comments