123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- namespace WeWork;
- use Doctrine\Common\Collections\ArrayCollection;
- use GuzzleHttp\Client;
- use Monolog\Handler\NullHandler;
- use Monolog\Handler\StreamHandler;
- use Monolog\Logger;
- use Psr\Log\LoggerInterface;
- use Psr\SimpleCache\CacheInterface;
- use Symfony\Component\HttpFoundation\Request;
- use WeWork\ApiCache\JsApiTicket;
- use WeWork\ApiCache\Ticket;
- use WeWork\ApiCache\Token;
- use WeWork\Crypt\WXBizMsgCrypt;
- use WeWork\Http\ClientFactory;
- use WeWork\Http\HttpClient;
- class App
- {
- /**
- * @var ArrayCollection
- */
- private $config;
- /**
- * @var array
- */
- private $apiServices = [
- 'agent' => Api\Agent::class,
- 'appChat' => Api\AppChat::class,
- 'batch' => Api\Batch::class,
- 'checkIn' => Api\CheckIn::class,
- 'corp' => Api\Corp::class,
- 'crm' => Api\CRM::class,
- 'department' => Api\Department::class,
- 'invoice' => Api\Invoice::class,
- 'media' => Api\Media::class,
- 'menu' => Api\Menu::class,
- 'message' => Api\Message::class,
- 'tag' => Api\Tag::class,
- 'user' => Api\User::class,
- ];
- /**
- * @param array $config
- */
- public function __construct(array $config)
- {
- parent::__construct();
- $this->config = new ArrayCollection($config);
- $this->registerServices();
- }
- /**
- * @return void
- */
- private function registerServices(): void
- {
- $this->registerLogger();
- $this->registerHttpClient();
- $this->registerCache();
- $this->registerToken();
- $this->registerCallback();
- $this->registerHttpClientWithToken();
- foreach ($this->apiServices as $id => $class) {
- $this->registerApi($id, $class);
- }
- $this->registerJsApiTicket();
- $this->registerTicket();
- $this->registerJssdk();
- }
- /**
- * @return void
- */
- private function registerLogger(): void
- {
- $log = $this->config->get('log');
- if (is_subclass_of($log, LoggerInterface::class)) {
- $this->register('logger', $log);
- } elseif ($log) {
- $this->register('logger_handler', StreamHandler::class)
- ->setArguments([$log['file'], isset($log['level']) ? $log['level'] : 'debug']);
- $this->registerMonolog();
- } else {
- $this->register('logger_handler', NullHandler::class);
- $this->registerMonolog();
- }
- }
- /**
- * @return void
- */
- private function registerMonolog(): void
- {
- $this->register('logger', Logger::class)
- ->addArgument('WeWork')
- ->addMethodCall('setTimezone', [new \DateTimeZone('PRC')])
- ->addMethodCall('pushHandler', [new Reference('logger_handler')]);
- }
- /**
- * @return void
- */
- private function registerHttpClient(): void
- {
- $this->register('client', Client::class)
- ->addArgument(new Reference('logger'))
- ->setFactory([ClientFactory::class, 'create']);
- $this->register('http_client', HttpClient::class)
- ->addArgument(new Reference('client'));
- }
- /**
- * @return void
- */
- private function registerCache(): void
- {
- $cache = $this->config->get('cache');
- if (is_subclass_of($cache, CacheInterface::class)) {
- $this->register('cache', $cache);
- } else {
- $service = $this->register('cache', FilesystemCache::class);
- if ($cache && isset($cache['path'])) {
- $service->setArguments(['', 0, $cache['path']]);
- }
- }
- }
- /**
- * @return void
- */
- private function registerToken(): void
- {
- $this->register('token', Token::class)
- ->addMethodCall('setCorpId', [$this->config->get('corp_id')])
- ->addMethodCall('setSecret', [$this->config->get('secret')])
- ->addMethodCall('setCache', [new Reference('cache')])
- ->addMethodCall('setHttpClient', [new Reference('http_client')]);
- }
- /**
- * @return void
- */
- private function registerCallback(): void
- {
- $this->register('request', Request::class)
- ->setFactory([Request::class, 'createFromGlobals']);
- $this->register('crypt', WXBizMsgCrypt::class)
- ->setArguments([$this->config->get('token'), $this->config->get('aes_key'), $this->config->get('corp_id')]);
- $this->register('callback', Callback::class)
- ->setArguments([new Reference('request'), new Reference('crypt')]);
- }
- /**
- * @return void
- */
- private function registerHttpClientWithToken(): void
- {
- $this->register('client_with_token', Client::class)
- ->setArguments([new Reference('logger'), new Reference('token')])
- ->setFactory([ClientFactory::class, 'create']);
- $this->register('http_client_with_token', HttpClient::class)
- ->addArgument(new Reference('client_with_token'));
- }
- /**
- * @param string $id
- * @param string $class
- *
- * @return void
- */
- private function registerApi(string $id, string $class): void
- {
- $api = $this->register($id, $class)
- ->addMethodCall('setHttpClient', [new Reference('http_client_with_token')]);
- if (in_array($id, ['agent', 'menu', 'message'])) {
- $api->addMethodCall('setAgentId', [$this->config->get('agent_id')]);
- }
- }
- /**
- * @return void
- */
- private function registerJsApiTicket(): void
- {
- $this->register('jsApiTicket', JsApiTicket::class)
- ->addMethodCall('setSecret', [$this->config->get('secret')])
- ->addMethodCall('setCache', [new Reference('cache')])
- ->addMethodCall('setHttpClient', [new Reference('http_client_with_token')]);
- }
- /**
- * @return void
- */
- private function registerTicket(): void
- {
- $this->register('ticket', Ticket::class)
- ->addMethodCall('setCache', [new Reference('cache')])
- ->addMethodCall('setHttpClient', [new Reference('http_client_with_token')]);
- }
- /**
- * @return void
- */
- private function registerJssdk(): void
- {
- $this->register('jssdk', JSSdk::class)
- ->addMethodCall('setCorpId', [$this->config->get('corp_id')])
- ->addMethodCall('setJsApiTicket', [new Reference('jsApiTicket')])
- ->addMethodCall('setTicket', [new Reference('ticket')]);
- }
- }
|