forked from luka8088/phops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.php
More file actions
53 lines (41 loc) · 1.33 KB
/
context.php
File metadata and controls
53 lines (41 loc) · 1.33 KB
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
<?php
/*
* This software is the property of its authors.
* See the copyright.txt file for more details.
*
*/
class context {
static $stacks = array();
static function exists ($type) {
return hasMember(self::$stacks, $type) && count(self::$stacks[$type]) > 0;
}
static function current ($type) {
version_assert and assertTrue(self::exists($type));
return end(self::$stacks[$type]);
}
static function __callStatic ($name, $arguments) {
version_assert and assertTrue(count($arguments) == 0);
return self::current($name);
}
static function enter ($type, $value) {
if (!hasMember(self::$stacks, $type))
self::$stacks[$type] = array();
// having context depth of 128 is most likely a memory leak bug
version_assert and assertTrue(count(self::$stacks[$type]) < 128, "Context too deep");
array_push(self::$stacks[$type], $value);
}
static function leave ($type) {
version_assert and assertTrue(self::exists($type));
array_pop(self::$stacks[$type]);
}
static function invoke ($type, $value, $callback) {
self::enter($type, $value);
try { $result = call_user_func($callback); }
// catch safeException only?
catch (Exception $e) {}
self::leave($type);
if (isset($e))
throw $e;
return $result;
}
}