-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge-wire.html
More file actions
1308 lines (1158 loc) · 72 KB
/
forge-wire.html
File metadata and controls
1308 lines (1158 loc) · 72 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
<!DOCTYPE html>
<html lang="en" class="scroll-smooth">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forge Kernel - ForgeWire</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/docs.css">
</head>
<body class="bg-gray-50 text-gray-900">
<!-- Navigation -->
<nav class="bg-white shadow-sm border-b">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex items-center">
<div class="flex-shrink-0">
<h1 class="text-xl font-bold text-gray-900">
Forge Kernel
</h1>
</div>
<div class="hidden sm:ml-6 sm:flex sm:space-x-8">
<a href="index.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600">
Home
</a>
<a href="getting-started.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600">
Getting Started
</a>
<a href="core-concepts.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600">
Core Concepts
</a>
<a href="modules.html"
class="text-blue-600 inline-flex items-center px-1 pt-1 text-sm font-medium border-b-2 border-blue-600">
Capabilities
</a>
<a href="api-reference.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600">
API Reference
</a>
<a href="tutorial.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600">
Tutorials
</a>
</div>
</div>
<div class="flex items-center sm:hidden">
<button id="mobile-menu-button" class="text-gray-700 hover:text-blue-600 p-2">
<i class="fas fa-bars text-xl"></i>
</button>
</div>
</div>
</div>
<div id="mobile-menu" class="sm:hidden hidden bg-white border-t border-gray-200">
<div class="px-2 pt-2 pb-3 space-y-1">
<a href="index.html" class="block px-3 py-2 text-gray-900 hover:text-blue-600">Home</a>
<a href="getting-started.html" class="block px-3 py-2 text-gray-900 hover:text-blue-600">Getting
Started</a>
<a href="core-concepts.html" class="block px-3 py-2 text-gray-900 hover:text-blue-600">Core Concepts</a>
<a href="modules.html" class="block px-3 py-2 text-blue-600 font-medium">Capabilities</a>
<a href="api-reference.html" class="block px-3 py-2 text-gray-900 hover:text-blue-600">API Reference</a>
<a href="tutorial.html" class="block px-3 py-2 text-gray-900 hover:text-blue-600">Tutorials</a>
</div>
</div>
</nav>
<!-- Main Content -->
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="flex flex-col lg:flex-row gap-8">
<!-- Sidebar Navigation -->
<div id="sidebar-nav" class="lg:w-1/4">
<div class="bg-white rounded-lg shadow-sm p-6 sticky top-8">
<h3 class="text-lg font-semibold text-gray-900 mb-4">ForgeWire</h3>
<nav class="space-y-2">
<a href="#overview"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Overview</a>
<a href="#architecture"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Architecture
& Design</a>
<a href="#installation"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Installation</a>
<a href="#asset-management"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Asset
Management</a>
<a href="#basic-usage"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Basic
Usage</a>
<a href="#component-structure"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Component
Structure</a>
<a href="#attributes"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Attributes
System</a>
<a href="#security"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Security
Features</a>
<a href="#state-management"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">State
Management</a>
<a href="#actions"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Actions
& Methods</a>
<a href="#validation"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Validation</a>
<a href="#frontend"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Frontend
Integration</a>
<a href="#architecture-features"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Architecture
& Features</a>
<a href="#wire-protocol"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Wire
Protocol</a>
<a href="#performance"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Performance
Optimizations</a>
<a href="#cli-commands"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">CLI
Commands</a>
<a href="#wire-protocol"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Wire
Protocol</a>
<a href="#performance"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Performance</a>
<a href="#design-philosophy"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Design
Philosophy</a>
<a href="#examples"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Usage
Examples</a>
<a href="#suggestions"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">Suggestions</a>
</nav>
</div>
</div>
<!-- Main Content -->
<div class="lg:w-3/4">
<div class="bg-white rounded-lg shadow-sm p-8">
<h1 class="text-4xl font-bold text-gray-900 mb-6">ForgeWire</h1>
<p class="text-xl text-gray-600 mb-8">
Lightweight Livewire-like reactive components for Forge Kernel. Build dynamic interfaces with
server-side interactivity, security by default, and support for complex data types.
</p>
<!-- Overview -->
<section id="overview" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Overview</h2>
<p class="text-gray-600 mb-6">
ForgeWire provides Livewire-like reactivity for <strong>standard Controllers</strong>. It
enables server-side interactivity without WebSockets, JavaScript frameworks, or complex
client-side state management. Unlike other frameworks, you don't need to create separate
"component classes" — any controller can become reactive with simple attributes.
</p>
<div class="bg-blue-50 border border-blue-200 rounded-lg p-6 mb-6">
<h2 class="font-semibold text-lg mb-4 text-blue-900">ForgeWire Power, Standard Controller
Simplicity</h2>
<div class="grid md:grid-cols-2 gap-4">
<div class="space-y-2">
<div class="flex items-center text-blue-800"><i
class="fas fa-check-circle mr-2"></i><span>No separate component classes
needed</span></div>
<div class="flex items-center text-blue-800"><i
class="fas fa-check-circle mr-2"></i><span>Reactive standard Controllers
(#[Reactive])</span></div>
<div class="flex items-center text-blue-800"><i
class="fas fa-check-circle mr-2"></i><span>Security by default (explicit
attributes)</span></div>
</div>
<div class="space-y-2">
<div class="flex items-center text-blue-800"><i
class="fas fa-check-circle mr-2"></i><span>Session-based state with signed
checksums</span></div>
<div class="flex items-center text-blue-800"><i
class="fas fa-check-circle mr-2"></i><span>Native support for DTOs and
Models</span></div>
<div class="flex items-center text-blue-800"><i
class="fas fa-check-circle mr-2"></i><span>Ultra-lightweight JS
client</span></div>
</div>
</div>
</div>
</section>
<!-- Architecture & Design -->
<section id="architecture" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Architecture & Design Philosophy</h2>
<p class="text-gray-600 mb-6">
ForgeWire transforms the standard request-response cycle into a persistent, reactive loop.
It achieves this by intercepting requests and re-rendering specific view fragments based on
the updated state of your controller.
</p>
<h3 class="text-lg font-semibold mb-3">The Anatomy of a Reactive Controller</h3>
<p class="text-gray-600 mb-4">
When a controller is marked with <code
class="bg-gray-100 px-2 py-1 rounded">#[Reactive]</code>, ForgeWire manages its
lifecycle:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>State Hydration:</strong> Upon an action, ForgeWire restores the controller's
properties from the session state.</li>
<li><strong>Action Execution:</strong> The requested method is called with arguments
provided by the frontend.</li>
<li><strong>View Re-rendering:</strong> After the action, the controller's primary view is
re-rendered with the updated state and sent back to the client.</li>
<li><strong>Checksum Security:</strong> Every state transfer is signed. Tampering with state
or calling unauthorized methods is impossible.</li>
</ul>
</section>
<!-- Basic Usage -->
<section id="basic-usage" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Basic Usage</h2>
<p class="text-gray-600 mb-6">
Creating a reactive feature requires three steps: mark the controller, mark the state, and
wrap the view with island identifiers.
</p>
<h3 class="text-lg font-semibold mb-3">1. The Controller</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php">#[Middleware("web")]
#[Reactive]
final class CounterController
{
use ControllerHelper;
#[State]
public int $count = 0;
#[Route("/counter")]
public function index(): Response
{
return $this->view("pages/counter", ['count' => $this->count]);
}
#[Action]
public function increment(): void
{
$this->count++;
}
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">2. The View with Islands</h3>
<p class="text-gray-600 mb-4">
Use <code class="bg-gray-100 px-2 py-1 rounded">fw_id()</code> helper to create reactive islands. <strong>Without this helper, there is no reactivity - the island boundary is required for ForgeWire to work.</strong> You can have multiple islands per page, each with its own reactive state.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><!-- resources/views/pages/counter.php -->
<div <?= fw_id('main-counter') ?> class="counter-box">
<h1>Count: <?= $count ?></h1>
<button fw:click="increment">Add One</button>
<div fw:target>Result: <?= $count ?></div>
</div>
<!-- Another island on same page -->
<div <?= fw_id('info-panel') ?> class="info-box">
<p fw:poll.5s>Updated at: <?= date('H:i:s') ?></p>
</div></code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">3. Island Architecture Benefits</h3>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Required Boundaries:</strong> <code class="bg-gray-100 px-2 py-1 rounded">fw_id()</code> creates the reactive boundary - no reactivity without it</li>
<li><strong>Multiple Islands:</strong> Create independent reactive areas on same page</li>
<li><strong>Targeted Updates:</strong> Use <code class="bg-gray-100 px-2 py-1 rounded">fw:target</code> for efficient partial updates</li>
<li><strong>Shared State:</strong> Islands can share state using <code class="bg-gray-100 px-2 py-1 rounded">#[State(shared: true)]</code> with <code class="bg-gray-100 px-2 py-1 rounded">fw:depends</code></li>
<li><strong>Independent Polling:</strong> Each island can have its own polling interval</li>
</ul>
</section>
<!-- Attributes System -->
<section id="attributes" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Attributes System</h2>
<p class="text-gray-600 mb-6">
ForgeWire attributes function as <strong>permissions and behavior modifiers</strong>, not data exposure. No data is automatically exposed to views - you explicitly pass data in your controller methods.
</p>
<h3 class="text-lg font-semibold mb-3">#[Reactive]</h3>
<p class="text-gray-600 mb-4">
Applied to the class. <strong>Permission gate</strong> - enables ForgeWire to monitor this controller. Controllers without this attribute are completely ignored by the reactive engine for security.
</p>
<h3 class="text-lg font-semibold mb-3">#[State(shared: bool)]</h3>
<p class="text-gray-600 mb-4">
Applied to properties. <strong>Permission to persist</strong> - marks properties that should be tracked between requests. The <code class="bg-gray-100 px-2 py-1 rounded">shared: true</code> option allows multiple islands to access the same state value.
</p>
<h3 class="text-lg font-semibold mb-3">#[Action(submit: bool)]</h3>
<p class="text-gray-600 mb-4">
Applied to methods. <strong>Permission to call</strong> - marks the method as callable from browser. Without this attribute, methods cannot be invoked from frontend. The optional <code class="bg-gray-100 px-2 py-1 rounded">submit</code> parameter indicates this action handles form submissions.
</p>
<h3 class="text-lg font-semibold mb-3">#[Validate]</h3>
<p class="text-gray-600 mb-4">
Applied to properties. <strong>Validation rules</strong> - defines validation rules and custom error messages for properties that receive user input.
</p>
</section>
<section id="state-management" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">State Management</h2>
<h3 class="text-lg font-semibold mb-3">#[State(shared: bool)]</h3>
<p class="text-gray-600 mb-4">
Applied to public properties. <strong>Permission to persist</strong> - marks properties that should be tracked between requests. Supports scalars (int, string, bool), arrays, and objects. The <code class="bg-gray-100 px-2 py-1 rounded">shared: true</code> option allows multiple islands to access the same state value.
</p>
<h3 class="text-lg font-semibold mb-3">Shared State Across Islands</h3>
<p class="text-gray-600 mb-4">
Use <code class="bg-gray-100 px-2 py-1 rounded">#[State(shared: true)]</code> to share state across multiple islands on the same page. Islands must opt-in to shared dependencies using <code class="bg-gray-100 px-2 py-1 rounded">fw:depends</code> to receive updates when shared state changes.
</p>
<div class="bg-yellow-50 border border-yellow-200 rounded-lg p-4 mb-4">
<p class="text-sm text-yellow-800 mb-2">
<strong>⚠️ Important:</strong> Islands <strong>must</strong> opt-in to shared state dependencies using <code class="bg-gray-100 px-2 py-1 rounded">fw:depends</code>. Without this directive, islands will not receive shared state updates even if marked with #[State(shared: true)].
</p>
</div>
<h4 class="text-md font-semibold mb-3 mt-4">Shared State Implementation</h4>
<div class="code-block p-6 text-white rounded-lg mb-4">
<pre><code class="language-php">// Controller
#[State(shared: true)]
public int $counter = 0;
#[State(shared: true)]
public array $jobs = [];</code></pre>
</div>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-html"><!-- View - Islands opt-in to specific shared state properties -->
<div <?= fw_id('todo-app') ?> fw:depends="counter">
<h1>Reactive Todo List counter: <?= $counter ?></h1>
<!-- Content that uses $counter -->
</div>
<div <?= fw_id('counter-app') ?> fw:depends="counter">
<h1>Counter</h1>
<button fw:click="increment">Increment</button>
<div fw:target><?= $counter ?></div>
</div>
<!-- Multiple dependencies separated by comma -->
<div <?= fw_id('queue-stats') ?> fw:depends="jobs,stats">
<h2>Statistics</h2>
<p fw:target>Total Jobs: <?= count($jobs) ?></p>
</div></code></pre>
</div>
<h4 class="text-md font-semibold mb-3">How Shared State Works</h4>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Mark Shared:</strong> Add <code class="bg-gray-100 px-2 py-1 rounded">#[State(shared: true)</code> to properties that should be shared</li>
<li><strong>Opt-In Dependencies:</strong> Islands use <code class="bg-gray-100 px-2 py-1 rounded">fw:depends="prop1,prop2"</code> to specify which shared properties they need</li>
<li><strong>Automatic Updates:</strong> When shared state changes, only islands that depend on that property are re-rendered</li>
<li><strong>Multiple Dependencies:</strong> Islands can depend on multiple shared properties separated by commas</li>
</ul>
<div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-4">
<p class="text-sm text-blue-800">
<strong>Use Cases:</strong> Perfect for shopping carts, user notifications, queue dashboards, or any data that needs to stay synchronized across different components on same page.
</p>
</div>
<div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-4">
<p class="text-sm text-blue-800">
<strong>Use Case:</strong> Perfect for shopping carts, user notifications, or any data that needs to stay synchronized across different components on the same page.
</p>
</div>
</section>
<!-- Frontend Directives -->
<section id="directives" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Frontend Directives</h2>
<p class="text-gray-600 mb-6">
ForgeWire uses HTML attributes (directives) to bind your UI to your controller.
</p>
<h3 class="text-lg font-semibold mb-3">Binding Data: fw:model</h3>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:model="name"</code> - Immediate binding
(updates on every keystroke)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:model.debounce="name"</code> - Updates
after 600ms of inactivity</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:model.defer="name"</code> - Updates only
when an action is triggered</li>
</ul>
<h3 class="text-lg font-semibold mb-3">Handling Events</h3>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:click="save"</code> - Call action on
click</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:submit="create"</code> - Call action on
form submit (auto-prevents refresh)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:keydown.enter="search"</code> - Call
action on Enter key</li>
</ul>
<h3 class="text-lg font-semibold mb-3">Passing Arguments: fw:param-*</h3>
<p class="text-gray-600 mb-4">
To pass data to an action, use the <code
class="bg-gray-100 px-2 py-1 rounded">fw:param-</code> prefix.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-html"><!-- Controller: public function delete(int $id) -->
<button fw:click="delete" fw:param-id="123">Delete Post</button></code></pre>
</div>
</section>
<!-- Security -->
<section id="security" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Security: The Sandbox</h2>
<p class="text-gray-600 mb-4">
ForgeWire operates on a "deny-by-default" principle:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Checksum signing:</strong> All state sent to the browser is HMAC-signed with
your <code class="bg-gray-100 px-2 py-1 rounded">APP_KEY</code>. If a user modifies the
state in the console, the server rejects the request.</li>
<li><strong>Method Whitelisting:</strong> Only <code
class="bg-gray-100 px-2 py-1 rounded">#[Action]</code> methods can be invoked. Even
protected/private methods are strictly blocked.</li>
<li><strong>Fingerprinting:</strong> ForgeWire tracks the controller class and current URL
path to prevent "component injection" or cross-page state reuse.</li>
</ul>
</section>
<section id="actions" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Actions & Methods</h2>
<h3 class="text-lg font-semibold mb-3">The #[Action] Attribute</h3>
<p class="text-gray-600 mb-4">
Applied to methods. Marks the method as "safe" to be called from the browser. ForgeWire will
refuse to call any method not explicitly marked as an action.
</p>
<p class="text-gray-600 mb-4">
The <code class="bg-gray-100 px-2 py-1 rounded">input()</code> action is built-in and
handles
<code>fw:model</code> updates:
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php">// Automatically called for fw:model
#[Action]
public function input(...$keys): void
{
// Called when fw:model values change
// $keys contains which properties changed
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">Calling Actions from Frontend</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-html"><!-- fw:click -->
<button fw:click="increment">+</button>
<button fw:click="update" fw:param-id="5" fw:param-name="New Name">Update</button>
<!-- fw:submit -->
<form fw:submit="save">
<input fw:model="name" type="text">
<button type="submit">Save</button>
</form></code></pre>
</div>
</section>
<!-- Validation -->
<section id="validation" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Validation</h2>
<p class="text-gray-600 mb-6">
Add validation rules to properties using #[Validate] attribute. Validation supports both array and JSON message formats for backward compatibility.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php">#[State]
#[Validate('required|min:3', messages: ['required' => 'Name is required', 'min' => 'Name must be at least :value characters'])]
public string $formName = '';
#[State]
#[Validate('required|email', messages: ['required' => 'Email is required', 'email' => 'Please enter a valid email address'])]
public string $formEmail = '';</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">Validation Features</h3>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Automatic Validation:</strong> Runs when properties marked with #[Validate] are updated</li>
<li><strong>Custom Messages:</strong> Provide user-friendly error messages for each rule</li>
<li><strong>Parameter Replacement:</strong> Use <code class="bg-gray-100 px-2 py-1 rounded">:value</code> placeholders in messages</li>
<li><strong>Form Submission:</strong> Use <code class="bg-gray-100 px-2 py-1 rounded">#[Action(submit: true)]</code> for form handling</li>
</ul>
<p class="text-gray-600 mb-4">
Validation runs automatically when dirty state includes the property.
</p>
</section>
<section id="frontend" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Frontend Integration</h2>
<p class="text-gray-600 mb-6">
ForgeWire's JavaScript client handles all frontend interactions automatically.
</p>
<h3 class="text-lg font-semibold mb-3">Directives Reference</h3>
<h4 class="text-md font-semibold mb-2">Event Handling</h4>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:click="save"</code> — Call action on
click (use <code>fw:param-*</code> for arguments)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:submit="save"</code> — Form
submission (auto-prevents refresh)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:keydown.enter="submit"</code> — Call action on Enter key</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:keydown.escape="cancel"</code> — Call action on Escape key</li>
</ul>
<h4 class="text-md font-semibold mb-2">Data Binding</h4>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:model="property"</code> — Two-way
binding (immediate)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:model.lazy</code> — Update on
blur/change</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:model.debounce</code> — Debounced
updates (600ms default)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:model.debounce.300ms</code> — Custom
debounce time</li>
</ul>
<h4 class="text-md font-semibold mb-2">Polling & Updates</h4>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:poll</code> — Auto-refresh every 2
seconds</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:poll.5s</code> — Custom poll interval</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:poll.3s fw:action="onPoll"</code> — Custom action on poll</li>
</ul>
<h4 class="text-md font-semibold mb-2">Island Management</h4>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:id("name")</code> — PHP helper to create unique island identifier</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:target</code> — Marks element for partial updates (performance optimization)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:loading</code> — Shows content during action processing</li>
</ul>
<h4 class="text-md font-semibold mb-2">Shared State</h4>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:depends="prop1,prop2"</code> — Opt-in to shared state dependencies (comma-separated)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:validation-error="fieldName"</code> — Display validation errors</li>
</ul>
<div class="bg-red-50 border border-red-200 rounded-lg p-4 mb-4">
<p class="text-sm text-red-800">
<strong>⚠️ Deprecated:</strong> <code class="bg-gray-100 px-2 py-1 rounded">fw:shared</code> is deprecated and should not be used. Use <code class="bg-gray-100 px-2 py-1 rounded">fw:depends</code> to opt-in to shared state dependencies.
</p>
</div>
<h4 class="text-md font-semibold mb-2">Arguments & Parameters</h4>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:param-id="123"</code> — Pass parameters to actions</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:param-name="value"</code> — String parameters</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:param-count="5"</code> — Numeric parameters</li>
</ul>
<h3 class="text-lg font-semibold mb-3">Island Management</h3>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:id("name")</code> — PHP helper to create unique island identifier</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:target</code> — Marks element for partial updates (performance optimization)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">fw:loading</code> — Shows content during action processing</li>
</ul>
<h3 class="text-lg font-semibold mb-3">Advanced Directives</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-html"><!-- Multiple islands on same page -->
<div <?= fw_id('counter-1') ?>>
<button fw:click="increment">+1</button>
<div fw:target>Count: <?= $count ?></div>
</div>
<div <?= fw_id('search-box') ?>>
<input fw:model.debounce="query" placeholder="Search...">
<div fw:loading>Searching...</div>
<div fw:target><?= $results ?></div>
</div></code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">Example HTML</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-html"><div fw:id="fw-0b4c5e3d9e83" >
<div class="counter">
<button fw:click="decrement">–</button>
<span>2 (even)</span>
<button fw:click="increment">+</button>
</div>
</div></code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">Polling with IntersectionObserver</h3>
<p class="text-gray-600 mb-4">
Components only poll when visible on screen (performance optimization):
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-html"><div fw:id="..." fw:poll.5s>
<!-- Only polls when visible -->
</div></code></pre>
</div>
</section>
<!-- ForgeWire Architecture -->
<section id="architecture-features" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">ForgeWire Architecture & Reactive Protocol</h2>
<p class="text-gray-600 mb-6">
ForgeWire implements a <strong>reactive protocol for PHP</strong> that enables server-side interactivity without WebSockets or complex client-side state management. It's built specifically for Forge's architecture and follows PHP conventions.
</p>
<h3 class="text-lg font-semibold mb-3">Core Principles</h3>
<div class="grid md:grid-cols-2 gap-6 mb-6">
<div class="border border-gray-200 rounded-lg p-6">
<h4 class="font-semibold text-lg mb-3">Permission-Based Attributes</h4>
<p class="text-sm text-gray-600 mb-2">Attributes work as permissions, not data exposure</p>
<ul class="text-sm text-gray-600 space-y-1">
<li>• #[Reactive] = Permission to monitor</li>
<li>• #[Action] = Permission to call</li>
<li>• #[State] = Permission to persist</li>
<li>• No automatic data exposure</li>
</ul>
</div>
<div class="border border-gray-200 rounded-lg p-6">
<h4 class="font-semibold text-lg mb-3">Island Boundaries Required</h4>
<p class="text-sm text-gray-600 mb-2">Reactivity only works with explicit island boundaries</p>
<ul class="text-sm text-gray-600 space-y-1">
<li>• fw_id() creates reactive boundary</li>
<li>• No reactivity without fw_id()</li>
<li>• Multiple islands per page</li>
<li>• Explicit data passing required</li>
</ul>
</div>
</div>
<h3 class="text-lg font-semibold mb-3">CLI Tools & Generation</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-bash"># Generate new ForgeWire island
forgewire:island --type=app --name=interactive-counter --kind=component
# Generate for module
forgewire:island --type=module --module=ForgeEvents --name=queue-dashboard --kind=page
# Interactive wizard
forgewire:island</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">ReactiveControllerHelper Trait</h3>
<p class="text-gray-600 mb-4">
Use this trait for enhanced reactive functionality:
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php">use ReactiveControllerHelper;
// Available methods
public function redirect(string $url, int $delay = 0): void
public function flash(string $type, string $message): void
public function dispatch(string $event, array $data = []): void
public function isWireRequest(Request $request): bool
public function isReactive(): bool</code></pre>
</div>
</section>
<!-- Wire Protocol -->
<section id="wire-protocol" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Wire Protocol</h2>
<p class="text-gray-600 mb-6">
ForgeWire uses a JSON-based protocol for client-server communication with advanced features like shared state updates.
</p>
<h3 class="text-lg font-semibold mb-3">Request Format</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-json">{
"id": "fw-0b4c5e3d9e83",
"controller": "App\\Controllers\\ForgeWireExamplesController",
"action": "incrementShared",
"args": [],
"dirty": {},
"depends": ["counter"],
"checksum": "632b5d500042bb4361657ea9f2991132d8a18208d0e38bf6a801f127f48f4e8c",
"fingerprint": {
"path": "/forge-wire-examples"
}
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">Response Format</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-json">{
"html": "<div fw:id=\"fw-0b4c5e3d9e83\"...>...</div>",
"state": {},
"checksum": "632b5d500042bb4361657ea9f2991132d8a18208d0e38bf6a801f127f48f4e8c",
"events": [{"name": "counterUpdated", "data": {"value": 5}}],
"redirect": null,
"flash": [{"type": "success", "message": "Counter updated"}],
"updates": [
{
"id": "fw-secondary-island",
"html": "<div fw:id=\"fw-secondary-island\">Updated content</div>",
"state": {},
"checksum": "abc123..."
}
]
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">Advanced Protocol Features</h3>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Shared State Updates:</strong> Multiple islands can update simultaneously when shared state changes</li>
<li><strong>Partial Updates:</strong> <code class="bg-gray-100 px-2 py-1 rounded">updates</code> array contains HTML for affected islands</li>
<li><strong>Event Dispatching:</strong> Client-side events can be triggered from server</li>
<li><strong>Flash Messages:</strong> Session-based flash messages for user feedback</li>
<li><strong>Redirect Support:</strong> Server-initiated redirects with optional delays</li>
</ul>
</section>
<!-- Performance -->
<section id="performance" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Performance Optimizations</h2>
<p class="text-gray-600 mb-6">
ForgeWire includes several performance optimizations for production use.
</p>
<h3 class="text-lg font-semibold mb-3">Targeted Updates with fw:target</h3>
<p class="text-gray-600 mb-4">
Instead of re-rendering entire islands, use <code class="bg-gray-100 px-2 py-1 rounded">fw:target</code> for partial updates:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li>Only the marked element gets updated on server response</li>
<li>Dramatically reduces HTML payload size</li>
<li>Perfect for counters, status messages, progress indicators</li>
</ul>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-html"><div <?= fw_id('counter') ?>>
<button fw:click="increment">+</button>
<!-- Only this div updates -->
<div fw:target>Count: <?= $count ?></div>
</div></code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">Loading States</h3>
<p class="text-gray-600 mb-4">
Show loading indicators during long-running actions using <code class="bg-gray-100 px-2 py-1 rounded">fw:loading</code>:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li>Automatically shows during action execution</li>
<li>Hidden when action completes</li>
<li>Provides immediate user feedback</li>
</ul>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-html"><div <?= fw_id('uploader') ?>>
<button fw:click="upload">Upload File</button>
<div fw:loading class="loading-spinner">
Processing... Please wait
</div>
<div fw:target><?= $status ?></div>
</div></code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">Recipe Caching</h3>
<p class="text-gray-600 mb-4">
Component recipes are built once per class and cached in memory:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li>No repeated reflection on every request</li>
<li>Recipe defines hydration/dehydration strategy</li>
<li>Cached in static array: <code
class="bg-gray-100 px-2 py-1 rounded">self::$recipe[$class]</code></li>
</ul>
<h3 class="text-lg font-semibold mb-3">IntersectionObserver Polling</h3>
<p class="text-gray-600 mb-4">
Components only poll when visible on screen:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li>Uses browser IntersectionObserver API</li>
<li>Stops polling when component is off-screen</li>
<li>Resumes automatically when visible again</li>
<li>Saves server resources</li>
</ul>
<h3 class="text-lg font-semibold mb-3">Request Prioritization</h3>
<p class="text-gray-600 mb-4">
Input events have lower priority than actions:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Permission-Based Attributes:</strong> Attributes work as permissions, not data exposure</li>
<li>• #[Reactive] = Permission to monitor</li>
<li>• #[Action] = Permission to call</li>
<li>• #[State] = Permission to persist</li>
<li>• #[Validate] = Validation rules</li>
<li>• No automatic data exposure</li>
</ul>
</section>
<!-- Design Philosophy -->
<section id="design-philosophy" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Design Philosophy & Principles</h2>
<p class="text-gray-600 mb-6">
ForgeWire follows PHP conventions and explicit permission patterns for secure, predictable server-side reactivity.
</p>
<div class="grid md:grid-cols-2 gap-6 mb-6">
<div class="border border-gray-200 rounded-lg p-6">
<h3 class="font-semibold text-lg mb-3">Security by Default</h3>
<ul class="text-sm text-gray-600 space-y-2">
<li><strong>Explicit Permissions:</strong> Attributes act as permission gates</li>
<li><strong>No Auto-Exposure:</strong> Nothing is exposed without explicit attributes</li>
<li><strong>Checksum Protection:</strong> All state transfers are signed and verified</li>
<li><strong>Action Whitelisting:</strong> Only #[Action] methods can be called</li>
</ul>
</div>
<div class="border border-gray-200 rounded-lg p-6">
<h3 class="font-semibold text-lg mb-3">PHP Conventions</h3>
<ul class="text-sm text-gray-600 space-y-2">
<li><strong>Standard Controllers:</strong> No special component classes needed</li>
<li><strong>Explicit Data Flow:</strong> You control what data reaches views</li>
<li><strong>Native PHP Types:</strong> Works with PHP 8.3+ type system</li>
<li><strong>Framework Agnostic:</strong> Built for Forge, follows PHP standards</li>
</ul>
</div>
<div class="border border-gray-200 rounded-lg p-6">
<h3 class="font-semibold text-lg mb-3">Performance Focused</h3>
<ul class="text-sm text-gray-600 space-y-2">
<li><strong>Island Boundaries:</strong> Only re-render what changes</li>
<li><strong>Targeted Updates:</strong> fw:target for partial updates</li>
<li><strong>Recipe Caching:</strong> No repeated reflection overhead</li>
<li><strong>Intersection Observer:</strong> Smart polling when visible</li>
</ul>
</div>
<div class="border border-gray-200 rounded-lg p-6">
<h3 class="font-semibold text-lg mb-3">Developer Experience</h3>
<ul class="text-sm text-gray-600 space-y-2">
<li><strong>CLI Generation:</strong> forgewire:island command for scaffolding</li>
<li><strong>Clear Errors:</strong> Validation with custom messages</li>
<li><strong>Reactive Helper:</strong> Built-in methods for common patterns</li>
<li><strong>Debug Support:</strong> Comprehensive debugging and error handling</li>
</ul>
</div>
</div>
</section>
<!-- Usage Examples -->
<section id="examples" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Usage Examples</h2>
<p class="text-gray-600 mb-6">
Complete examples of ForgeWire components based on real implementations.
</p>
<h3 class="text-lg font-semibold mb-3">Comprehensive Example Controller</h3>
<p class="text-gray-600 mb-4">Based on ForgeWireExamplesController - shows all major features:</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php">#[Middleware("web")]
#[Reactive]
final class ForgeWireExamplesController
{
use ControllerHelper;
#[State]
public int $pollCount = 0;
#[State]
public int $counter = 0;
#[State(shared: true)]
public int $sharedCounter = 0;
#[State]
public string $immediateValue = '';
#[State]
public string $lazyValue = '';
#[State]
public string $debounceValue = '';
#[State]
#[Validate('required|min:3', messages: ['required' => 'Name is required', 'min' => 'Name must be at least :value characters'])]
public string $formName = '';
#[State]
#[Validate('required|email', messages: ['required' => 'Email is required', 'email' => 'Please enter a valid email address'])]
public string $formEmail = '';
#[Action]
public function onPoll(): void
{
$this->pollCount++;
}
#[Action]
public function increment(): void
{
$this->counter += $this->step;
}
#[Action(submit: true)]
public function saveForm(): void
{
$this->formMessage = 'Form saved successfully at ' . date('H:i:s');
}
#[Action]
public function handleEnter(): void
{
$this->lastKey = 'Enter pressed at ' . date('H:i:s');
}
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3">Complex Reactive Controller - Queue Management</h3>
<p class="text-gray-600 mb-4">Real-world example from ForgeEvents module showing advanced patterns:</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php">#[Reactive]
#[Middleware('web')]
#[Middleware('auth')]
final class QueueController
{
use ControllerHelper;
use ReactiveControllerHelper;
#[State(shared: true)]
public array $jobs = [];
#[State]
public string $statusFilter = '';
#[State]
public string $queueFilter = '';
#[State]
public string $search = '';
#[State]
public int $currentPage = 1;
#[State]
public array $selectedJobs = [];
#[State]
public bool $showJobModal = false;
public function __construct(
private readonly QueueHubService $queueService
) {}
#[Route("/hub/queues")]
public function index(): Response
{
$this->loadJobs();
$this->loadStats();
return $this->view("pages/hub/queues", [
'jobs' => $this->jobs,
'selectedJobs' => $this->selectedJobs,
'filters' => [
'status' => $this->statusFilter,
'queue' => $this->queueFilter,
'search' => $this->search,
],
]);
}
#[Action]
public function filterJobs(string $status, string $queue): void
{
$this->statusFilter = $status;
$this->queueFilter = $queue;
$this->currentPage = 1;
$this->loadJobs();
}
#[Action]
public function selectJob(int $jobId): void
{
$this->selectedJobs[] = $jobId;
}
#[Action]
public function bulkDelete(): void
{
foreach ($this->selectedJobs as $jobId) {
$this->queueService->deleteJob($jobId);
}
$this->selectedJobs = [];
$this->loadJobs();
}
private function loadJobs(): void