Skip to content

Commit 56ffd7b

Browse files
Merge pull request #306 from GrahamCampbell/refactoring
Broken up the loader, and made load() return the actual environment variables
2 parents 58ac8e7 + 34e7ad7 commit 56ffd7b

File tree

7 files changed

+358
-280
lines changed

7 files changed

+358
-280
lines changed

src/Dotenv.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* This is the dotenv class.
1111
*
1212
* It's responsible for loading a `.env` file in the given directory and
13-
* setting the environment vars.
13+
* setting the environment variables.
1414
*/
1515
class Dotenv
1616
{
@@ -53,7 +53,6 @@ public static function create($path, $file = null, FactoryInterface $envFactory
5353
return new self($loader);
5454
}
5555

56-
5756
/**
5857
* Returns the full path to the file.
5958
*
@@ -76,17 +75,19 @@ private static function getFilePath($path, $file)
7675
*
7776
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException
7877
*
79-
* @return array
78+
* @return string[]
8079
*/
8180
public function load()
8281
{
8382
return $this->loadData();
8483
}
8584

8685
/**
87-
* Load environment file in given directory, suppress InvalidPathException.
86+
* Load environment file in given directory, silently failing if it doesn't exist.
87+
*
88+
* @throws \Dotenv\Exception\InvalidFileException
8889
*
89-
* @return array
90+
* @return string[]
9091
*/
9192
public function safeLoad()
9293
{
@@ -103,7 +104,7 @@ public function safeLoad()
103104
*
104105
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException
105106
*
106-
* @return array
107+
* @return string[]
107108
*/
108109
public function overload()
109110
{
@@ -117,7 +118,7 @@ public function overload()
117118
*
118119
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException
119120
*
120-
* @return array
121+
* @return string[]
121122
*/
122123
protected function loadData($overload = false)
123124
{

src/Lines.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace Dotenv;
4+
5+
class Lines
6+
{
7+
/**
8+
* Process the array of lines of environment variables.
9+
*
10+
* This will produce an array of entries, one per variable.
11+
*
12+
* @param string[] $lines
13+
*
14+
* @return string[]
15+
*/
16+
public static function process(array $lines)
17+
{
18+
$output = [];
19+
$multiline = false;
20+
$multilineBuffer = [];
21+
22+
foreach ($lines as $line) {
23+
list($multiline, $line, $multilineBuffer) = self::multilineProcess($multiline, $line, $multilineBuffer);
24+
25+
if (!$multiline && !self::isComment($line) && self::looksLikeSetter($line)) {
26+
$output[] = $line;
27+
}
28+
}
29+
30+
return $output;
31+
}
32+
33+
/**
34+
* Used to make all multiline variable process.
35+
*
36+
* @param bool $multiline
37+
* @param string $line
38+
* @param string[] $buffer
39+
*
40+
* @return string[]
41+
*/
42+
private static function multilineProcess($multiline, $line, array $buffer)
43+
{
44+
// check if $line can be multiline variable
45+
if (self::looksLikeMultilineStart($line)) {
46+
$multiline = true;
47+
}
48+
49+
if ($multiline) {
50+
array_push($buffer, $line);
51+
52+
if (self::looksLikeMultilineStop($line)) {
53+
$multiline = false;
54+
$line = implode("\n", $buffer);
55+
$buffer = [];
56+
}
57+
}
58+
59+
return [$multiline, $line, $buffer];
60+
}
61+
62+
/**
63+
* Determine if the given line can be the start of a multiline variable.
64+
*
65+
* @param string $line
66+
*
67+
* @return bool
68+
*/
69+
private static function looksLikeMultilineStart($line)
70+
{
71+
return strpos($line, '="') !== false && substr_count($line, '"') === 1;
72+
}
73+
74+
/**
75+
* Determine if the given line can be the start of a multiline variable.
76+
*
77+
* @param string $line
78+
*
79+
* @return bool
80+
*/
81+
private static function looksLikeMultilineStop($line)
82+
{
83+
return strpos($line, '"') !== false && substr_count($line, '="') === 0;
84+
}
85+
86+
/**
87+
* Determine if the line in the file is a comment, e.g. begins with a #.
88+
*
89+
* @param string $line
90+
*
91+
* @return bool
92+
*/
93+
private static function isComment($line)
94+
{
95+
$line = ltrim($line);
96+
97+
return isset($line[0]) && $line[0] === '#';
98+
}
99+
100+
/**
101+
* Determine if the given line looks like it's setting a variable.
102+
*
103+
* @param string $line
104+
*
105+
* @return bool
106+
*/
107+
private static function looksLikeSetter($line)
108+
{
109+
return strpos($line, '=') !== false;
110+
}
111+
}

0 commit comments

Comments
 (0)