Passed
Push — main ( 1e2d69...a54930 )
by Sílvio
02:38
created

EnvHelper::getRootPath()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 21
rs 9.2222
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Helpers;
4
5
use Composer\InstalledVersions;
6
7
class EnvHelper {
8
9
    /**
10
     * @return string
11
     * @throws \RuntimeException se o arquivo .env não for encontrado
12
     */
13
    public static function getRootPath()
14
    {
15
        // Tenta obter a raiz do projeto via Composer, se disponível
16
        if (class_exists(InstalledVersions::class)) {
17
            $rootPackage = InstalledVersions::getRootPackage();
18
            if (!empty($rootPackage['install_path'])) {
19
                return rtrim($rootPackage['install_path'], DIRECTORY_SEPARATOR);
20
            }
21
        }
22
23
        // Fallback: sobe os diretórios a partir do __DIR__ procurando o .env
24
        $baseDir = __DIR__;
25
        while (!file_exists($baseDir . DIRECTORY_SEPARATOR . '.env') && $baseDir !== dirname($baseDir)) {
26
            $baseDir = dirname($baseDir);
27
        }
28
29
        if (!file_exists($baseDir . DIRECTORY_SEPARATOR . '.env')) {
30
            throw new \RuntimeException('<CacheerPHP>: Arquivo .env não encontrado na raiz do projeto.');
31
        }
32
33
        return rtrim($baseDir, DIRECTORY_SEPARATOR);
34
    }
35
}
36