Skip to content

Commit d0acc39

Browse files
committed
Add var dump handling
1 parent 4b3df21 commit d0acc39

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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": {

src/Datastar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Craft;
99
use craft\web\Response;
1010
use putyourlightson\datastar\assets\DatastarAssetBundle;
11+
use putyourlightson\datastar\dumpers\SseDumper;
1112
use putyourlightson\datastar\models\Settings;
1213
use putyourlightson\datastar\services\SseService;
1314
use 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) {

src/dumpers/SseDumper.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

src/services/SseService.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Exception;
1313
use putyourlightson\datastar\Datastar;
1414
use putyourlightson\datastar\helpers\Request;
15+
use starfederation\datastar\enums\ElementPatchMode;
1516
use starfederation\datastar\events\EventInterface;
1617
use starfederation\datastar\events\ExecuteScript;
1718
use 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
*/

0 commit comments

Comments
 (0)