Passed
Pull Request — master (#107)
by
unknown
03:04
created

Environment::getValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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.8.0
13
 */
14
15
namespace Quantum\Environment;
16
17
use Quantum\Libraries\Storage\FileSystem;
18
use Quantum\Exceptions\EnvException;
19
use Quantum\Loader\Loader;
20
use Quantum\Loader\Setup;
21
use Quantum\Di\Di;
22
use Dotenv\Dotenv;
23
24
/**
25
 * Class Environment
26
 * @package Quantum\Environment
27
 * @uses Dotenv
28
 */
29
class Environment
30
{
31
32
    /**
33
     * FileSystem instance
34
     * @var \Quantum\Libraries\Storage\FileSystem
35
     */
36
    private $fs;
37
38
    /**
39
     * Environment file
40
     * @var string
41
     */
42
    private $envFile = '.env';
43
44
    /**
45
     * Loaded env content
46
     * @var array
47
     */
48
    private $envContent = [];
49
50
    /**
51
     * Instance of Environment
52
     * @var \Quantum\Environment\Environment
53
     */
54
    private static $instance = null;
55
56
    /**
57
     * GetInstance
58
     * @return \Quantum\Environment\Environment
59
     */
60
    public static function getInstance(): Environment
61
    {
62
        if (self::$instance === null) {
63
            self::$instance = new self();
64
        }
65
66
        return self::$instance;
67
    }
68
69
    /**
70
     * Loads environment variables from file
71
     * @param \Quantum\Loader\Setup $setup
72
     * @return $this
73
     * @throws \Quantum\Exceptions\DiException
74
     * @throws \ReflectionException
75
     * @throws \Quantum\Exceptions\EnvException
76
     */
77
    public function load(Setup $setup): Environment
78
    {
79
        $env = Di::get(Loader::class)->setup($setup)->load();
80
81
        if (isset($env['app_env']) && $env['app_env'] != 'production') {
82
            $this->envFile = '.env.' . $env['app_env'];
83
        }
84
85
        $this->fs = Di::get(FileSystem::class);
86
87
        if (!$this->fs->exists(base_dir() . DS . $this->envFile)) {
88
            throw EnvException::fileNotFound();
89
        }
90
91
        $this->envContent = Dotenv::createMutable(base_dir(), $this->envFile)->load();
92
93
        return $this;
94
    }
95
96
    /**
97
     * Gets the environment variable value
98
     * @param string $key
99
     * @param null|mixed $default
100
     * @return mixed
101
     */
102
    public function getValue(string $key, $default = null)
103
    {
104
        $val = getenv($key);
105
106
        if ($val === false) {
107
            if ($default) {
108
                return $default;
109
            }
110
111
            return null;
112
        }
113
114
        return $val;
115
    }
116
117
    /**
118
     * Creates or updates the row in .env
119
     * @param string $key
120
     * @param string|null $value
121
     */
122
    public function updateRow(string $key, ?string $value)
123
    {
124
        $row = $this->getRow($key);
125
126
        $envFilePath = base_dir() . DS . $this->envFile;
127
128
        if ($row) {
129
            $this->fs->put($envFilePath, preg_replace(
130
                '/^' . $key . "=" . $row . '/m',
131
                $key . "=" . $value,
132
                $this->fs->get($envFilePath)
133
            ));
134
        } else {
135
            $this->fs->append($envFilePath, $key . "=" . $value . PHP_EOL);
136
        }
137
138
        $this->envContent = Dotenv::createMutable(base_dir(), $this->envFile)->load();
139
    }
140
141
    /**
142
     * Gets the row of .env file by given key
143
     * @param string $key
144
     * @return string|null
145
     */
146
    private function getRow(string $key): ?string
147
    {
148
        foreach ($this->envContent as $index => $row) {
149
            if (preg_match('/^' . $key . '/', $index)) {
150
                return $key . '=' . preg_quote($row, '/');
151
            }
152
        }
153
154
        return null;
155
    }
156
    
157
    /**
158
     * Checks if there is a such key
159
     * @param string $key
160
     */
161
    public function hasKey(string $key): bool
162
    {
163
        foreach ($this->envContent as $index => $row) {
164
            if (preg_match('/^' . $key . '/', $index)) {
165
                return true;
166
            }
167
        }
168
169
        return false;
170
    }
171
}
172