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

Commit d372bf8

Browse files
committed
Merge branch 'master' of https://github.com/inhere/php-librarys
2 parents 84a7213 + 445182c commit d372bf8

File tree

4 files changed

+76
-94
lines changed

4 files changed

+76
-94
lines changed

src/Utils/LocalConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* ```
2020
* in code:
2121
* ```
22-
* $lev = new LocalEnv(__DIE__, '.local');
22+
* $lev = new LocalConfig(__DIE__, '.local');
2323
* $debug = $lev->get('debug', false);// can also use function: local_env('debug', false)
2424
* $env = $lev->get('env', 'pdt');
2525
* ```

src/Utils/LocalEnv.php

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/Utils/PhpDotEnv.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/6/17
6+
* Time: 上午10:11
7+
*/
8+
9+
namespace Inhere\Library\Utils;
10+
11+
/**
12+
* Class PhpDotEnv - local env read
13+
* @package Inhere\Library\Utils
14+
* in local config file(must is 'ini' format):
15+
* ```ini
16+
* env=dev
17+
* debug=true
18+
* ... ...
19+
* ```
20+
* IN CODE:
21+
* ```php
22+
* new PhpDotEnv(__DIE__, '.env');
23+
* env('debug', false);
24+
* env('env_name', 'pdt');
25+
* ```
26+
*/
27+
class PhpDotEnv
28+
{
29+
/**
30+
* @param string $fileDir
31+
* @param string $fileName
32+
* @return static
33+
*/
34+
public static function init(string $fileDir, string $fileName = '.env')
35+
{
36+
return new static($fileDir, $fileName);
37+
}
38+
39+
/**
40+
* constructor.
41+
* @param string $fileDir
42+
* @param string $fileName
43+
*/
44+
public function __construct(string $fileDir, string $fileName = '.env')
45+
{
46+
$file = $fileDir . DIRECTORY_SEPARATOR . ($fileName ?: '.env');
47+
48+
if (is_file($file) && is_readable($file)) {
49+
$this->load(parse_ini_file($file));
50+
}
51+
}
52+
53+
/**
54+
* load env data
55+
*/
56+
protected function load($data)
57+
{
58+
foreach ($data as $name => $value) {
59+
if (!is_string($value)) {
60+
continue;
61+
}
62+
63+
// is a constant var
64+
if (isset($value[2]) && defined($value)) {
65+
$value = constant($value);
66+
}
67+
68+
// eg: "FOO=BAR"
69+
putenv(strtoupper($name) . "=$value");
70+
}
71+
}
72+
}

src/functions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ function retry($times, callable $callback, $sleep = 0)
6262
if (!function_exists('env')) {
6363
function env(string $name, $default = null)
6464
{
65-
return Inhere\Library\Utils\LocalEnv::instance()->env($name, $default);
65+
$value = getenv(strtoupper($name));
66+
67+
return false !== $value ? $value : $default;
6668
}
6769
}
6870

0 commit comments

Comments
 (0)