5
5
use Salient \Utility \Exception \InvalidEnvFileSyntaxException ;
6
6
use Salient \Utility \Exception \InvalidEnvironmentException ;
7
7
use Closure ;
8
- use InvalidArgumentException ;
9
8
use RuntimeException ;
10
9
11
10
/**
12
11
* Work with environment variables and .env files
13
12
*
14
- * {@see Env::get()}, {@see Env::getInt()}, etc. check `$_ENV`, `$_SERVER` and
15
- * {@see getenv()} for a given variable and return the first value found. If the
16
- * value is not of the expected type, an {@see InvalidEnvironmentException} is
17
- * thrown. If the variable is not present in the environment, `$default` is
18
- * returned if given, otherwise an {@see InvalidEnvironmentException} is thrown.
19
- *
20
- * @todo Add support for variable expansion
13
+ * Methods that get a value from the environment check `$_ENV`, `$_SERVER` and
14
+ * {@see getenv()}, and return the first value found. If it is not of the
15
+ * expected type, an {@see InvalidEnvironmentException} is thrown. If it is not
16
+ * present in the environment, `$default` is returned if given, otherwise an
17
+ * {@see InvalidEnvironmentException} is thrown.
21
18
*
22
19
* @api
23
20
*/
@@ -97,7 +94,7 @@ public static function apply(int $flags = Env::APPLY_ALL): void
97
94
98
95
if ($ flags & self ::APPLY_TIMEZONE ) {
99
96
$ tz = Regex::replace (
100
- ['/^:?(.*\/zoneinfo\/)?/ ' , '/^(UTC)0$/ ' ],
97
+ ['/^:?(?: .*\/zoneinfo\/)?/ ' , '/^(UTC)0$/ ' ],
101
98
['' , '$1 ' ],
102
99
self ::get ('TZ ' , '' )
103
100
);
@@ -154,7 +151,9 @@ public static function unset(string $name): void
154
151
*/
155
152
public static function has (string $ name ): bool
156
153
{
157
- return self ::_get ($ name , false ) !== false ;
154
+ return array_key_exists ($ name , $ _ENV )
155
+ || array_key_exists ($ name , $ _SERVER )
156
+ || getenv ($ name ) !== false ;
158
157
}
159
158
160
159
/**
@@ -236,13 +235,11 @@ public static function getBool(string $name, $default = -1): ?bool
236
235
* @template T of string[]|null|false
237
236
*
238
237
* @param T|Closure(): T $default
238
+ * @param non-empty-string $delimiter
239
239
* @return (T is string[] ? string[] : (T is null ? string[]|null : string[]|never))
240
240
*/
241
241
public static function getList (string $ name , $ default = false , string $ delimiter = ', ' ): ?array
242
242
{
243
- if ($ delimiter === '' ) {
244
- throw new InvalidArgumentException ('Invalid delimiter ' );
245
- }
246
243
$ value = self ::_get ($ name );
247
244
if ($ value === false ) {
248
245
return self ::_default ($ name , $ default , false );
@@ -256,13 +253,11 @@ public static function getList(string $name, $default = false, string $delimiter
256
253
* @template T of int[]|null|false
257
254
*
258
255
* @param T|Closure(): T $default
256
+ * @param non-empty-string $delimiter
259
257
* @return (T is int[] ? int[] : (T is null ? int[]|null : int[]|never))
260
258
*/
261
259
public static function getIntList (string $ name , $ default = false , string $ delimiter = ', ' ): ?array
262
260
{
263
- if ($ delimiter === '' ) {
264
- throw new InvalidArgumentException ('Invalid delimiter ' );
265
- }
266
261
$ value = self ::_get ($ name );
267
262
if ($ value === false ) {
268
263
return self ::_default ($ name , $ default , false );
@@ -354,9 +349,9 @@ public static function getNullableBool(string $name, $default = -1): ?bool
354
349
}
355
350
356
351
/**
357
- * @return ($assertValueIsString is true ? string|false : mixed)
352
+ * @return string|false
358
353
*/
359
- private static function _get (string $ name, bool $ assertValueIsString = true )
354
+ private static function _get (string $ name )
360
355
{
361
356
if (array_key_exists ($ name , $ _ENV )) {
362
357
$ value = $ _ENV [$ name ];
@@ -366,7 +361,7 @@ private static function _get(string $name, bool $assertValueIsString = true)
366
361
$ value = getenv ($ name , true );
367
362
return $ value === false ? getenv ($ name ) : $ value ;
368
363
}
369
- if ($ assertValueIsString && !is_string ($ value )) {
364
+ if (!is_string ($ value )) {
370
365
throw new InvalidEnvironmentException (sprintf (
371
366
'Value is not a string: %s ' ,
372
367
$ name ,
@@ -473,7 +468,8 @@ public static function setFlag(string $name, bool $value): void
473
468
}
474
469
475
470
/**
476
- * Get the current user's home directory from the environment
471
+ * Get the current user's home directory, or null if a value for the user's
472
+ * home directory is not found in the environment
477
473
*/
478
474
public static function getHomeDir (): ?string
479
475
{
@@ -495,8 +491,6 @@ public static function getHomeDir(): ?string
495
491
* @param string[] $lines
496
492
* @param array<string,string> $queue
497
493
* @param string[] $errors
498
- * @param-out array<string,string> $queue
499
- * @param-out string[] $errors
500
494
*/
501
495
private static function parseLines (
502
496
array $ lines ,
@@ -525,29 +519,19 @@ private static function parseLines(
525
519
526
520
/** @var string */
527
521
$ name = $ matches ['name ' ];
528
- if (
529
- array_key_exists ($ name , $ _ENV )
530
- || array_key_exists ($ name , $ _SERVER )
531
- || getenv ($ name ) !== false
532
- ) {
522
+ if (self ::has ($ name )) {
533
523
continue ;
534
524
}
535
525
536
- $ double = $ matches ['double ' ];
537
- if ($ double !== null ) {
526
+ if (($ double = $ matches ['double ' ]) !== null ) {
538
527
$ queue [$ name ] = Regex::replace ('/ \\\\(["$ \\\\`])/ ' , '$1 ' , $ double );
539
- continue ;
540
- }
541
-
542
- $ single = $ matches ['single ' ];
543
- if ($ single !== null ) {
528
+ } elseif (($ single = $ matches ['single ' ]) !== null ) {
544
529
$ queue [$ name ] = str_replace ("'\'' " , "' " , $ single );
545
- continue ;
530
+ } else {
531
+ /** @var string */
532
+ $ none = $ matches ['none ' ];
533
+ $ queue [$ name ] = Regex::replace ('/ \\\\(.)/ ' , '$1 ' , $ none );
546
534
}
547
-
548
- /** @var string */
549
- $ none = $ matches ['none ' ];
550
- $ queue [$ name ] = Regex::replace ('/ \\\\(.)/ ' , '$1 ' , $ none );
551
535
}
552
536
}
553
537
}
0 commit comments