Skip to content

Commit 66d8e7d

Browse files
committed
1.0.0 bump
1 parent 050f472 commit 66d8e7d

File tree

8 files changed

+127
-60
lines changed

8 files changed

+127
-60
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All Notable changes to `Env` will be documented in this file
44

5+
## 1.0.0 - 2015-12-13
6+
### Altered
7+
- Bump to version 1
8+
- Refactored
9+
510
## 0.2.0 - 2015-12-13
611
### Altered
712
- General refactoring and clean up for 0.2.0

src/Env.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*
1111
* @package m1/env
12-
* @version 0.2.0
12+
* @version 1.0.0
1313
* @author Miles Croxford <[email protected]>
1414
* @copyright Copyright (c) Miles Croxford <[email protected]>
1515
* @license http://github.com/m1/env/blob/master/LICENSE.md

src/Exception/ParseException.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*
1111
* @package m1/env
12-
* @version 0.2.0
12+
* @version 1.0.0
1313
* @author Miles Croxford <[email protected]>
1414
* @copyright Copyright (c) Miles Croxford <[email protected]>
1515
* @license http://github.com/m1/env/blob/master/LICENSE.md
@@ -38,8 +38,27 @@ class ParseException extends \ErrorException
3838
*/
3939
public function __construct($message, $origin_exception = false, $file = null, $line = null, $line_num = null)
4040
{
41-
$message = $message;
41+
$message = $this->createMessage($message, $file, $line, $line_num);
4242

43+
if ($origin_exception) {
44+
parent::__construct($message, 0, 1, $file, $line_num);
45+
}
46+
47+
parent::__construct($message);
48+
}
49+
50+
/**
51+
* Constructs a ParseException message
52+
*
53+
* @param string $message The value to parse
54+
* @param string $file The .env file
55+
* @param string $line The line of the value
56+
* @param int $line_num The line num of the value
57+
*
58+
* @return string The exception message
59+
*/
60+
private function createMessage($message, $file, $line, $line_num)
61+
{
4362
if (!is_null($file)) {
4463
$message .= sprintf(" in %s", $file);
4564
}
@@ -52,10 +71,6 @@ public function __construct($message, $origin_exception = false, $file = null, $
5271
$message .= sprintf(" at line %d", $line_num);
5372
}
5473

55-
if ($origin_exception) {
56-
parent::__construct($message, 0, 1, $file, $line_num);
57-
}
58-
59-
parent::__construct($message);
74+
return $message;
6075
}
6176
}

src/Parser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*
1111
* @package m1/env
12-
* @version 0.2.0
12+
* @version 1.0.0
1313
* @author Miles Croxford <[email protected]>
1414
* @copyright Copyright (c) Miles Croxford <[email protected]>
1515
* @license http://github.com/m1/env/blob/master/LICENSE.md
@@ -67,6 +67,10 @@ public function parse()
6767
{
6868
$raw_content = file($this->file, FILE_IGNORE_NEW_LINES);
6969

70+
if (!$raw_content) {
71+
return array();
72+
}
73+
7074
return $this->parseContent($raw_content);
7175
}
7276

@@ -81,10 +85,6 @@ public function parse()
8185
*/
8286
public function parseContent(array $raw_content)
8387
{
84-
if (!$raw_content) {
85-
return array();
86-
}
87-
8888
$lines = array();
8989
$line_num = 0;
9090

src/Parser/AbstractParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*
1111
* @package m1/env
12-
* @version 0.2.0
12+
* @version 1.0.0
1313
* @author Miles Croxford <[email protected]>
1414
* @copyright Copyright (c) Miles Croxford <[email protected]>
1515
* @license http://github.com/m1/env/blob/master/LICENSE.md
@@ -63,4 +63,4 @@ protected function startsWith($string, $line)
6363
{
6464
return $string === "" || strrpos($line, $string, -strlen($line)) !== false;
6565
}
66-
}
66+
}

src/Parser/KeyParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*
1111
* @package m1/env
12-
* @version 0.2.0
12+
* @version 1.0.0
1313
* @author Miles Croxford <[email protected]>
1414
* @copyright Copyright (c) Miles Croxford <[email protected]>
1515
* @license http://github.com/m1/env/blob/master/LICENSE.md
@@ -58,4 +58,4 @@ public function parse($key, $line_num)
5858

5959
return $key;
6060
}
61-
}
61+
}

src/Parser/ValueParser.php

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*
1111
* @package m1/env
12-
* @version 0.2.0
12+
* @version 1.0.0
1313
* @author Miles Croxford <[email protected]>
1414
* @copyright Copyright (c) Miles Croxford <[email protected]>
1515
* @license http://github.com/m1/env/blob/master/LICENSE.md
@@ -157,20 +157,11 @@ private function parseVariables($value, $quoted_string = false)
157157
$matches = $this->fetchVariableMatches($value);
158158

159159
if (is_array($matches)) {
160-
$replacements = array();
161-
162160
if ($this->isVariableClone($value, $matches, $quoted_string)) {
163161
return $this->fetchVariable($value, $matches[1][0], $matches, $quoted_string);
164162
}
165163

166-
for ($i = 0; $i <= (count($matches[0]) - 1); $i++) {
167-
$replacement = $this->fetchVariable($value, $matches[1][$i], $matches, $quoted_string);
168-
$replacements[$matches[0][$i]] = $replacement;
169-
}
170-
171-
if (!empty($replacements)) {
172-
$value = strtr($value, $replacements);
173-
}
164+
$value = $this->doReplacements($value, $matches, $quoted_string);
174165
}
175166

176167
return $value;
@@ -208,46 +199,102 @@ private function fetchVariableMatches($value)
208199
*/
209200
private function fetchVariable($value, $variable_name, $matches, $quoted_string)
210201
{
202+
$this->checkVariableExists($value, $variable_name, $this->lines);
211203

212-
if (!isset($this->lines[$variable_name])) {
204+
$replacement = $this->lines[$variable_name];
205+
206+
if ($this->isBoolInString($replacement, $quoted_string, count($matches[0]))) {
207+
$replacement = ($replacement) ? 'true' : 'false';
208+
}
209+
210+
return $replacement;
211+
}
212+
213+
/**
214+
* Checks to see if a variable exists
215+
*
216+
* @param string $value The value to throw an error with if doesn't exist
217+
* @param string $variable The variable name to get
218+
* @param array $lines The lines already parsed
219+
*
220+
* @throws \M1\Env\Exception\ParseException If the variable can not be found
221+
*/
222+
private function checkVariableExists($value, $variable, $lines)
223+
{
224+
if (!isset($lines[$variable])) {
213225
throw new ParseException(
214-
sprintf('Variable has not been defined: %s', $variable_name),
226+
sprintf('Variable has not been defined: %s', $variable),
215227
$this->origin_exception,
216228
$this->file,
217229
$value,
218230
$this->line_num
219231
);
220232
}
233+
}
221234

222-
$replacement = $this->lines[$variable_name];
235+
/**
236+
* Checks to see if a variable exists
237+
*
238+
* @param string $value The value to throw an error with if doesn't exist
239+
* @param array $matches The matches of the variables
240+
* @param bool $quoted_string Is the value in a quoted string
241+
*
242+
* @return string The parsed value
243+
*/
244+
private function doReplacements($value, $matches, $quoted_string)
245+
{
246+
$replacements = array();
223247

224-
if (is_bool($replacement) &&
225-
($quoted_string || count($matches[0]) >= 2)) {
226-
$replacement = ($replacement) ? 'true' : 'false';
248+
for ($i = 0; $i <= (count($matches[0]) - 1); $i++) {
249+
$replacement = $this->fetchVariable($value, $matches[1][$i], $matches, $quoted_string);
250+
$replacements[$matches[0][$i]] = $replacement;
227251
}
228252

229-
return $replacement;
253+
if (!empty($replacements)) {
254+
$value = strtr($value, $replacements);
255+
}
256+
257+
return $value;
230258
}
231259

232260
/**
233261
* Parses a .env string
234262
*
235263
* @param string $value The value to parse
236264
*
237-
* @throws \M1\Env\Exception\ParseException If the string has a missing end quote
238-
*
239265
* @return string The parsed string
240266
*/
241267
private function parseString($value)
242268
{
269+
$regex = self::REGEX_QUOTE_DOUBLE_STRING;
270+
$symbol = '"';
271+
243272
if ($this->startsWith('\'', $value)) {
244273
$regex = self::REGEX_QUOTE_SINGLE_STRING;
245274
$symbol = "'";
246-
} else {
247-
$regex = self::REGEX_QUOTE_DOUBLE_STRING;
248-
$symbol = '"';
249275
}
250276

277+
$matches = $this->fetchStringMatches($value, $regex, $symbol);
278+
279+
$value = trim($matches[0], $symbol);
280+
$value = strtr($value, self::$character_map);
281+
282+
return $this->parseVariables($value, true);
283+
}
284+
285+
/**
286+
* Gets the regex matches in the string
287+
*
288+
* @param string $regex The regex to use
289+
* @param string $value The value to parse
290+
* @param string $symbol The symbol we're parsing for
291+
*
292+
* @throws \M1\Env\Exception\ParseException If the string has a missing end quote
293+
*
294+
* @return array The matches based on the regex and the value
295+
*/
296+
private function fetchStringMatches($value, $regex, $symbol)
297+
{
251298
if (!preg_match('/'.$regex.'/', $value, $matches)) {
252299
throw new ParseException(
253300
sprintf('Missing end %s quote', $symbol),
@@ -258,12 +305,8 @@ private function parseString($value)
258305
);
259306
}
260307

261-
$value = trim($matches[0], $symbol);
262-
$value = strtr($value, self::$character_map);
263-
264-
return $this->parseVariables($value, true);
308+
return $matches;
265309
}
266-
267310
/**
268311
* Parses a .env null value
269312
*
@@ -301,19 +344,9 @@ private function parseUnquotedString($value)
301344
*/
302345
private function parseBool($value)
303346
{
304-
switch (strtolower($value)) {
305-
case 'true':
306-
case 'yes':
307-
$value = true;
308-
break;
309-
case 'false':
310-
case 'no':
311-
default:
312-
$value = false;
313-
break;
314-
}
347+
$value = strtolower($value);
315348

316-
return $value;
349+
return $value === "true" || $value === "yes";
317350
}
318351

319352
/**
@@ -343,4 +376,4 @@ private function stripComments($value)
343376
{
344377
return trim(explode("#", $value, 2)[0]);
345378
}
346-
}
379+
}

src/Traits/ValueTypeCheckable.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*
1111
* @package m1/env
12-
* @version 0.2.0
12+
* @version 1.0.0
1313
* @author Miles Croxford <[email protected]>
1414
* @copyright Copyright (c) Miles Croxford <[email protected]>
1515
* @license http://github.com/m1/env/blob/master/LICENSE.md
@@ -50,6 +50,20 @@ private function isBool($value)
5050
return in_array(strtolower($value), self::$bool_variants);
5151
}
5252

53+
/**
54+
* Returns if the bool is in a string
55+
*
56+
* @param string $value The value to test
57+
* @param bool $quoted_string Is the context a quoted string
58+
* @param int $word_count The amount of words in the sentence
59+
*
60+
* @return bool Is a value a bool in a string
61+
*/
62+
private function isBoolInString($value, $quoted_string, $word_count)
63+
{
64+
return (is_bool($value)) && ($quoted_string || $word_count >= 2);
65+
}
66+
5367
/**
5468
* Returns if value is number
5569
*
@@ -87,4 +101,4 @@ private function isVariableClone($value, $matches, $quoted_string)
87101
{
88102
return count($matches[0] === 1) && $value == $matches[0][0] && !$quoted_string;
89103
}
90-
}
104+
}

0 commit comments

Comments
 (0)