Completed
Pull Request — master (#40)
by Arman
03:47
created

Environment::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.0.0
13
 */
14
15
namespace Quantum\Environment;
16
17
use Quantum\Libraries\Storage\FileSystem;
18
use Quantum\Loader\Loader;
19
use Quantum\Loader\Setup;
20
use Dotenv\Dotenv;
21
22
/**
23
 * Class Environment
24
 * @package Quantum\Environment
25
 * @uses Dotenv
26
 */
27
class Environment
28
{
29
30
    /**
31
     * Environment file
32
     * @var string 
33
     */
34
    private $envFile = '.env';
35
36
    /**
37
     * Loaded env content
38
     * @var array 
39
     */
40
    private $envContent = [];
41
42
    /**
43
     * Instance of Environment
44
     * @var Environment 
45
     */
46
    private static $envInstance = null;
47
48
    /**
49
     * GetInstance
50
     * @return Environment
51
     */
52
    public static function getInstance()
53
    {
54
        if (self::$envInstance === null) {
55
            self::$envInstance = new self();
56
        }
57
58
        return self::$envInstance;
59
    }
60
61
    /**
62
     * Loads the environment variables from .env file
63
     * @return $this
64
     */
65
    public function load(Loader $loader)
66
    {
67
        $env = $loader->setup(new Setup('config', 'env', true))->load();
68
69
        if ($env['app_env'] != 'production') {
70
            $this->envFile = '.env.' . $env['app_env'];
71
        }
72
73
        $this->envContent = (new Dotenv(base_dir(), $this->envFile))->load();
74
        return $this;
75
    }
76
77
    /**
78
     * Gets the environment variable value
79
     * @param string $key
80
     * @param mixed $default
81
     * @return mixed
82
     */
83
    public function getValue($key, $default = null)
84
    {
85
        $val = getenv($key);
86
87
        if ($val === false) {
88
            if ($default) {
89
                return $default;
90
            }
91
92
            return null;
93
        } else {
94
            return $val;
95
        }
96
    }
97
98
    /**
99
     * Creates or updates the row in .env
100
     * @param FileSystem $fs
101
     * @param string $key
102
     * @param string $value
103
     */
104
    public function updateRow(FileSystem $fs, $key, $value)
105
    {
106
        $oldRow = $this->getRow($key);
107
108
        $envFilePath = base_dir() . DS . $this->envFile;
109
110
        if ($oldRow) {
111
            $fs->put($envFilePath, preg_replace(
112
                            '/^' . $oldRow . '/m',
113
                            $key . "=" . $value . PHP_EOL,
114
                            $fs->get($envFilePath)
115
            ));
116
        } else {
117
            $fs->put($envFilePath, $key . "=" . $value . PHP_EOL, FILE_APPEND);
118
        }
119
120
        $this->envContent = (new Dotenv(base_dir(), $this->envFile))->overload();
121
    }
122
123
    /**
124
     * Gets the row of .env file by given key
125
     * @param $key
126
     * @return string|null
127
     */
128
    private function getRow($key)
129
    {
130
        foreach ($this->envContent as $row) {
131
            if (preg_match('/^' . $key . '=/', $row)) {
132
                return preg_quote($row, '/');
133
            }
134
        }
135
136
        return null;
137
    }
138
139
}
140