Created
October 30, 2024 21:53
-
-
Save aklump/03b743c3b53315ad25869f7775d75a8a to your computer and use it in GitHub Desktop.
Create a pretty/short path, removing CWD or basepath.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @code | |
* // Print a shortened, nice-to-read path when possible. | |
* echo (new GetShortPath(getcwd())($long_path) | |
* @endcode | |
*/ | |
class GetShortPath { | |
private string $basepath; | |
public function __construct(string $basepath) { | |
$this->basepath = $basepath; | |
} | |
public function __invoke(string $path): string { | |
if (!str_starts_with($path, $this->basepath)) { | |
return $path; | |
} | |
$short_path = substr($path, strlen($this->basepath) + 1); | |
if ($this->basepath === getcwd()) { | |
$short_path = "./$short_path"; | |
} | |
return $short_path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment