Skip to content

Commit

Permalink
feature: improve header and footer sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Oct 24, 2023
1 parent 5914557 commit 1f99599
Showing 1 changed file with 13 additions and 77 deletions.
90 changes: 13 additions & 77 deletions router.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,11 @@
use Gt\WebEngine\View\HTMLView;

class DefaultRouter extends BaseRouter {
#[Any(name: "page-route", accept: "text/html,application/xhtml+xml,*/*")]
public function page(
Request $request
):void {
#[Any(name: "page-route", accept: "text/html,application/xhtml+xml")]
public function page(Request $request):void {
$pathMatcher = new PathMatcher("page");
$this->setViewClass(HTMLView::class);
$pathMatcher->addFilter(function(string $filePath, string $uriPath, string $baseDir):bool {
foreach(glob($baseDir . $uriPath . ".*") as $globMatch) {
$URI_CONTAINER = pathinfo($uriPath, PATHINFO_DIRNAME);
$TRIM_THIS = $baseDir . $URI_CONTAINER;
if(str_starts_with($globMatch, $TRIM_THIS)) {
$trimmed = substr($filePath, strlen($TRIM_THIS));
if(str_contains($trimmed, "@")) {
return false;
}
}
}

// There are three types of matching files: Basic, Magic and Dynamic.
// Basic is where a URI matches directly to a file on disk.
// Magic is where a URI matches a PHP.Gt-specific file, like _common or _header.
Expand All @@ -48,69 +35,15 @@ public function page(

return false;
});

// This sort function allow multiple headers and footers to be in nested
// directories, so the highest level header is at the start of the list,
// with the reverse logic applied to footers.
// TODO: Extract into own function. Should this be maintained within PHP.Gt/Routing ?
$headerFooterSort = function(string $a, string $b):int {
$fileNameA = pathinfo($a, PATHINFO_FILENAME);
$fileNameB = pathinfo($b, PATHINFO_FILENAME);

if($fileNameA === "_header") {
if($fileNameB === "_header") {
$aDepth = substr_count($a, "/");
$bDepth = substr_count($b, "/");
if($aDepth > $bDepth) {
return 1;
}
elseif($aDepth < $bDepth) {
return -1;
}
else {
return 0;
}
}


return -1;
}

if($fileNameA === "_footer") {
if($fileNameB === "_footer") {
$aDepth = substr_count($a, "/");
$bDepth = substr_count($b, "/");
if($aDepth < $bDepth) {
return 1;
}
elseif($aDepth > $bDepth) {
return -1;
}
else {
return 0;
}
}

return 1;
}

if($fileNameB === "_header") {
return 1;
}

if($fileNameB === "_footer") {
return -1;
}

return 0;
};
// TODO: add logic and view assembly in the api directory
// (configured from $this->routerConfig)

$sortNestLevelCallback = fn(string $a, string $b) =>
substr_count($a, "/") > substr_count($b, "/")
? 1
: (substr_count($a, "/") < substr_count($b, "/")
? -1
: 0);
substr_count($a, "/") > substr_count($b, "/");
$headerSort = fn(string $a, string $b) =>
strtok(basename($a), ".") === "_header" ? -1 : 1;
$footerSort = fn(string $a, string $b) =>
strtok(basename($a), ".") === "_footer" ? 1 : -1;

$matchingLogics = $pathMatcher->findForUriPath(
$request->getUri()->getPath(),
Expand All @@ -127,7 +60,10 @@ public function page(
"page",
"html"
);
usort($matchingViews, $headerFooterSort);
usort($matchingViews, $sortNestLevelCallback);
usort($matchingViews, $headerSort);
usort($matchingViews, $footerSort);

foreach($matchingViews as $path) {
$this->addToViewAssembly($path);
}
Expand Down

0 comments on commit 1f99599

Please sign in to comment.