Skip to content

Commit

Permalink
Merge branch 'master' of github.com:/PhpGt/Routing
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Nov 26, 2024
2 parents 79ac35a + 9304e14 commit f9acf4e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Assembly.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Gt\Routing;

use Countable;
use Gt\Routing\Path\FileMatch\MagicFileMatch;
use Iterator;

/** @implements Iterator<int, string> */
Expand Down Expand Up @@ -36,7 +37,8 @@ public function remove(string $path):void {
public function containsDistinctFile():bool {
foreach($this->pathList as $path) {
$fileName = pathinfo($path, PATHINFO_FILENAME);
if($fileName[0] !== "_") {
if(!str_starts_with($fileName, "_")
|| !in_array($fileName, MagicFileMatch::MAGIC_FILENAME_ARRAY)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Path/FileMatch/FileMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ 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;
}

$matchingSibling = in_array($uriPathParts, $this->siblingFilePathParts);
$filePathPart = $filePathParts[$i];

// On the last iteration, don't convert if there's a sibling match.
Expand Down
14 changes: 14 additions & 0 deletions test/phpunit/AssemblyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@ public function testContainsDistinctFile():void {

self::assertTrue($sut->containsDistinctFile());
}

public function testContainsDistinctFile_magicFilename():void {
$pathList = [
"/var/www/_header.html",
"/var/www/_new.html",
"/var/www/_footer.html",
];
$sut = new Assembly();
foreach($pathList as $path) {
$sut->add($path);
}

self::assertTrue($sut->containsDistinctFile());
}
}
13 changes: 13 additions & 0 deletions test/phpunit/Path/FileMatch/BasicFileMatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,17 @@ public function testMatches_dynamicPath_siblingMatchesIndex():void {
self::assertTrue($sut->matches("/request/dynamic-example"));
self::assertFalse($sut->matches("/request/secrets"));
}

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

0 comments on commit f9acf4e

Please sign in to comment.