-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAbstractPublishType.php
93 lines (84 loc) · 2.65 KB
/
AbstractPublishType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* This file is part of workbunny.
*
* Redistributions of files must retain the above copyright notice.
*
* @author chaz6chez<[email protected]>
* @copyright chaz6chez<[email protected]>
* @link https://github.com/workbunny/webman-push-server
* @license https://github.com/workbunny/webman-push-server/blob/main/LICENSE
*/
declare(strict_types=1);
namespace Workbunny\WebmanPushServer\PublishTypes;
use InvalidArgumentException;
use Workbunny\WebmanPushServer\Events\AbstractEvent;
use Workbunny\WebmanPushServer\Traits\HelperMethods;
use Workerman\Connection\TcpConnection;
use const Workbunny\WebmanPushServer\EVENT_PING;
use const Workbunny\WebmanPushServer\EVENT_SUBSCRIBE;
use const Workbunny\WebmanPushServer\EVENT_UNSUBSCRIBE;
use const Workbunny\WebmanPushServer\PUSH_SERVER_EVENT_CLIENT_EVENT;
use const Workbunny\WebmanPushServer\PUSH_SERVER_EVENT_SERVER_EVENT;
abstract class AbstractPublishType
{
use HelperMethods;
const PUBLISH_TYPE_SERVER = 'server';
const PUBLISH_TYPE_CLIENT = 'client';
/**
* @var AbstractPublishType[]|string[]
*/
protected static array $_publishTypes = [
self::PUBLISH_TYPE_CLIENT => ClientType::class,
self::PUBLISH_TYPE_SERVER => ServerType::class,
];
/**
* 工厂
*
* @param string $publishType
* @return AbstractPublishType|null
*/
public static function factory(string $publishType): ?string
{
if (self::exists($publishType)) {
return self::$_publishTypes[$publishType];
}
return null;
}
/**
* 注册publish type响应
*
* @param string $publishType
* @param string $className
* @param bool $reset
* @return void
*/
final public static function register(string $publishType, string $className, bool $reset = false): void
{
if (!$reset and self::exists($publishType)) {
throw new InvalidArgumentException("Event $publishType already exists. ");
}
if (!is_a($className, AbstractPublishType::class, true)) {
throw new InvalidArgumentException("Invalid event class $className. ");
}
self::$_publishTypes[$publishType] = $className;
}
/**
* 检测publish type
*
* @param string $publishType
* @return bool
*/
final public static function exists(string $publishType): bool
{
return isset(self::$_publishTypes[$publishType]);
}
/**
* publish type响应
*
* @param array $data
* @return void
* @throws InvalidArgumentException
*/
abstract public static function response(array $data): void;
}