1 | <?php |
||
23 | abstract class Event extends SymfonyEvent implements \Serializable |
||
24 | { |
||
25 | /** |
||
26 | * Serialize an event so that it can be stored in the database |
||
27 | */ |
||
28 | 1 | public function serialize() |
|
29 | { |
||
30 | 1 | $class = new \ReflectionObject($this); |
|
31 | 1 | $params = $class->getConstructor()->getParameters(); |
|
32 | |||
33 | 1 | $data = array(); |
|
34 | |||
35 | // Iterate through all the parameters of the constructor |
||
36 | 1 | foreach ($params as $param) { |
|
37 | // Find the related class property |
||
38 | 1 | $property = $class->getProperty($param->getName()); |
|
39 | 1 | $property->setAccessible(true); |
|
40 | 1 | $value = $property->getValue($this); |
|
41 | |||
42 | // We just need to store IDs and types for models |
||
43 | 1 | if ($value !== null && $this->isModel($param)) { |
|
44 | 1 | if (!$param->getClass()->isAbstract()) { |
|
45 | // The parameter is a non-abstract model, we can just |
||
46 | // store its ID since the type will be known when |
||
47 | // unserializing |
||
48 | 1 | $value = $value->getId(); |
|
49 | 1 | } else { |
|
50 | // The parameter is an abstract model class, we need to |
||
51 | // store the model's type as well |
||
52 | $value = array( |
||
53 | 'id' => $value->getId(), |
||
54 | 'type' => get_class($value) |
||
55 | ); |
||
56 | } |
||
57 | 1 | } |
|
58 | |||
59 | 1 | $data[$property->getName()] = $value; |
|
60 | 1 | } |
|
61 | |||
62 | 1 | return serialize($data); |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Call the event's constructor using serialized data |
||
67 | * |
||
68 | * @param string $data |
||
69 | */ |
||
70 | 1 | public function unserialize($data) |
|
71 | { |
||
72 | 1 | $data = unserialize($data); |
|
73 | |||
74 | 1 | $class = new \ReflectionObject($this); |
|
75 | 1 | $params = $class->getConstructor()->getParameters(); |
|
76 | |||
77 | 1 | $pass = array(); |
|
78 | |||
79 | // Iterate through all the parameters of the constructor and try to |
||
80 | // locate the value for each one in the serialized data |
||
81 | 1 | foreach ($params as $param) { |
|
82 | 1 | if (isset($data[$param->getName()])) { |
|
83 | 1 | $value = $data[$param->getName()]; |
|
84 | |||
85 | // If the serialized data contained a model's ID (and type), |
||
86 | // pass a new instance of it |
||
87 | 1 | if ($this->isModel($param)) { |
|
88 | 1 | if (!$param->getClass()->isAbstract()) { |
|
89 | 1 | $value = $param->getClass() |
|
90 | 1 | ->getMethod('get') |
|
91 | 1 | ->invoke(null, $value); |
|
92 | 1 | } else { |
|
93 | $id = $value['id']; |
||
94 | $type = $value['type']; |
||
95 | $value = call_user_func(array($type, 'get'), $id); |
||
96 | } |
||
97 | 1 | } |
|
98 | 1 | } else { |
|
99 | $value = null; |
||
100 | } |
||
101 | |||
102 | 1 | $pass[$param->getName()] = $value; |
|
103 | 1 | } |
|
104 | |||
105 | 1 | $class->getConstructor()->invokeArgs($this, $pass); |
|
106 | 1 | } |
|
107 | |||
108 | /** |
||
109 | * Find out if the specified parameter of the Event's constructor needs a |
||
110 | * Model |
||
111 | * |
||
112 | * @param \ReflectionParameter $param The constructor's parameter |
||
113 | * @return bool |
||
114 | */ |
||
115 | 1 | private static function isModel($param) |
|
133 | |||
134 | /** |
||
135 | * Send a notification to the players affected by this event |
||
136 | * |
||
137 | * @param string $type The type of the event |
||
138 | */ |
||
139 | public function notify($type) |
||
142 | |||
143 | /** |
||
144 | * Sends a notification to some players |
||
145 | * |
||
146 | * @param mixed $players A single player/ID or a player/ID list |
||
147 | * @param string $type The type of the event |
||
148 | * @param null|\Player|int $except A player who should not receive a notification |
||
149 | * @param \Player $except |
||
150 | */ |
||
151 | protected function doNotify($players, $type, $except = null) |
||
181 | } |
||
182 |