Skip to content

Commit

Permalink
feature: MagicFileMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jun 14, 2021
1 parent fe8a2b8 commit f3770b2
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 24 deletions.
24 changes: 24 additions & 0 deletions src/LogicStream/LogicStreamNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Gt\Routing\LogicStream;

class LogicStreamNamespace implements \Stringable {
public function __construct(
private string $path,
private string $namespacePrefix = ""
) {
}

public function __toString() {
$namespace = $this->namespacePrefix;
$pathParts = explode(DIRECTORY_SEPARATOR, $this->path);
foreach($pathParts as $part) {
$namespace .= "\\$part";
}

return str_replace(
["@", "-", "+", "~", "%", "."],
"_",
$namespace
);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
namespace Gt\Routing;
namespace Gt\Routing\LogicStream;

use SplFileObject;

class LogicStreamWrapper {
const NAMESPACE_PREFIX = "Gt_App_Automatic_Routing";
private int $position;
private string $path;
private string $contents;
Expand Down Expand Up @@ -58,6 +59,8 @@ private function loadContents(SplFileObject $file):void {
while(!$file->eof() && !$foundNamespace) {
$line = $file->fgets();
if($lineNumber === 0) {
// TODO: Allow hashbangs before <?php
// Maybe this is possible by just skipping while the first character is a hash, and not increasing the line number.
if(!str_starts_with($line, "<?php")) {
throw new \Exception("Logic file at " . $this->path . " must start by opening a PHP tag. See https://www.php.gt/routing/logic-stream-wrapper");
}
Expand All @@ -78,7 +81,10 @@ private function loadContents(SplFileObject $file):void {
$foundNamespace = true;
}
elseif($trimmedLine ) {
$namespace = $this->getNamespace();
$namespace = new LogicStreamNamespace(
$this->path,
self::NAMESPACE_PREFIX
);
$this->contents .= "namespace $namespace;\n\n";
$foundNamespace = true;
}
Expand All @@ -93,19 +99,4 @@ private function loadContents(SplFileObject $file):void {
$this->contents .= $line;
}
}

private function getNamespace():string {
$namespace = "Gt_App_Automatic_Routing";
$pathParts = explode(DIRECTORY_SEPARATOR, $this->path);
// array_pop($pathParts);
foreach($pathParts as $part) {
$namespace .= "\\$part";
}

return str_replace(
["@", "-", "+", "~", "%", "."],
"_",
$namespace
);
}
}
4 changes: 3 additions & 1 deletion src/Path/DynamicPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
namespace Gt\Routing\Path;

class DynamicPath {

public function get(string $key):string {
return "TODO";
}
}
6 changes: 1 addition & 5 deletions src/Path/FileMatch/BasicFileMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public function matches(string $uriPath):bool {
$uriPath = trim($uriPath, "/");
$uriPathIndex = trim("$uriPath/index", "/");

$filePathTrimmed = implode("/", [
pathinfo($this->filePath, PATHINFO_DIRNAME),
pathinfo($this->filePath, PATHINFO_FILENAME),
]);
$filePathTrimmed = substr($filePathTrimmed, strlen($this->baseDir . "/"));
$filePathTrimmed = $this->getTrimmedFilePath();

if($uriPath === $filePathTrimmed
|| $uriPathIndex === $filePathTrimmed) {
Expand Down
19 changes: 19 additions & 0 deletions src/Path/FileMatch/FileMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,23 @@ public function __construct(
}

abstract public function matches(string $uriPath):bool;

protected function getTrimmedFilePath(
bool $includeBaseDir = false
):string {
$filePathTrimmed = implode("/", [
pathinfo($this->filePath, PATHINFO_DIRNAME),
pathinfo($this->filePath, PATHINFO_FILENAME),
]);

if($includeBaseDir) {
return $filePathTrimmed;
}
else {
return substr(
$filePathTrimmed,
strlen($this->baseDir . "/")
);
}
}
}
24 changes: 24 additions & 0 deletions src/Path/FileMatch/MagicFileMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
namespace Gt\Routing\Path\FileMatch;

class MagicFileMatch extends FileMatch {
const MAGIC_FILENAME_ARRAY = [
"_common",
"_header",
"_footer",
];

public function matches(string $uriPath):bool {
$uriPath = trim($uriPath, "/");
$uriPathParts = explode("/", $uriPath);

$filePathTrimmed = $this->getTrimmedFilePath(true);

$searchDir = $this->baseDir;
foreach($uriPathParts as $pathPart) {
foreach(self::MAGIC_FILENAME_ARRAY as $magicFilename) {
$searchFilepath = "$searchDir/$magicFilename";
if($searchFilepath === $filePathTrimmed) {
return true;
}
}

$searchDir .= "/$pathPart";
}

return false;
}
}
4 changes: 3 additions & 1 deletion src/Path/PathMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class PathMatcher {
private array $filterArray;

public function __construct(
private string $baseDirectory,
DirectoryExpander $expander = null
) {
$this->expander = $expander ?? new DirectoryExpander();
Expand Down Expand Up @@ -88,7 +89,8 @@ private function filterUri(
fn(string $filePath):bool => call_user_func(
$filter,
$filePath,
$uriPath
$uriPath,
$this->baseDirectory
)
);
}
Expand Down
47 changes: 47 additions & 0 deletions test/phpunit/Path/FileMatch/MagicFileMatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace Gt\Routing\Test\Path\FileMatch;

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

class MagicFileMatchTest extends TestCase {
public function testMatches_none():void {
$sut = new MagicFileMatch(
"basedir/something/nested.txt",
"basedir"
);
self::assertFalse($sut->matches("/"));
}

public function testMatches_noDirectMatch():void {
$sut = new MagicFileMatch(
"basedir/something/nested.txt",
"basedir"
);
self::assertFalse($sut->matches("/something/nested"));
}

public function testMatches_sameDir():void {
$sut = new MagicFileMatch(
"basedir/something/_common.php",
"basedir"
);
self::assertTrue($sut->matches("/something/nested"));
}

public function testMatches_parentDir():void {
$sut = new MagicFileMatch(
"basedir/_common.php",
"basedir"
);
self::assertTrue($sut->matches("/something/nested"));
}

public function testMatches_ancestorDir():void {
$sut = new MagicFileMatch(
"basedir/something/_common.php",
"basedir"
);
self::assertTrue($sut->matches("/something/very/deeply/nested"));
}
}

0 comments on commit f3770b2

Please sign in to comment.