1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Traits; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\DeferredEvent; |
5
|
|
|
use PHPDaemon\Exceptions\UndefinedEventCalledException; |
6
|
|
|
use PHPDaemon\Exceptions\UndefinedMethodCalled; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Deferred event handlers trait |
10
|
|
|
* @package PHPDaemon\Traits |
11
|
|
|
* @author Vasily Zorin <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
trait DeferredEventHandlers |
14
|
|
|
{ |
15
|
|
|
protected $DefEvHandlersUsed = false; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $event |
19
|
|
|
* @throws UndefinedEventCalledException |
20
|
|
|
* @return mixed |
21
|
|
|
*/ |
22
|
|
|
public function __get($event) |
23
|
|
|
{ |
24
|
|
|
if (!$this->DefEvHandlersUsed) { |
25
|
|
|
$this->DefEvHandlersUsed = true; |
26
|
|
|
$this->firstDeferredEventUsed(); |
27
|
|
|
} |
28
|
|
|
if (substr($event, 0, 2) !== 'on') { |
29
|
|
|
return $this->{$event}; |
30
|
|
|
} |
31
|
|
|
if (!method_exists($this, $event . 'Event')) { |
32
|
|
|
throw new \PHPDaemon\Exceptions\UndefinedEventCalled('Undefined event called: ' . get_class($this) . '->' . $event); |
33
|
|
|
} |
34
|
|
|
$e = new DeferredEvent($this->{$event . 'Event'}()); |
35
|
|
|
$e->name = $event; |
36
|
|
|
$e->parent = $this; |
|
|
|
|
37
|
|
|
$this->{$event} = &$e; |
38
|
|
|
return $e; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Called when first deferred event is used |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
protected function firstDeferredEventUsed() |
46
|
|
|
{ |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Cleans up events |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function cleanupDeferredEventHandlers() |
54
|
|
|
{ |
55
|
|
|
foreach ($this as $key => $property) { |
|
|
|
|
56
|
|
|
if ($property instanceof DeferredEvent) { |
57
|
|
|
$property->cleanup(); |
58
|
|
|
$this->{$key} = null; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $method Method name |
65
|
|
|
* @param array $args Arguments |
66
|
|
|
* @throws UndefinedMethodCalled |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function __call($method, $args) |
70
|
|
|
{ |
71
|
|
|
if (substr($method, 0, 2) !== 'on') { |
72
|
|
|
throw new UndefinedMethodCalled('Call to undefined method ' . get_class($this) . '->' . $method); |
73
|
|
|
} |
74
|
|
|
$o = $this->{$method}; |
75
|
|
|
if (!$o) { |
76
|
|
|
throw new UndefinedMethodCalled('Call to undefined method ' . get_class($this) . '->' . $method); |
77
|
|
|
} |
78
|
|
|
return $o(...$args); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..