Skip to content

Instantly share code, notes, and snippets.

@aklump
Created October 30, 2024 21:53
Show Gist options
  • Save aklump/03b743c3b53315ad25869f7775d75a8a to your computer and use it in GitHub Desktop.
Save aklump/03b743c3b53315ad25869f7775d75a8a to your computer and use it in GitHub Desktop.
Create a pretty/short path, removing CWD or basepath.
<?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