-
Notifications
You must be signed in to change notification settings - Fork 1
/
cap.php
99 lines (92 loc) · 2.99 KB
/
cap.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
<?php
require_once('BFCollections/BFArray.php');
require_once('BFCollections/Box.php');
require_once('super_closure/SerializableClosure.php');
/**
* CAP class.
*
* The core of the CAP system with various operations common across CAP apps.
*/
class CAP {
// the current scope (i.e. form) of the CAP system
static $scope;
// the results of the current scope
static $results;
// all results
static $__results;
/**
* register function.
*
* Register the provided closure by serializing and placing it in the session.
*
* @static
* @param Closure $closure
* @param string $id (Default: NULL)
* @return Box[string] The boxed session id of the registered closure or a Failure indicating why the closure wasn't registered.
*/
static function register(Closure $closure, $id = NULL) {
if (is_null(self::$scope)) {
self::setScope(self::makeID());
}
$id = is_null($id) ? self::makeID() : $id;
try {
$sc = new SerializableClosure($closure);
$_SESSION[self::$scope][$id] = array('type' => 'closure', 'function' => $sc->serialize());
return new Full($id);
} catch (InvalidArgumentException $e) {
return new Failure('Parameter is not callable.', new Full($e));
} catch (RuntimeException $e) {
return new Failure('Cannot serialize closure via reflection.', new Full($e));
}
}
/**
* setScope function.
*
* A simple helper message for setting the current scope (i.e. form). Perhaps in the future this will do more.
*
* @static
* @param string $id
* @return void
*/
static function setScope($id) {
self::$scope = $id;
}
/**
* makeID function.
*
* Make a unique id. Used to associate form elements with (serialized) callback functions.
* @static
* @return string
*/
static function makeID() {
return uniqid();
}
}
// start our session and go through the data sent to us, seeing if we should call any of the registered callbacks
session_start();
CAP::$__results = new BFArray();
$k = 0;
foreach ($_REQUEST as $scope => $values) {
CAP::$results = new BFArray();
foreach ($values as $id => $value) {
if (array_key_exists($id, $_SESSION[$scope]) && $_SESSION[$scope][$id]['type'] == 'closure') {
$b = new SerializableClosure(function() {});
try {
$b->unserialize($_SESSION[$scope][$id]['function']);
// call the unserialized closure with the appropriate input
CAP::$__results[$scope][$id] = $b($value);
CAP::$results[$id] = CAP::$__results[$scope][$id];
} catch (Exception $e) {
}
// keep the session clean
unset($_SESSION[$scope][$id]);
$k++;
}
}
// keep the session clean
unset($_SESSION[$scope]);
}
unset($k);
CAP::$results = NULL;
CAP::$__results = NULL;
?>