1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
/** |
3
|
|
|
* Jam Event acts as a manager for all events bound to a model |
4
|
|
|
* |
5
|
|
|
* The standard events are documented in the guide. Binding and |
6
|
|
|
* triggering custom events is entirely possible. |
7
|
|
|
* |
8
|
|
|
* @package Jam |
9
|
|
|
* @category Events |
10
|
|
|
* @author Ivan Kerin |
11
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
12
|
|
|
* @license http://www.opensource.org/licenses/isc-license.txt |
13
|
|
|
*/ |
14
|
|
|
abstract class Kohana_Jam_Event { |
15
|
|
|
|
16
|
|
|
const ATTRIBUTE_PRIORITY = 20; |
17
|
|
|
const BEHAVIOR_PRIORITY = 10; |
18
|
|
|
|
19
|
|
|
protected $serial = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array The current model |
23
|
|
|
*/ |
24
|
|
|
protected $_model = NULL; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array Bound events |
28
|
|
|
*/ |
29
|
|
|
protected $_events = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $model |
35
|
|
|
*/ |
36
|
163 |
|
public function __construct($model) |
37
|
|
|
{ |
38
|
163 |
|
$this->_model = $model; |
|
|
|
|
39
|
163 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Binds an event. |
43
|
|
|
* |
44
|
|
|
* @param string $event |
45
|
|
|
* @param callable $callback |
46
|
|
|
* @return Jam_Event |
47
|
|
|
*/ |
48
|
155 |
|
public function bind($event, $callback, $priority = 0) |
49
|
|
|
{ |
50
|
155 |
|
if ( ! isset($this->_events[$event])) |
51
|
|
|
{ |
52
|
148 |
|
$this->_events[$event] = new SplPriorityQueue(); |
53
|
|
|
} |
54
|
|
|
|
55
|
155 |
|
$this->_events[$event]->insert($callback, $priority*100000 + $this->serial--); |
56
|
|
|
|
57
|
155 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
155 |
|
public function discover_events($from, $priority = 0) |
61
|
|
|
{ |
62
|
155 |
|
foreach (get_class_methods($from) as $method) |
63
|
|
|
{ |
64
|
155 |
|
if (($ns = substr($method, 0, 5)) === 'model' |
65
|
155 |
|
OR ($ns = substr($method, 0, 4)) === 'meta' |
66
|
155 |
|
OR ($ns = substr($method, 0, 7)) === 'builder') |
67
|
|
|
{ |
68
|
155 |
|
$this->bind(strtolower($ns.'.'.substr($method, strlen($ns) + 1)), array($from, $method), $priority); |
69
|
|
|
} |
70
|
|
|
} |
71
|
155 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Triggers an event. |
75
|
|
|
* |
76
|
|
|
* @param string $event |
77
|
|
|
* @param mixed $sender |
78
|
|
|
* @param mixed $params... |
|
|
|
|
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
530 |
|
public function trigger($event, $sender, $params = array()) |
82
|
|
|
{ |
83
|
530 |
|
if ( ! empty($this->_events[$event])) |
84
|
|
|
{ |
85
|
65 |
|
$data = new Jam_Event_Data(array( |
86
|
65 |
|
'event' => $event, |
87
|
65 |
|
'sender' => $sender, |
88
|
65 |
|
'args' => $params, |
89
|
|
|
)); |
90
|
|
|
|
91
|
|
|
// Create the params to be passed to the callback |
92
|
|
|
// Sender, Params and Event, then params passed from the trigger |
93
|
65 |
|
array_unshift($params, $data); |
94
|
65 |
|
array_unshift($params, $sender); |
95
|
|
|
|
96
|
65 |
|
$queue = clone $this->_events[$event]; |
97
|
|
|
|
98
|
65 |
|
foreach ($queue as $callback) |
99
|
|
|
{ |
100
|
65 |
|
call_user_func_array($callback, $params); |
101
|
|
|
|
102
|
55 |
|
if ($data->stop) |
103
|
|
|
{ |
104
|
55 |
|
break; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
55 |
|
return $data->return; |
109
|
|
|
} |
110
|
|
|
|
111
|
529 |
|
return NULL; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Trigger a callback, if there are no callbacks found, throws an exception |
116
|
|
|
* |
117
|
|
|
* @param string $type 'model', 'builder' or 'meta' |
118
|
|
|
* @param mixed $sender Jam_Model, Jam_Builder or Jam_Meta |
119
|
|
|
* @param string $method |
120
|
|
|
* @param array $params passed to the method |
121
|
|
|
* @return mixed returns the response from the callback |
122
|
|
|
* @throws Jam_Exception_Methodmissing If no method is found |
123
|
|
|
*/ |
124
|
53 |
|
public function trigger_callback($type, $sender, $method, $params) |
125
|
|
|
{ |
126
|
53 |
|
$event = strtolower("{$type}.call_{$method}"); |
127
|
|
|
|
128
|
53 |
|
if (empty($this->_events[$event])) |
129
|
|
|
throw new Jam_Exception_Methodmissing($sender, $method, $params); |
130
|
|
|
|
131
|
53 |
|
return $this->trigger($event, $sender, $params); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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..