|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/craft-integration/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\integration\services; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\helpers\ArrayHelper; |
|
13
|
|
|
use craft\helpers\Component as ComponentHelper; |
|
14
|
|
|
use flipbox\craft\integration\exceptions\ConnectionNotFound; |
|
15
|
|
|
use Flipbox\Skeleton\Exceptions\InvalidConfigurationException; |
|
16
|
|
|
use Flipbox\Skeleton\Helpers\ObjectHelper; |
|
17
|
|
|
use yii\base\Component; |
|
18
|
|
|
use yii\db\Query; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Flipbox Factory <[email protected]> |
|
22
|
|
|
* @since 2.2.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class IntegrationConnections extends Component |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The app connection handle |
|
28
|
|
|
*/ |
|
29
|
|
|
const CONNECTION = 'app'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The default connection identifier |
|
33
|
|
|
*/ |
|
34
|
|
|
const DEFAULT_CONNECTION = 'DEFAULT'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The override file |
|
38
|
|
|
*/ |
|
39
|
|
|
public $overrideFile; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array|null |
|
43
|
|
|
*/ |
|
44
|
|
|
private $overrides; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private $connections = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
private $enabled = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
abstract protected static function tableName(): string; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
abstract protected static function connectionInstance(): string; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Load override cache configurations |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function loadOverrides() |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->overrides === null) { |
|
72
|
|
|
$this->overrides = []; |
|
73
|
|
|
|
|
74
|
|
|
if ($this->overrideFile !== null) { |
|
75
|
|
|
$this->overrides = Craft::$app->getConfig()->getConfigFromFile($this->overrideFile); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns any configurations from the config file. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $handle |
|
84
|
|
|
* @return array|null |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getOverrides(string $handle) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->loadOverrides(); |
|
89
|
|
|
return $this->overrides[$handle] ?? null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function getDefaultConnection(): string |
|
96
|
|
|
{ |
|
97
|
|
|
return static::CONNECTION; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $handle |
|
102
|
|
|
* @throws ConnectionNotFound |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function handleCacheNotFound(string $handle) |
|
105
|
|
|
{ |
|
106
|
|
|
throw new ConnectionNotFound( |
|
107
|
|
|
sprintf( |
|
108
|
|
|
"Unable to find connection '%s'.", |
|
109
|
|
|
$handle |
|
110
|
|
|
) |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $handle |
|
116
|
|
|
* @param bool $enabledOnly |
|
117
|
|
|
* @return mixed|null |
|
118
|
|
|
*/ |
|
119
|
|
|
public function find( |
|
120
|
|
|
string $handle = self::DEFAULT_CONNECTION, |
|
121
|
|
|
bool $enabledOnly = true |
|
122
|
|
|
) { |
|
123
|
|
|
if ($handle === self::DEFAULT_CONNECTION) { |
|
124
|
|
|
$handle = $this->getDefaultConnection(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (!array_key_exists($handle, $this->connections)) { |
|
128
|
|
|
$connection = null; |
|
129
|
|
|
$enabled = false; |
|
130
|
|
|
|
|
131
|
|
|
if ($config = (new Query()) |
|
132
|
|
|
->select([ |
|
133
|
|
|
'id', |
|
134
|
|
|
'handle', |
|
135
|
|
|
'class', |
|
136
|
|
|
'settings', |
|
137
|
|
|
'enabled' |
|
138
|
|
|
]) |
|
139
|
|
|
->from(static::tableName()) |
|
140
|
|
|
->andWhere([ |
|
141
|
|
|
'handle' => $handle |
|
142
|
|
|
]) |
|
143
|
|
|
->one() |
|
144
|
|
|
) { |
|
145
|
|
|
$enabled = (bool)ArrayHelper::remove($config, 'enabled', false); |
|
146
|
|
|
$connection = $this->create($config); |
|
147
|
|
|
} |
|
148
|
|
|
$this->enabled[$handle] = $enabled; |
|
149
|
|
|
$this->connections[$handle] = $connection; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// Disabled? |
|
153
|
|
|
if ($enabledOnly === true && ($this->enabled[$handle] ?? false) === false) { |
|
154
|
|
|
return null; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $this->connections[$handle]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $handle |
|
162
|
|
|
* @param bool $enabledOnly |
|
163
|
|
|
* @return mixed |
|
164
|
|
|
* @throws ConnectionNotFound |
|
165
|
|
|
*/ |
|
166
|
|
|
public function get( |
|
167
|
|
|
string $handle = self::DEFAULT_CONNECTION, |
|
168
|
|
|
bool $enabledOnly = true |
|
169
|
|
|
) { |
|
170
|
|
|
if (null === ($connection = $this->find($handle, $enabledOnly))) { |
|
171
|
|
|
return $this->handleCacheNotFound($handle); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $connection; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param $config |
|
179
|
|
|
* @return mixed|null |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function create(array $config) |
|
182
|
|
|
{ |
|
183
|
|
|
// Merge settings |
|
184
|
|
|
$config = ComponentHelper::mergeSettings($config); |
|
185
|
|
|
|
|
186
|
|
|
// Apply overrides |
|
187
|
|
|
if (null !== ($handle = ArrayHelper::remove($config, 'handle')) && |
|
188
|
|
|
null !== ($override = $this->getOverrides($handle)) |
|
189
|
|
|
) { |
|
190
|
|
|
$config = array_merge($config, $override); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
try { |
|
194
|
|
|
$connection = ObjectHelper::create( |
|
195
|
|
|
$config, |
|
196
|
|
|
static::connectionInstance() |
|
197
|
|
|
); |
|
198
|
|
|
} catch (InvalidConfigurationException $e) { |
|
199
|
|
|
return null; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $connection; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return array |
|
207
|
|
|
*/ |
|
208
|
|
|
public function all(): array |
|
209
|
|
|
{ |
|
210
|
|
|
$configs = (new Query()) |
|
211
|
|
|
->select([ |
|
212
|
|
|
'handle', |
|
213
|
|
|
'class', |
|
214
|
|
|
'settings', |
|
215
|
|
|
'enabled' |
|
216
|
|
|
]) |
|
217
|
|
|
->from(static::tableName()) |
|
218
|
|
|
->all(); |
|
219
|
|
|
|
|
220
|
|
|
foreach ($configs as $config) { |
|
221
|
|
|
$handle = ArrayHelper::getValue($config, 'handle'); |
|
222
|
|
|
|
|
223
|
|
|
if (!array_key_exists($handle, $this->connections)) { |
|
224
|
|
|
$this->enabled[$handle] = (bool)ArrayHelper::remove($config, 'enabled', false);; |
|
225
|
|
|
$this->connections[$handle] = $this->create($config); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $this->connections; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|