bundel-acties
Code (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
-------------------------------------------------------
ACTION CLUSTER
-------------------------------------------------------
E.J. Klein Ovink <[email protected]>
http://www.sparkle6.com
Copyright (c) 2007, All rights reserved.
-------------------------------------------------------
- I don't need a link or anything, just keep the stuff above in tact
// DESCRIPTION
This class allows you to create clusters of actions. It is a memory you can store actions in, so they can
be launched all together at a specific moment.
If you load modules in the header of a content managament system wich contain functions you want to be launched later or
on another moment in the header this can be handy.
You can also store shutdown functions in a cluster and launch this cluster on shutdown, wich allows you to register and
unregister shutdown functions. This is not possible with php's standard functions.
For extra flexibility you can arrange the actions in different sequences to make sure the actions will be launched in the
prefered order.
Within the sequences the actions are launched in the order they are added to the action cluster.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Example for registering a shutdown function:
function foo($bar) {
print $bar;
}
$actions = new ActionCluster();
register_shutdown_function(array(&$actions, 'launch'));
$actions->add('foo' , "<!-- If you check your html source, you see this printed -->");
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
class ActionCluster {
var $actions, // (array) Actions storage
$sequences, // (array) Sequence storage
$seq, // (int) Default sequence
$id; // (int) The id counter
// Constructor
function ActionCluster() {
$this->initialize();
}
function initialize() {
$this->actions = array();
$this->sequences = array();
$this->seq = 1;
$this->id = 1;
}
/* Add actions to action storage
*
* This method can be used to add new actions to the current action storage. Everytime an action
* is added the id increases by 1
* An action is added by giving its name as first argument followed by the actions own parameters.
*
*/
function add() {
$parameters = func_get_args();
$this->saveSequence($this->seq);
$this->saveAction($parameters, $this->seq);
}
/* Add actions to a specified sequence
*
* Same as add() only this enables you to give a sequence as first parameter, followed by the action
* and its parameters.
*
*/
function addTo($seq) {
$parameters = func_get_args();
if( is_int($parameters[0]) )
$seq = array_shift($parameters); // The first argument can be used to set sequences
else
trigger_error("Given variable <code>\$seq = $seq</code> but should be an integer.");
$this->saveSequence($seq);
$this->saveAction($parameters, $seq);
}
/* Save sequence
*
* This function saves the sequence
* $seq = integer
*
*/
function saveSequence($seq) {
if( !isset($this->sequences[$seq]) )
$this->sequences[$seq] = array();
array_unshift($this->sequences[$seq], $this->id);
}
/* Save sequence
*
* This function saves the action and its parameters.
* $parameters = array , 0 => name , followed by parameters in the other keys
*
*/
function saveAction($parameters, $seq) {
$action = array_shift($parameters);
if( !is_callable($action) ) {
trigger_error("<code>$action()</code> is no valid callback action");
exit;
} else {
$this->actions[$this->id] = array('action' => $action, 'parameters' => $parameters, 'sequences' => $seq);
$this->id++;
}
}
/* Delete an action
*
* When a valid action id is passed as argument this function will delete that action
* from the action cluster.
*
* $id = integer
*
*/
function delete($id) {
$seq = $this->actions[$id]['sequences'];
if( in_array($id, $this->sequences[$seq]) ){
list( $key ) = array_keys( $this->sequences[$seq], $id );
unset($this->actions[$id]);
unset($this->sequences[$seq][$key]);
} else {
trigger_error("Id <code>$id</code> could not be found in sequence");
exit;
}
}
/* Launch all actions
*
* This method launches all the actions stored in the current action cluster. The sequence with the
* highest value first, and every action in a sequence in the order they are added.
*
*/
function launch() {
krsort($this->sequences);
foreach($this->sequences as $seq => $actions){
sort($this->sequences[$seq]);
foreach ( $this->sequences[$seq] as $seq => $id ) {
if( isset($this->actions[$id]['action']) )
call_user_func_array($this->actions[$id]['action'], $this->actions[$id]['parameters']);
else
trigger_error("Action where <code>\$id = $id</code> could not be launched.");
}
}
}
}
?>
/*
-------------------------------------------------------
ACTION CLUSTER
-------------------------------------------------------
E.J. Klein Ovink <[email protected]>
http://www.sparkle6.com
Copyright (c) 2007, All rights reserved.
-------------------------------------------------------
- I don't need a link or anything, just keep the stuff above in tact
// DESCRIPTION
This class allows you to create clusters of actions. It is a memory you can store actions in, so they can
be launched all together at a specific moment.
If you load modules in the header of a content managament system wich contain functions you want to be launched later or
on another moment in the header this can be handy.
You can also store shutdown functions in a cluster and launch this cluster on shutdown, wich allows you to register and
unregister shutdown functions. This is not possible with php's standard functions.
For extra flexibility you can arrange the actions in different sequences to make sure the actions will be launched in the
prefered order.
Within the sequences the actions are launched in the order they are added to the action cluster.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Example for registering a shutdown function:
function foo($bar) {
print $bar;
}
$actions = new ActionCluster();
register_shutdown_function(array(&$actions, 'launch'));
$actions->add('foo' , "<!-- If you check your html source, you see this printed -->");
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
class ActionCluster {
var $actions, // (array) Actions storage
$sequences, // (array) Sequence storage
$seq, // (int) Default sequence
$id; // (int) The id counter
// Constructor
function ActionCluster() {
$this->initialize();
}
function initialize() {
$this->actions = array();
$this->sequences = array();
$this->seq = 1;
$this->id = 1;
}
/* Add actions to action storage
*
* This method can be used to add new actions to the current action storage. Everytime an action
* is added the id increases by 1
* An action is added by giving its name as first argument followed by the actions own parameters.
*
*/
function add() {
$parameters = func_get_args();
$this->saveSequence($this->seq);
$this->saveAction($parameters, $this->seq);
}
/* Add actions to a specified sequence
*
* Same as add() only this enables you to give a sequence as first parameter, followed by the action
* and its parameters.
*
*/
function addTo($seq) {
$parameters = func_get_args();
if( is_int($parameters[0]) )
$seq = array_shift($parameters); // The first argument can be used to set sequences
else
trigger_error("Given variable <code>\$seq = $seq</code> but should be an integer.");
$this->saveSequence($seq);
$this->saveAction($parameters, $seq);
}
/* Save sequence
*
* This function saves the sequence
* $seq = integer
*
*/
function saveSequence($seq) {
if( !isset($this->sequences[$seq]) )
$this->sequences[$seq] = array();
array_unshift($this->sequences[$seq], $this->id);
}
/* Save sequence
*
* This function saves the action and its parameters.
* $parameters = array , 0 => name , followed by parameters in the other keys
*
*/
function saveAction($parameters, $seq) {
$action = array_shift($parameters);
if( !is_callable($action) ) {
trigger_error("<code>$action()</code> is no valid callback action");
exit;
} else {
$this->actions[$this->id] = array('action' => $action, 'parameters' => $parameters, 'sequences' => $seq);
$this->id++;
}
}
/* Delete an action
*
* When a valid action id is passed as argument this function will delete that action
* from the action cluster.
*
* $id = integer
*
*/
function delete($id) {
$seq = $this->actions[$id]['sequences'];
if( in_array($id, $this->sequences[$seq]) ){
list( $key ) = array_keys( $this->sequences[$seq], $id );
unset($this->actions[$id]);
unset($this->sequences[$seq][$key]);
} else {
trigger_error("Id <code>$id</code> could not be found in sequence");
exit;
}
}
/* Launch all actions
*
* This method launches all the actions stored in the current action cluster. The sequence with the
* highest value first, and every action in a sequence in the order they are added.
*
*/
function launch() {
krsort($this->sequences);
foreach($this->sequences as $seq => $actions){
sort($this->sequences[$seq]);
foreach ( $this->sequences[$seq] as $seq => $id ) {
if( isset($this->actions[$id]['action']) )
call_user_func_array($this->actions[$id]['action'], $this->actions[$id]['parameters']);
else
trigger_error("Action where <code>\$id = $id</code> could not be launched.");
}
}
}
}
?>