Skip to content

Commit 68838fb

Browse files
committed
wip: non-static protected globals
1 parent 28dee6a commit 68838fb

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"phpgt/input": "^1.3",
3434
"phpgt/logger": "^1.0",
3535
"phpgt/promise": "^2.4",
36-
"phpgt/protectedglobal": "^1.1",
36+
"phpgt/protectedglobal": "^2.0",
3737
"phpgt/routing": "^1.1",
3838
"phpgt/server": "^1.1",
3939
"phpgt/servicecontainer": "^1.3",

composer.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Application.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Application {
3434
private RequestFactory $requestFactory;
3535
/** @var array<string, array<string, string|array<string, string>>> */
3636
private array $globals;
37+
private Protection $globalProtection;
3738
private Config $config;
3839
private DispatcherFactory $dispatcherFactory;
3940
private Dispatcher $dispatcher;
@@ -52,6 +53,7 @@ public function __construct(
5253
?DispatcherFactory $dispatcherFactory = null,
5354
?array $globals = null,
5455
?Closure $handleShutdown = null,
56+
?Protection $globalProtection = null,
5557
) {
5658
$this->gtCompatibility();
5759
$this->redirect = $redirect ?? new Redirect();
@@ -71,6 +73,7 @@ public function __construct(
7173
"_ENV" => [],
7274
"_COOKIE" => [],
7375
], $globals ?? $GLOBALS);
76+
$this->globalProtection = $globalProtection ?? new Protection();
7477
register_shutdown_function($handleShutdown ?? $this->handleShutdown(...));
7578
}
7679

@@ -191,9 +194,8 @@ class_alias($legacyClass, $class);
191194
}
192195

193196
private function protectGlobals():void {
194-
$protection = new Protection();
195-
$protection->overrideInternals(
196-
$protection->removeGlobals([
197+
$this->globalProtection->overrideInternals(
198+
$this->globalProtection->removeGlobals([
197199
"server" => $this->globals["_SERVER"],
198200
"files" => $this->globals["_FILES"],
199201
"get" => $this->globals["_GET"],

src/Dispatch/DispatcherFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace Gt\WebEngine\Dispatch;
2+
namespace GT\WebEngine\Dispatch;
33

44
use Closure;
55
use Gt\Config\Config;

test/phpunit/ApplicationTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<?php
22
namespace GT\WebEngine\Test;
33

4+
use Gt\Http\Request;
45
use Gt\Http\RequestFactory;
56
use Gt\Http\Response;
7+
use Gt\Http\ServerRequest;
8+
use Gt\Http\Uri;
9+
use Gt\ProtectedGlobal\Protection;
610
use GT\WebEngine\Application;
711
use GT\WebEngine\Debug\OutputBuffer;
812
use GT\WebEngine\Debug\Timer;
@@ -17,8 +21,19 @@ public function testStart_callsRedirectExecute():void {
1721
$redirect->expects(self::once())
1822
->method("execute");
1923

24+
$globalProtection = self::createMock(Protection::class);
25+
$serverRequest = self::createMock(ServerRequest::class);
26+
$serverRequest->method("getUri")
27+
->willReturn(self::createMock(Uri::class));
28+
$serverRequest->method("getHeaderLine")
29+
->willReturn("Accept: text/test");
30+
$requestFactory = self::createMock(RequestFactory::class);
31+
$requestFactory->method("createServerRequestFromGlobalState")
32+
->willReturn($serverRequest);
2033
$sut = new Application(
2134
redirect: $redirect,
35+
requestFactory: $requestFactory,
36+
globalProtection: $globalProtection,
2237
);
2338
$sut->start();
2439
}
@@ -45,8 +60,11 @@ public function testStart_callsOutputBufferFunctions():void {
4560
$outputBuffer->expects(self::once())
4661
->method("debugOutput");
4762

63+
$globalProtection = self::createMock(Protection::class);
64+
4865
$sut = new Application(
4966
outputBuffer: $outputBuffer,
67+
globalProtection: $globalProtection,
5068
);
5169
$sut->start();
5270
}
@@ -87,8 +105,11 @@ public function testStart_callsDispatcherFactoryFunctions():void {
87105
$response->expects(self::once())
88106
->method("getBody");
89107

108+
$globalProtection = self::createMock(Protection::class);
109+
90110
$sut = new Application(
91111
dispatcherFactory: $dispatcherFactory,
112+
globalProtection: $globalProtection,
92113
);
93114
$sut->start();
94115
}

0 commit comments

Comments
 (0)