@@ -24,7 +24,7 @@ class SiteURI extends URI
2424 /**
2525 * The current baseURL.
2626 */
27- private string $ baseURL ;
27+ private URI $ baseURL ;
2828
2929 /**
3030 * The path part of baseURL.
@@ -96,32 +96,54 @@ public function __construct(
9696 ) {
9797 $ this ->indexPage = $ configApp ->indexPage ;
9898
99- $ baseURL = $ this ->normalizeBaseURL ($ configApp );
100- $ this ->setBasePath ($ baseURL );
99+ $ this ->baseURL = $ this ->determineBaseURL ($ configApp , $ host , $ scheme );
101100
102- $ relativePath = URI ::removeDotSegments ($ relativePath );
103- // Remove starting slash unless it is `/`.
104- if ($ relativePath !== '' && $ relativePath [0 ] === '/ ' && $ relativePath !== '/ ' ) {
105- $ relativePath = ltrim ($ relativePath , '/ ' );
106- }
101+ $ this ->setBasePath ();
107102
108- $ tempPath = $ relativePath ;
103+ // Fix routePath, query, fragment
104+ [$ routePath , $ query , $ fragment ] = $ this ->parseRelativePath ($ relativePath );
109105
110- // Check for an index page
111- $ indexPage = '' ;
112- if ($ configApp ->indexPage !== '' ) {
113- $ indexPage = $ configApp ->indexPage ;
106+ // Fix indexPage and routePath
107+ $ indexPageRoutePath = $ this ->getIndexPageRoutePath ($ routePath );
114108
115- // Check if we need a separator
116- if ($ relativePath !== '' && $ relativePath [0 ] !== '/ ' && $ relativePath [0 ] !== '? ' ) {
117- $ indexPage .= '/ ' ;
118- }
119- } elseif ($ relativePath === '/ ' ) {
120- $ tempPath = '' ;
109+ // Fix the current URI
110+ $ uri = $ this ->baseURL . $ indexPageRoutePath ;
111+
112+ // applyParts
113+ $ parts = parse_url ($ uri );
114+ if ($ parts === false ) {
115+ throw HTTPException ::forUnableToParseURI ($ uri );
116+ }
117+ $ parts ['query ' ] = $ query ;
118+ $ parts ['fragment ' ] = $ fragment ;
119+ $ this ->applyParts ($ parts );
120+
121+ $ this ->setRoutePath ($ routePath );
122+ }
123+
124+ private function parseRelativePath (string $ relativePath ): array
125+ {
126+ $ parts = parse_url ('http://dummy/ ' . $ relativePath );
127+ if ($ parts === false ) {
128+ throw HTTPException ::forUnableToParseURI ($ relativePath );
121129 }
122130
123- $ tempUri = $ baseURL . $ indexPage . $ tempPath ;
124- $ uri = new URI ($ tempUri );
131+ $ routePath = $ relativePath === '/ ' ? '/ ' : ltrim ($ parts ['path ' ], '/ ' );
132+
133+ $ query = $ parts ['query ' ] ?? '' ;
134+ $ fragment = $ parts ['fragment ' ] ?? '' ;
135+
136+ return [$ routePath , $ query , $ fragment ];
137+ }
138+
139+ private function determineBaseURL (
140+ App $ configApp ,
141+ ?string $ host ,
142+ ?string $ scheme
143+ ): URI {
144+ $ baseURL = $ this ->normalizeBaseURL ($ configApp );
145+
146+ $ uri = new URI ($ baseURL );
125147
126148 // Update scheme
127149 if ($ scheme !== null ) {
@@ -135,24 +157,34 @@ public function __construct(
135157 $ uri ->setHost ($ host );
136158 }
137159
138- $ parts = parse_url ((string ) $ uri );
139- if ($ parts === false ) {
140- throw HTTPException ::forUnableToParseURI ($ uri );
160+ return $ uri ;
161+ }
162+
163+ private function getIndexPageRoutePath (string $ routePath ): string
164+ {
165+ // Remove starting slash unless it is `/`.
166+ if ($ routePath !== '' && $ routePath [0 ] === '/ ' && $ routePath !== '/ ' ) {
167+ $ routePath = ltrim ($ routePath , '/ ' );
141168 }
142- $ this ->applyParts ($ parts );
143169
144- // Set routePath
145- $ parts = explode ('? ' , $ relativePath );
146- $ parts = explode ('# ' , $ parts [0 ]);
147- $ routePath = $ parts [0 ];
148- $ this ->setRoutePath ($ routePath );
170+ // Check for an index page
171+ $ indexPage = '' ;
172+ if ($ this ->indexPage !== '' ) {
173+ $ indexPage = $ this ->indexPage ;
149174
150- // Set baseURL
151- $ this ->baseURL = URI ::createURIString (
152- $ this ->getScheme (),
153- $ this ->getAuthority (),
154- $ this ->basePathWithoutIndexPage ,
155- );
175+ // Check if we need a separator
176+ if ($ routePath !== '' && $ routePath [0 ] !== '/ ' && $ routePath [0 ] !== '? ' ) {
177+ $ indexPage .= '/ ' ;
178+ }
179+ }
180+
181+ $ indexPageRoutePath = $ indexPage . $ routePath ;
182+
183+ if ($ indexPageRoutePath === '/ ' ) {
184+ $ indexPageRoutePath = '' ;
185+ }
186+
187+ return $ indexPageRoutePath ;
156188 }
157189
158190 private function checkHost (string $ host , array $ allowedHostnames ): bool
@@ -179,9 +211,9 @@ private function normalizeBaseURL(App $configApp): string
179211 /**
180212 * Sets basePathWithoutIndexPage and baseSegments.
181213 */
182- private function setBasePath (string $ baseURL ): void
214+ private function setBasePath (): void
183215 {
184- $ this ->basePathWithoutIndexPage = ( new URI ( $ baseURL)) ->getPath ();
216+ $ this ->basePathWithoutIndexPage = $ this -> baseURL ->getPath ();
185217
186218 $ this ->baseSegments = $ this ->convertToSegments ($ this ->basePathWithoutIndexPage );
187219
@@ -213,7 +245,7 @@ public function setURI(?string $uri = null)
213245 */
214246 public function getBaseURL (): string
215247 {
216- return $ this ->baseURL ;
248+ return ( string ) $ this ->baseURL ;
217249 }
218250
219251 /**
@@ -285,13 +317,17 @@ public function setPath(string $path)
285317 /**
286318 * Sets the route path (and segments).
287319 */
288- private function setRoutePath (string $ path ): void
320+ private function setRoutePath (string $ routePath ): void
289321 {
290- $ this -> routePath = $ this ->filterPath ($ path );
322+ $ routePath = $ this ->filterPath ($ routePath );
291323
292- $ this -> segments = $ this ->convertToSegments ( $ this -> routePath );
324+ $ indexPageRoutePath = $ this ->getIndexPageRoutePath ( $ routePath );
293325
294- $ this ->refreshPath ();
326+ $ this ->path = $ this ->basePathWithoutIndexPage . $ indexPageRoutePath ;
327+
328+ $ this ->routePath = ltrim ($ routePath , '/ ' );
329+
330+ $ this ->segments = $ this ->convertToSegments ($ this ->routePath );
295331 }
296332
297333 /**
0 commit comments