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