88
99namespace 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 };
1315use 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
1819const PSR17_INCLUDES = true ;
1920
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/**
0 commit comments