-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironmentVariableTrait.php
More file actions
66 lines (54 loc) · 1.68 KB
/
EnvironmentVariableTrait.php
File metadata and controls
66 lines (54 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
/*
* This file is part of ezkoding
*
* (c) 2025 Oliver Glowa, coding.glowa.com
*
* This source file is subject to the Apache-2.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace ollily\Tools;
use Composer\Factory;
/**
* TODO: Replace literals with constant.
*/
trait EnvironmentVariableTrait
{
final public static function getHome(string $homeVariable = 'HOME'): string
{
$home = getenv($homeVariable);
/** @psalm-suppress RiskyTruthyFalsyComparison */
if ($homeVariable == 'HOME' && empty($home)) {
// we are on windows?
$home = getenv('USERPROFILE');
}
if (is_bool($home)) {
$home = '';
}
return $home;
}
final public static function getProjectRoot(): string
{
$projectRoot = self::getComposerFilePath();
if (empty($projectRoot)) {
$projectRoot = self::getProjectRootFallback();
}
return (string)realpath($projectRoot);
}
private static function getComposerFilePath(): string
{
$composerFile = Factory::getComposerFile();
$composerPath = (string)realpath(dirname($composerFile));
if ('.' == $composerPath) {
$composerPath = '';
}
return $composerPath;
}
private static function getProjectRootFallback(int $folderOffset = 2): string
{
$rootClazz = new \ReflectionClass(EnvironmentVariableTrait::class);
$rootPath = dirname((string)realpath((string)$rootClazz->getFileName()));
return (string)realpath($rootPath . str_repeat(DIRECTORY_SEPARATOR . '..', $folderOffset));
}
}