Created
May 9, 2026 14:14
-
-
Save aklump/56425150ff346b229adab691cf36f307 to your computer and use it in GitHub Desktop.
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 | |
| namespace AKlump\Knowledge\Helpers; | |
| /** | |
| * Retrieves the home directory of the current user. | |
| * | |
| * The method first attempts to fetch the home directory using the `HOME` | |
| * or `USERPROFILE` environment variables. If neither is set, it combines | |
| * the `HOMEDRIVE` and `HOMEPATH` environment variables to determine the | |
| * home directory. If no home directory can be determined, it returns null. | |
| * | |
| * @return string|null The home directory path, or null if it cannot be determined. | |
| */ | |
| class GetUserHomeDirectory { | |
| public function __invoke() { | |
| $home = getenv('HOME') ?: getenv('USERPROFILE'); | |
| if ($home) { | |
| return rtrim($home, DIRECTORY_SEPARATOR); | |
| } | |
| $home_drive = getenv('HOMEDRIVE'); | |
| $home_path = getenv('HOMEPATH'); | |
| if ($home_drive && $home_path) { | |
| return rtrim($home_drive . $home_path, DIRECTORY_SEPARATOR); | |
| } | |
| return NULL; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment