Skip to content

Commit a23593c

Browse files
committed
Improve dir helpers
1 parent 0918e90 commit a23593c

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

Dir.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getDir(string $path = ""): string
4545
*/
4646
public function getRoot(string $path = ""): string
4747
{
48-
return $this->getDir($path);
48+
return $this->getDir("../" . $path);
4949
}
5050

5151
/**

Url.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function getParts(): array
147147
public function getRealPath(): string
148148
{
149149
if (is_null($this->realPath)) {
150-
$this->realPath = str_replace($this->getDirPath(), "", $this->uri->getPath());
150+
$this->realPath = str_replace(rtrim($this->getDirPath(), "/"), "", $this->uri->getPath());
151151
}
152152
if (!is_string($this->realPath)) {
153153
throw new \Exception("Could not create realPath", 1);
@@ -158,8 +158,29 @@ public function getRealPath(): string
158158
/**
159159
* Extract and get directories from the simulated htaccess path
160160
* @return string
161-
*/
161+
*/
162162
public function getDirPath(): string
163+
{
164+
if (is_null($this->dirPath)) {
165+
// Absolute path to the "httpdocs" directory (document root)
166+
$documentRoot = (isset($_SERVER['DOCUMENT_ROOT'])) ? $_SERVER['DOCUMENT_ROOT'] : "";
167+
$documentRoot = htmlspecialchars($documentRoot, ENT_QUOTES, 'UTF-8');
168+
169+
// Absolute path to the current script's directory
170+
$currentScriptDir = $this->request->getUri()->getDir();
171+
172+
// Calculate the relative path from "httpdocs" to the script
173+
$relativePath = str_replace($documentRoot, '', $currentScriptDir);
174+
175+
// Trim any leading or trailing slashes from the relative path
176+
$this->dirPath = "/" . trim($relativePath, '/');
177+
}
178+
179+
return $this->dirPath;
180+
}
181+
182+
/*
183+
public function getDirPathOLD(): string
163184
{
164185
165186
if (is_null($this->dirPath)) {
@@ -178,7 +199,8 @@ public function getDirPath(): string
178199
}
179200
return $this->dirPath;
180201
}
181-
202+
*/
203+
182204
/**
183205
* Get expected slug from path
184206
* @return string
@@ -284,6 +306,7 @@ public function getRoot(string $path = "", bool $endSlash = false): string
284306
if (!is_null($this->handler)) {
285307
$url .= $this->handler->getPublicDirPath();
286308
}
309+
$url = rtrim($url, '/');
287310
return $url . (($endSlash) ? "/" : "") . $path;
288311
}
289312

0 commit comments

Comments
 (0)