diff --git a/src/FileSystem/Path.php b/src/FileSystem/Path.php index f66c291c..12301e39 100644 --- a/src/FileSystem/Path.php +++ b/src/FileSystem/Path.php @@ -2,6 +2,7 @@ namespace Gt\WebEngine\FileSystem; use DirectoryIterator; +use Gt\WebEngine\Route\Router; class Path { protected static $appRoot; @@ -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; diff --git a/src/Logic/LogicFactory.php b/src/Logic/LogicFactory.php index 2d0c95ed..994d8120 100644 --- a/src/Logic/LogicFactory.php +++ b/src/Logic/LogicFactory.php @@ -145,12 +145,12 @@ 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] !== "@") { @@ -158,7 +158,10 @@ public static function getDynamicPathParameters( } $partName = substr($part, 1); - $keyValuePairs[$partName] = $uriParts[$i]; + + if(isset($uriParts[$i])) { + $keyValuePairs[$partName] = $uriParts[$i]; + } } return new DynamicPath($keyValuePairs); diff --git a/src/Route/Router.php b/src/Route/Router.php index 9daaa17a..ee660db3 100644 --- a/src/Route/Router.php +++ b/src/Route/Router.php @@ -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 @@ -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, @@ -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; } diff --git a/test/project/dynamic-uris/page/dir/nested-no-index/@dynamicPage.php b/test/project/dynamic-uris/page/dir/nested-no-index/@dynamicPage.php index ba059a7e..4c9e3dec 100644 --- a/test/project/dynamic-uris/page/dir/nested-no-index/@dynamicPage.php +++ b/test/project/dynamic-uris/page/dir/nested-no-index/@dynamicPage.php @@ -10,7 +10,7 @@ public function go() { $this->document->bindKeyValue( "dynamic-page", - $this->dynamicPath->get("dynamicPage") + $this->dynamicPath->get("dynamicPage") ?? "index" ); } } \ No newline at end of file