Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 52b4c7b

Browse files
committed
update for dot env class
1 parent d372bf8 commit 52b4c7b

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

src/Utils/PhpDotEnv.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@
1111
/**
1212
* Class PhpDotEnv - local env read
1313
* @package Inhere\Library\Utils
14-
* in local config file(must is 'ini' format):
14+
* in local config file `.env` (must is 'ini' format):
1515
* ```ini
16-
* env=dev
17-
* debug=true
16+
* ENV=dev
17+
* DEBUG=true
1818
* ... ...
1919
* ```
2020
* IN CODE:
2121
* ```php
22-
* new PhpDotEnv(__DIE__, '.env');
23-
* env('debug', false);
24-
* env('env_name', 'pdt');
22+
* PhpDotEnv::load(__DIE__);
23+
* env('DEBUG', false);
24+
* env('ENV', 'pdt');
2525
* ```
2626
*/
27-
class PhpDotEnv
27+
final class PhpDotEnv
2828
{
2929
/**
3030
* @param string $fileDir
3131
* @param string $fileName
3232
* @return static
3333
*/
34-
public static function init(string $fileDir, string $fileName = '.env')
34+
public static function load(string $fileDir, string $fileName = '.env')
3535
{
36-
return new static($fileDir, $fileName);
36+
return new self($fileDir, $fileName);
3737
}
3838

3939
/**
@@ -46,17 +46,17 @@ public function __construct(string $fileDir, string $fileName = '.env')
4646
$file = $fileDir . DIRECTORY_SEPARATOR . ($fileName ?: '.env');
4747

4848
if (is_file($file) && is_readable($file)) {
49-
$this->load(parse_ini_file($file));
49+
$this->settingEnv(parse_ini_file($file));
5050
}
5151
}
5252

5353
/**
54-
* load env data
54+
* setting env data
5555
*/
56-
protected function load($data)
56+
protected function settingEnv($data)
5757
{
5858
foreach ($data as $name => $value) {
59-
if (!is_string($value)) {
59+
if (is_int($name) || !is_string($value)) {
6060
continue;
6161
}
6262

src/functions.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,21 @@ function class_basename($class)
4343
function retry($times, callable $callback, $sleep = 0)
4444
{
4545
$times--;
46+
4647
beginning:
4748
try {
4849
return $callback();
4950
} catch (Exception $e) {
5051
if (!$times) {
5152
throw $e;
5253
}
54+
5355
$times--;
56+
5457
if ($sleep) {
5558
usleep($sleep * 1000);
5659
}
60+
5761
goto beginning;
5862
}
5963
}
@@ -62,9 +66,24 @@ function retry($times, callable $callback, $sleep = 0)
6266
if (!function_exists('env')) {
6367
function env(string $name, $default = null)
6468
{
65-
$value = getenv(strtoupper($name));
69+
$value = getenv($name);
6670

67-
return false !== $value ? $value : $default;
71+
if ($value === false) {
72+
return value($default);
73+
}
74+
75+
switch (strtolower($value)) {
76+
case 'true':
77+
return true;
78+
case 'false':
79+
return false;
80+
case 'empty':
81+
return '';
82+
case 'null':
83+
return null;
84+
}
85+
86+
return $value;
6887
}
6988
}
7089

0 commit comments

Comments
 (0)