-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmx.module.php
More file actions
1232 lines (1075 loc) · 42.7 KB
/
Copy pathHtmx.module.php
File metadata and controls
1232 lines (1075 loc) · 42.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace ProcessWire;
use Totoglu\Htmx\Request;
use Totoglu\Htmx\Response;
use Totoglu\Htmx\Fragment;
use Totoglu\Htmx\Component;
use Totoglu\Htmx\Debug\HtmxTracyPanel;
/**
* HTMX v4 Module for ProcessWire
*
* Bringing HTMX v4 integrations to ProcessWire natively.
* Provides an elegant Request/Response API (HX-Request-Type, HX-Source, HX-Reselect,
* deferred HX-Trigger, <hx-partial>), Out-of-band swaps, Extensions,
* State-Aware Components, and SSE/WebSockets support.
*
* @property bool $loadFrontendAssets Load HTMX in Frontend?
* @property bool $loadHyperscript Load _hyperscript library?
* @property bool $autoFlashMessages Auto trigger flash messages?
* @property bool $autoExtractTargets Auto extract targets using DOM?
* @property array $extensions Extensions to load (ws, sse, loading, etc)
* @property string $endpointUrl URL hook endpoint for state components (default: /hx/req)
*
* @author Iskender TOTOGLU, @ukyo (community), @trk (GitHub)
* @website https://www.totoglu.com
*/
class Htmx extends WireData implements Module, ConfigurableModule
{
/** @var Request */
public $request;
/** @var Response */
public $response;
/** @var Fragment */
public $fragment;
/** @var array<string, string> component alias map */
protected array $components = [];
/** @var array<string> Valid and checked paths to components and UI that are allowed for rendering */
public array $allowedComponentPaths = [];
/** @var array<string, bool> Tracks which short-name aliases have already been registered (perf guard) */
private array $registeredAliases = [];
/**
* Resolve HMAC salt defensively from ProcessWire config.
*/
private function getHmacSalt(): string
{
$config = $this->wire('config');
if ($config === null || !isset($config->userAuthSalt) || !is_string($config->userAuthSalt) || $config->userAuthSalt === '') {
throw new \ProcessWire\WireException(
"HTMX HMAC Salt Missing: config->userAuthSalt must be a non-empty string for Htmx module state signing."
);
}
return $config->userAuthSalt;
}
/**
* Internal debug info for Tracy panel / response headers (per request).
* @var array<string, mixed>
*/
protected array $tracyDebug = [];
public static function getModuleInfo()
{
return [
'title' => 'HTMX',
'version' => 200,
'summary' => 'HTMX v4 integration: native Request/Response API (HX-Request-Type, HX-Source, HX-Reselect, deferred triggers, <hx-partial>), state-aware Components, OOB swaps, Morph/TextContent swaps, and SSE/WS extensions.',
'href' => 'https://github.com/trk/Htmx',
'author' => 'Iskender TOTOGLU @trk @ukyo',
'requires' => [
'PHP>=8.4',
'ProcessWire>=3.0.271'
],
'installs' => [],
'permissions' => [],
'icon' => 'code',
'autoload' => true,
'singular' => true
];
}
public function __construct()
{
$this->wire('classLoader')->addNamespace('Totoglu\Htmx', __DIR__ . '/src/');
$this->set('loadFrontendAssets', true);
$this->set('loadHyperscript', false);
$this->set('endpointUrl', '/hx/req');
$this->set('componentsPath', 'components/');
$this->set('uiPath', 'ui/');
$this->set('autoFlashMessages', true);
$this->set('autoExtractTargets', false);
$this->set('allowComponentPaths', false);
$this->set('oobStrictIdOnly', false);
$this->set('oobStrictSwapStyle', true);
$this->set('lifecycleBridge', true);
$this->set('lifecycleEventPrefix', 'pw-htmx');
$this->set('extensions', []);
$this->set('tracySupport', true);
}
public function wired()
{
$this->wire('htmx', $this);
}
public function init()
{
$this->request = new Request();
$this->response = new Response();
$this->fragment = new Fragment();
$sitePath = $this->wire('config')->paths->site;
$componentsDir = trim($this->componentsPath ?: 'components/', '/');
$uiDir = trim($this->uiPath ?: 'ui/', '/');
if (is_dir($sitePath . $componentsDir . '/')) {
$this->wire('classLoader')->addNamespace('Htmx\Component', $sitePath . $componentsDir . '/');
if ($this->allowComponentPaths) {
$this->allowedComponentPaths[] = $sitePath . $componentsDir . '/';
}
}
if (is_dir($sitePath . $uiDir . '/')) {
$this->wire('classLoader')->addNamespace('Htmx\Ui', $sitePath . $uiDir . '/');
if ($this->allowComponentPaths) {
$this->allowedComponentPaths[] = $sitePath . $uiDir . '/';
}
}
$this->discoverModuleComponents();
$this->wire()->config->htmx = $this->request->isHtmx() || $this->request->isBoosted();
}
public function ready()
{
if ($this->isTracySupportEnabled()) {
$this->primeTracyDebugContext();
$this->registerTracyPanel();
}
if ($this->inAdmin()) {
foreach ($this->getAssetUrls() as $url) {
$this->wire('config')->scripts->add($url);
}
foreach ($this->getAdminHelperAssetUrls() as $url) {
$this->wire('config')->scripts->add($url);
}
}
$this->endpointUrl = '/' . ltrim($this->endpointUrl ?: '/hx/req', '/');
if ($this->request->isHtmx() || $this->request->isBoosted()) {
$this->wire()->addHook($this->endpointUrl, $this, 'handleEndpoint');
}
$this->wire()->addHookAfter('Modules::refresh', function () {
$cache = $this->wire('cache');
if ($cache) {
$cache->delete('htmx.module-components');
}
});
$this->wire()->addHookAfter('Page::render', function (HookEvent $e) {
$e->replace = true;
$html = $e->return;
// 1. Target Auto-Extraction (Partial Render)
if ($this->autoExtractTargets && $this->request->isHtmx() && !$this->request->isBoosted()) {
$targetId = $this->request->target();
if ($targetId && preg_match('/^[A-Za-z][A-Za-z0-9\-_:.]*$/', $targetId) && strpos($html, 'id="' . $targetId . '"') !== false) {
try {
libxml_use_internal_errors(true);
$dom = new \DOMDocument();
$encodedHtml = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
if (@$dom->loadHTML('<?xml encoding="utf-8" ?>' . $encodedHtml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD)) {
$xpath = new \DOMXPath($dom);
$safeId = htmlspecialchars($targetId, ENT_XML1 | ENT_QUOTES, 'UTF-8');
$nodes = $xpath->query("//*[@id=\"{$safeId}\"]");
if ($nodes && $nodes->length > 0) {
$html = html_entity_decode($dom->saveHTML($nodes->item(0)));
}
}
libxml_clear_errors();
} catch (\Throwable $e) {
if ($this->wire('config')->debug) {
$this->wire('log')->error("HTMX autoExtractTargets DOM parser failed: " . $e->getMessage());
}
}
}
}
// 2. Inject OOB swaps + <hx-partial> entries if any accumulated
$oob = $this->fragment->getOobSwaps();
if (!empty($oob) && $this->request->isHtmx() && !$this->request->isBoosted()) {
$html .= "\n" . $oob;
}
// 3. Load Frontend Assets + Lifecycle Bridge
$isFullRender = !$this->request->isHtmx() || $this->request->isBoosted() || $this->request->isHistoryRestore();
if ($isFullRender && $this->lifecycleBridge && strpos($html, '</head>') !== false && strpos($html, 'window.__pwHtmxBridgeLoaded') === false) {
$prefix = $this->wire('sanitizer')->name((string) $this->lifecycleEventPrefix);
if ($prefix === '') {
$prefix = 'pw-htmx';
}
$bridge = <<<HTML
<script>
window.__pwHtmxBridgeLoaded = true;
window.pwHtmx = window.pwHtmx || { hooks: {} };
window.pwHtmx.on = window.pwHtmx.on || function(name, fn) {
if (!window.pwHtmx.hooks[name]) window.pwHtmx.hooks[name] = [];
window.pwHtmx.hooks[name].push(fn);
};
function pwHtmxEmit(name, detail) {
var alias = typeof name === 'string' ? name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase() : name;
try {
window.dispatchEvent(new CustomEvent('{$prefix}:' + name, { detail: detail }));
if (alias && alias !== name) {
window.dispatchEvent(new CustomEvent('{$prefix}:' + alias, { detail: detail }));
}
} catch (e) {}
var list = (window.pwHtmx && window.pwHtmx.hooks && window.pwHtmx.hooks[name]) ? window.pwHtmx.hooks[name] : [];
for (var i = 0; i < list.length; i++) {
try { list[i](detail); } catch (e) {}
}
if (alias && alias !== name) {
var aliasList = (window.pwHtmx && window.pwHtmx.hooks && window.pwHtmx.hooks[alias]) ? window.pwHtmx.hooks[alias] : [];
for (var j = 0; j < aliasList.length; j++) {
try { aliasList[j](detail); } catch (e) {}
}
}
}
// HTMX v4 colon-format lifecycle events
document.addEventListener('htmx:before:request', function(evt){ pwHtmxEmit('beforeRequest', evt.detail); });
document.addEventListener('htmx:after:request', function(evt){ pwHtmxEmit('afterRequest', evt.detail); });
document.addEventListener('htmx:before:swap', function(evt){ pwHtmxEmit('beforeSwap', evt.detail); });
document.addEventListener('htmx:after:swap', function(evt){ pwHtmxEmit('afterSwap', evt.detail); });
document.addEventListener('htmx:after:settle', function(evt){ pwHtmxEmit('afterSettle', evt.detail); });
document.addEventListener('htmx:config:request', function(evt){ pwHtmxEmit('configRequest', evt.detail); });
document.addEventListener('htmx:response:error', function(evt){ pwHtmxEmit('responseError', evt.detail); });
document.addEventListener('htmx:history:restore',function(evt){ pwHtmxEmit('historyRestore',evt.detail); });
</script>
HTML;
$html = str_replace('</head>', "{$bridge}\n</head>", $html);
}
if (!$this->inAdmin() && $this->loadFrontendAssets) {
if ($isFullRender) {
$scripts = "";
foreach ($this->getAssetUrls() as $url) {
$scripts .= "\n<script src=\"{$url}\"></script>";
}
// HTMX v4: ctx.request (fetch Request object) not ctx.xhr
$csrfName = $this->wire('session')->CSRF->getTokenName();
$csrfValue = $this->wire('session')->CSRF->getTokenValue();
$scripts .= <<<HTML
<script>
document.addEventListener('htmx:config:request', function(evt) {
var ctx = evt.detail && evt.detail.ctx ? evt.detail.ctx : null;
var request = ctx && ctx.request ? ctx.request : null;
if (!request || !request.method || request.method.toLowerCase() === "get") {
return;
}
// HTMX v4: request.body might be a FormData or plain object depending on phase.
// htmx provides evt.detail.parameters too for simple key injection.
if (evt.detail && evt.detail.parameters && typeof evt.detail.parameters === 'object') {
evt.detail.parameters['{$csrfName}'] = '{$csrfValue}';
}
if (request && request.body instanceof FormData) {
request.body.set('{$csrfName}', '{$csrfValue}');
}
if (request && request.headers && typeof request.headers.set === 'function') {
request.headers.set('X-Requested-With', 'XMLHttpRequest');
} else if (request && request.headers && typeof request.headers === 'object') {
request.headers['X-Requested-With'] = 'XMLHttpRequest';
}
});
</script>
HTML;
if (strpos($html, '</head>') !== false) {
$html = str_replace('</head>', "{$scripts}\n</head>", $html);
}
}
}
// 4. Auto Flash Messages (deferred afterSwap event)
if ($this->autoFlashMessages && ($this->request->isHtmx() || $this->request->isBoosted())) {
$flash = [];
try {
$notices = $this->wire('notices');
if ($notices && is_iterable($notices)) {
foreach ($notices as $n) {
$type = 'message';
if (strpos(get_class($n), 'Error') !== false) $type = 'error';
elseif (strpos(get_class($n), 'Warning') !== false) $type = 'warning';
$flash[] = ['type' => $type, 'text' => $n->text];
}
if (method_exists($notices, 'removeAll')) {
$notices->removeAll();
}
}
} catch (\Throwable $e) {
}
if (!empty($flash)) {
$this->response->triggerAfterSwap('pw-messages', $flash);
}
}
// 5. Flush deferred HTMX v4 response headers (HX-Trigger, HX-Reselect, HX-Retarget, HX-Reswap, etc)
if ($this->request->isHtmx() || $this->request->isBoosted()) {
$this->response->applyHeaders();
}
$this->sendTracyHeaders();
$e->return = $html;
});
}
/**
* Handle the stateless component POST / GET-QUERY request endpoint.
*
* HTMX v4: POST for mutations, GET with `query*` action names for read-only operations.
*/
public function handleEndpoint(\ProcessWire\HookEvent $e)
{
$input = $this->wire('input');
if ($input === null) {
$this->tracyDebug['errorCode'] = 'no_input';
$this->sendTracyHeaders();
http_response_code(500);
return "Internal Server Error";
}
$this->tracyDebug = [
'reqId' => $this->tracyDebug['reqId'] ?? null,
'isHtmx' => (bool) $this->request->isHtmx(),
'isBoosted' => (bool) $this->request->isBoosted(),
'isHistoryRestore' => (bool) $this->request->isHistoryRestore(),
'isFullPage' => (bool) $this->request->isFullPage(),
'isPartial' => (bool) $this->request->isPartial(),
'endpointUrl' => (string) $this->endpointUrl,
'method' => (string) ($_SERVER['REQUEST_METHOD'] ?? ''),
'uri' => (string) ($_SERVER['REQUEST_URI'] ?? ''),
'target' => $this->request ? $this->request->rawTarget() : null,
'targetTag' => $this->request ? $this->request->targetTag() : null,
'source' => $this->request ? $this->request->rawSource() : null,
'sourceTag' => $this->request ? $this->request->sourceTag() : null,
'triggerName' => $this->request ? $this->request->triggerName() : null,
'prompt' => $this->request ? $this->request->prompt() : null,
'currentUrl' => $this->request ? $this->request->currentUrl() : null,
'requestType' => $this->request ? $this->request->requestType() : null,
'postKeys' => is_array($_POST ?? null) ? array_keys($_POST) : [],
'getKeys' => is_array($_GET ?? null) ? array_keys($_GET) : [],
'component' => null,
'action' => $input->post('hx__action') ?: $input->get('hx__action'),
'stateKey' => null,
'oobCount' => null,
'errorCode' => null,
'exceptionClass' => null,
'timingMs' => null,
];
$this->primeTracyDebugContext();
$method = strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET'));
$isPost = ($method === 'POST');
// Prefer POST payload, then GET query (HTMX v4 hx-query / QUERY actions)
$payload = null;
$payloadKey = 'hx__state';
$target = $this->request ? $this->request->target() : null;
if ($target) {
$key = 'hx__state__' . $target;
$payload = $isPost ? $input->post($key) : $input->get($key);
if ($payload) {
$payloadKey = $key;
}
}
if (!$payload) {
$payload = $isPost ? $input->post('hx__state') : $input->get('hx__state');
}
if (is_array($payload)) {
$first = reset($payload);
$payload = is_string($first) ? $first : '';
}
if (!$payload) {
$this->wire('log')->error("HTMX Endpoint Failed: Missing hx__state in request data.");
$this->tracyDebug['errorCode'] = 'missing_state';
$this->tracyDebug['stateKey'] = $payloadKey;
$this->sendTracyHeaders();
$this->response->applyHeaders();
http_response_code(400);
return "Bad Request: Missing HTMX State Payload.";
}
$parts = explode('|', $payload, 2);
if (count($parts) !== 2) {
$this->wire('log')->error("HTMX Endpoint Failed: Malformed payload. Parts count: " . count($parts));
$this->tracyDebug['errorCode'] = 'malformed_state';
$this->tracyDebug['stateKey'] = $payloadKey;
$this->sendTracyHeaders();
$this->response->applyHeaders();
http_response_code(400);
return "Bad Request: Malformed HTMX State Payload.";
}
list($encoded, $hash) = $parts;
try {
$salt = $this->getHmacSalt();
} catch (\Throwable $e) {
$this->wire('log')->error("HTMX Endpoint Failed: " . $e->getMessage());
$this->tracyDebug['errorCode'] = 'missing_salt';
$this->tracyDebug['stateKey'] = $payloadKey;
$this->sendTracyHeaders();
$this->response->applyHeaders();
http_response_code(500);
return "Internal Server Error: Module crypto configuration missing.";
}
$expectedHash = hash_hmac('sha256', $encoded, $salt);
if (!hash_equals($expectedHash, $hash)) {
$this->wire('log')->error("HTMX Endpoint Failed: Invalid HMAC signature.");
$this->tracyDebug['errorCode'] = 'bad_hmac';
$this->tracyDebug['stateKey'] = $payloadKey;
$this->sendTracyHeaders();
$this->response->applyHeaders();
http_response_code(403);
return "Forbidden: Invalid HTMX State Signature.";
}
$decodedJson = base64_decode($encoded, true);
$decoded = is_string($decodedJson) ? json_decode($decodedJson, true) : null;
if (!is_array($decoded) || empty($decoded['__cmp']) || !is_string($decoded['__cmp'])) {
$this->wire('log')->error("HTMX Endpoint Failed: Invalid State Structure or missing __cmp.");
$this->tracyDebug['errorCode'] = 'invalid_state';
$this->tracyDebug['stateKey'] = $payloadKey;
$this->sendTracyHeaders();
$this->response->applyHeaders();
http_response_code(400);
return "Bad Request: Invalid State Structure.";
}
$class = $decoded['__cmp'];
$aliasLookup = $this->components[$class] ?? null;
if (is_string($aliasLookup)) {
$class = $aliasLookup;
} else {
$knownFullNames = array_flip(array_values($this->components));
if (!isset($knownFullNames[$class])) {
$this->wire('log')->error("HTMX Endpoint Failed: Component not registered with global endpoint. Class: " . $class);
$this->tracyDebug['errorCode'] = 'component_unregistered';
$this->tracyDebug['component'] = $class;
$this->tracyDebug['stateKey'] = $payloadKey;
$this->sendTracyHeaders();
$this->response->applyHeaders();
http_response_code(400);
return "Bad Request: Component not registered for the global HTMX endpoint.";
}
}
if (!class_exists($class) || !is_subclass_of($class, Component::class)) {
$this->wire('log')->error("HTMX Endpoint Failed: Invalid Component Class. Class: " . $class);
$this->tracyDebug['errorCode'] = 'invalid_component';
$this->tracyDebug['component'] = $class;
$this->tracyDebug['stateKey'] = $payloadKey;
$this->sendTracyHeaders();
$this->response->applyHeaders();
http_response_code(400);
return "Bad Request: Invalid Component Class.";
}
try {
$t0 = microtime(true);
/** @var Component $cmp */
$cmp = new $class();
$this->tracyDebug['component'] = $class;
$this->tracyDebug['stateKey'] = $payloadKey;
if ($payloadKey !== 'hx__state' && method_exists($cmp, 'setStateKey')) {
$cmp->setStateKey($payloadKey);
}
$tHydrate = microtime(true);
$cmp->hydrate();
$tAfterHydrate = microtime(true);
$tAction = microtime(true);
$cmp->executeAction();
$tAfterAction = microtime(true);
$tRender = microtime(true);
$html = $cmp->renderToString();
$tAfterRender = microtime(true);
// 1. Inject accumulated OOB swaps + <hx-partial>
$oob = $this->fragment->getOobSwaps();
if (!empty($oob)) {
$html .= "\n" . $oob;
}
if ($oob === '' || $oob === null) {
$oobCount = 0;
} else {
$oobSplit = preg_split('/\R+/', trim((string) $oob), -1, PREG_SPLIT_NO_EMPTY);
$oobCount = is_array($oobSplit) ? count($oobSplit) : 1;
}
$this->tracyDebug['oobCount'] = $oobCount;
$this->tracyDebug['timingMs'] = [
'hydrate' => (int) round(($tAfterHydrate - $tHydrate) * 1000),
'action' => (int) round(($tAfterAction - $tAction) * 1000),
'render' => (int) round(($tAfterRender - $tRender) * 1000),
'total' => (int) round((microtime(true) - $t0) * 1000),
];
// 2. Process Auto Flash Messages (deferred afterSwap)
if ($this->autoFlashMessages) {
$flash = [];
try {
$notices = $this->wire('notices');
if ($notices && is_iterable($notices)) {
foreach ($notices as $n) {
$type = 'message';
if (strpos(get_class($n), 'Error') !== false) $type = 'error';
elseif (strpos(get_class($n), 'Warning') !== false) $type = 'warning';
$flash[] = ['type' => $type, 'text' => $n->text];
}
if (method_exists($notices, 'removeAll')) {
$notices->removeAll();
}
}
} catch (\Throwable $ex) {
}
if (!empty($flash)) {
$this->response->triggerAfterSwap('pw-messages', $flash);
}
}
// 3. Flush HTMX v4 headers (single HX-Trigger with deferred swap/settle blocks)
$this->response->applyHeaders();
$this->sendTracyHeaders();
return $html;
} catch (\Throwable $ex) {
$this->wire('log')->error("HTMX Endpoint Crash: " . $ex->getMessage() . " in " . $ex->getFile() . ":" . $ex->getLine() . "\nTrace: " . $ex->getTraceAsString());
$this->tracyDebug['errorCode'] = 'exception';
$this->tracyDebug['exceptionClass'] = get_class($ex);
if ($this->isTracySupportEnabled() && class_exists(\Tracy\Debugger::class)) {
try {
\Tracy\Debugger::log($ex);
} catch (\Throwable $ignore) {
}
}
$this->response->applyHeaders();
$this->sendTracyHeaders($ex);
if ($this->isTracySupportEnabled() && $this->wire('config')->debug) {
throw $ex;
}
http_response_code(500);
if ($this->wire('config')->debug) {
return "<!-- HTMX Endpoint Error: " . htmlspecialchars($ex->getMessage(), ENT_QUOTES, 'UTF-8') . " in " . $ex->getFile() . ":" . $ex->getLine() . " -->";
}
return "Internal Server Error";
}
}
protected function isTracySupportEnabled(): bool
{
if (!(bool) $this->tracySupport) return false;
$config = $this->wire('config');
if (!$config || !(bool) $config->debug) return false;
$modules = $this->wire('modules');
if (!$modules || !method_exists($modules, 'isInstalled') || !$modules->isInstalled('TracyDebugger')) return false;
if (!class_exists(\Tracy\Debugger::class)) return false;
return true;
}
protected function registerTracyPanel(): void
{
try {
$bar = \Tracy\Debugger::getBar();
if (!$bar) return;
$bar->addPanel(new HtmxTracyPanel(function (): array {
return $this->getTracyPanelData();
}));
} catch (\Throwable $ignore) {
}
}
/**
* @return array<string, mixed>
*/
protected function getTracyPanelData(): array
{
$this->primeTracyDebugContext();
$data = $this->tracyDebug ?: [];
$data['debugEnabled'] = (bool) ($this->wire('config')->debug ?? false);
$data['tracySupport'] = (bool) $this->tracySupport;
return $data;
}
protected function primeTracyDebugContext(): void
{
if (!$this->request || !($this->request->isHtmx() || $this->request->isBoosted())) return;
$input = $this->wire('input');
if (!isset($this->tracyDebug['reqId']) || !$this->tracyDebug['reqId']) {
$this->tracyDebug['reqId'] = substr(str_replace('.', '', uniqid('hx', true)), 0, 24);
}
$defaults = [
'reqId' => $this->tracyDebug['reqId'],
'isHtmx' => (bool) $this->request->isHtmx(),
'isBoosted' => (bool) $this->request->isBoosted(),
'isHistoryRestore' => (bool) $this->request->isHistoryRestore(),
'isFullPage' => (bool) $this->request->isFullPage(),
'isPartial' => (bool) $this->request->isPartial(),
'endpointUrl' => (string) $this->endpointUrl,
'method' => (string) ($_SERVER['REQUEST_METHOD'] ?? ''),
'uri' => (string) ($_SERVER['REQUEST_URI'] ?? ''),
'target' => $this->request ? $this->request->rawTarget() : null,
'targetTag' => $this->request ? $this->request->targetTag() : null,
'source' => $this->request ? $this->request->rawSource() : null,
'sourceTag' => $this->request ? $this->request->sourceTag() : null,
'triggerName' => $this->request ? $this->request->triggerName() : null,
'prompt' => $_SERVER['HTTP_HX_PROMPT'] ?? null,
'currentUrl' => $_SERVER['HTTP_HX_CURRENT_URL'] ?? null,
'requestType' => $_SERVER['HTTP_HX_REQUEST_TYPE'] ?? null,
'postKeys' => is_array($_POST ?? null) ? array_keys($_POST) : [],
'getKeys' => is_array($_GET ?? null) ? array_keys($_GET) : [],
'component' => $this->tracyDebug['component'] ?? null,
'action' => ($this->tracyDebug['action'] ?? null) ?? ($input ? ($input->post('hx__action') ?: $input->get('hx__action')) : null),
'stateKey' => $this->tracyDebug['stateKey'] ?? null,
'oobCount' => $this->tracyDebug['oobCount'] ?? null,
'errorCode' => $this->tracyDebug['errorCode'] ?? null,
'exceptionClass' => $this->tracyDebug['exceptionClass'] ?? null,
'timingMs' => $this->tracyDebug['timingMs'] ?? null,
];
foreach ($defaults as $k => $v) {
if (!array_key_exists($k, $this->tracyDebug)) {
$this->tracyDebug[$k] = $v;
}
}
}
protected function sendTracyHeaders(?\Throwable $ex = null): void
{
if (!$this->isTracySupportEnabled()) return;
if (!$this->request || !($this->request->isHtmx() || $this->request->isBoosted())) return;
if (headers_sent()) return;
$this->primeTracyDebugContext();
$d = $this->tracyDebug ?: [];
$safe = static function ($v): string {
$s = is_scalar($v) ? (string) $v : '';
$s = preg_replace('/\\s+/', ' ', $s ?? '');
return substr($s, 0, 200);
};
header('X-PW-HTMX: 1');
if (!empty($d['reqId'])) header('X-PW-HTMX-ReqId: ' . $safe($d['reqId']));
if (!empty($d['component'])) header('X-PW-HTMX-Component: ' . $safe($d['component']));
if (!empty($d['action'])) header('X-PW-HTMX-Action: ' . $safe($d['action']));
if (!empty($d['target'])) header('X-PW-HTMX-Target: ' . $safe($d['target']));
if (!empty($d['source'])) header('X-PW-HTMX-Source: ' . $safe($d['source']));
if (!empty($d['stateKey'])) header('X-PW-HTMX-StateKey: ' . $safe($d['stateKey']));
if (isset($d['oobCount'])) header('X-PW-HTMX-OOB: ' . $safe($d['oobCount']));
if (isset($d['requestType'])) header('X-PW-HTMX-RequestType: ' . $safe($d['requestType']));
if (!empty($d['errorCode'])) {
header('X-PW-HTMX-Error-Code: ' . $safe($d['errorCode']));
}
if ($ex) {
header('X-PW-HTMX-Exception: ' . $safe(get_class($ex)));
} elseif (!empty($d['exceptionClass'])) {
header('X-PW-HTMX-Exception: ' . $safe($d['exceptionClass']));
}
}
public function registerComponent(string $alias, string $class): self
{
$this->components[$alias] = $class;
return $this;
}
public function renderComponent(string $classOrAlias, array $props = [], mixed $view = null): string
{
$class = $this->components[$classOrAlias] ?? $classOrAlias;
if (!class_exists($class) || !is_subclass_of($class, Component::class)) {
if ($this->wire('config')->debug) {
return "<!-- HTMX Error: {$class} is not a valid Totoglu\Htmx\Component. -->";
}
return '';
}
try {
/** @var Component $cmp */
$cmp = new $class();
if ($cmp->requestUrl() === $this->endpointUrl) {
$shortName = (new \ReflectionClass($cmp))->getShortName();
if (!isset($this->registeredAliases[$shortName]) || ($this->components[$shortName] ?? null) !== $class) {
$this->registerComponent($shortName, $class);
$this->registeredAliases[$shortName] = true;
}
}
if ($view !== null) {
$cmp->setView($view);
}
$cmp->fill($props);
$cmp->mount();
$cmp->hydrate();
$cmp->executeAction();
return (string) $cmp;
} catch (\Throwable $e) {
if ($this->wire('config')->debug) {
return "<!-- HTMX Component Lifecycle Error ({$class}): " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8') . " -->";
}
return '';
}
}
public function loadExtension($extension): self
{
$extensions = (array) $extension;
$config = $this->wire('config');
$extDir = rtrim($config->paths->$this, '/') . '/resources/htmx/ext/';
foreach ($extensions as $ext) {
$normalized = $this->normalizeExtensionName((string) $ext);
$debugFile = $extDir . $normalized . '.js';
$minFile = $extDir . $normalized . '.min.js';
$fileExists = file_exists($debugFile) || file_exists($minFile);
if (!$fileExists) {
if ($config->debug) {
$this->wire('log')->warning(
"Htmx module: extension '{$normalized}' requested but file not found at {$extDir}{$normalized}.(min.).js. Skipping."
);
}
continue;
}
if (!in_array($normalized, $this->extensions, true)) {
$this->extensions[] = $normalized;
if ($this->inAdmin()) {
$suffix = $config->debug ? '.js' : (file_exists($minFile) ? '.min.js' : '.js');
$config->scripts->add($this->getHtmxBaseUrl() . "ext/{$normalized}{$suffix}");
}
}
}
return $this;
}
public function loadHyperscript(bool $load = true): self
{
if ($load && !$this->loadHyperscript) {
$this->loadHyperscript = true;
if ($this->inAdmin()) {
$config = $this->wire('config');
$minified = $config->debug ? '.js' : '.min.js';
$config->scripts->add($this->getHyperscriptBaseUrl() . "_hyperscript" . $minified);
}
} else {
$this->loadHyperscript = $load;
}
return $this;
}
/**
* Developer Convenience API: Opt-in to HTMX and inject assets for this specific request.
*
* Example: $htmx->use('class-tools');
* Example: $htmx->use(extensions: ['sse', 'ws'], hyperscript: true);
*
* @param string|array $extensions
* @param bool|null $hyperscript
*/
public function use($extensions = [], ?bool $hyperscript = null): self
{
$this->loadFrontendAssets = true;
if (!empty($extensions)) {
$this->loadExtension($extensions);
}
if ($hyperscript !== null) {
$this->loadHyperscript($hyperscript);
}
return $this;
}
public function renderScripts(): string
{
$scripts = "";
foreach ($this->getAssetUrls() as $url) {
$scripts .= "<script src=\"{$url}\"></script>\n";
}
$csrfName = $this->wire('session')->CSRF->getTokenName();
$csrfValue = $this->wire('session')->CSRF->getTokenValue();
$scripts .= <<<HTML
<script>
document.addEventListener('htmx:config:request', function(evt) {
var ctx = evt.detail && evt.detail.ctx ? evt.detail.ctx : null;
var request = ctx && ctx.request ? ctx.request : null;
if (!request || !request.method || request.method.toLowerCase() === "get") {
return;
}
if (evt.detail && evt.detail.parameters && typeof evt.detail.parameters === 'object') {
evt.detail.parameters['{$csrfName}'] = '{$csrfValue}';
}
if (request && request.body instanceof FormData) {
request.body.set('{$csrfName}', '{$csrfValue}');
}
if (request && request.headers && typeof request.headers.set === 'function') {
request.headers.set('X-Requested-With', 'XMLHttpRequest');
} else if (request && request.headers && typeof request.headers === 'object') {
request.headers['X-Requested-With'] = 'XMLHttpRequest';
}
});
</script>
HTML;
$this->loadFrontendAssets = false;
return $scripts;
}
protected function getAssetUrls(): array
{
/** @var Config $config */
$config = $this->wire('config');
$minified = $config->debug ? '.js' : '.min.js';
$extDir = rtrim($config->paths->$this, '/') . '/resources/htmx/ext/';
$urls = [$this->getHtmxBaseUrl() . "htmx" . $minified];
// Runtime helper dispatches deferred HX-Trigger blocks at afterSwap / afterSettle
$urls[] = $this->getHtmxBaseUrl() . "pw-htmx-runtime.js";
if ($this->inAdmin() || $this->loadHyperscript) {
$urls[] = $this->getHyperscriptBaseUrl() . "_hyperscript" . $minified;
}
foreach ($this->getNormalizedExtensions() as $ext) {
$debugFile = $extDir . $ext . '.js';
$minFile = $extDir . $ext . '.min.js';
if (!file_exists($debugFile) && !file_exists($minFile)) {
if ($config->debug) {
$this->wire('log')->warning(
"Htmx module: extension '{$ext}' file not found. Skipping from frontend asset URLs."
);
}
continue;
}
$suffix = $config->debug ? '.js' : (file_exists($minFile) ? '.min.js' : '.js');
$urls[] = $this->getHtmxBaseUrl() . "ext/{$ext}{$suffix}";
}
return $urls;
}
/**
* @return array<int, string>
*/
protected function getAdminHelperAssetUrls(): array
{
$urls = [
$this->getHtmxBaseUrl() . 'pw-csrf.js',
$this->getHtmxBaseUrl() . 'pw-htmx-guard.js',
];
if ($this->wire('config')->debug) {
$urls[] = $this->getHtmxBaseUrl() . 'pw-htmx-debug.js';
}
return $urls;
}
protected function getHtmxBaseUrl(): string
{
return $this->wire('config')->urls->siteModules . $this->className() . "/resources/htmx/";
}
protected function getHyperscriptBaseUrl(): string
{
return $this->wire('config')->urls->siteModules . $this->className() . "/resources/_hyperscript/";
}
/**
* @return array<int, string>
*/
protected function getNormalizedExtensions(): array
{
return array_values(array_unique(array_map(
fn(string $ext): string => $this->normalizeExtensionName($ext),
array_map('strval', (array) $this->extensions)
)));
}
/**
* HTMX v4 extension name normalizer.
*
* Legacy short names (ws, sse, head-support) map to their hx-* canonical names
* so a single list works regardless of naming convention.
*/
protected function normalizeExtensionName(string $extension): string
{
$extension = trim($extension);
return match ($extension) {
'ws' => 'hx-ws',
'sse' => 'hx-sse',
'head-support', 'head' => 'hx-head',
'preload' => 'hx-preload',
'response-targets', 'targets' => 'hx-targets',
'prompt' => 'hx-prompt',
'loading' => 'hx-loading',
'disable-element', 'disabled-elt' => 'hx-disable-element',
'path-deps' => 'hx-path-deps',
'remove-me' => 'hx-remove-me',
'restore', 'restore-history' => 'hx-restore',
'class-tools' => 'hx-class-tools',
'include-vals', 'vals' => 'hx-include-vals',
default => $extension,
};
}
/**
* Configuration options (admin UI).
*/
public function getModuleConfigInputfields(InputfieldWrapper $inputfields)
{
$modules = $this->wire('modules');
$f = $modules->get('InputfieldText');
$f->attr('name', 'endpointUrl');
$f->label = $this->_('HTMX Component Endpoint URL');
$f->description = $this->_('The dedicated stateless path where HTMX Component POST/QUERY actions are dispatched to (e.g. `hx/req`). Must be a valid URI path.');
$f->value = $this->endpointUrl;
$f->columnWidth = 100;
$inputfields->add($f);
$f = $modules->get('InputfieldText');
$f->attr('name', 'componentsPath');
$f->label = $this->_('Components Directory Path');
$f->description = $this->_('The directory relative to your `site/` folder where HTMX components are stored. Default is `components/`. If this directory exists, classes inside it will be registered under the `Htmx\Component` namespace.');
$f->value = $this->componentsPath;
$f->columnWidth = 50;
$inputfields->add($f);
$f = $modules->get('InputfieldText');
$f->attr('name', 'uiPath');
$f->label = $this->_('UI Directory Path');
$f->description = $this->_('The directory relative to your `site/` folder where UI elements are stored. Default is `ui/`. If this directory exists, classes inside it will be registered under the `Htmx\Ui` namespace.');
$f->value = $this->uiPath;
$f->columnWidth = 50;
$inputfields->add($f);
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'allowComponentPaths');
$f->label = $this->_('Allow Component Paths in File Render');
$f->description = $this->_('When enabled, the configured components and UI directories will be automatically added to `allowedPaths` in `$files->render()`.');
$f->checked = (bool)$this->allowComponentPaths;
$f->columnWidth = 100;
$inputfields->add($f);
$f = $modules->get('InputfieldCheckbox');