1 | <?php |
||
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() |
||
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) |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | protected function getDefaultConnection(): string |
||
99 | |||
100 | /** |
||
101 | * @param string $handle |
||
102 | * @throws ConnectionNotFound |
||
103 | */ |
||
104 | protected function handleConnectionNotFound(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 | 'handle', |
||
134 | 'class', |
||
135 | 'settings', |
||
136 | 'enabled' |
||
137 | ]) |
||
138 | ->from(static::tableName()) |
||
139 | ->andWhere([ |
||
140 | is_numeric($handle) ? 'id' : 'handle' => $handle |
||
141 | ]) |
||
142 | ->one() |
||
143 | ) { |
||
144 | $enabled = (bool)ArrayHelper::remove($config, 'enabled', false); |
||
145 | $connection = $this->create($config); |
||
146 | } |
||
147 | $this->enabled[$handle] = $enabled; |
||
148 | $this->connections[$handle] = $connection; |
||
149 | } |
||
150 | |||
151 | // Disabled? |
||
152 | if ($enabledOnly === true && ($this->enabled[$handle] ?? false) === false) { |
||
153 | return null; |
||
154 | } |
||
155 | |||
156 | return $this->connections[$handle]; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param string $handle |
||
161 | * @param bool $enabledOnly |
||
162 | * @return mixed |
||
163 | * @throws ConnectionNotFound |
||
164 | */ |
||
165 | public function get( |
||
175 | |||
176 | /** |
||
177 | * @param $config |
||
178 | * @return mixed|null |
||
179 | */ |
||
180 | protected function create(array $config) |
||
203 | |||
204 | /** |
||
205 | * @return array |
||
206 | */ |
||
207 | public function all(): array |
||
230 | } |
||
231 |