Webklex /
php-imap
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * File: ClientManager.php |
||
| 4 | * Category: - |
||
| 5 | * Author: M. Goldenbaum |
||
| 6 | * Created: 19.01.17 22:21 |
||
| 7 | * Updated: - |
||
| 8 | * |
||
| 9 | * Description: |
||
| 10 | * - |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Webklex\PHPIMAP; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class ClientManager |
||
| 17 | * |
||
| 18 | * @package Webklex\IMAP |
||
| 19 | * |
||
| 20 | * @mixin Client |
||
| 21 | */ |
||
| 22 | class ClientManager { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * All library config |
||
| 26 | * |
||
| 27 | * @var array $config |
||
| 28 | */ |
||
| 29 | public static $config = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array $accounts |
||
| 33 | */ |
||
| 34 | protected $accounts = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * ClientManager constructor. |
||
| 38 | * @param array|string $config |
||
| 39 | */ |
||
| 40 | public function __construct($config = []) { |
||
| 41 | $this->setConfig($config); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Dynamically pass calls to the default account. |
||
| 46 | * @param string $method |
||
| 47 | * @param array $parameters |
||
| 48 | * |
||
| 49 | * @return mixed |
||
| 50 | * @throws Exceptions\MaskNotFoundException |
||
| 51 | */ |
||
| 52 | public function __call(string $method, array $parameters) { |
||
| 53 | $callable = [$this->account(), $method]; |
||
| 54 | |||
| 55 | return call_user_func_array($callable, $parameters); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Safely create a new client instance which is not listed in accounts |
||
| 60 | * @param array $config |
||
| 61 | * |
||
| 62 | * @return Client |
||
| 63 | * @throws Exceptions\MaskNotFoundException |
||
| 64 | */ |
||
| 65 | public function make(array $config): Client { |
||
| 66 | return new Client($config); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get a dotted config parameter |
||
| 71 | * @param string $key |
||
| 72 | * @param null $default |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 73 | * |
||
| 74 | * @return mixed|null |
||
| 75 | */ |
||
| 76 | public static function get(string $key, $default = null) { |
||
| 77 | $parts = explode('.', $key); |
||
| 78 | $value = null; |
||
| 79 | foreach($parts as $part) { |
||
| 80 | if($value === null) { |
||
| 81 | if(isset(self::$config[$part])) { |
||
| 82 | $value = self::$config[$part]; |
||
| 83 | }else{ |
||
| 84 | break; |
||
| 85 | } |
||
| 86 | }else{ |
||
| 87 | if(isset($value[$part])) { |
||
| 88 | $value = $value[$part]; |
||
| 89 | }else{ |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return $value === null ? $default : $value; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Resolve a account instance. |
||
| 100 | * @param string|null $name |
||
| 101 | * |
||
| 102 | * @return Client |
||
| 103 | * @throws Exceptions\MaskNotFoundException |
||
| 104 | */ |
||
| 105 | public function account(string $name = null): Client { |
||
| 106 | $name = $name ?: $this->getDefaultAccount(); |
||
| 107 | |||
| 108 | // If the connection has not been resolved we will resolve it now as all |
||
| 109 | // the connections are resolved when they are actually needed, so we do |
||
| 110 | // not make any unnecessary connection to the various queue end-points. |
||
| 111 | if (!isset($this->accounts[$name])) { |
||
| 112 | $this->accounts[$name] = $this->resolve($name); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $this->accounts[$name]; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Resolve an account. |
||
| 120 | * @param string $name |
||
| 121 | * |
||
| 122 | * @return Client |
||
| 123 | * @throws Exceptions\MaskNotFoundException |
||
| 124 | */ |
||
| 125 | protected function resolve(string $name): Client { |
||
| 126 | $config = $this->getClientConfig($name); |
||
| 127 | |||
| 128 | return new Client($config); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the account configuration. |
||
| 133 | * @param string|null $name |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | protected function getClientConfig($name): array { |
||
| 138 | if ($name === null || $name === 'null') { |
||
| 139 | return ['driver' => 'null']; |
||
| 140 | } |
||
| 141 | |||
| 142 | return is_array(self::$config["accounts"][$name]) ? self::$config["accounts"][$name] : []; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the name of the default account. |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getDefaultAccount(): string { |
||
| 151 | return self::$config['default']; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set the name of the default account. |
||
| 156 | * @param string $name |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function setDefaultAccount(string $name) { |
||
| 161 | self::$config['default'] = $name; |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * Merge the vendor settings with the local config |
||
| 167 | * |
||
| 168 | * The default account identifier will be used as default for any missing account parameters. |
||
| 169 | * If however the default account is missing a parameter the package default account parameter will be used. |
||
| 170 | * This can be disabled by setting imap.default in your config file to 'false' |
||
| 171 | * |
||
| 172 | * @param array|string $config |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function setConfig($config): ClientManager { |
||
| 177 | |||
| 178 | if(is_array($config) === false) { |
||
| 179 | $config = require $config; |
||
| 180 | } |
||
| 181 | |||
| 182 | $config_key = 'imap'; |
||
| 183 | $path = __DIR__.'/config/'.$config_key.'.php'; |
||
| 184 | |||
| 185 | $vendor_config = require $path; |
||
| 186 | $config = $this->array_merge_recursive_distinct($vendor_config, $config); |
||
| 187 | |||
| 188 | if(is_array($config)){ |
||
|
0 ignored issues
–
show
|
|||
| 189 | if(isset($config['default'])){ |
||
| 190 | if(isset($config['accounts']) && $config['default']){ |
||
| 191 | |||
| 192 | $default_config = $vendor_config['accounts']['default']; |
||
| 193 | if(isset($config['accounts'][$config['default']])){ |
||
| 194 | $default_config = array_merge($default_config, $config['accounts'][$config['default']]); |
||
| 195 | } |
||
| 196 | |||
| 197 | if(is_array($config['accounts'])){ |
||
| 198 | foreach($config['accounts'] as $account_key => $account){ |
||
| 199 | $config['accounts'][$account_key] = array_merge($default_config, $account); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | self::$config = $config; |
||
| 207 | |||
| 208 | return $this; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Marge arrays recursively and distinct |
||
| 213 | * |
||
| 214 | * Merges any number of arrays / parameters recursively, replacing |
||
| 215 | * entries with string keys with values from latter arrays. |
||
| 216 | * If the entry or the next value to be assigned is an array, then it |
||
| 217 | * automatically treats both arguments as an array. |
||
| 218 | * Numeric entries are appended, not replaced, but only if they are |
||
| 219 | * unique |
||
| 220 | * |
||
| 221 | * @return array|mixed |
||
| 222 | * |
||
| 223 | * @link http://www.php.net/manual/en/function.array-merge-recursive.php#96201 |
||
| 224 | * @author Mark Roduner <[email protected]> |
||
| 225 | */ |
||
| 226 | private function array_merge_recursive_distinct() { |
||
| 227 | |||
| 228 | $arrays = func_get_args(); |
||
| 229 | $base = array_shift($arrays); |
||
| 230 | |||
| 231 | // From https://stackoverflow.com/a/173479 |
||
| 232 | $isAssoc = function(array $arr) { |
||
| 233 | if (array() === $arr) return false; |
||
| 234 | return array_keys($arr) !== range(0, count($arr) - 1); |
||
| 235 | }; |
||
| 236 | |||
| 237 | if(!is_array($base)) $base = empty($base) ? array() : array($base); |
||
| 238 | |||
| 239 | foreach($arrays as $append) { |
||
| 240 | |||
| 241 | if(!is_array($append)) $append = array($append); |
||
| 242 | |||
| 243 | foreach($append as $key => $value) { |
||
| 244 | |||
| 245 | if(!array_key_exists($key, $base) and !is_numeric($key)) { |
||
| 246 | $base[$key] = $value; |
||
| 247 | continue; |
||
| 248 | } |
||
| 249 | |||
| 250 | if( |
||
| 251 | ( |
||
| 252 | is_array($value) |
||
| 253 | && $isAssoc($value) |
||
| 254 | ) |
||
| 255 | || ( |
||
| 256 | is_array($base[$key]) |
||
| 257 | && $isAssoc($base[$key]) |
||
| 258 | ) |
||
| 259 | ) { |
||
| 260 | // If the arrays are not associates we don't want to array_merge_recursive_distinct |
||
| 261 | // else merging $baseConfig['dispositions'] = ['attachment', 'inline'] with $customConfig['dispositions'] = ['attachment'] |
||
| 262 | // results in $resultConfig['dispositions'] = ['attachment', 'inline'] |
||
| 263 | $base[$key] = $this->array_merge_recursive_distinct($base[$key], $value); |
||
| 264 | } else if(is_numeric($key)) { |
||
| 265 | if(!in_array($value, $base)) $base[] = $value; |
||
| 266 | } else { |
||
| 267 | $base[$key] = $value; |
||
| 268 | } |
||
| 269 | |||
| 270 | } |
||
| 271 | |||
| 272 | } |
||
| 273 | |||
| 274 | return $base; |
||
| 275 | } |
||
| 276 | } |