Skip to content

Commit

Permalink
Check for dynamic page after index, fixes #320
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jul 23, 2019
1 parent 523e898 commit 58f23b9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
20 changes: 14 additions & 6 deletions src/FileSystem/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Gt\WebEngine\FileSystem;

use DirectoryIterator;
use Gt\WebEngine\Route\Router;

class Path {
protected static $appRoot;
Expand Down Expand Up @@ -145,19 +146,26 @@ public static function fixPathCase(string $path):string {
return $output;
}

public static function isDirectoryOrDynamic(string $absolutePath):bool {
if(is_dir($absolutePath)) {
return true;
}
public static function isDynamic(string $absolutePath):bool {
$pathParts = explode(
DIRECTORY_SEPARATOR,
$absolutePath
);

$pathParts = explode(DIRECTORY_SEPARATOR, $absolutePath);
while(count($pathParts) > 2) {
array_pop($pathParts);
$removed = array_pop($pathParts);
$searchPath = implode(
DIRECTORY_SEPARATOR,
$pathParts
);

if($removed === Router::DEFAULT_BASENAME) {
$indexFiles = glob("$searchPath/index.*");
if(!empty($indexFiles)) {
return false;
}
}

$dynamicFiles = glob("$searchPath/@*");
if(!empty($dynamicFiles)) {
return true;
Expand Down
9 changes: 6 additions & 3 deletions src/Logic/LogicFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,23 @@ public static function getDynamicPathParameters(
$uriParts = explode("/", $uriPath);
$uriParts = array_filter($uriParts);

if(Path::isDirectoryOrDynamic($absolutePath)) {
if(!Path::isDynamic($absolutePath)
&& is_dir($absolutePath)) {
$uriParts []= "index";
}

$keyValuePairs = [];

foreach($relativeDirParts as $i => $part) {
$part = strtok($part, ".");
if($part[0] !== "@") {
continue;
}

$partName = substr($part, 1);
$keyValuePairs[$partName] = $uriParts[$i];

if(isset($uriParts[$i])) {
$keyValuePairs[$partName] = $uriParts[$i];
}
}

return new DynamicPath($keyValuePairs);
Expand Down
17 changes: 12 additions & 5 deletions src/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public function getViewAssembly(string $uri):Assembly {
protected function getDirectoryForUri(string $uri):string {
$basePath = $this->getBaseViewLogicPath();
$subPath = $this->getViewLogicSubPath($uri);
$absolutePath = $basePath . $subPath;

if(!Path::isDirectoryOrDynamic($basePath . $subPath)) {
// Note: use of forward slash here is correct due to working with URL, not directory path.
if(Path::isDynamic($absolutePath)) {
$lastSlashPosition = strrpos(
$subPath,
DIRECTORY_SEPARATOR
Expand All @@ -79,6 +79,7 @@ protected function getDirectoryForUri(string $uri):string {
);
}

// Note: use of forward slash here is correct due to working with URL, not directory path.
$subPath = str_replace(
"/",
DIRECTORY_SEPARATOR,
Expand All @@ -92,14 +93,20 @@ protected function getBasenameForUri(string $uri):string {
$subPath = $this->getViewLogicSubPath($uri);
$baseName = static::DEFAULT_BASENAME;

if(!Path::isDirectoryOrDynamic($basePath . $subPath)) {
$lastSlashPosition = strrpos($subPath, DIRECTORY_SEPARATOR);
$absolutePath = $basePath . $subPath;
$lastSlashPosition = strrpos(
$subPath,
DIRECTORY_SEPARATOR
);

if(Path::isDynamic($absolutePath)) {
$baseName = substr(
$subPath,
$absolutePath,
$lastSlashPosition + 1
);
}


return $baseName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function go() {

$this->document->bindKeyValue(
"dynamic-page",
$this->dynamicPath->get("dynamicPage")
$this->dynamicPath->get("dynamicPage") ?? "index"
);
}
}

0 comments on commit 58f23b9

Please sign in to comment.