| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP; |
| 14 |
|
| 15 |
use Webklex\PHPIMAP\Decoder\DecoderInterface; |
| 16 |
use Webklex\PHPIMAP\Exceptions\DecoderNotFoundException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class Config { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
protected array $config = []; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function __construct(array $config = []) { |
| 36 |
$this->config = $config; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function get(string $key, $default = null): mixed { |
| 47 |
$parts = explode('.', $key); |
| 48 |
$value = null; |
| 49 |
foreach ($parts as $part) { |
| 50 |
if ($value === null) { |
| 51 |
if (isset($this->config[$part])) { |
| 52 |
$value = $this->config[$part]; |
| 53 |
} else { |
| 54 |
break; |
| 55 |
} |
| 56 |
} else { |
| 57 |
if (isset($value[$part])) { |
| 58 |
$value = $value[$part]; |
| 59 |
} else { |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return $value === null ? $default : $value; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function set(string $key, mixed $value): void { |
| 76 |
$parts = explode('.', $key); |
| 77 |
$config = &$this->config; |
| 78 |
|
| 79 |
foreach ($parts as $part) { |
| 80 |
if (!isset($config[$part])) { |
| 81 |
$config[$part] = []; |
| 82 |
} |
| 83 |
$config = &$config[$part]; |
| 84 |
} |
| 85 |
|
| 86 |
if(is_array($config) && is_array($value)){ |
| 87 |
$config = array_merge($config, $value); |
| 88 |
}else{ |
| 89 |
$config = $value; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function getDecoder(string $name): DecoderInterface { |
| 101 |
$default_decoders = $this->get('decoding.decoder', [ |
| 102 |
'header' => \Webklex\PHPIMAP\Decoder\HeaderDecoder::class, |
| 103 |
'message' => \Webklex\PHPIMAP\Decoder\MessageDecoder::class, |
| 104 |
'attachment' => \Webklex\PHPIMAP\Decoder\AttachmentDecoder::class |
| 105 |
]); |
| 106 |
$options = $this->get('decoding.options', [ |
| 107 |
'header' => 'utf-8', |
| 108 |
'message' => 'utf-8', |
| 109 |
'attachment' => 'utf-8', |
| 110 |
]); |
| 111 |
if (isset($default_decoders[$name])) { |
| 112 |
if (class_exists($default_decoders[$name])) { |
| 113 |
return new $default_decoders[$name]($options); |
| 114 |
} |
| 115 |
} |
| 116 |
throw new DecoderNotFoundException(); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function getMask(string $section): ?string { |
| 126 |
$default_masks = $this->get('masks', []); |
| 127 |
if (isset($default_masks[$section])) { |
| 128 |
if (class_exists($default_masks[$section])) { |
| 129 |
return $default_masks[$section]; |
| 130 |
} |
| 131 |
} |
| 132 |
return null; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function getClientConfig(?string $name): self { |
| 142 |
$config = $this->all(); |
| 143 |
$defaultName = $this->getDefaultAccount(); |
| 144 |
$defaultAccount = $this->get('accounts.'.$defaultName, []); |
| 145 |
|
| 146 |
if ($name === null || $name === 'null' || $name === "") { |
| 147 |
$account = $defaultAccount; |
| 148 |
$name = $defaultName; |
| 149 |
}else{ |
| 150 |
$account = $this->get('accounts.'.$name, $defaultAccount); |
| 151 |
} |
| 152 |
|
| 153 |
$config["default"] = $name; |
| 154 |
$config["accounts"] = [ |
| 155 |
$name => $account |
| 156 |
]; |
| 157 |
|
| 158 |
return new self($config); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function getDefaultAccount(): string { |
| 167 |
return $this->get('default', 'default'); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function setDefaultAccount(string $name): void { |
| 177 |
$this->set('default', $name); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public static function make(array|string $config = []): Config { |
| 186 |
if (is_array($config) === false) { |
| 187 |
$config = require $config; |
| 188 |
} |
| 189 |
|
| 190 |
$config_key = 'imap'; |
| 191 |
$path = __DIR__ . '/config/' . $config_key . '.php'; |
| 192 |
|
| 193 |
$vendor_config = require $path; |
| 194 |
$config = self::array_merge_recursive_distinct($vendor_config, $config); |
| 195 |
|
| 196 |
if (isset($config['default'])) { |
| 197 |
if (isset($config['accounts']) && $config['default']) { |
| 198 |
|
| 199 |
$default_config = $vendor_config['accounts']['default']; |
| 200 |
if (isset($config['accounts'][$config['default']])) { |
| 201 |
$default_config = array_merge($default_config, $config['accounts'][$config['default']]); |
| 202 |
} |
| 203 |
|
| 204 |
if (is_array($config['accounts'])) { |
| 205 |
foreach ($config['accounts'] as $account_key => $account) { |
| 206 |
$config['accounts'][$account_key] = array_merge($default_config, $account); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return new self($config); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
private static function array_merge_recursive_distinct(): array { |
| 231 |
$arrays = func_get_args(); |
| 232 |
$base = array_shift($arrays); |
| 233 |
|
| 234 |
|
| 235 |
$isAssoc = function(array $arr) { |
| 236 |
if (array() === $arr) return false; |
| 237 |
return array_keys($arr) !== range(0, count($arr) - 1); |
| 238 |
}; |
| 239 |
|
| 240 |
if (!is_array($base)) $base = empty($base) ? array() : array($base); |
| 241 |
|
| 242 |
foreach ($arrays as $append) { |
| 243 |
if (!is_array($append)) $append = array($append); |
| 244 |
|
| 245 |
foreach ($append as $key => $value) { |
| 246 |
|
| 247 |
if (!array_key_exists($key, $base) and !is_numeric($key)) { |
| 248 |
$base[$key] = $value; |
| 249 |
continue; |
| 250 |
} |
| 251 |
|
| 252 |
if ((is_array($value) && $isAssoc($value)) || (is_array($base[$key]) && $isAssoc($base[$key]))) { |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
$base[$key] = self::array_merge_recursive_distinct($base[$key], $value); |
| 257 |
} else if (is_numeric($key)) { |
| 258 |
if (!in_array($value, $base)) $base[] = $value; |
| 259 |
} else { |
| 260 |
$base[$key] = $value; |
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
return $base; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public function all(): array { |
| 275 |
return $this->config; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public function has(string $key): bool { |
| 284 |
return $this->get($key) !== null; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public function clear(): static { |
| 292 |
$this->config = []; |
| 293 |
return $this; |
| 294 |
} |
| 295 |
} |