|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tworzenieweb\SqlProvisioner\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Dotenv\Dotenv; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ProvisionConfig |
|
9
|
|
|
* |
|
10
|
|
|
* @package Tworzenieweb\SqlProvisioner\Config |
|
11
|
|
|
*/ |
|
12
|
|
|
class ProvisionConfig |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $envPath; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
private $force = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
private $skipProvisioned = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
private $skipSyntaxCheck = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var EmailConfig |
|
36
|
|
|
*/ |
|
37
|
|
|
private $emailConfig; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* ProvisionConfig constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $envPath |
|
43
|
|
|
* @param EmailConfig $emailConfig |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(string $envPath, EmailConfig $emailConfig) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->envPath = $envPath; |
|
48
|
|
|
$this->emailConfig = $emailConfig; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $envPath |
|
53
|
|
|
* |
|
54
|
|
|
* @return ProvisionConfig |
|
55
|
|
|
*/ |
|
56
|
|
|
public function withEnvPath(string $envPath): ProvisionConfig |
|
57
|
|
|
{ |
|
58
|
|
|
$this->envPath = $envPath; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param bool $force |
|
65
|
|
|
* |
|
66
|
|
|
* @return ProvisionConfig |
|
67
|
|
|
*/ |
|
68
|
|
|
public function force(bool $force = true): ProvisionConfig |
|
69
|
|
|
{ |
|
70
|
|
|
$this->force = $force; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param bool $skipProvisioned |
|
77
|
|
|
* |
|
78
|
|
|
* @return ProvisionConfig |
|
79
|
|
|
*/ |
|
80
|
|
|
public function skipProvisioned(bool $skipProvisioned = true): ProvisionConfig |
|
81
|
|
|
{ |
|
82
|
|
|
$this->skipProvisioned = $skipProvisioned; |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param bool $skipSyntaxCheck |
|
89
|
|
|
* |
|
90
|
|
|
* @return ProvisionConfig |
|
91
|
|
|
*/ |
|
92
|
|
|
public function skipSyntaxCheck(bool $skipSyntaxCheck = true): ProvisionConfig |
|
93
|
|
|
{ |
|
94
|
|
|
$this->skipSyntaxCheck = $skipSyntaxCheck; |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return ProvisionConfig |
|
101
|
|
|
*/ |
|
102
|
|
|
public function sendEmail(): ProvisionConfig |
|
103
|
|
|
{ |
|
104
|
|
|
$this->emailConfig->enable(); |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return ProvisionConfig |
|
111
|
|
|
*/ |
|
112
|
|
|
public function skipEmail(): ProvisionConfig |
|
113
|
|
|
{ |
|
114
|
|
|
$this->emailConfig->disable(); |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
public function load() |
|
123
|
|
|
{ |
|
124
|
|
|
$loader = $this->composeDotenv(); |
|
125
|
|
|
$loader->load(); |
|
126
|
|
|
|
|
127
|
|
|
if ($this->emailConfig->isEnabled()) { |
|
128
|
|
|
$loader->required(EmailConfig::MANDATORY_ENV_VARIABLES)->notEmpty(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getEnvPath(): string |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->envPath; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function isForce(): bool |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->force; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return bool |
|
150
|
|
|
*/ |
|
151
|
|
|
public function isSkipProvisioned(): bool |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->skipProvisioned; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
|
public function isSkipSyntaxCheck(): bool |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->skipSyntaxCheck; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public function isSendEmail(): bool |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->sendEmail; |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return Dotenv |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function composeDotenv(): Dotenv |
|
176
|
|
|
{ |
|
177
|
|
|
if (!is_readable($this->envPath)) { |
|
178
|
|
|
throw new \RuntimeException( |
|
179
|
|
|
sprintf('Env file path [%s] is not readable', $this->envPath) |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if (is_dir($this->envPath)) { |
|
184
|
|
|
return new Dotenv($this->envPath); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if (is_file($this->envPath)) { |
|
188
|
|
|
return new Dotenv(dirname($this->envPath), basename($this->envPath)); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
throw new \RuntimeException( |
|
192
|
|
|
sprintf('Could not define whether provided env path [%s] is a directory or file', $this->envPath) |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: