1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rfussien\Dotenv; |
4
|
|
|
|
5
|
|
|
class Parser |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* parsed content of the dotenv file |
9
|
|
|
*/ |
10
|
|
|
private $content = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Which parsing style is used |
14
|
|
|
* |
15
|
|
|
* @see http://php.net/parse_ini_file |
16
|
|
|
*/ |
17
|
|
|
private $scannerMode; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set the default parsing style |
21
|
|
|
*/ |
22
|
36 |
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* INI_SCANNER_TYPED won't work on hhvm |
26
|
|
|
* @see https://github.com/facebook/hhvm/issues/5239 |
27
|
|
|
*/ |
28
|
36 |
|
if (defined('HHVM_VERSION')) { |
29
|
|
|
$this->setScannerMode(\INI_SCANNER_NORMAL); |
30
|
|
|
} else { |
31
|
36 |
|
$this->setScannerMode(\INI_SCANNER_TYPED); |
32
|
|
|
} |
33
|
36 |
|
} |
34
|
|
|
|
35
|
36 |
|
public function setScannerMode($scannerMode) |
36
|
|
|
{ |
37
|
36 |
|
$this->scannerMode = $scannerMode; |
38
|
36 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* parse the .env file |
42
|
|
|
* |
43
|
|
|
* @param string $file file to parse |
44
|
|
|
* |
45
|
|
|
* @return string Returns the phrase passed in |
46
|
|
|
*/ |
47
|
36 |
|
public function parse($file) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
36 |
|
$this->content = parse_ini_file( |
51
|
24 |
|
$file, |
52
|
36 |
|
false, |
53
|
36 |
|
$this->scannerMode |
54
|
24 |
|
); |
55
|
26 |
|
} catch (\Exception $e) { |
56
|
6 |
|
throw new Exception\ParseException($e); |
57
|
|
|
} |
58
|
|
|
|
59
|
30 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function sanitizeValues() |
63
|
|
|
{ |
64
|
5 |
|
array_walk($this->content, function (&$value, $key) { |
|
|
|
|
65
|
|
|
// sanitize boolean values |
66
|
5 |
|
if (in_array($value, ['true', 'on', 'yes', 'false', 'off', 'no'])) { |
67
|
3 |
|
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
68
|
2 |
|
} |
69
|
3 |
|
}); |
70
|
|
|
|
71
|
3 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
30 |
|
public function getContent() |
75
|
|
|
{ |
76
|
30 |
|
return $this->content; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.