1414use Gt \WebEngine \View \HTMLView ;
1515
1616class DefaultRouter extends BaseRouter {
17- #[Any(name: "page-route " , accept: "text/html,application/xhtml+xml,*/* " )]
18- public function page (
19- Request $ request
20- ):void {
17+ #[Any(name: "page-route " , accept: "text/html,application/xhtml+xml " )]
18+ public function page (Request $ request ):void {
2119 $ pathMatcher = new PathMatcher ("page " );
2220 $ this ->setViewClass (HTMLView::class);
2321 $ pathMatcher ->addFilter (function (string $ filePath , string $ uriPath , string $ baseDir ):bool {
24- foreach (glob ($ baseDir . $ uriPath . ".* " ) as $ globMatch ) {
25- $ URI_CONTAINER = pathinfo ($ uriPath , PATHINFO_DIRNAME );
26- $ TRIM_THIS = $ baseDir . $ URI_CONTAINER ;
27- if (str_starts_with ($ globMatch , $ TRIM_THIS )) {
28- $ trimmed = substr ($ filePath , strlen ($ TRIM_THIS ));
29- if (str_contains ($ trimmed , "@ " )) {
30- return false ;
31- }
32- }
33- }
34-
3522// There are three types of matching files: Basic, Magic and Dynamic.
3623// Basic is where a URI matches directly to a file on disk.
3724// Magic is where a URI matches a PHP.Gt-specific file, like _common or _header.
@@ -48,69 +35,15 @@ public function page(
4835
4936 return false ;
5037 });
51-
52- // This sort function allow multiple headers and footers to be in nested
53- // directories, so the highest level header is at the start of the list,
54- // with the reverse logic applied to footers.
55- // TODO: Extract into own function. Should this be maintained within PHP.Gt/Routing ?
56- $ headerFooterSort = function (string $ a , string $ b ):int {
57- $ fileNameA = pathinfo ($ a , PATHINFO_FILENAME );
58- $ fileNameB = pathinfo ($ b , PATHINFO_FILENAME );
59-
60- if ($ fileNameA === "_header " ) {
61- if ($ fileNameB === "_header " ) {
62- $ aDepth = substr_count ($ a , "/ " );
63- $ bDepth = substr_count ($ b , "/ " );
64- if ($ aDepth > $ bDepth ) {
65- return 1 ;
66- }
67- elseif ($ aDepth < $ bDepth ) {
68- return -1 ;
69- }
70- else {
71- return 0 ;
72- }
73- }
74-
75-
76- return -1 ;
77- }
78-
79- if ($ fileNameA === "_footer " ) {
80- if ($ fileNameB === "_footer " ) {
81- $ aDepth = substr_count ($ a , "/ " );
82- $ bDepth = substr_count ($ b , "/ " );
83- if ($ aDepth < $ bDepth ) {
84- return 1 ;
85- }
86- elseif ($ aDepth > $ bDepth ) {
87- return -1 ;
88- }
89- else {
90- return 0 ;
91- }
92- }
93-
94- return 1 ;
95- }
96-
97- if ($ fileNameB === "_header " ) {
98- return 1 ;
99- }
100-
101- if ($ fileNameB === "_footer " ) {
102- return -1 ;
103- }
104-
105- return 0 ;
106- };
38+ // TODO: add logic and view assembly in the api directory
39+ // (configured from $this->routerConfig)
10740
10841 $ sortNestLevelCallback = fn (string $ a , string $ b ) =>
109- substr_count ($ a , "/ " ) > substr_count ($ b , "/ " )
110- ? 1
111- : ( substr_count ($ a , "/ " ) < substr_count ( $ b , " / " )
112- ? - 1
113- : 0 ) ;
42+ substr_count ($ a , "/ " ) > substr_count ($ b , "/ " );
43+ $ headerSort = fn ( string $ a , string $ b ) =>
44+ strtok ( basename ($ a) , ". " ) === " _header " ? - 1 : 1 ;
45+ $ footerSort = fn ( string $ a , string $ b ) =>
46+ strtok ( basename ( $ a ), " . " ) === " _footer " ? 1 : - 1 ;
11447
11548 $ matchingLogics = $ pathMatcher ->findForUriPath (
11649 $ request ->getUri ()->getPath (),
@@ -127,7 +60,10 @@ public function page(
12760 "page " ,
12861 "html "
12962 );
130- usort ($ matchingViews , $ headerFooterSort );
63+ usort ($ matchingViews , $ sortNestLevelCallback );
64+ usort ($ matchingViews , $ headerSort );
65+ usort ($ matchingViews , $ footerSort );
66+
13167 foreach ($ matchingViews as $ path ) {
13268 $ this ->addToViewAssembly ($ path );
13369 }
0 commit comments