11<?php
22namespace GT \WebEngine ;
33
4- use GT \Config \Config ;
5- use GT \Config \ConfigFactory ;
6- use GT \Http \Response ;
7- use Gt \Http \ServerRequest ;
8- use GT \Http \Stream ;
9- use GT \ProtectedGlobal \Protection ;
4+ use Closure ;
5+ use Throwable ;
6+ use ErrorException ;
107use GT \WebEngine \Debug \OutputBuffer ;
118use GT \WebEngine \Debug \Timer ;
129use GT \WebEngine \Redirection \Redirect ;
13- use GT \Http \RequestFactory ;
14- use ErrorException ;
15- use Throwable ;
10+ use GT \WebEngine \Dispatch \Dispatcher ;
11+ use GT \WebEngine \Dispatch \DispatcherFactory ;
12+ use Gt \Config \Config ;
13+ use Gt \Config \ConfigFactory ;
14+ use Gt \Http \RequestFactory ;
15+ use Gt \Http \Response ;
16+ use Gt \Http \ServerRequest ;
17+ use Gt \Http \Stream ;
18+ use Gt \ProtectedGlobal \Protection ;
1619
1720/**
1821 * The fundamental purpose of any PHP framework is to provide a mechanism for
@@ -26,9 +29,11 @@ class Application {
2629 private Redirect $ redirect ;
2730 private Timer $ timer ;
2831 private OutputBuffer $ outputBuffer ;
32+ private RequestFactory $ requestFactory ;
2933 /** @var array<string, array<string, string>> */
3034 private array $ globals ;
3135 private Config $ config ;
36+ private DispatcherFactory $ dispatcherFactory ;
3237 private Dispatcher $ dispatcher ;
3338 private bool $ finished = false ;
3439
@@ -39,13 +44,25 @@ class Application {
3944 public function __construct (
4045 ?Redirect $ redirect = null ,
4146 ?Config $ config = null ,
47+ ?Timer $ timer = null ,
48+ ?OutputBuffer $ outputBuffer = null ,
49+ ?RequestFactory $ requestFactory = null ,
50+ ?DispatcherFactory $ dispatcherFactory = null ,
4251 ?array $ globals = null ,
52+ ?Closure $ handleShutdown = null ,
4353 ) {
4454 $ this ->gtCompatibility ();
45- $ this ->config = $ config ?? $ this ->loadConfig ();
4655 $ this ->redirect = $ redirect ?? new Redirect ();
56+ $ this ->config = $ config ?? $ this ->loadConfig ();
57+ $ this ->timer = $ timer ?? new Timer (
58+ $ this ->config ->getFloat ("app.slow_delta " ),
59+ $ this ->config ->getFloat ("app.very_slow_delta " ),
60+ );
61+ $ this ->outputBuffer = $ outputBuffer ?? new OutputBuffer ();
62+ $ this ->requestFactory = $ requestFactory ?? new RequestFactory ();
63+ $ this ->dispatcherFactory = $ dispatcherFactory ?? new DispatcherFactory ();
4764 $ this ->globals = $ globals ?? $ GLOBALS ;
48- register_shutdown_function ($ this ->handleShutdown (...));
65+ register_shutdown_function ($ handleShutdown ?? $ this ->handleShutdown (...));
4966 }
5067
5168 public function start ():void {
@@ -56,37 +73,32 @@ public function start():void {
5673// This timer is only used again at the end of the call, when finish() is
5774// called - at which point the entire duration of the request is logged out (and
5875// slow requests are highlighted as a NOTICE).
59- $ this ->timer = new Timer (
60- $ this ->config ->getFloat ("app.slow_delta " ),
61- $ this ->config ->getFloat ("app.very_slow_delta " ),
62- );
76+ $ this ->timer ->start ();
6377
6478// Starting the output buffer is done before any logic is executed, so any calls
6579// to any area of code will not accidentally send output to the web browser.
66- $ this ->outputBuffer = new OutputBuffer ();
80+ $ this ->outputBuffer -> start ();
6781
6882// PHP.GT provides object-oriented interfaces to all values stored in $_SERVER,
6983// $_FILES, $_GET, and $_POST - to enforce good encapsulation and safe variable
7084// usage, the globals are protected against accidental misuse.
7185 $ this ->protectGlobals ();
7286
73- $ requestFactory = new RequestFactory ();
74-
7587 /** @var ServerRequest $request */
76- $ request = $ requestFactory ->createServerRequestFromGlobalState (
77- $ this ->globals ["server " ],
78- $ this ->globals ["files " ],
79- $ this ->globals ["get " ],
80- $ this ->globals ["post " ],
88+ $ request = $ this -> requestFactory ->createServerRequestFromGlobalState (
89+ $ this ->globals ["_SERVER " ] ?? [ ],
90+ $ this ->globals ["_FILES " ] ?? [ ],
91+ $ this ->globals ["_GET " ] ?? [ ],
92+ $ this ->globals ["_POST " ] ?? [ ],
8193 );
8294
83- $ this ->dispatcher = new Dispatcher (
95+ $ this ->dispatcher = $ this -> dispatcherFactory -> create (
8496 $ this ->config ,
8597 $ request ,
86- $ this ->globals ["get " ],
87- $ this ->globals ["post " ],
88- $ this ->globals ["files " ],
89- $ this ->globals ["server " ],
98+ $ this ->globals ["_GET " ],
99+ $ this ->globals ["_POST " ],
100+ $ this ->globals ["_FILES " ],
101+ $ this ->globals ["_SERVER " ],
90102 );
91103
92104 try {
0 commit comments