App.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace WeWork;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use GuzzleHttp\Client;
  5. use Monolog\Handler\NullHandler;
  6. use Monolog\Handler\StreamHandler;
  7. use Monolog\Logger;
  8. use Psr\Log\LoggerInterface;
  9. use Psr\SimpleCache\CacheInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use WeWork\ApiCache\JsApiTicket;
  12. use WeWork\ApiCache\Ticket;
  13. use WeWork\ApiCache\Token;
  14. use WeWork\Crypt\WXBizMsgCrypt;
  15. use WeWork\Http\ClientFactory;
  16. use WeWork\Http\HttpClient;
  17. class App
  18. {
  19. /**
  20. * @var ArrayCollection
  21. */
  22. private $config;
  23. /**
  24. * @var array
  25. */
  26. private $apiServices = [
  27. 'agent' => Api\Agent::class,
  28. 'appChat' => Api\AppChat::class,
  29. 'batch' => Api\Batch::class,
  30. 'checkIn' => Api\CheckIn::class,
  31. 'corp' => Api\Corp::class,
  32. 'crm' => Api\CRM::class,
  33. 'department' => Api\Department::class,
  34. 'invoice' => Api\Invoice::class,
  35. 'media' => Api\Media::class,
  36. 'menu' => Api\Menu::class,
  37. 'message' => Api\Message::class,
  38. 'tag' => Api\Tag::class,
  39. 'user' => Api\User::class,
  40. ];
  41. /**
  42. * @param array $config
  43. */
  44. public function __construct(array $config)
  45. {
  46. parent::__construct();
  47. $this->config = new ArrayCollection($config);
  48. $this->registerServices();
  49. }
  50. /**
  51. * @return void
  52. */
  53. private function registerServices(): void
  54. {
  55. $this->registerLogger();
  56. $this->registerHttpClient();
  57. $this->registerCache();
  58. $this->registerToken();
  59. $this->registerCallback();
  60. $this->registerHttpClientWithToken();
  61. foreach ($this->apiServices as $id => $class) {
  62. $this->registerApi($id, $class);
  63. }
  64. $this->registerJsApiTicket();
  65. $this->registerTicket();
  66. $this->registerJssdk();
  67. }
  68. /**
  69. * @return void
  70. */
  71. private function registerLogger(): void
  72. {
  73. $log = $this->config->get('log');
  74. if (is_subclass_of($log, LoggerInterface::class)) {
  75. $this->register('logger', $log);
  76. } elseif ($log) {
  77. $this->register('logger_handler', StreamHandler::class)
  78. ->setArguments([$log['file'], isset($log['level']) ? $log['level'] : 'debug']);
  79. $this->registerMonolog();
  80. } else {
  81. $this->register('logger_handler', NullHandler::class);
  82. $this->registerMonolog();
  83. }
  84. }
  85. /**
  86. * @return void
  87. */
  88. private function registerMonolog(): void
  89. {
  90. $this->register('logger', Logger::class)
  91. ->addArgument('WeWork')
  92. ->addMethodCall('setTimezone', [new \DateTimeZone('PRC')])
  93. ->addMethodCall('pushHandler', [new Reference('logger_handler')]);
  94. }
  95. /**
  96. * @return void
  97. */
  98. private function registerHttpClient(): void
  99. {
  100. $this->register('client', Client::class)
  101. ->addArgument(new Reference('logger'))
  102. ->setFactory([ClientFactory::class, 'create']);
  103. $this->register('http_client', HttpClient::class)
  104. ->addArgument(new Reference('client'));
  105. }
  106. /**
  107. * @return void
  108. */
  109. private function registerCache(): void
  110. {
  111. $cache = $this->config->get('cache');
  112. if (is_subclass_of($cache, CacheInterface::class)) {
  113. $this->register('cache', $cache);
  114. } else {
  115. $service = $this->register('cache', FilesystemCache::class);
  116. if ($cache && isset($cache['path'])) {
  117. $service->setArguments(['', 0, $cache['path']]);
  118. }
  119. }
  120. }
  121. /**
  122. * @return void
  123. */
  124. private function registerToken(): void
  125. {
  126. $this->register('token', Token::class)
  127. ->addMethodCall('setCorpId', [$this->config->get('corp_id')])
  128. ->addMethodCall('setSecret', [$this->config->get('secret')])
  129. ->addMethodCall('setCache', [new Reference('cache')])
  130. ->addMethodCall('setHttpClient', [new Reference('http_client')]);
  131. }
  132. /**
  133. * @return void
  134. */
  135. private function registerCallback(): void
  136. {
  137. $this->register('request', Request::class)
  138. ->setFactory([Request::class, 'createFromGlobals']);
  139. $this->register('crypt', WXBizMsgCrypt::class)
  140. ->setArguments([$this->config->get('token'), $this->config->get('aes_key'), $this->config->get('corp_id')]);
  141. $this->register('callback', Callback::class)
  142. ->setArguments([new Reference('request'), new Reference('crypt')]);
  143. }
  144. /**
  145. * @return void
  146. */
  147. private function registerHttpClientWithToken(): void
  148. {
  149. $this->register('client_with_token', Client::class)
  150. ->setArguments([new Reference('logger'), new Reference('token')])
  151. ->setFactory([ClientFactory::class, 'create']);
  152. $this->register('http_client_with_token', HttpClient::class)
  153. ->addArgument(new Reference('client_with_token'));
  154. }
  155. /**
  156. * @param string $id
  157. * @param string $class
  158. *
  159. * @return void
  160. */
  161. private function registerApi(string $id, string $class): void
  162. {
  163. $api = $this->register($id, $class)
  164. ->addMethodCall('setHttpClient', [new Reference('http_client_with_token')]);
  165. if (in_array($id, ['agent', 'menu', 'message'])) {
  166. $api->addMethodCall('setAgentId', [$this->config->get('agent_id')]);
  167. }
  168. }
  169. /**
  170. * @return void
  171. */
  172. private function registerJsApiTicket(): void
  173. {
  174. $this->register('jsApiTicket', JsApiTicket::class)
  175. ->addMethodCall('setSecret', [$this->config->get('secret')])
  176. ->addMethodCall('setCache', [new Reference('cache')])
  177. ->addMethodCall('setHttpClient', [new Reference('http_client_with_token')]);
  178. }
  179. /**
  180. * @return void
  181. */
  182. private function registerTicket(): void
  183. {
  184. $this->register('ticket', Ticket::class)
  185. ->addMethodCall('setCache', [new Reference('cache')])
  186. ->addMethodCall('setHttpClient', [new Reference('http_client_with_token')]);
  187. }
  188. /**
  189. * @return void
  190. */
  191. private function registerJssdk(): void
  192. {
  193. $this->register('jssdk', JSSdk::class)
  194. ->addMethodCall('setCorpId', [$this->config->get('corp_id')])
  195. ->addMethodCall('setJsApiTicket', [new Reference('jsApiTicket')])
  196. ->addMethodCall('setTicket', [new Reference('ticket')]);
  197. }
  198. }