Env   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setPKG() 0 4 2
A getAutoloaders() 0 10 3
A getConfig() 0 7 2
A getPKG() 0 4 2
A findComposerJSON() 0 12 4
B getEnvironment() 0 20 5
1
<?php
2
/*
3
 * This file is part of the Divergence package.
4
 *
5
 * (c) Henry Paradiz <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Divergence\CLI;
11
12
class Env
13
{
14
    public static $me; // the command used to launch this binary
15
    public static $self; // json data from composer.json for \Divergence\Cli
16
    public static $package; // json data from composer.json for the folder from where you launched this binary
17
18
    public static $hasComposer = false;
19
    public static $isRequired = false;
20
    public static $isRequireDev = false;
21
    public static $namespace = null;
22
23
    public static $autoloaders = null;
24
25
    public static function findComposerJSON($path)
26
    {
27
        while (!file_exists($path.'/composer.json')) {
28
            $path = dirname($path);
29
            if ($path == '/') {
30
                return false; // no composer file
31
            }
32
        }
33
        if (file_exists($path.'/composer.json')) {
34
            return $path.'/composer.json';
35
        }
36
        return false;
37
    }
38
39
    public static function getPKG($path)
40
    {
41
        if ($composer = static::findComposerJSON($path)) {
42
            return json_decode(file_get_contents($composer), true);
43
        }
44
    }
45
46
    public static function setPKG($path, $json)
47
    {
48
        if ($composer = static::findComposerJSON($path)) {
49
            file_put_contents($composer, json_encode($json, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
50
        }
51
    }
52
53
    public static function getEnvironment()
54
    {
55
        static::$self = static::getPKG(__DIR__); // this gets the composer.json info for Divergence\CLI no matter where you ran the binary from
56
        static::$package = static::getPKG(getcwd()); // this gets the composer.json info for the directory from which you ran the binary
57
58
        if (static::$package) {
59
            static::$hasComposer = true;
60
        }
61
        
62
        if (in_array('divergence/divergence', array_keys(static::$package['require']))) {
63
            static::$isRequired = true;
64
        }
65
        if (in_array('divergence/divergence', array_keys(static::$package['require-dev']))) {
66
            static::$isRequireDev = true;
67
        }
68
69
        if (!static::$package['autoload']) {
70
            static::$namespace = null;
71
        } else {
72
            static::$autoloaders = static::getAutoloaders();
73
        }
74
    }
75
76
    public static function getConfig($dir, $Label)
77
    {
78
        $Config = $dir . '/config/' . $Label . '.php';
79
        if (!file_exists($Config)) {
80
            throw new \Exception($Config . ' not found in '.static::class.'::config()');
81
        }
82
        return require $Config;
83
    }
84
85
    public static function getAutoloaders()
86
    {
87
        $autoloaders = [];
88
        if (static::$package['autoload']['psr-4']) {
89
            $autoloaders = array_merge($autoloaders, Env::$package['autoload']['psr-4']);
90
        }
91
        if (static::$package['autoload']['psr-0']) {
92
            $autoloaders = array_merge($autoloaders, Env::$package['autoload']['psr-4']);
93
        }
94
        return $autoloaders;
95
    }
96
}
97