@@ -46,6 +46,13 @@ class Client {
46
46
*/
47
47
public ?ProtocolInterface $ connection = null ;
48
48
49
+ /**
50
+ * Client configuration
51
+ *
52
+ * @var Config
53
+ */
54
+ protected Config $ config ;
55
+
49
56
/**
50
57
* Server hostname.
51
58
*
@@ -174,14 +181,14 @@ class Client {
174
181
175
182
/**
176
183
* Client constructor.
177
- * @param array $config
184
+ * @param Config $config
178
185
*
179
186
* @throws MaskNotFoundException
180
187
*/
181
- public function __construct (array $ config = [] ) {
188
+ public function __construct (Config $ config ) {
182
189
$ this ->setConfig ($ config );
183
- $ this ->setMaskFromConfig ($ config );
184
- $ this ->setEventsFromConfig ($ config );
190
+ $ this ->setMaskFromConfig ();
191
+ $ this ->setEventsFromConfig ();
185
192
}
186
193
187
194
/**
@@ -199,16 +206,17 @@ public function __destruct() {
199
206
* Clone the current Client instance
200
207
*
201
208
* @return Client
209
+ * @throws MaskNotFoundException
202
210
*/
203
211
public function clone (): Client {
204
- $ client = new self ();
212
+ $ client = new self ($ this -> config );
205
213
$ client ->events = $ this ->events ;
206
214
$ client ->timeout = $ this ->timeout ;
207
215
$ client ->active_folder = $ this ->active_folder ;
208
216
$ client ->default_account_config = $ this ->default_account_config ;
209
217
$ config = $ this ->getAccountConfig ();
210
218
foreach ($ config as $ key => $ value ) {
211
- $ client ->setAccountConfig ($ key , $ config , $ this ->default_account_config );
219
+ $ client ->setAccountConfig ($ key , $ this ->default_account_config );
212
220
}
213
221
$ client ->default_message_mask = $ this ->default_message_mask ;
214
222
$ client ->default_attachment_mask = $ this ->default_message_mask ;
@@ -217,16 +225,17 @@ public function clone(): Client {
217
225
218
226
/**
219
227
* Set the Client configuration
220
- * @param array $config
228
+ * @param Config $config
221
229
*
222
230
* @return self
223
231
*/
224
- public function setConfig (array $ config ): Client {
225
- $ default_account = ClientManager::get ('default ' );
226
- $ default_config = ClientManager::get ("accounts. $ default_account " );
232
+ public function setConfig (Config $ config ): Client {
233
+ $ this ->config = $ config ;
234
+ $ default_account = $ this ->config ->get ('default ' );
235
+ $ default_config = $ this ->config ->get ("accounts. $ default_account " );
227
236
228
237
foreach ($ this ->default_account_config as $ key => $ value ) {
229
- $ this ->setAccountConfig ($ key , $ config , $ default_config );
238
+ $ this ->setAccountConfig ($ key , $ default_config );
230
239
}
231
240
232
241
return $ this ;
@@ -235,27 +244,20 @@ public function setConfig(array $config): Client {
235
244
/**
236
245
* Get the current config
237
246
*
238
- * @return array
247
+ * @return Config
239
248
*/
240
- public function getConfig (): array {
241
- $ config = [];
242
- foreach ($ this ->default_account_config as $ key => $ value ) {
243
- $ config [$ key ] = $ this ->$ key ;
244
- }
245
- return $ config ;
249
+ public function getConfig (): Config {
250
+ return $ this ->config ;
246
251
}
247
252
248
253
/**
249
254
* Set a specific account config
250
255
* @param string $key
251
- * @param array $config
252
256
* @param array $default_config
253
257
*/
254
- private function setAccountConfig (string $ key , array $ config , array $ default_config ): void {
258
+ private function setAccountConfig (string $ key , array $ default_config ): void {
255
259
$ value = $ this ->default_account_config [$ key ];
256
- if (isset ($ config [$ key ])) {
257
- $ value = $ config [$ key ];
258
- }elseif (isset ($ default_config [$ key ])) {
260
+ if (isset ($ default_config [$ key ])) {
259
261
$ value = $ default_config [$ key ];
260
262
}
261
263
$ this ->$ key = $ value ;
@@ -278,10 +280,9 @@ public function getAccountConfig(): array {
278
280
279
281
/**
280
282
* Look for a possible events in any available config
281
- * @param $config
282
283
*/
283
- protected function setEventsFromConfig ($ config ): void {
284
- $ this ->events = ClientManager:: get ("events " );
284
+ protected function setEventsFromConfig (): void {
285
+ $ this ->events = $ this -> config -> get ("events " );
285
286
if (isset ($ config ['events ' ])){
286
287
foreach ($ config ['events ' ] as $ section => $ events ) {
287
288
$ this ->events [$ section ] = array_merge ($ this ->events [$ section ], $ events );
@@ -291,50 +292,50 @@ protected function setEventsFromConfig($config): void {
291
292
292
293
/**
293
294
* Look for a possible mask in any available config
294
- * @param $config
295
295
*
296
296
* @throws MaskNotFoundException
297
297
*/
298
- protected function setMaskFromConfig ($ config ): void {
298
+ protected function setMaskFromConfig (): void {
299
+ $ masks = $ this ->config ->get ("masks " );
299
300
300
- if (isset ($ config [ ' masks ' ] )){
301
- if (isset ($ config [ ' masks ' ] ['message ' ])) {
302
- if (class_exists ($ config [ ' masks ' ] ['message ' ])) {
303
- $ this ->default_message_mask = $ config [ ' masks ' ] ['message ' ];
301
+ if (isset ($ masks )){
302
+ if (isset ($ masks ['message ' ])) {
303
+ if (class_exists ($ masks ['message ' ])) {
304
+ $ this ->default_message_mask = $ masks ['message ' ];
304
305
}else {
305
- throw new MaskNotFoundException ("Unknown mask provided: " .$ config [ ' masks ' ] ['message ' ]);
306
+ throw new MaskNotFoundException ("Unknown mask provided: " .$ masks ['message ' ]);
306
307
}
307
308
}else {
308
- $ default_mask = ClientManager:: getMask ("message " );
309
+ $ default_mask = $ this -> config -> getMask ("message " );
309
310
if ($ default_mask != "" ){
310
311
$ this ->default_message_mask = $ default_mask ;
311
312
}else {
312
313
throw new MaskNotFoundException ("Unknown message mask provided " );
313
314
}
314
315
}
315
- if (isset ($ config [ ' masks ' ] ['attachment ' ])) {
316
- if (class_exists ($ config [ ' masks ' ] ['attachment ' ])) {
317
- $ this ->default_attachment_mask = $ config [ ' masks ' ] ['attachment ' ];
316
+ if (isset ($ masks ['attachment ' ])) {
317
+ if (class_exists ($ masks ['attachment ' ])) {
318
+ $ this ->default_attachment_mask = $ masks ['attachment ' ];
318
319
}else {
319
- throw new MaskNotFoundException ("Unknown mask provided: " . $ config [ ' masks ' ] ['attachment ' ]);
320
+ throw new MaskNotFoundException ("Unknown mask provided: " . $ masks ['attachment ' ]);
320
321
}
321
322
}else {
322
- $ default_mask = ClientManager:: getMask ("attachment " );
323
+ $ default_mask = $ this -> config -> getMask ("attachment " );
323
324
if ($ default_mask != "" ){
324
325
$ this ->default_attachment_mask = $ default_mask ;
325
326
}else {
326
327
throw new MaskNotFoundException ("Unknown attachment mask provided " );
327
328
}
328
329
}
329
330
}else {
330
- $ default_mask = ClientManager:: getMask ("message " );
331
+ $ default_mask = $ this -> config -> getMask ("message " );
331
332
if ($ default_mask != "" ){
332
333
$ this ->default_message_mask = $ default_mask ;
333
334
}else {
334
335
throw new MaskNotFoundException ("Unknown message mask provided " );
335
336
}
336
337
337
- $ default_mask = ClientManager:: getMask ("attachment " );
338
+ $ default_mask = $ this -> config -> getMask ("attachment " );
338
339
if ($ default_mask != "" ){
339
340
$ this ->default_attachment_mask = $ default_mask ;
340
341
}else {
@@ -424,25 +425,25 @@ public function connect(): Client {
424
425
$ protocol = strtolower ($ this ->protocol );
425
426
426
427
if (in_array ($ protocol , ['imap ' , 'imap4 ' , 'imap4rev1 ' ])) {
427
- $ this ->connection = new ImapProtocol ($ this ->validate_cert , $ this ->encryption );
428
+ $ this ->connection = new ImapProtocol ($ this ->config , $ this -> validate_cert , $ this ->encryption );
428
429
$ this ->connection ->setConnectionTimeout ($ this ->timeout );
429
430
$ this ->connection ->setProxy ($ this ->proxy );
430
431
}else {
431
432
if (extension_loaded ('imap ' ) === false ) {
432
433
throw new ConnectionFailedException ("connection setup failed " , 0 , new ProtocolNotSupportedException ($ protocol ." is an unsupported protocol " ));
433
434
}
434
- $ this ->connection = new LegacyProtocol ($ this ->validate_cert , $ this ->encryption );
435
+ $ this ->connection = new LegacyProtocol ($ this ->config , $ this -> validate_cert , $ this ->encryption );
435
436
if (str_starts_with ($ protocol , "legacy- " )) {
436
437
$ protocol = substr ($ protocol , 7 );
437
438
}
438
439
$ this ->connection ->setProtocol ($ protocol );
439
440
}
440
441
441
- if (ClientManager:: get ('options.debug ' )) {
442
+ if ($ this -> config -> get ('options.debug ' )) {
442
443
$ this ->connection ->enableDebug ();
443
444
}
444
445
445
- if (!ClientManager:: get ('options.uid_cache ' )) {
446
+ if (!$ this -> config -> get ('options.uid_cache ' )) {
446
447
$ this ->connection ->disableUidCache ();
447
448
}
448
449
@@ -507,7 +508,7 @@ public function disconnect(): Client {
507
508
*/
508
509
public function getFolder (string $ folder_name , ?string $ delimiter = null , bool $ utf7 = false ): ?Folder {
509
510
// Set delimiter to false to force selection via getFolderByName (maybe useful for uncommon folder names)
510
- $ delimiter = is_null ($ delimiter ) ? ClientManager:: get ('options.delimiter ' , "/ " ) : $ delimiter ;
511
+ $ delimiter = is_null ($ delimiter ) ? $ this -> config -> get ('options.delimiter ' , "/ " ) : $ delimiter ;
511
512
512
513
if (str_contains ($ folder_name , (string )$ delimiter )) {
513
514
return $ this ->getFolderByPath ($ folder_name , $ utf7 );
0 commit comments