Skip to content

Commit 8c012cf

Browse files
author
Greg Bowler
authored
handle globals in lifecycle (#589)
* handle globals in lifecycle * handle globals in request handler * handle globals in error request handler * clone globals before passing elsewhere
1 parent c7407cf commit 8c012cf

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

src/Middleware/ErrorRequestHandler.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ public function __construct(
1717
callable $finishCallback,
1818
private Throwable $throwable,
1919
protected Container $serviceContainer,
20+
protected array $getArray,
21+
protected array $postArray,
22+
protected array $filesArray,
23+
protected array $serverArray,
2024
) {
21-
parent::__construct($config, $finishCallback);
25+
parent::__construct(
26+
$config,
27+
$finishCallback,
28+
$this->getArray,
29+
$this->postArray,
30+
$this->filesArray,
31+
$this->serverArray,
32+
);
2233
}
2334

2435
public function handle(

src/Middleware/Lifecycle.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,20 @@ public function start():void {
5555
// to any area of code will not accidentally send output to the client.
5656
ob_start();
5757

58+
$originalGlobals = [
59+
"get" => $_GET,
60+
"post" => $_POST,
61+
"files" => $_FILES,
62+
"server" => $_SERVER,
63+
];
5864
// A PSR-7 HTTP Request object is created from the current global state, ready
5965
// for processing by the Handler.
6066
$requestFactory = new RequestFactory();
6167
$request = $requestFactory->createServerRequestFromGlobalState(
62-
$_SERVER,
63-
$_FILES,
64-
$_GET,
65-
$_POST,
68+
$originalGlobals["server"],
69+
$originalGlobals["files"],
70+
$originalGlobals["get"],
71+
$originalGlobals["post"],
6672
);
6773

6874
// The handler is an individual component that processes a request and produces
@@ -75,6 +81,10 @@ public function start():void {
7581
"vendor/phpgt/webengine/config.default.ini"
7682
),
7783
$this->finish(...),
84+
$originalGlobals["get"],
85+
$originalGlobals["post"],
86+
$originalGlobals["files"],
87+
$originalGlobals["server"],
7888
);
7989

8090
// The request and request handler are passed to the PSR-15 process function,
@@ -93,6 +103,10 @@ public function start():void {
93103
$this->finish(...),
94104
$throwable,
95105
$handler->getServiceContainer(),
106+
$originalGlobals["get"],
107+
$originalGlobals["post"],
108+
$originalGlobals["files"],
109+
$originalGlobals["server"],
96110
);
97111
$response = $this->process($request, $errorHandler);
98112

src/Middleware/RequestHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class RequestHandler implements RequestHandlerInterface {
5353
public function __construct(
5454
protected readonly Config $config,
5555
callable $finishCallback,
56+
protected array $getArray,
57+
protected array $postArray,
58+
protected array $filesArray,
59+
protected array $serverArray,
5660
) {
5761
$this->finishCallback = $finishCallback;
5862

@@ -96,15 +100,15 @@ protected function completeRequestHandling(
96100
$this->forceTrailingSlashes($request);
97101
$this->setupServiceContainer();
98102

99-
$input = new Input($_GET, $_POST, $_FILES);
103+
$input = new Input($this->getArray, $this->postArray, $this->filesArray);
100104

101105
$this->serviceContainer->set(
102106
$this->config,
103107
$request,
104108
$this->response,
105109
$this->response->headers,
106110
$input,
107-
new ServerInfo($_SERVER),
111+
new ServerInfo($this->serverArray),
108112
);
109113
$this->injector = new Injector($this->serviceContainer);
110114

0 commit comments

Comments
 (0)