|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.tintsoft.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd. |
|
5
|
|
|
* @license http://www.tintsoft.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yuncms\broadcast; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\base\BaseObject; |
|
13
|
|
|
use yii\base\ErrorHandler; |
|
14
|
|
|
use yuncms\helpers\Json; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class BaseMessage |
|
18
|
|
|
* |
|
19
|
|
|
* @author Tongle Xu <[email protected]> |
|
20
|
|
|
* @since 3.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class BaseMessage extends BaseObject implements MessageInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var BroadcastInterface the broadcast instance that created this message. |
|
26
|
|
|
* For independently created messages this is `null`. |
|
27
|
|
|
*/ |
|
28
|
|
|
public $broadcast; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $body; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string message tag. |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $tag; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var null|array message attributes. |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $attributes; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* set the message body |
|
47
|
|
|
* @param array $body |
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setBody($body) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->body = $body; |
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* get the message body |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getBody() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->body; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* get the message tag |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getTag() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->tag; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* set the message tag |
|
76
|
|
|
* @param string $tag |
|
77
|
|
|
* @return BaseMessage |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setTag($tag) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->tag = $tag; |
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array|null |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getAttributes() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->attributes; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* set message attributes |
|
95
|
|
|
* @param array $attributes |
|
96
|
|
|
* @return BaseMessage |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setAttributes(array $attributes) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->attributes = $attributes; |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Sends this broadcast message. |
|
106
|
|
|
* @param BroadcastInterface $broadcast the broadcast that should be used to send this message. |
|
107
|
|
|
* If no broadcast is given it will first check if [[broadcast]] is set and if not, |
|
108
|
|
|
* the "broadcast" application component will be used instead. |
|
109
|
|
|
* @return bool whether this message is sent successfully. |
|
110
|
|
|
*/ |
|
111
|
|
|
public function send(BroadcastInterface $broadcast = null) |
|
112
|
|
|
{ |
|
113
|
|
|
if ($broadcast === null && $this->broadcast === null) { |
|
114
|
|
|
$broadcast = Yii::$app->getBroadcast(); |
|
115
|
|
|
} elseif ($broadcast === null) { |
|
116
|
|
|
$broadcast = $this->broadcast; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $broadcast->send($this); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns string representation of this message. |
|
124
|
|
|
* @return string the string representation of this message. |
|
125
|
|
|
*/ |
|
126
|
|
|
public function toString() |
|
127
|
|
|
{ |
|
128
|
|
|
return Json::encode($this->body); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* PHP magic method that returns the string representation of this object. |
|
133
|
|
|
* @return string the string representation of this object. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function __toString() |
|
136
|
|
|
{ |
|
137
|
|
|
// __toString cannot throw exception |
|
138
|
|
|
// use trigger_error to bypass this limitation |
|
139
|
|
|
try { |
|
140
|
|
|
return $this->toString(); |
|
141
|
|
|
} catch (\Exception $e) { |
|
142
|
|
|
ErrorHandler::convertExceptionToError($e); |
|
143
|
|
|
return ''; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |