Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ffbb7f6

Browse files
author
Fady Michel
committed
init project
1 parent 8cd5f70 commit ffbb7f6

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# php-dotenv
1+
# php-dotenv

‎composer.json‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "devcoder-xyz/php-dotenv",
3+
"description": "Parses .env files",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Fad M.R",
9+
"email": "fadymichel@devcoder.xyz"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"DevCoder\\": "src"
15+
}
16+
},
17+
"require": {}
18+
}

‎src/DotEnv.php‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace DevCoder;
4+
5+
class DotEnv
6+
{
7+
/**
8+
* The directory where the .env file can be located.
9+
*
10+
* @var string
11+
*/
12+
protected $path;
13+
14+
15+
public function __construct(string $path)
16+
{
17+
if(!file_exists($path)) {
18+
throw new \InvalidArgumentException(sprintf('%s does not exist', $path));
19+
}
20+
$this->path = $path;
21+
}
22+
23+
public function load() :void
24+
{
25+
if (!is_readable($this->path)) {
26+
throw new \InvalidArgumentException(sprintf('%s file is not readable', $this->path));
27+
}
28+
29+
$lines = file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
30+
foreach ($lines as $line) {
31+
32+
if (strpos(trim($line), '#') === 0) {
33+
continue;
34+
}
35+
36+
list($name, $value) = explode('=', $line, 2);
37+
$name = trim($name);
38+
$value = trim($value);
39+
40+
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
41+
putenv(sprintf('%s=%s', $name, $value));
42+
$_ENV[$name] = $value;
43+
$_SERVER[$name] = $value;
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /