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
|
54 |
|
*/ |
17
|
|
|
private $scannerMode = \INI_SCANNER_TYPED; |
18
|
|
|
|
19
|
54 |
|
/** |
20
|
36 |
|
* Set the default parsing style |
21
|
54 |
|
*/ |
22
|
18 |
|
public function __construct() |
23
|
36 |
|
{ |
24
|
38 |
|
/** |
25
|
6 |
|
* INI_SCANNER_TYPED won't work on hhvm |
26
|
|
|
* @see https://github.com/facebook/hhvm/issues/5239 |
27
|
|
|
*/ |
28
|
48 |
|
if (defined('HHVM_VERSION')) { |
29
|
|
|
$this->setScannerMode(\INI_SCANNER_NORMAL); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setScannerMode($scannerMode) |
34
|
|
|
{ |
35
|
|
|
$this->scannerMode = $scannerMode; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* parse the .env file |
40
|
|
|
* |
41
|
|
|
* @param string $file file to parse |
42
|
|
|
* |
43
|
|
|
* @return string Returns the phrase passed in |
44
|
|
|
*/ |
45
|
|
|
public function parse($file) |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
$this->content = parse_ini_file( |
49
|
|
|
$file, |
50
|
|
|
false, |
51
|
|
|
$this->scannerMode |
52
|
|
|
); |
53
|
|
|
} catch (\Exception $e) { |
54
|
|
|
throw new Exception\ParseException($e); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function sanitizeKeys() |
61
|
|
|
{ |
62
|
|
|
$sanitisedContent = []; |
63
|
|
|
|
64
|
|
|
foreach ($this->content as $key => $value) { |
65
|
|
|
$key = preg_replace('/ *export */', '', trim($key)); |
66
|
|
|
$sanitisedContent[$key] = $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->content = $sanitisedContent; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function sanitizeValues() |
75
|
|
|
{ |
76
|
|
|
array_walk($this->content, function(&$value, $key) { |
|
|
|
|
77
|
|
|
// sanitize boolean values |
78
|
|
|
if (in_array($value, ['true', 'on', 'yes', 'false', 'off', 'no'])) { |
79
|
|
|
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
80
|
|
|
} |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getContent() |
87
|
|
|
{ |
88
|
|
|
return $this->content; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.