@@ -171,6 +171,7 @@ public function getPage() {
171
171
172
172
// Strip of the leading part
173
173
$ afterName = substr ($ pagePath , strlen ($ mapEntry ));
174
+ Log::debug ($ this ->searchPageInNamespace ($ afterName , $ name ));
174
175
175
176
// Fix special cases such as empty start or trailing slash
176
177
if ($ afterName == '' ) $ afterName .= '/index ' ;
@@ -208,13 +209,7 @@ public function getPage() {
208
209
}
209
210
}
210
211
211
- // Check whether the class exists
212
- if ($ className != NULL ) {
213
- if (substr ($ className , 0 , 1 ) != '\\' ) $ className = '\\' .$ className ;
214
- if (class_exists ($ className )) $ page = new $ className ($ this ->app );
215
- }
216
-
217
- return $ page ;
212
+ return $ this ->createPageInstance ($ className );
218
213
}
219
214
220
215
/** Returns the absolute path to a relative app path */
@@ -223,5 +218,50 @@ public function getAbsolutePath($relativePath) {
223
218
return $ request ->webRoot .$ request ->relativeAppPath .$ relativePath ;
224
219
}
225
220
221
+ protected function searchPageInNamespace ($ pagePath , $ namespace ) {
222
+ // Fix special cases such as empty start or trailing slash
223
+ if ($ pagePath == '' ) $ pagePath .= '/index ' ;
224
+ if (substr ($ pagePath , -1 ) == '/ ' ) $ pagePath .= 'index ' ;
225
+
226
+ // strip of the ending .html
227
+ if (substr ($ pagePath , -5 ) == '.html ' ) $ pagePath = substr ($ pagePath , 0 , strlen ($ pagePath )-5 );
228
+
229
+ // split in parts and remove first part if it's empty
230
+ $ names = explode ('/ ' , $ pagePath );
231
+ if ($ names [0 ] == '' ) array_shift ($ names );
232
+
233
+ // Replace special characters by whitespace and make it camel case and concatenate again
234
+ $ className = $ name ;
235
+ foreach ($ names AS $ idx => $ path ) {
236
+ if ($ idx > 0 ) $ className .= '\\' ;
237
+ $ path = str_replace (' ' , '' , ucwords (str_replace (array ('_ ' ,'- ' ), array (' ' , ' ' ), $ path )));
238
+ $ className .= ucfirst ($ path );
239
+ }
240
+
241
+ // Make a bottom-2-top search for the class as long as you are in the namespace of the mapEntry
242
+ $ toSearch = '\\' .$ className .'Page ' ;
243
+ while (!class_exists ($ toSearch ) && (strpos ($ toSearch , $ namespace ) !== FALSE )) {
244
+ // strip off from last backslash
245
+ $ toSearch = substr ($ toSearch , 0 , strrpos ($ toSearch , '\\' )).'Page ' ;
246
+ }
247
+ if (class_exists ($ toSearch )) {
248
+ $ className = $ toSearch ;
249
+ } else {
250
+ $ className .= 'Page ' ;
251
+ }
252
+ Log::debug ('Router::searchPageInNamespace(): ' .$ namespace .$ className );
253
+ return $ namespace .$ className ;
254
+ }
255
+
256
+ /** Returns the instance of the page or NULL if it doesnt exist */
257
+ protected function createPageInstance ($ className ) {
258
+ $ page = NULL ;
259
+ // Check whether the class exists
260
+ if ($ className != NULL ) {
261
+ if (substr ($ className , 0 , 1 ) != '\\' ) $ className = '\\' .$ className ;
262
+ if (class_exists ($ className )) $ page = new $ className ($ this ->app );
263
+ }
264
+ return $ page ;
265
+ }
226
266
}
227
267
0 commit comments