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