1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Abacaphiliac\ZendPhpDotEnv; |
4
|
|
|
|
5
|
|
|
use Zend\ModuleManager\ModuleEvent; |
6
|
|
|
use Zend\ModuleManager\ModuleManager; |
7
|
|
|
|
8
|
|
|
class Module |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
private $constant = 'APPLICATION_PATH'; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $variable = 'APPLICATION_PATH'; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $file = '.env'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Module constructor. |
21
|
|
|
* @param string $constant |
22
|
|
|
* @param string $variable |
23
|
|
|
* @param string $file |
24
|
|
|
*/ |
25
|
|
|
public function __construct($constant = null, $variable = null, $file = null) |
26
|
|
|
{ |
27
|
|
|
if ($constant) { |
|
|
|
|
28
|
|
|
$this->constant = $constant; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if ($variable) { |
|
|
|
|
32
|
|
|
$this->variable = $variable; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ($file) { |
|
|
|
|
36
|
|
|
$this->file = $file; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ModuleManager $moduleManager |
42
|
|
|
*/ |
43
|
|
|
public function init(ModuleManager $moduleManager) |
44
|
|
|
{ |
45
|
|
|
$events = $moduleManager->getEventManager(); |
46
|
|
|
|
47
|
|
|
$events->attach(ModuleEvent::EVENT_LOAD_MODULE, array($this, 'loadEnvironmentVariables'), -1); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return mixed[] |
52
|
|
|
* @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidConstantPathException |
53
|
|
|
* @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidEnvironmentVariablePathException |
54
|
|
|
* @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidWorkingDirectoryPathException |
55
|
|
|
*/ |
56
|
|
|
public function loadEnvironmentVariables() |
57
|
|
|
{ |
58
|
|
|
// Load from application-path defined by constant. |
59
|
|
|
if ($this->constant && defined($this->constant)) { |
60
|
|
|
return DotEnvLoader::loadFromConstant($this->constant, $this->file); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Load from application-path defined by environment-variable. |
64
|
|
|
if ($this->variable && DotEnvLoader::hasEnvironmentVariable($this->variable)) { |
65
|
|
|
return DotEnvLoader::loadFromEnvironmentVariable($this->variable, $this->file); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Load from working directory. ZF2 applications change the working directory to the application root. |
69
|
|
|
return DotEnvLoader::loadFromWorkingDirectory($this->file); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: