-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotenv.php
executable file
·161 lines (150 loc) · 4.26 KB
/
dotenv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace PMVC\PlugIn\dotenv;
${_INIT_CONFIG}[_CLASS] = __NAMESPACE__ . '\dotenv';
const ENV_FILE = 'envFile';
const ENV_FOLDER = 'envFolder';
const ESCAPE = 'escape';
/**
* @parameters string envFile ENV_FILE
* @parameters string envFolder ENV_FOLDER
* @parameters string escape if key start with escape will bypass underscore
*/
class dotenv extends \PMVC\PlugIn
{
public function init()
{
if (isset($this[0])) {
$this[ENV_FILE] = $this[0];
unset($this[0]);
}
if (isset($this[ENV_FILE])) {
$this->initEnvFile();
}
if (empty($this[ENV_FOLDER]) && \PMVC\exists('controller', 'plug')) {
$this[ENV_FOLDER] = \PMVC\plug('controller')->getAppsParent();
}
}
public function initEnvFile()
{
$path = $this->fileExists($this[ENV_FILE]);
if (!$path) {
if ($this['bypass']) {
return;
}
return !trigger_error(
'[DotEnv:init] File not found. [' . $this[ENV_FILE] . ']',
E_USER_WARNING
);
}
if (is_file($path)) {
$this->toPMVC($path);
if (empty($this[ENV_FOLDER])) {
$this[ENV_FOLDER] = dirname($path);
}
} else {
if (empty($this[ENV_FOLDER])) {
$this[ENV_FOLDER] = $path;
}
unset($this[ENV_FILE]);
}
}
public function replace(&$arr, $replaces, $cb)
{
array_walk_recursive($arr, function (&$item, $key) use (
$replaces,
$cb
) {
$item = \PMVC\tpl($item, $replaces, $cb);
});
}
public function toPMVC($file, $prefix = '')
{
$arr = $this->getUnderscoreToArray($file);
if (!is_array($arr)) {
return false;
}
if (isset($arr['_'])) {
$this->_processConstantArray($arr);
}
$to = ['__DIR__' => dirname(\PMVC\realpath($file))];
$this->replace($arr, ['__DIR__'], function ($payload) use ($to) {
return \PMVC\get($to, $payload['replaceKey']);
});
foreach ($arr as $k => $v) {
$old = \PMVC\getOption($k);
if (!empty($old) && is_array($old) && is_array($v)) {
$v = array_merge_recursive($old, $v);
}
\PMVC\option('set', $k, $v);
}
}
/**
* __VIEW_ENGINE='react'
* replace to
* constant('_VIEW_ENGINE') = 'react'
*/
private function _processConstantArray(&$arr)
{
$_ = \PMVC\plug('underscore')
->array()
->toUnderscore($arr['_']);
unset($arr['_']);
foreach ($_ as $k => $v) {
if (defined($k)) {
$k = constant($k);
} else {
$k = '_' . $k;
}
$arr[$k] = $v;
}
return $arr;
}
public function processConstantArray($arr)
{
return $this->_processConstantArray($arr);
}
public function getUnderscoreToArray($file)
{
$arr = $this->getArray($file);
if (!is_array($arr)) {
return false;
}
return \PMVC\plug('underscore')
->underscore()
->toArray($arr, $this[ESCAPE]);
}
public function fileExists($file)
{
$realPath = \PMVC\realpath($file);
if ($realPath) {
return $realPath;
}
$realPath = \PMVC\realpath(\PMVC\lastSlash($this[ENV_FOLDER]) . $file);
if ($realPath) {
return $realPath;
}
return false;
}
public function getArray($file)
{
if (is_string($file)) {
$realPath = $this->fileExists($file);
if (!$realPath) {
return !trigger_error(
'[DotEnv:getArray] File not found. [' . $file . ']',
E_USER_WARNING
);
}
$content = file_get_contents($realPath);
} elseif (is_object($file) && isset($file->raw)) {
$content = $file->raw;
} else {
$content = null;
}
if ($content) {
return parse_ini_string($content, false);
} else {
return [];
}
}
}