|
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\OpenPlatform\Traits\VerifyTicket; |
|
|
|
|
|
|
31
|
|
|
use EasyWeChat\Server\Guard as ServerGuard; |
|
32
|
|
|
use EasyWeChat\Support\Arr; |
|
33
|
|
|
|
|
34
|
|
|
class Guard extends ServerGuard |
|
35
|
|
|
{ |
|
36
|
|
|
use VerifyTicket; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Wechat push event types. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $eventTypeMappings = [ |
|
44
|
|
|
'authorized' => EventHandlers\Authorized::class, |
|
45
|
|
|
'unauthorized' => EventHandlers\Unauthorized::class, |
|
46
|
|
|
'updateauthorized' => EventHandlers\UpdateAuthorized::class, |
|
47
|
|
|
'component_verify_ticket' => EventHandlers\ComponentVerifyTicket::class, |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return for laravel-wechat. |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function listServe() |
|
56
|
|
|
{ |
|
57
|
|
|
$class = $this->getHandleClass(); |
|
58
|
|
|
|
|
59
|
|
|
$message = $this->getCollectedMessage(); |
|
60
|
|
|
|
|
61
|
|
|
call_user_func([new $class($this->verifyTicket), 'handle'], $message); |
|
62
|
|
|
|
|
63
|
|
|
return [ |
|
64
|
|
|
$message->get('InfoType'), $message, |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Listen for wechat push event. |
|
70
|
|
|
* |
|
71
|
|
|
* @param callable|null $callback |
|
72
|
|
|
* |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
* |
|
75
|
|
|
* @throws InvalidArgumentException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function listen($callback = null) |
|
78
|
|
|
{ |
|
79
|
|
|
$message = $this->getCollectedMessage(); |
|
80
|
|
|
|
|
81
|
|
|
$class = $this->getHandleClass(); |
|
82
|
|
|
|
|
83
|
|
|
if (is_callable($callback)) { |
|
84
|
|
|
$callback( |
|
85
|
|
|
call_user_func([new $class($this->verifyTicket), 'forward'], $message) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return call_user_func([new $class($this->verifyTicket), 'handle'], $message); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get handler class. |
|
94
|
|
|
* |
|
95
|
|
|
* @return \EasyWeChat\OpenPlatform\EventHandlers\EventHandler |
|
96
|
|
|
* |
|
97
|
|
|
* @throws InvalidArgumentException |
|
98
|
|
|
*/ |
|
99
|
|
|
private function getHandleClass() |
|
100
|
|
|
{ |
|
101
|
|
|
$message = $this->getCollectedMessage(); |
|
102
|
|
|
|
|
103
|
|
|
$type = $message->get('InfoType'); |
|
104
|
|
|
|
|
105
|
|
|
if (!$class = Arr::get($this->eventTypeMappings, $type)) { |
|
106
|
|
|
throw new InvalidArgumentException("Event Info Type \"$type\" does not exists."); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $class; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: