Skip to content

Commit

Permalink
Fix for dynamic sibling files (#56)
Browse files Browse the repository at this point in the history
* feature: deeper dynamic pages
closes #4

* build: upgrade build packages for downstream deps

* tweak: phpstorm analysis

* wip: isolate and work on #55

* fix: fix logic comparing sibling files
closes #55
  • Loading branch information
g105b authored Oct 19, 2023
1 parent 3390be9 commit 131946a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/Path/FileMatch/FileMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,39 @@
namespace Gt\Routing\Path\FileMatch;

abstract class FileMatch {
/** @var array<array<string>> An array of paths parts for the siblings of the file path */
protected array $siblingFilePathParts;

/** @param null|array<string> $siblingFiles Sibling files on disk to the current file path */
public function __construct(
protected string $filePath,
protected string $baseDir,
?array $siblingFiles = null,
) {
$dirname = dirname($this->filePath);
$ext = pathinfo($this->filePath, PATHINFO_EXTENSION);

$this->siblingFilePathParts = [];
foreach($siblingFiles ?? glob("$dirname/*.$ext") as $filePath) {
$filePathNoBaseDir = substr($filePath, strlen($this->baseDir . "/"));
$pathParts = explode("/", $filePathNoBaseDir);
foreach($pathParts as $i => $pathPart) {
if(str_contains($pathPart, "@")) {
$pathParts[$i] = "@";
}
else {
$dotPos = strpos($pathPart, ".");
if($dotPos !== false) {
$pathParts[$i] = substr($pathPart, 0, $dotPos);
}
}
}

array_push(
$this->siblingFilePathParts,
$pathParts,
);
}
}

abstract public function matches(string $uriPath):bool;
Expand Down Expand Up @@ -44,14 +73,20 @@ protected function filterDynamicPathParts(
array $uriPathParts
):array {
$filePathParts = explode("/", $filePath);
$matchingSibling = in_array($uriPathParts, $this->siblingFilePathParts);

foreach($uriPathParts as $i => $uriPathPart) {
if(!isset($filePathParts[$i])) {
break;
}

$filePathPart = $filePathParts[$i];
if($filePathPart === "@") {
$uriPathParts[$i] = "@";

// On the last iteration, don't convert if there's a sibling match.
if(isset($filePathParts[$i + 1]) || !$matchingSibling) {
if($filePathPart === "@") {
$uriPathParts[$i] = "@";
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/phpunit/Path/FileMatch/BasicFileMatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,17 @@ public function testMatches_dynamicDir_matchesIndex():void {
);
self::assertTrue($sut->matches("/shop/cakes"));
}

public function testMatches_dynamicPath_siblingMatchesIndex():void {
$sut = new BasicFileMatch(
"page/request/@request-id.html",
"page",
[
"page/request/index.html",
"page/request/secrets.html",
]
);
self::assertTrue($sut->matches("/request/dynamic-example"));
self::assertFalse($sut->matches("/request/secrets"));
}
}

0 comments on commit 131946a

Please sign in to comment.