2
2
3
3
namespace DevCoder ;
4
4
5
+ use DevCoder \Processor \AbstractProcessor ;
6
+ use DevCoder \Processor \BooleanProcessor ;
7
+ use DevCoder \Processor \QuotedProcessor ;
8
+
5
9
class DotEnv
6
10
{
7
- /**
8
- * Convert true and false to booleans, instead of:
9
- *
10
- * VARIABLE=false -> ['VARIABLE' => 'false']
11
- *
12
- * it will be
13
- *
14
- * VARIABLE=false -> ['VARIABLE' => false]
15
- *
16
- * default = true
17
- */
18
- const PROCESS_BOOLEANS = 'PROCESS_BOOLEANS ' ;
19
-
20
11
/**
21
12
* The directory where the .env file can be located.
22
13
*
@@ -27,26 +18,42 @@ class DotEnv
27
18
/**
28
19
* Configure the options on which the parsed will act
29
20
*
30
- * @var array
21
+ * @var string[]
31
22
*/
32
- protected $ options = [];
23
+ protected $ processors = [];
33
24
34
- public function __construct (string $ path , array $ options = [] )
25
+ public function __construct (string $ path , array $ processors = null )
35
26
{
36
27
if (!file_exists ($ path )) {
37
28
throw new \InvalidArgumentException (sprintf ('%s does not exist ' , $ path ));
38
29
}
39
30
40
31
$ this ->path = $ path ;
41
32
42
- $ this ->processOptions ( $ options );
33
+ $ this ->setProcessors ( $ processors );
43
34
}
44
35
45
- private function processOptions (array $ options ) : void
36
+ private function setProcessors (array $ processors = null ) : DotEnv
46
37
{
47
- $ this ->options = array_merge ([
48
- static ::PROCESS_BOOLEANS => true
49
- ], $ options );
38
+ /**
39
+ * Fill with default processors
40
+ */
41
+ if ($ processors === null ) {
42
+ $ this ->processors = [
43
+ BooleanProcessor::class,
44
+ QuotedProcessor::class
45
+ ];
46
+
47
+ return $ this ;
48
+ }
49
+
50
+ foreach ($ processors as $ processor ) {
51
+ if (is_subclass_of ($ processor , AbstractProcessor::class)) {
52
+ $ this ->processors [] = $ processor ;
53
+ }
54
+ }
55
+
56
+ return $ this ;
50
57
}
51
58
52
59
/**
@@ -78,19 +85,31 @@ public function load() : void
78
85
}
79
86
}
80
87
81
- private function processValue (string $ value ) {
88
+ /**
89
+ * Process the value with the configured processors
90
+ *
91
+ * @param string $value The value to process
92
+ * @return string|bool
93
+ */
94
+ private function processValue (string $ value )
95
+ {
96
+ /**
97
+ * First trim spaces and quotes if configured
98
+ */
82
99
$ trimmedValue = trim ($ value );
83
100
84
- if (!empty ($ this ->options [static ::PROCESS_BOOLEANS ])) {
85
- $ loweredValue = strtolower ($ trimmedValue );
86
-
87
- $ isBoolean = in_array ($ loweredValue , ['true ' , 'false ' ], true );
101
+ foreach ($ this ->processors as $ processor ) {
102
+ /** @var AbstractProcessor $processorInstance */
103
+ $ processorInstance = new $ processor ($ trimmedValue );
88
104
89
- if ($ isBoolean ) {
90
- return $ loweredValue === ' true ' ;
105
+ if ($ processorInstance -> canBeProcessed () ) {
106
+ return $ processorInstance -> execute () ;
91
107
}
92
108
}
93
109
110
+ /**
111
+ * Does not match any processor options, return as is
112
+ */
94
113
return $ trimmedValue ;
95
114
}
96
115
}
0 commit comments