-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore-concepts.html
More file actions
1756 lines (1575 loc) · 88.1 KB
/
core-concepts.html
File metadata and controls
1756 lines (1575 loc) · 88.1 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>Core Concepts - Forge Kernel Documentation</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-blue-600 inline-flex items-center px-1 pt-1 text-sm font-medium border-b-2 border-blue-600">
Core Concepts
</a>
<a href="modules.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-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>
<!-- Mobile menu button -->
<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>
<!-- Mobile menu -->
<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-blue-600 font-medium">
Core Concepts
</a>
<a href="modules.html" class="block px-3 py-2 text-gray-900 hover:text-blue-600">
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">
<!-- Mobile menu overlay -->
<div id="mobile-menu-overlay" class="mobile-menu-overlay"></div>
<div class="bg-white rounded-lg shadow-sm p-6 sticky top-8">
<h3 class="text-lg font-semibold text-gray-900 mb-4">
Core Concepts
</h3>
<nav class="space-y-2">
<a href="#architecture"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
Architecture
</a>
<a href="#dependency-injection"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
Dependency Injection
</a>
<a href="#routing-system"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
Routing System
</a>
<a href="#middleware"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
Middleware
</a>
<a href="#view-engine"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
View Engine
</a>
<a href="#components"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
Components
</a>
<a href="#database-orm"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
Database & ORM
</a>
<a href="#capability-system"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
Capability System
</a>
<a href="#configuration"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
Configuration
</a>
<a href="#async-processing"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
Async Processing
</a>
<a href="#cli-kernel"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600">
CLI Kernel
</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">
Core Concepts
</h1>
<p class="text-xl text-gray-600 mb-8">
How Forge Kernel works under the hood.
</p>
<section id="architecture" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Architecture</h2>
<p class="text-gray-600 mb-4">
Forge Kernel uses a modular architecture with dependency injection. It's built to be simple
and flexible. Think of it like a foundation with utilities — you get the foundation,
plumbing, electrical, and basic structure. You decide what to build on top.
</p>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Kernel Components (Built-in)</h3>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li>Kernel Core</li>
<li>Router</li>
<li>View Engine</li>
<li>DI Container</li>
<li>Capability Loader</li>
<li>CLI Kernel</li>
<li>Bootstrap</li>
<li>Config Manager</li>
</ul>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Capabilities (Pluggable)</h3>
<p class="text-gray-600 mb-4">
These are <strong>not</strong> built into the kernel. Install them as capabilities when
needed:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li>Database (ForgeDatabaseSQL)</li>
<li>ORM (ForgeSqlOrm)</li>
<li>Authentication (ForgeAuth)</li>
<li>Storage (ForgeStorage)</li>
<li>And more...</li>
</ul>
</section>
<section id="dependency-injection" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Dependency Injection</h2>
<p class="text-gray-600 mb-4">
Forge's DI container is like a smart warehouse manager. It knows where everything is stored,
gets you what you need when you ask, and manages dependencies automatically. It resolves
dependencies using PHP attributes, keeping code testable and clean.
</p>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Service Discovery</h3>
<p class="text-gray-600 mb-4">
Services are automatically discovered from <strong>any folder</strong> in your application
or modules when they have the <code class="bg-gray-100 px-2 py-1 rounded">#[Service]</code>
or <code class="bg-gray-100 px-2 py-1 rounded">#[Discoverable]</code> attribute. The
framework recursively scans all directories, so you can organize your code however you
prefer.
</p>
<p class="text-gray-600 mb-4">
<strong>Attributes:</strong>
</p>
<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">#[Service]</code> - Register a class as a
service in the dependency injection container</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">#[Discoverable]</code> - Semantically marks
a class as discoverable (same behavior as <code
class="bg-gray-100 px-2 py-1 rounded">#[Service]</code>, useful for non-service
classes that need DI)</li>
</ul>
<p class="text-gray-600 mb-4">
<strong>Discovery Scope:</strong> Services are discovered from:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li>All directories under <code class="bg-gray-100 px-2 py-1 rounded">app/</code></li>
<li>All directories under <code class="bg-gray-100 px-2 py-1 rounded">modules/*/src/</code>
</li>
<li>Engine core directories</li>
</ul>
<p class="text-gray-600 mb-4">
Service discovery happens once at bootstrap and uses an incremental class map cache for
performance. The cache automatically updates when files change. If you add a new service and
it's not being discovered, clear the cache with <code
class="bg-gray-100 px-2 py-1 rounded">php forge.php cache:flush</code>.
</p>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Service Registration</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
use Forge\Core\DI\Attributes\Service;
#[Service]
class UserService
{
public function __construct(
private UserRepository $repository,
private EmailService $emailService
) {}
public function createUser(array $data): User
{
$user = $this->repository->create($data);
$this->emailService->sendWelcomeEmail($user);
return $user;
}
}
#[Service(singleton: true)] // Only one instance throughout the application
class CacheService
{
private array $cache = [];
public function get(string $key): mixed
{
return $this->cache[$key] ?? null;
}
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Interface Binding</h3>
<p class="text-gray-600 mb-4">
You can bind interfaces to implementations using the container's <code
class="bg-gray-100 px-2 py-1 rounded">bind()</code> method. In modules, this is
typically done in the module's <code class="bg-gray-100 px-2 py-1 rounded">register()</code>
method.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
// In a module's register() method
namespace App\Modules\ForgeAuth;
use Forge\Core\DI\Container;
use App\Modules\ForgeAuth\Contracts\ForgeAuthInterface;
use App\Modules\ForgeAuth\Services\ForgeAuthService;
use App\Modules\ForgeAuth\Contracts\UserRepositoryInterface;
use App\Modules\ForgeAuth\Repositories\UserRepository;
public function register(Container $container): void
{
// Bind interface to implementation
$container->bind(ForgeAuthInterface::class, ForgeAuthService::class);
// Bind with closure for complex dependencies
$container->bind(UserRepositoryInterface::class, function ($container) {
return new UserRepository($container->get(QueryCache::class));
});
}</code></pre>
</div>
<p class="text-gray-600 mb-4">
<strong>Note:</strong> There is no <code
class="bg-gray-100 px-2 py-1 rounded">#[Bind]</code> attribute. Interface binding is
done programmatically in the container, typically in module registration or service
providers.
</p>
</section>
<section id="routing-system" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Routing System</h2>
<p class="text-gray-600 mb-4">
Attribute-based routing that auto-discovers routes from controllers. No separate route files
needed.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
use Forge\Core\Routing\Route;
use Forge\Core\Http\Request;
use Forge\Core\Http\Response;
class ApiController
{
#[Route("/api/users")]
public function listUsers(): Response
{
return $this->json(User::query()->get());
}
#[Route("/api/users/{id}", method: "GET")]
public function getUser(Request $request, int $id): Response
{
$user = User::query()->id($id)->first();
return $this->json($user);
}
#[Route("/api/users", method: "POST")]
public function createUser(Request $request): Response
{
$data = $request->json();
$user = User::create($data);
return $this->json($user, 201);
}
#[Route("/api/users/{id}", method: "PUT")]
public function updateUser(Request $request, int $id): Response
{
$user = User::query()->id($id)->first();
$user->update($request->json());
return $this->json($user);
}
}</code></pre>
</div>
<p class="text-gray-600 mb-4">
<strong>Note:</strong> Use curly braces for route parameters. Names must match method
params.
</p>
</section>
<section id="middleware" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Middleware</h2>
<p class="text-gray-600 mb-4">
Filter HTTP requests before they hit your application.
</p>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Built-in Middleware</h3>
<p class="text-gray-600 mb-4">
The engine comes with a comprehensive set of middleware organized into groups:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Global group:</strong>
<ul class="list-disc list-inside ml-6 mt-2 space-y-1">
<li><code class="bg-gray-100 px-2 py-1 rounded">SessionMiddleware</code> (order: 0)
</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">RateLimitMiddleware</code> (order:
0)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">CircuitBreakerMiddleware</code>
(order: 1)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">CorsMiddleware</code> (order: 2)
</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">SanitizeInputMiddleware</code>
(order: 3, enabled: false by default)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">CompressionMiddleware</code> (order:
4)</li>
</ul>
</li>
<li><strong>Web group:</strong>
<ul class="list-disc list-inside ml-6 mt-2 space-y-1">
<li><code class="bg-gray-100 px-2 py-1 rounded">CsrfMiddleware</code> (order: 1)
</li>
<li><code
class="bg-gray-100 px-2 py-1 rounded">RelaxSecurityHeadersMiddleware</code>
(order: 3)</li>
</ul>
</li>
<li><strong>API group:</strong>
<ul class="list-disc list-inside ml-6 mt-2 space-y-1">
<li><code class="bg-gray-100 px-2 py-1 rounded">IpWhiteListMiddleware</code> (order:
0)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">ApiKeyMiddleware</code> (order: 1)
</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">ApiMiddleware</code> (order: 2)</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">CookieMiddleware</code> (order: 2)
</li>
</ul>
</li>
</ul>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Middleware Configuration</h3>
<p class="text-gray-600 mb-4">
Configure middleware groups, order, and overrides in <code
class="bg-gray-100 px-2 py-1 rounded">config/middleware.php</code>:
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
// config/middleware.php
return [
'global' => [
// Add your own middleware to global group
\App\Middlewares\CustomMiddleware::class,
],
'web' => [
// Override order or add middleware to web group
\App\Middlewares\CustomWebMiddleware::class,
],
'api' => [
// Create custom API middleware groups
\App\Middlewares\ApiAuthMiddleware::class,
],
'api-auth' => [
// Create new middleware groups
App\Modules\ForgeAuth\Middlewares\ApiJwtMiddleware::class,
]
];</code></pre>
</div>
<p class="text-gray-600 mb-4">
You can:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Create new middleware groups:</strong> Add new keys to the array</li>
<li><strong>Override order:</strong> Change the order of built-in middleware by adjusting
their position in the array</li>
<li><strong>Override built-in middleware:</strong> If you create a middleware with the same
class name as a built-in one, your custom version will be used instead</li>
<li><strong>Apply groups:</strong> Use <code
class="bg-gray-100 px-2 py-1 rounded">#[Middleware("group-name")]</code> on
controllers or methods</li>
</ul>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Creating Middleware</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
use Forge\Core\Http\Middleware;
use Forge\Core\Http\Request;
use Forge\Core\Http\Response;
class AuthMiddleware extends Middleware
{
public function handle(Request $request, callable $next): Response
{
if (!$request->hasHeader('Authorization')) {
return new Response('Unauthorized', 401);
}
// Continue to next middleware or controller
return $next($request);
}
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Applying Middleware</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
use Forge\Core\Http\Attributes\Middleware;
// Apply middleware to entire controller
#[Middleware("auth")]
class DashboardController
{
#[Route("/dashboard")]
public function index(): Response
{
return $this->view('dashboard/index');
}
#[Route("/dashboard/settings")]
public function settings(): Response
{
return $this->view('dashboard/settings');
}
}
// Apply middleware to specific methods
class UserController
{
#[Middleware("App\Modules\ForgeAuth\Middlewares\AuthMiddleware")]
#[Route("/profile")]
public function profile(): Response
{
return $this->view('user/profile');
}
// Multiple middleware using repeatable attribute
#[Middleware("web")]
#[Middleware("auth")]
#[Route("/admin")]
public function admin(): Response
{
return $this->view('admin/dashboard');
}
}</code></pre>
</div>
<p class="text-gray-600 mb-4">
Use <code>#[Middleware]</code> for controllers/methods, or <code>middlewares</code> in
<code>#[Route]</code> for routes.
</p>
</section>
<section id="view-engine" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">View Engine</h2>
<p class="text-gray-600 mb-4">
PHP-first templating. No new syntax to learn.
</p>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Template Inheritance</h3>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><!-- layouts/app.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= $title ?? 'My App' ?></title>
<?php use Forge\Core\View\View; echo View::section('head'); ?>
</head>
<body>
<header>
<?php use Forge\Core\View\View; echo View::section('header'); ?>
</header>
<main>
<?php use Forge\Core\View\View; echo View::section('content'); ?>
</main>
<footer>
<?php use Forge\Core\View\View; echo View::section('footer'); ?>
</footer>
</body>
</html></code></pre>
</div>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Child Templates</h3>
<p class="text-gray-600 mb-4">
The <code class="bg-gray-100 px-2 py-1 rounded">loadFromModule</code> parameter is optional
and defaults to <code class="bg-gray-100 px-2 py-1 rounded">false</code>. When <code
class="bg-gray-100 px-2 py-1 rounded">false</code> (default), the layout must exist in
<code class="bg-gray-100 px-2 py-1 rounded">app/resources/views/layouts/</code>. When <code
class="bg-gray-100 px-2 py-1 rounded">true</code>, the layout is loaded from <code
class="bg-gray-100 px-2 py-1 rounded">modules/ModuleName/src/Resources/views/layouts/</code>.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><!-- pages/home.php -->
<?php
use Forge\Core\View\View;
// Default (loadFromModule: false) - layout must be in app/resources/views/layouts/
View::layout(name: "layouts/app");
// Or explicitly specify
View::layout(name: "layouts/app", loadFromModule: false);
// Load from module
View::layout(name: "layouts/app", loadFromModule: true);
?>
<?php View::startSection('head'); ?>
<link rel="stylesheet" href="/css/home.css">
<?php View::endSection(); ?>
<?php View::startSection('content'); ?>
<div class="hero">
<h1>Welcome to <?= $appName ?></h1>
<p><?= $description ?></p>
</div>
<?php View::endSection(); ?></code></pre>
</div>
</section>
<section id="components" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Components</h2>
<p class="text-gray-600 mb-4">
Components in Forge are simple, reusable UI pieces. Unlike other frameworks that
force you into complex class structures, Forge components are just standard PHP
templates. They are lightning-fast to render and easy to organize.
</p>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Component Locations</h3>
<p class="text-gray-600 mb-4">
Forge resolutions components from several standard locations:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>App Scope:</strong> <code>app/resources/components/</code></li>
<li><strong>Module Scope (Standard):</strong>
<code>modules/ModuleName/src/Resources/components/</code>
</li>
<li><strong>Module Scope (Views):</strong>
<code>modules/ModuleName/src/Resources/views/</code>
</li>
</ul>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Creating a Component</h3>
<p class="text-gray-600 mb-4">
A component is just a PHP file. Variables passed as props are automatically
extracted for use in the template.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><!-- app/resources/components/ui/alert.php -->
<div class="alert alert-<?= $type ?? 'info' ?>">
<?= $message ?>
</div></code></pre>
</div>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Using Components</h3>
<p class="text-gray-600 mb-4">
Use the global <code>component()</code> helper to render your UI pieces anywhere.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
// Render an app-scope component
echo component('ui/alert', [
'type' => 'success',
'message' => 'Operation successful!'
]);
// Render a module-scope component using ":" syntax
echo component('ForgeNexus:sidebar/item', [
'label' => 'Dashboard',
'icon' => 'fa-home'
]);
?></code></pre>
</div>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Automatic Data Extraction</h3>
<p class="text-gray-600 mb-4">
Forge's view engine is smart. If you pass an array, it extracts its keys. If you
pass an <strong>object</strong>, it automatically extracts all its public properties
into local variables. This is perfect for passing DTOs or reactive state objects
directly to components.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
// Passing an object as props
$user = new UserDto(name: "John Doe", email: "john@example.com");
echo component('user/card', $user);
?>
<!-- Inside user/card.php, $name and $email are available! -->
<div class="card">
<h3><?= $name ?></h3>
<p><?= $email ?></p>
</div></code></pre>
</div>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Key Benefits</h3>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Pure PHP:</strong> No new syntax to learn, full IDE support.</li>
<li><strong>No Overheard:</strong> No base component classes or complex lifecycles
for simple UI.</li>
<li><strong>Flexible:</strong> Pass arrays or objects seamlessly.</li>
<li><strong>Namespaced:</strong> Easily share components across modules using the
<code>Module:path</code> syntax.
</li>
</ul>
</section>
<section id="database-orm" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">Database & ORM (Capabilities)</h2>
<p class="text-gray-600 mb-4">
Database and ORM are <strong>not built into the kernel</strong>. They're
capabilities you install when you need them. The kernel provides <strong>contracts
(interfaces)</strong> for database operations, but these contracts must be
implemented by a module.
</p>
<p class="text-gray-600 mb-4">
<strong>Important:</strong> The kernel provides <code
class="bg-gray-100 px-2 py-1 rounded">DatabaseConnectionInterface</code> and
<code class="bg-gray-100 px-2 py-1 rounded">QueryBuilderInterface</code> contracts,
but they won't work unless you install a module that implements them. For example:
</p>
<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">ForgeDatabaseSQL</code> implements
<code class="bg-gray-100 px-2 py-1 rounded">DatabaseConnectionInterface</code>
</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">ForgeSqlOrm</code> implements <code
class="bg-gray-100 px-2 py-1 rounded">QueryBuilderInterface</code></li>
<li>You can also create your own module that implements these contracts</li>
</ul>
<p class="text-gray-600 mb-4">
Forge provides <strong>three ways to work with data</strong>:
</p>
<ol class="list-decimal list-inside space-y-2 text-gray-600 mb-4">
<li><strong>Raw SQL Queries:</strong> Use <code
class="bg-gray-100 px-2 py-1 rounded">DatabaseConnectionInterface</code>
(requires ForgeDatabaseSQL) or <code
class="bg-gray-100 px-2 py-1 rounded">QueryBuilderInterface</code> (requires
ForgeSqlOrm) for direct database access.</li>
<li><strong>Query Builder:</strong> Use the fluent query builder interface for
type-safe database operations (requires ForgeSqlOrm).</li>
<li><strong>ORM:</strong> Use the ForgeSqlOrm capability for attribute-based models
and relationships.</li>
</ol>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Raw SQL Queries with
DatabaseConnectionInterface</h3>
<p class="text-gray-600 mb-4">
When using <code class="bg-gray-100 px-2 py-1 rounded">DatabaseConnectionInterface</code>,
you
have direct access to PDO methods. This requires the <code
class="bg-gray-100 px-2 py-1 rounded">ForgeDatabaseSQL</code> module to be
installed.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
use Forge\Core\Contracts\Database\DatabaseConnectionInterface;
class MyController
{
public function __construct(
private readonly DatabaseConnectionInterface $connection
) {}
// Execute raw SQL (DDL statements)
public function createTable(): void
{
$this->connection->exec(
"CREATE TABLE IF NOT EXISTS example_table (id INTEGER PRIMARY KEY, name TEXT)"
);
}
// Query with prepared statements
public function findUser(int $id): array
{
$stmt = $this->connection->prepare("SELECT * FROM users WHERE id = :id");
$stmt->execute([':id' => $id]);
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
// Simple query (no parameters)
public function getAllUsers(): array
{
$stmt = $this->connection->query("SELECT * FROM users LIMIT 5");
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Raw SQL Queries with
QueryBuilderInterface</h3>
<p class="text-gray-600 mb-4">
When using <code class="bg-gray-100 px-2 py-1 rounded">QueryBuilderInterface</code>,
you can use raw SQL methods or combine them with the query builder. This requires
the <code class="bg-gray-100 px-2 py-1 rounded">ForgeSqlOrm</code> module to be
installed.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
use App\Modules\ForgeSqlOrm\ORM\QueryBuilder;
class MyController
{
public function __construct(
private readonly QueryBuilder $builder
) {}
// Raw SQL query
public function rawQuery(): array
{
return $this->builder->raw(
"SELECT * FROM users WHERE status = :status",
[':status' => 'active']
);
}
// WhereRaw with query builder
public function whereRawExample(): array
{
return $this->builder
->table('users')
->whereRaw('status = :status', [':status' => 'active'])
->get();
}
// Combined: query builder + raw SQL
public function combinedExample(): array
{
return $this->builder
->table('users')
->select('id', 'email', 'identifier')
->where('status', '=', 'active')
->whereRaw('identifier IS NOT NULL', [])
->orderBy('created_at', 'DESC')
->limit(10)
->get();
}
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Transactions</h3>
<p class="text-gray-600 mb-4">
Both interfaces support database transactions. Here are examples of commit and
rollback operations:
</p>
<h4 class="text-lg font-semibold mb-3 text-gray-900">Transactions with
DatabaseConnectionInterface</h4>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
// Transaction with commit
public function transactionCommit(): array
{
$this->connection->beginTransaction();
try {
$stmt = $this->connection->prepare("INSERT INTO example_table (name) VALUES (:name)");
$stmt->execute([':name' => 'transaction_test_commit']);
$this->connection->commit();
return ['status' => 'committed', 'message' => 'Transaction committed successfully'];
} catch (\Exception $e) {
$this->connection->rollBack();
return ['status' => 'error', 'message' => $e->getMessage()];
}
}
// Transaction with rollback
public function transactionRollback(): array
{
$this->connection->beginTransaction();
try {
$stmt = $this->connection->prepare("INSERT INTO example_table (name) VALUES (:name)");
$stmt->execute([':name' => 'transaction_test_rollback']);
throw new \Exception('Simulated error to trigger rollback');
} catch (\Exception $e) {
$this->connection->rollBack();
return ['status' => 'rolled_back', 'message' => 'Transaction rolled back successfully'];
}
}</code></pre>
</div>
<h4 class="text-lg font-semibold mb-3 text-gray-900">Transactions with
QueryBuilderInterface</h4>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
// Transaction with commit
public function transactionCommit(): array
{
try {
$this->builder->beginTransaction();
$id = $this->builder->table('example_table')->insert(['name' => 'orm_transaction_commit']);
$this->builder->commit();
return ['status' => 'committed', 'inserted_id' => $id];
} catch (\Exception $e) {
$this->builder->rollback();
return ['status' => 'error', 'message' => $e->getMessage()];
}
}
// Transaction with rollback
public function transactionRollback(): array
{
try {
$this->builder->beginTransaction();
$this->builder->table('example_table')->insert(['name' => 'orm_transaction_rollback']);
throw new \Exception('Simulated error to trigger rollback');
$this->builder->commit();
} catch (\Exception $e) {
$this->builder->rollback();
return ['status' => 'rolled_back', 'message' => 'Transaction rolled back successfully'];
}
}</code></pre>
</div>
<h3 class="text-lg font-semibold mb-3 text-gray-900">Model Definition</h3>
<p class="text-gray-600 mb-4">
Models extend the <code class="bg-gray-100 px-2 py-1 rounded">Model</code> base
class and use attributes to define table structure, columns, and relationships. You
can also use traits to add common functionality.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
use App\Modules\ForgeSqlOrm\ORM\Model;
use App\Modules\ForgeSqlOrm\ORM\Attributes\Table;
use App\Modules\ForgeSqlOrm\ORM\Attributes\Column;
use App\Modules\ForgeSqlOrm\ORM\Attributes\ProtectedFields;
use App\Modules\ForgeSqlOrm\ORM\Values\Cast;
use App\Modules\ForgeSqlOrm\ORM\Values\Relate;
use App\Modules\ForgeSqlOrm\ORM\Values\Relation;
use App\Modules\ForgeSqlOrm\ORM\Values\RelationKind;
use App\Modules\ForgeSqlOrm\Traits\HasTimeStamps;
use App\Modules\ForgeSqlOrm\Traits\HasMetaData;
use App\Modules\ForgeSqlOrm\ORM\CanLoadRelations;
#[Table('users')]
#[ProtectedFields(['password'])]
class User extends Model
{
use HasTimeStamps; // Adds created_at and updated_at columns
use CanLoadRelations; // Enables relationship loading methods
use HasMetaData; // Adds metadata column for JSON data
#[Column(primary: true, cast: Cast::INT)]
public int $id;
#[Column(cast: Cast::STRING)]
public string $status;
#[Column(cast: Cast::STRING)]
public string $identifier;
#[Column(cast: Cast::STRING)]
public string $email;
#[Column(cast: Cast::STRING)]
public string $password;
#[Column(cast: Cast::JSON)]
public ?UserMetadataDto $metadata; // Uses HasMetaData trait
// Relationships use #[Relate] attribute
#[Relate(RelationKind::HasOne, Profile::class, "user_id")]
public function profile(): Relation
{
return self::describe(__FUNCTION__);
}
}</code></pre>
</div>
<h4 class="text-lg font-semibold mb-3 text-gray-900">Available Traits</h4>
<p class="text-gray-600 mb-4">
ForgeSqlOrm provides several traits to add common functionality to your models:
</p>
<h5 class="text-lg font-semibold mb-3 text-gray-900">HasTimeStamps</h5>
<p class="text-gray-600 mb-4">
Automatically adds <code class="bg-gray-100 px-2 py-1 rounded">created_at</code> and
<code class="bg-gray-100 px-2 py-1 rounded">updated_at</code> timestamp columns to
your model. These are automatically managed by the ORM when creating or updating
records.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
use App\Modules\ForgeSqlOrm\Traits\HasTimeStamps;
class User extends Model
{
use HasTimeStamps;
// Automatically adds:
// public ?DateTimeImmutable $created_at = null;
// public ?DateTimeImmutable $updated_at = null;
}</code></pre>
</div>
<h5 class="text-lg font-semibold mb-3 text-gray-900">HasMetaData</h5>
<p class="text-gray-600 mb-4">
Adds a <code class="bg-gray-100 px-2 py-1 rounded">metadata</code> column for
storing JSON data. Useful for flexible, schema-less data that doesn't need its own
table.
</p>
<div class="code-block p-6 text-white rounded-lg mb-6">
<pre><code class="language-php"><?php
use App\Modules\ForgeSqlOrm\Traits\HasMetaData;
class User extends Model
{
use HasMetaData;
// Automatically adds:
// public ?array $metadata = null;
// Usage:
// $user->metadata = ['preferences' => ['theme' => 'dark']];
// $user->save();
}</code></pre>
</div>
<h5 class="text-lg font-semibold mb-3 text-gray-900">CanLoadRelations</h5>
<p class="text-gray-600 mb-4">
Provides methods for loading and working with relationships. This trait is already
included in the <code class="bg-gray-100 px-2 py-1 rounded">Model</code> base class,
but you can use it explicitly if needed.