1212namespace CodeIgniter \HTTP ;
1313
1414use CodeIgniter \HTTP \Exceptions \HTTPException ;
15+ use CodeIgniter \Superglobals ;
1516use Config \App ;
1617
1718class SiteURIFactory
1819{
19- /**
20- * @var array Superglobal SERVER array
21- */
22- private array $ server ;
23-
20+ private Superglobals $ superglobals ;
2421 private App $ appConfig ;
2522
26- /**
27- * @param array $server Superglobal $_SERVER array
28- */
29- public function __construct (array $ server , App $ appConfig )
23+ public function __construct (Superglobals $ superglobals , App $ appConfig )
3024 {
31- $ this ->server = $ server ;
32- $ this ->appConfig = $ appConfig ;
25+ $ this ->superglobals = $ superglobals ;
26+ $ this ->appConfig = $ appConfig ;
3327 }
3428
3529 /**
@@ -103,7 +97,7 @@ public function detectRoutePath(string $protocol = ''): string
10397
10498 case 'PATH_INFO ' :
10599 default :
106- $ routePath = $ this ->server [ $ protocol] ?? $ this ->parseRequestURI ();
100+ $ routePath = $ this ->superglobals -> server ( $ protocol) ?? $ this ->parseRequestURI ();
107101 break ;
108102 }
109103
@@ -120,27 +114,30 @@ public function detectRoutePath(string $protocol = ''): string
120114 */
121115 private function parseRequestURI (): string
122116 {
123- if (! isset ($ this ->server ['REQUEST_URI ' ], $ this ->server ['SCRIPT_NAME ' ])) {
117+ if (
118+ $ this ->superglobals ->server ('REQUEST_URI ' ) === null
119+ || $ this ->superglobals ->server ('SCRIPT_NAME ' ) === null
120+ ) {
124121 return '' ;
125122 }
126123
127124 // parse_url() returns false if no host is present, but the path or query
128125 // string contains a colon followed by a number. So we attach a dummy
129126 // host since REQUEST_URI does not include the host. This allows us to
130127 // parse out the query string and path.
131- $ parts = parse_url ('http://dummy ' . $ this ->server [ 'REQUEST_URI ' ] );
128+ $ parts = parse_url ('http://dummy ' . $ this ->superglobals -> server ( 'REQUEST_URI ' ) );
132129 $ query = $ parts ['query ' ] ?? '' ;
133130 $ path = $ parts ['path ' ] ?? '' ;
134131
135132 // Strip the SCRIPT_NAME path from the URI
136133 if (
137- $ path !== '' && isset ($ this ->server [ 'SCRIPT_NAME ' ] [0 ])
138- && pathinfo ($ this ->server [ 'SCRIPT_NAME ' ] , PATHINFO_EXTENSION ) === 'php '
134+ $ path !== '' && isset ($ this ->superglobals -> server ( 'SCRIPT_NAME ' ) [0 ])
135+ && pathinfo ($ this ->superglobals -> server ( 'SCRIPT_NAME ' ) , PATHINFO_EXTENSION ) === 'php '
139136 ) {
140137 // Compare each segment, dropping them until there is no match
141138 $ segments = $ keep = explode ('/ ' , $ path );
142139
143- foreach (explode ('/ ' , $ this ->server [ 'SCRIPT_NAME ' ] ) as $ i => $ segment ) {
140+ foreach (explode ('/ ' , $ this ->superglobals -> server ( 'SCRIPT_NAME ' ) ) as $ i => $ segment ) {
144141 // If these segments are not the same then we're done
145142 if (! isset ($ segments [$ i ]) || $ segment !== $ segments [$ i ]) {
146143 break ;
@@ -160,30 +157,18 @@ private function parseRequestURI(): string
160157 $ path = $ parts [0 ];
161158 $ newQuery = $ query [1 ] ?? '' ;
162159
163- $ this ->server ['QUERY_STRING ' ] = $ newQuery ;
164- $ this ->updateServer ('QUERY_STRING ' , $ newQuery );
160+ $ this ->superglobals ->setServer ('QUERY_STRING ' , $ newQuery );
165161 } else {
166- $ this ->server ['QUERY_STRING ' ] = $ query ;
167- $ this ->updateServer ('QUERY_STRING ' , $ query );
162+ $ this ->superglobals ->setServer ('QUERY_STRING ' , $ query );
168163 }
169164
170165 // Update our global GET for values likely to have been changed
171- parse_str ($ this ->server [ 'QUERY_STRING ' ] , $ get );
172- $ this ->updateGetArray ($ get );
166+ parse_str ($ this ->superglobals -> server ( 'QUERY_STRING ' ) , $ get );
167+ $ this ->superglobals -> setGetArray ($ get );
173168
174169 return URI ::removeDotSegments ($ path );
175170 }
176171
177- private function updateServer (string $ key , string $ value ): void
178- {
179- $ _SERVER [$ key ] = $ value ;
180- }
181-
182- private function updateGetArray (array $ array ): void
183- {
184- $ _GET = $ array ;
185- }
186-
187172 /**
188173 * Will parse QUERY_STRING and automatically detect the URI from it.
189174 *
@@ -193,7 +178,7 @@ private function updateGetArray(array $array): void
193178 */
194179 private function parseQueryString (): string
195180 {
196- $ query = $ this ->server [ 'QUERY_STRING ' ] ?? @getenv ('QUERY_STRING ' );
181+ $ query = $ this ->superglobals -> server ( 'QUERY_STRING ' ) ?? @getenv ('QUERY_STRING ' );
197182
198183 if (trim ($ query , '/ ' ) === '' ) {
199184 return '/ ' ;
@@ -204,15 +189,14 @@ private function parseQueryString(): string
204189 $ path = $ parts [0 ];
205190 $ newQuery = $ parts [1 ] ?? '' ;
206191
207- $ this ->server ['QUERY_STRING ' ] = $ newQuery ;
208- $ this ->updateServer ('QUERY_STRING ' , $ newQuery );
192+ $ this ->superglobals ->setServer ('QUERY_STRING ' , $ newQuery );
209193 } else {
210194 $ path = $ query ;
211195 }
212196
213197 // Update our global GET for values likely to have been changed
214- parse_str ($ this ->server [ 'QUERY_STRING ' ] , $ get );
215- $ this ->updateGetArray ($ get );
198+ parse_str ($ this ->superglobals -> server ( 'QUERY_STRING ' ) , $ get );
199+ $ this ->superglobals -> setGetArray ($ get );
216200
217201 return URI ::removeDotSegments ($ path );
218202 }
@@ -224,7 +208,7 @@ private function parseQueryString(): string
224208 */
225209 private function createURIFromRoutePath (string $ routePath ): SiteURI
226210 {
227- $ query = $ this ->server [ 'QUERY_STRING ' ] ?? '' ;
211+ $ query = $ this ->superglobals -> server ( 'QUERY_STRING ' ) ?? '' ;
228212
229213 $ relativePath = $ query !== '' ? $ routePath . '? ' . $ query : $ routePath ;
230214
@@ -236,7 +220,7 @@ private function createURIFromRoutePath(string $routePath): SiteURI
236220 */
237221 private function getHost (): ?string
238222 {
239- $ httpHostPort = $ this ->server [ 'HTTP_HOST ' ] ?? null ;
223+ $ httpHostPort = $ this ->superglobals -> server ( 'HTTP_HOST ' ) ?? null ;
240224
241225 if ($ httpHostPort !== null ) {
242226 [$ httpHost ] = explode (': ' , $ httpHostPort , 2 );
0 commit comments