Skip to content

Commit b316cd1

Browse files
committed
🛀 server request make helpers implementation agnostic
1 parent b70a854 commit b316cd1

File tree

5 files changed

+48
-31
lines changed

5 files changed

+48
-31
lines changed

src/Psr17/StreamFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ final class StreamFactory implements StreamFactoryInterface{
2222
* @inheritDoc
2323
*/
2424
public function createStream(string $content = ''):StreamInterface{
25-
return create_stream($content);
25+
$stream = new Stream(fopen('php://temp', 'r+'));
26+
27+
if($content !== ''){
28+
$stream->write($content);
29+
}
30+
31+
return $stream;
2632
}
2733

2834
/**

src/Psr17/factory_helpers.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
namespace chillerlan\HTTP\Psr17;
1010

11-
use Psr\Http\Message\ServerRequestInterface;
12-
use chillerlan\HTTP\Psr7\{File, ServerRequest, Stream, Uri};
11+
use Psr\Http\Message\{
12+
ServerRequestFactoryInterface, ServerRequestInterface, StreamInterface, UriInterface, UriFactoryInterface
13+
};
14+
use chillerlan\HTTP\Psr7\{File, Stream};
1315
use InvalidArgumentException;
14-
use Psr\Http\Message\StreamInterface;
1516

16-
use function explode, function_exists, getallheaders, is_scalar, method_exists, str_replace;
17+
use function explode, function_exists, getallheaders, is_scalar, method_exists, substr;
1718

1819
const PSR17_INCLUDES = true;
1920

@@ -54,18 +55,25 @@
5455
* $_FILES
5556
* $_SERVER
5657
*/
57-
function create_server_request_from_globals():ServerRequestInterface{
58-
59-
$serverRequest = new ServerRequest(
60-
$_SERVER['REQUEST_METHOD'] ?? ServerRequest::METHOD_GET,
61-
create_uri_from_globals(),
62-
function_exists('getallheaders') ? getallheaders() : [],
63-
(new StreamFactory)->createStream(),
64-
isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1',
58+
function create_server_request_from_globals(
59+
ServerRequestFactoryInterface $serverRequestFactory,
60+
UriFactoryInterface $uriFactory
61+
):ServerRequestInterface{
62+
63+
$serverRequest = $serverRequestFactory->createServerRequest(
64+
$_SERVER['REQUEST_METHOD'] ?? 'GET',
65+
create_uri_from_globals($uriFactory),
6566
$_SERVER
6667
);
6768

69+
if(function_exists('getallheaders')){
70+
foreach(getallheaders() ?: [] as $name => $value){
71+
$serverRequest = $serverRequest->withHeader($name, $value);
72+
}
73+
}
74+
6875
return $serverRequest
76+
->withProtocolVersion(isset($_SERVER['SERVER_PROTOCOL']) ? substr($_SERVER['SERVER_PROTOCOL'], 5) : '1.1')
6977
->withCookieParams($_COOKIE)
7078
->withQueryParams($_GET)
7179
->withParsedBody($_POST)
@@ -74,50 +82,50 @@ function_exists('getallheaders') ? getallheaders() : [],
7482
}
7583

7684
/**
77-
* Get a Uri populated with values from $_SERVER.
85+
* Create a Uri populated with values from $_SERVER.
7886
*/
79-
function create_uri_from_globals():Uri{
80-
$parts = [];
87+
function create_uri_from_globals(UriFactoryInterface $uriFactory):UriInterface{
8188
$hasPort = false;
8289
$hasQuery = false;
8390

84-
$parts['scheme'] = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
91+
$uri = $uriFactory->createUri()
92+
->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
8593

8694
if(isset($_SERVER['HTTP_HOST'])){
8795
$hostHeaderParts = explode(':', $_SERVER['HTTP_HOST']);
88-
$parts['host'] = $hostHeaderParts[0];
96+
$uri = $uri->withHost($hostHeaderParts[0]);
8997

9098
if(isset($hostHeaderParts[1])){
9199
$hasPort = true;
92-
$parts['port'] = $hostHeaderParts[1];
100+
$uri = $uri->withPort($hostHeaderParts[1]);
93101
}
94102
}
95103
elseif(isset($_SERVER['SERVER_NAME'])){
96-
$parts['host'] = $_SERVER['SERVER_NAME'];
104+
$uri = $uri->withHost($_SERVER['SERVER_NAME']);
97105
}
98106
elseif(isset($_SERVER['SERVER_ADDR'])){
99-
$parts['host'] = $_SERVER['SERVER_ADDR'];
107+
$uri = $uri->withHost($_SERVER['SERVER_ADDR']);
100108
}
101109

102110
if(!$hasPort && isset($_SERVER['SERVER_PORT'])){
103-
$parts['port'] = $_SERVER['SERVER_PORT'];
111+
$uri = $uri->withPort($_SERVER['SERVER_PORT']);
104112
}
105113

106114
if(isset($_SERVER['REQUEST_URI'])){
107115
$requestUriParts = explode('?', $_SERVER['REQUEST_URI']);
108-
$parts['path'] = $requestUriParts[0];
116+
$uri = $uri->withPath($requestUriParts[0]);
109117

110118
if(isset($requestUriParts[1])){
111119
$hasQuery = true;
112-
$parts['query'] = $requestUriParts[1];
120+
$uri = $uri->withQuery($requestUriParts[1]);
113121
}
114122
}
115123

116124
if(!$hasQuery && isset($_SERVER['QUERY_STRING'])){
117-
$parts['query'] = $_SERVER['QUERY_STRING'];
125+
$uri = $uri->withQuery($_SERVER['QUERY_STRING']);
118126
}
119127

120-
return new Uri($parts);
128+
return $uri;
121129
}
122130

123131
/**

tests/Psr15/PriorityQueueRequestHandlerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace chillerlan\HTTPTest\Psr15;
1212

13+
use chillerlan\HTTP\Psr17\{ServerRequestFactory, UriFactory};
1314
use chillerlan\HTTP\Psr15\{MiddlewareException, PriorityMiddleware, PriorityMiddlewareInterface, PriorityQueueDispatcher};
1415
use PHPUnit\Framework\TestCase;
1516
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
@@ -36,7 +37,7 @@ public function testHandler():void{
3637
$handler->add($this->getNonPriorityMiddleware(1));
3738

3839
// execute it:
39-
$response = $handler->handle(create_server_request_from_globals());
40+
$response = $handler->handle(create_server_request_from_globals(new ServerRequestFactory, new UriFactory));
4041

4142
// highest priority shall be processed first and go out last
4243
$this::assertSame(
@@ -70,7 +71,7 @@ public function testNestedHandler():void{
7071
];
7172

7273
$handler = new PriorityQueueDispatcher($middlewareStack);
73-
$response = $handler->handle(create_server_request_from_globals());
74+
$response = $handler->handle(create_server_request_from_globals(new ServerRequestFactory, new UriFactory));
7475

7576
$this::assertSame(
7677
[

tests/Psr15/QueueRequestHandlerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace chillerlan\HTTPTest\Psr15;
1212

13+
use chillerlan\HTTP\Psr17\{ServerRequestFactory, UriFactory};
1314
use chillerlan\HTTP\Psr7\Response;
1415
use chillerlan\HTTP\Psr15\{MiddlewareException, QueueDispatcher};
1516
use PHPUnit\Framework\TestCase;
@@ -40,7 +41,7 @@ protected function getDispatcher():RequestHandlerInterface{
4041
public function testDispatcher():void{
4142

4243
// execute it:
43-
$response = $this->dispatcher->handle(create_server_request_from_globals());
44+
$response = $this->dispatcher->handle(create_server_request_from_globals(new ServerRequestFactory, new UriFactory));
4445

4546
$this::assertSame(
4647
['X-Out-First', 'X-Out-Second', 'X-Out-Third'],

tests/Psr17/FactoryHelpersTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace chillerlan\HTTPTest\Psr17;
1212

13+
use chillerlan\HTTP\Psr17\{ServerRequestFactory, UriFactory};
1314
use chillerlan\HTTP\Psr7\{UploadedFile, Uri};
1415
use InvalidArgumentException, stdClass;
1516
use PHPUnit\Framework\TestCase;
@@ -98,7 +99,7 @@ public function dataGetUriFromGlobals():array{
9899
public function testCreateUriFromGlobals(string $expected, array $serverParams){
99100
$_SERVER = $serverParams;
100101

101-
$this::assertEquals(new Uri($expected), create_uri_from_globals());
102+
$this::assertEquals(new Uri($expected), create_uri_from_globals(new UriFactory));
102103
}
103104

104105
public function testCreateServerRequestFromGlobals():void{
@@ -146,7 +147,7 @@ public function testCreateServerRequestFromGlobals():void{
146147
]
147148
];
148149

149-
$server = create_server_request_from_globals();
150+
$server = create_server_request_from_globals(new ServerRequestFactory, new UriFactory);
150151

151152
$this::assertSame('POST', $server->getMethod());
152153
$this::assertSame(['Host' => ['www.example.org']], $server->getHeaders());

0 commit comments

Comments
 (0)