1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Config; |
4
|
|
|
|
5
|
|
|
use Dotenv\Dotenv; |
6
|
|
|
use Sinergi\Config\Path\PathCollection; |
7
|
|
|
|
8
|
|
|
class Configuration |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
public static $env; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var PathCollection |
17
|
|
|
*/ |
18
|
|
|
protected $paths; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Dotenv |
22
|
|
|
*/ |
23
|
|
|
protected $dotenv; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $environment; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array|Configuration $config |
32
|
|
|
*/ |
33
|
|
|
public function __construct($config) |
34
|
|
|
{ |
35
|
|
|
$this->paths = new PathCollection(); |
36
|
|
|
if (is_array($config)) { |
37
|
|
|
if (isset($config['path'])) { |
38
|
|
|
$this->paths->add($config['path']); |
39
|
|
|
} |
40
|
|
|
if (isset($config['paths'])) { |
41
|
|
|
foreach ($config['paths'] as $path) { |
42
|
|
|
$this->paths->add($path); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
if (isset($config['environment'])) { |
46
|
|
|
$this->setEnvironment($config['environment']); |
47
|
|
|
} |
48
|
|
|
if (isset($config['dotenv'])) { |
49
|
|
|
$this->setDotenv(new Dotenv($config['dotenv'])); |
50
|
|
|
} |
51
|
|
|
} elseif ($config instanceof Configuration) { |
52
|
|
|
$this->setPaths($config->getPaths()); |
53
|
|
|
$this->setEnvironment($config->getEnvironment()); |
54
|
|
|
if ($config->getDotenv()) { |
55
|
|
|
$this->setDotenv($config->getDotenv()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return PathCollection |
62
|
|
|
*/ |
63
|
|
|
public function getPaths() |
64
|
|
|
{ |
65
|
|
|
return $this->paths; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param PathCollection $pathCollection |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function setPaths(PathCollection $pathCollection) |
73
|
|
|
{ |
74
|
|
|
$this->paths = $pathCollection; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param null|string $environment |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setEnvironment($environment) |
83
|
|
|
{ |
84
|
|
|
if ($this->environment !== $environment) { |
85
|
|
|
if (method_exists($this, 'reset')) { |
86
|
|
|
$this->reset(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
$this->environment = $environment; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function removeEnvironment() |
97
|
|
|
{ |
98
|
|
|
if ($this->environment !== null) { |
99
|
|
|
if (method_exists($this, 'reset')) { |
100
|
|
|
$this->reset(); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
$this->environment = null; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return null|string |
109
|
|
|
*/ |
110
|
|
|
public function getEnvironment() |
111
|
|
|
{ |
112
|
|
|
return $this->environment; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Dotenv |
117
|
|
|
*/ |
118
|
|
|
public function getDotenv() |
119
|
|
|
{ |
120
|
|
|
return $this->dotenv; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Dotenv $dotenv |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
public function setDotenv(Dotenv $dotenv) |
128
|
|
|
{ |
129
|
|
|
$this->dotenv = $dotenv; |
130
|
|
|
$this->loadDotenv(); |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function loadDotenv() |
135
|
|
|
{ |
136
|
|
|
$retval = []; |
137
|
|
|
$values = $this->dotenv->load(); |
138
|
|
|
foreach ($values as $value) { |
139
|
|
|
$parts = explode("=", $value, 2); |
140
|
|
|
$retval[$parts[0]] = getenv($parts[0]); |
141
|
|
|
} |
142
|
|
|
return self::$env = $retval; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: