-
Notifications
You must be signed in to change notification settings - Fork 0
/
butler.module
84 lines (74 loc) · 1.95 KB
/
butler.module
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
<?php
// $Id$
class ButlerContext {
protected $cache = array();
protected $handlers;
protected $handler_objects = array();
function get($selector) {
// Just do a quick one-off caching to catch most cases.
if (isset($this->cache[$selector])) {
return $this->cache[$selector];
}
$parts = explode(':', $selector);
$ancestors = array();
for ($i = 0; $i <= count($parts); $i++) {
$current = array();
$children = array();
for ($j = 0; $j < count($parts); $j++) {
if ($j < $i) {
$current[] = $parts[$j];
}
else {
$children[] = $parts[$j];
}
}
$ancestors[implode(':', $current)] = $children;
}
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $ancestor => $children) {
if ($handler = $this->handler($ancestor)) {
$this->cache[$selector] = $handler->get($children);
return $this->cache[$selector];
}
}
}
function set($selector, $value) {
if (is_object($value)) {
$this->handlers[$selector] = TRUE;
$this->handler_objects[$selector] = $value;
}
else {
$this->cache[$selector] = $value;
}
}
protected function handler($selector) {
if (!isset($this->handlers)) {
$this->handlers = module_invoke_all('butler');
}
if (!isset($this->handlers[$selector])) {
return FALSE;
}
if (!isset($this->handler_objects[$selector])) {
$this->handler_objects[$selector] = new $this->handlers[$selector]();
}
return $this->handler_objects[$selector];
}
}
abstract class ButlerContextItem {
abstract function get($parts);
}
function butler_context() {
static $context;
if (!isset($context)) {
$context = new ButlerContext();
}
return $context;
}
function butler_butler() {
return array(
'http' => 'ButlerContextHTTP',
'conf' => 'ButlerContextConf',
'user' => 'ButlerContextUser',
'node' => 'ButlerContextUser',
);
}