ClientManager.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. namespace Webklex\PHPIMAP;
  13. /**
  14. * Class ClientManager
  15. *
  16. * @package Webklex\IMAP
  17. *
  18. * @mixin Client
  19. */
  20. class ClientManager {
  21. /**
  22. * All library config
  23. *
  24. * @var array $config
  25. */
  26. public static $config = [];
  27. /**
  28. * @var array $accounts
  29. */
  30. protected $accounts = [];
  31. /**
  32. * ClientManager constructor.
  33. * @param array|string $config
  34. */
  35. public function __construct($config = []) {
  36. $this->setConfig($config);
  37. }
  38. /**
  39. * Dynamically pass calls to the default account.
  40. * @param string $method
  41. * @param array $parameters
  42. *
  43. * @return mixed
  44. * @throws Exceptions\MaskNotFoundException
  45. */
  46. public function __call($method, $parameters) {
  47. $callable = [$this->account(), $method];
  48. return call_user_func_array($callable, $parameters);
  49. }
  50. /**
  51. * Safely create a new client instance which is not listed in accounts
  52. * @param array $config
  53. *
  54. * @return Client
  55. * @throws Exceptions\MaskNotFoundException
  56. */
  57. public function make($config) {
  58. return new Client($config);
  59. }
  60. /**
  61. * Get a dotted config parameter
  62. * @param string $key
  63. * @param null $default
  64. *
  65. * @return mixed|null
  66. */
  67. public static function get($key, $default = null) {
  68. $parts = explode('.', $key);
  69. $value = null;
  70. foreach($parts as $part) {
  71. if($value === null) {
  72. if(isset(self::$config[$part])) {
  73. $value = self::$config[$part];
  74. }else{
  75. break;
  76. }
  77. }else{
  78. if(isset($value[$part])) {
  79. $value = $value[$part];
  80. }else{
  81. break;
  82. }
  83. }
  84. }
  85. return $value === null ? $default : $value;
  86. }
  87. /**
  88. * Resolve a account instance.
  89. * @param string $name
  90. *
  91. * @return Client
  92. * @throws Exceptions\MaskNotFoundException
  93. */
  94. public function account($name = null) {
  95. $name = $name ?: $this->getDefaultAccount();
  96. // If the connection has not been resolved yet we will resolve it now as all
  97. // of the connections are resolved when they are actually needed so we do
  98. // not make any unnecessary connection to the various queue end-points.
  99. if (!isset($this->accounts[$name])) {
  100. $this->accounts[$name] = $this->resolve($name);
  101. }
  102. return $this->accounts[$name];
  103. }
  104. /**
  105. * Resolve a account.
  106. *
  107. * @param string $name
  108. *
  109. * @return Client
  110. * @throws Exceptions\MaskNotFoundException
  111. */
  112. protected function resolve($name) {
  113. $config = $this->getClientConfig($name);
  114. return new Client($config);
  115. }
  116. /**
  117. * Get the account configuration.
  118. * @param string $name
  119. *
  120. * @return array
  121. */
  122. protected function getClientConfig($name) {
  123. if ($name === null || $name === 'null') {
  124. return ['driver' => 'null'];
  125. }
  126. return self::$config["accounts"][$name];
  127. }
  128. /**
  129. * Get the name of the default account.
  130. *
  131. * @return string
  132. */
  133. public function getDefaultAccount() {
  134. return self::$config['default'];
  135. }
  136. /**
  137. * Set the name of the default account.
  138. * @param string $name
  139. *
  140. * @return void
  141. */
  142. public function setDefaultAccount($name) {
  143. self::$config['default'] = $name;
  144. }
  145. /**
  146. * Merge the vendor settings with the local config
  147. *
  148. * The default account identifier will be used as default for any missing account parameters.
  149. * If however the default account is missing a parameter the package default account parameter will be used.
  150. * This can be disabled by setting imap.default in your config file to 'false'
  151. *
  152. * @param array|string $config
  153. *
  154. * @return $this
  155. */
  156. public function setConfig($config) {
  157. if(is_array($config) === false) {
  158. $config = require $config;
  159. }
  160. $config_key = 'imap';
  161. $path = __DIR__.'/config/'.$config_key.'.php';
  162. $vendor_config = require $path;
  163. $config = $this->array_merge_recursive_distinct($vendor_config, $config);
  164. if(is_array($config)){
  165. if(isset($config['default'])){
  166. if(isset($config['accounts']) && $config['default'] != false){
  167. $default_config = $vendor_config['accounts']['default'];
  168. if(isset($config['accounts'][$config['default']])){
  169. $default_config = array_merge($default_config, $config['accounts'][$config['default']]);
  170. }
  171. if(is_array($config['accounts'])){
  172. foreach($config['accounts'] as $account_key => $account){
  173. $config['accounts'][$account_key] = array_merge($default_config, $account);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. self::$config = $config;
  180. return $this;
  181. }
  182. /**
  183. * Marge arrays recursively and distinct
  184. *
  185. * Merges any number of arrays / parameters recursively, replacing
  186. * entries with string keys with values from latter arrays.
  187. * If the entry or the next value to be assigned is an array, then it
  188. * automatically treats both arguments as an array.
  189. * Numeric entries are appended, not replaced, but only if they are
  190. * unique
  191. *
  192. * @param array $array1 Initial array to merge.
  193. * @param array ... Variable list of arrays to recursively merge.
  194. *
  195. * @return array|mixed
  196. *
  197. * @link http://www.php.net/manual/en/function.array-merge-recursive.php#96201
  198. * @author Mark Roduner <mark.roduner@gmail.com>
  199. */
  200. private function array_merge_recursive_distinct() {
  201. $arrays = func_get_args();
  202. $base = array_shift($arrays);
  203. if(!is_array($base)) $base = empty($base) ? array() : array($base);
  204. foreach($arrays as $append) {
  205. if(!is_array($append)) $append = array($append);
  206. foreach($append as $key => $value) {
  207. if(!array_key_exists($key, $base) and !is_numeric($key)) {
  208. $base[$key] = $append[$key];
  209. continue;
  210. }
  211. if(is_array($value) or is_array($base[$key])) {
  212. $base[$key] = $this->array_merge_recursive_distinct($base[$key], $append[$key]);
  213. } else if(is_numeric($key)) {
  214. if(!in_array($value, $base)) $base[] = $value;
  215. } else {
  216. $base[$key] = $value;
  217. }
  218. }
  219. }
  220. return $base;
  221. }
  222. }