Skip to content

Commit

Permalink
Update router with changes to dynamic pages for #320 *NEEDS TEST*
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jul 25, 2019
1 parent e0d5203 commit 2c6b3c5
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 39 deletions.
50 changes: 42 additions & 8 deletions src/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,24 @@ protected function getDirectoryForUri(string $uri):string {
}

protected function getBasenameForUri(string $uri):string {
$basePath = $this->getBaseViewLogicPath();
$subPath = $this->getViewLogicSubPath($uri);
$baseName = static::DEFAULT_BASENAME;
$pageDirPath = $this->getBaseViewLogicPath();
$subDirPath = $this->getViewLogicSubPath($uri);
$fileBasename = $this->getViewLogicBasename($uri);

$absolutePath = $basePath . $subPath;
$absolutePath = $pageDirPath . $subDirPath . "/" . $fileBasename;
$lastSlashPosition = strrpos(
$subPath,
$subDirPath,
DIRECTORY_SEPARATOR
);

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


return $baseName;
return $fileBasename;
}

/**
Expand All @@ -153,10 +152,45 @@ protected function getViewLogicSubPath(string $uriPath):string {
$baseViewLogicPath = $this->getBaseViewLogicPath();
$absolutePath = $baseViewLogicPath . $uriPath;

if(!is_dir($absolutePath)) {
$absolutePath = dirname($absolutePath);
}

$relativePath = substr($absolutePath, strlen($baseViewLogicPath));
if(strlen($relativePath) > 1) {
$relativePath = rtrim($relativePath, DIRECTORY_SEPARATOR);
}

return $relativePath;
}

protected function getViewLogicBasename(string $uriPath):string {
$basename = self::DEFAULT_BASENAME;

$uriPath = str_replace(
"/",
DIRECTORY_SEPARATOR,
$uriPath
);
$baseViewLogicPath = $this->getBaseViewLogicPath();
$absolutePath = $baseViewLogicPath . $uriPath;

$lastSlashPos = strrpos($uriPath, "/");
$lastSlashAbsolutePos = strrpos($absolutePath, "/");
$lastPathPart = substr($uriPath, $lastSlashPos + 1);
$absolutePathWithoutLastPart = substr(
$absolutePath,
0,
$lastSlashAbsolutePos
);

$matchingPageFiles = glob("$absolutePath.*");
$matchingDynamics = glob("$absolutePathWithoutLastPart/@*");
if(!empty($matchingPageFiles)
|| !empty($matchingDynamics)) {
$basename = $lastPathPart;
}

return $basename;
}
}
115 changes: 84 additions & 31 deletions test/project/dynamic-uris/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2c6b3c5

Please sign in to comment.