EnvHelper::getRootPath()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 2
b 0
f 0
nc 5
nop 0
dl 0
loc 17
rs 9.6111
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Helpers;
4
5
use Composer\InstalledVersions;
6
7
/**
8
 * Class EnvHelper
9
 * @author Sílvio Silva <https://github.com/silviooosilva>
10
 * @package Silviooosilva\CacheerPhp
11
 */
12
class EnvHelper {
13
14
    /**
15
     * Gets the root path of the project.
16
     * 
17
     * @return string
18
     */
19
    public static function getRootPath()
20
    {
21
        // Try to get the root path from Composer's installed versions
22
        if (class_exists(InstalledVersions::class)) {
23
            $rootPackage = InstalledVersions::getRootPackage();
24
            if (!empty($rootPackage['install_path'])) {
25
                return rtrim($rootPackage['install_path'], DIRECTORY_SEPARATOR);
26
            }
27
        }
28
29
        // Fallback: traverse directories from __DIR__ looking for .env.example
30
        $baseDir = __DIR__;
31
        while (!file_exists($baseDir . DIRECTORY_SEPARATOR . '.env.example') && $baseDir !== dirname($baseDir)) {
32
            $baseDir = dirname($baseDir);
33
        }
34
35
        return rtrim($baseDir, DIRECTORY_SEPARATOR);
36
    }
37
}
38