Skip to content

Commit

Permalink
feature: dynamic path matching
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jun 14, 2021
1 parent f3770b2 commit 495aa20
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/Path/FileMatch/BasicFileMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

/**
* A "Basic" FileMatch is one that directly matches a Uri path to a
* file path by name. Uris are
* file path by name.
*/
class BasicFileMatch extends FileMatch {
public function matches(string $uriPath):bool {
$uriPath = trim($uriPath, "/");
$uriPathIndex = trim("$uriPath/index", "/");

$uriPathParts = explode("/", $uriPath);
$filePathTrimmed = $this->getTrimmedFilePath();
$uriPathParts = $this->filterDynamicPathParts($filePathTrimmed, $uriPathParts);
$uriPath = implode("/", $uriPathParts);
$uriPathIndex = trim("$uriPath/index", "/");

if($uriPath === $filePathTrimmed
|| $uriPathIndex === $filePathTrimmed) {
Expand Down
7 changes: 0 additions & 7 deletions src/Path/FileMatch/DynamicFileMatch.php

This file was deleted.

29 changes: 29 additions & 0 deletions src/Path/FileMatch/FileMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ protected function getTrimmedFilePath(
pathinfo($this->filePath, PATHINFO_DIRNAME),
pathinfo($this->filePath, PATHINFO_FILENAME),
]);
$filePathTrimmed = preg_replace(
"/\/@[^\/]+/",
"/@",
$filePathTrimmed
);

if($includeBaseDir) {
return $filePathTrimmed;
Expand All @@ -28,4 +33,28 @@ protected function getTrimmedFilePath(
);
}
}

/**
* @param string $filePath
* @param string[] $uriPathParts
* @return string[]
*/
protected function filterDynamicPathParts(
string $filePath,
array $uriPathParts
):array {
$filePathParts = explode("/", $filePath);
foreach($uriPathParts as $i => $uriPathPart) {
if(!isset($filePathParts[$i])) {
break;
}

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

return $uriPathParts;
}
}
7 changes: 4 additions & 3 deletions src/Path/FileMatch/MagicFileMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public function matches(string $uriPath):bool {
$uriPath = trim($uriPath, "/");
$uriPathParts = explode("/", $uriPath);

$filePathTrimmed = $this->getTrimmedFilePath(true);
$filePathTrimmed = $this->getTrimmedFilePath();
$uriPathParts = $this->filterDynamicPathParts($filePathTrimmed, $uriPathParts);

$searchDir = $this->baseDir;
$searchDir = "";
foreach($uriPathParts as $pathPart) {
foreach(self::MAGIC_FILENAME_ARRAY as $magicFilename) {
$searchFilepath = "$searchDir/$magicFilename";
$searchFilepath = trim("$searchDir/$magicFilename", "/");
if($searchFilepath === $filePathTrimmed) {
return true;
}
Expand Down
33 changes: 33 additions & 0 deletions test/phpunit/Path/FileMatch/BasicFileMatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Gt\Routing\Test\Path\FileMatch;

use Gt\Routing\Path\FileMatch\BasicFileMatch;
use Gt\Routing\Path\FileMatch\FileMatch;
use PHPUnit\Framework\TestCase;

class BasicFileMatchTest extends TestCase {
Expand Down Expand Up @@ -44,4 +45,36 @@ public function testMatches_shouldMatchIndexNoNesting():void {
);
self::assertTrue($sut->matches("/"));
}

public function testMatches_dynamicDir():void {
$sut = new BasicFileMatch(
"page/shop/@category/@item.html",
"page"
);
self::assertTrue($sut->matches("/shop/cakes/chocolate"));
}

public function testMatches_dynamicDir_doesNotMatchIndex():void {
$sut = new BasicFileMatch(
"page/shop/@category/@item.html",
"page"
);
self::assertFalse($sut->matches("/shop/cakes"));
}

public function testMatches_dynamicDir_doesNotMatchDeeper():void {
$sut = new BasicFileMatch(
"page/shop/@category/@item.html",
"page"
);
self::assertFalse($sut->matches("/shop/cakes/chocolate/nothing"));
}

public function testMatches_dynamicDir_matchesIndex():void {
$sut = new BasicFileMatch(
"page/shop/@category/index.html",
"page"
);
self::assertTrue($sut->matches("/shop/cakes"));
}
}
8 changes: 8 additions & 0 deletions test/phpunit/Path/FileMatch/MagicFileMatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public function testMatches_ancestorDir():void {
);
self::assertTrue($sut->matches("/something/very/deeply/nested"));
}

public function testMatches_dynamicDir():void {
$sut = new MagicFileMatch(
"basedir/something/@dynamic/_common.php",
"basedir"
);
self::assertTrue($sut->matches("/something/this-is-dynamic/test"));
}
}

0 comments on commit 495aa20

Please sign in to comment.