|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the m1\env library |
|
5
|
|
|
* |
|
6
|
|
|
* (c) m1 <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @package m1/env |
|
12
|
|
|
* @version 1.1.0 |
|
13
|
|
|
* @author Miles Croxford <[email protected]> |
|
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
|
15
|
|
|
* @license http://github.com/m1/env/blob/master/LICENSE.md |
|
16
|
|
|
* @link http://github.com/m1/env/blob/master/README.md Documentation |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace M1\Env; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Env core class |
|
23
|
|
|
* |
|
24
|
|
|
* @since 0.1.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Env |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The .env contents |
|
30
|
|
|
* |
|
31
|
|
|
* @var array $contents |
|
32
|
|
|
*/ |
|
33
|
|
|
private $contents = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Creates a new instance of Env |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $file The .env to parse |
|
39
|
|
|
* @param bool $origin_exception Whether or not to throw ParseException in the .env |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \InvalidArgumentException If the file does not exist or is not readable |
|
42
|
|
|
*/ |
|
43
|
66 |
|
public function __construct($file, $origin_exception = false) |
|
44
|
|
|
{ |
|
45
|
66 |
|
if (!is_file($file) || !is_readable($file)) { |
|
46
|
3 |
|
throw new \InvalidArgumentException(sprintf('%s is not a file or readable', $file)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
63 |
|
$parser = new Parser($file, $origin_exception); |
|
50
|
|
|
|
|
51
|
63 |
|
$this->contents = $parser->parse(); |
|
52
|
42 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Parses the .env and returns the contents statically |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $file The .env to parse |
|
58
|
|
|
* @param bool $origin_exception Whether or not to throw ParseException in the .env |
|
59
|
|
|
* |
|
60
|
|
|
* @return array The .env contents |
|
61
|
|
|
*/ |
|
62
|
66 |
|
public static function parse($file, $origin_exception = false) |
|
63
|
|
|
{ |
|
64
|
66 |
|
$env = new Env($file, $origin_exception); |
|
65
|
|
|
|
|
66
|
42 |
|
return $env->getContents(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the contents of the .env |
|
71
|
|
|
* |
|
72
|
|
|
* @return array The .env contents |
|
73
|
|
|
*/ |
|
74
|
42 |
|
public function getContents() |
|
75
|
|
|
{ |
|
76
|
42 |
|
return $this->contents; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|