|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Guard.php. |
|
14
|
|
|
* |
|
15
|
|
|
* Part of Overtrue\WeChat. |
|
16
|
|
|
* |
|
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
18
|
|
|
* file that was distributed with this source code. |
|
19
|
|
|
* |
|
20
|
|
|
* @author mingyoung <[email protected]> |
|
21
|
|
|
* @copyright 2016 |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://github.com/overtrue |
|
24
|
|
|
* @see http://overtrue.me |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace EasyWeChat\OpenPlatform; |
|
28
|
|
|
|
|
29
|
|
|
use EasyWeChat\Core\Exceptions\InvalidArgumentException; |
|
30
|
|
|
use EasyWeChat\Server\Guard as ServerGuard; |
|
31
|
|
|
use EasyWeChat\Support\Arr; |
|
32
|
|
|
|
|
33
|
|
|
class Guard extends ServerGuard |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Wechat push event types. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $eventTypeMappings = [ |
|
41
|
|
|
'component_verify_ticket' => EventHandlers\ComponentVerifyTicket::class, |
|
42
|
|
|
'authorized' => EventHandlers\Authorized::class, |
|
43
|
|
|
'unauthorized' => EventHandlers\Unauthorized::class, |
|
44
|
|
|
'updateauthorized' => EventHandlers\UpdateAuthorized::class, |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Listen for wechat push event. |
|
49
|
|
|
* |
|
50
|
|
|
* @param $type |
|
51
|
|
|
* @param callable $callback |
|
52
|
|
|
* |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
* |
|
55
|
|
|
* @throws InvalidArgumentException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function listenFor($type, callable $callback) |
|
58
|
|
|
{ |
|
59
|
|
|
$message = $this->getCollectedMessage(); |
|
60
|
|
|
|
|
61
|
|
|
if (!$class = Arr::get($this->eventTypeMappings, $type)) { |
|
62
|
|
|
throw new InvalidArgumentException("Event Info Type \"$type\" does not exists."); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$callback( |
|
66
|
|
|
call_user_func([new $class(), 'forward'], $message) |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
return call_user_func([new $class(), 'handle'], $message); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|