| 1 |
<?php
|
| 2 |
// $Id: context.module,v 1.5 2008/08/10 09:25:04 yhahn Exp $
|
| 3 |
|
| 4 |
define('CONTEXT_GET', 0);
|
| 5 |
define('CONTEXT_SET', 1);
|
| 6 |
define('CONTEXT_ISSET', 2);
|
| 7 |
define('CONTEXT_CLEAR', 3);
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Master context function. Avoid calling this directly -- use one of the helper functions below.
|
| 11 |
*
|
| 12 |
* @param $op
|
| 13 |
* The operation to perform - handled by the context helper functions. Use them.
|
| 14 |
* @param $namespace
|
| 15 |
* A string to be used as the namespace for the context information.
|
| 16 |
* @param $attribute
|
| 17 |
* Usually a string to be used as a key to set/retrieve context information. An array can
|
| 18 |
* also be used when setting context to establish an entire context namespace at once.
|
| 19 |
* (At some point objects may also be accepted, but currently functionaliy isn't complete.)
|
| 20 |
* @param $value
|
| 21 |
* A value to set for the provided key. If omitted the value will be set to true.
|
| 22 |
*
|
| 23 |
* @return
|
| 24 |
* Either the requested value, or false if the operation fails.
|
| 25 |
*/
|
| 26 |
function context_context($op = CONTEXT_GET, $namespace = null, $attribute = null, $value = null) {
|
| 27 |
static $context;
|
| 28 |
$context = !$context ? array() : $context;
|
| 29 |
switch ($op) {
|
| 30 |
case CONTEXT_GET:
|
| 31 |
// return entire context
|
| 32 |
if (!$namespace) {
|
| 33 |
return $context;
|
| 34 |
}
|
| 35 |
// return entire space if set
|
| 36 |
else if (isset($context[(string) $namespace])) {
|
| 37 |
// return val of key from space
|
| 38 |
if (is_array($context[(string) $namespace]) && isset($context[(string) $namespace][(string) $attribute])) {
|
| 39 |
return $context[(string) $namespace][(string) $attribute];
|
| 40 |
}
|
| 41 |
elseif (!$attribute){
|
| 42 |
return $context[(string) $namespace];
|
| 43 |
}
|
| 44 |
}
|
| 45 |
break;
|
| 46 |
case CONTEXT_SET:
|
| 47 |
// bail if invalid space is specified or context is already set
|
| 48 |
if (is_string($namespace) || is_int($namespace)) {
|
| 49 |
// initialize namespace if no key is specified
|
| 50 |
if (!$attribute) {
|
| 51 |
$context[(string) $namespace] = array();
|
| 52 |
return true;
|
| 53 |
}
|
| 54 |
// set to true if key is a usable identifier. otherwise, allow a key or object to be inserted
|
| 55 |
if (!$value) {
|
| 56 |
if (is_string($attribute) || is_int($attribute)) {
|
| 57 |
$context[(string) $namespace][(string) $attribute] = true;
|
| 58 |
return true;
|
| 59 |
}
|
| 60 |
elseif (is_array($attribute) || is_object($attribute)) {
|
| 61 |
$context[(string) $namespace] = $attribute;
|
| 62 |
return true;
|
| 63 |
}
|
| 64 |
}
|
| 65 |
// set value if key is valid
|
| 66 |
if ((is_string($attribute) || is_int($attribute)) && $value) {
|
| 67 |
$context[$namespace][$attribute] = $value;
|
| 68 |
return true;
|
| 69 |
}
|
| 70 |
}
|
| 71 |
break;
|
| 72 |
case CONTEXT_ISSET:
|
| 73 |
// return entire context
|
| 74 |
if (!$namespace) return false;
|
| 75 |
if (!$attribute) {
|
| 76 |
// return entire space if set
|
| 77 |
return isset($context[$namespace]);
|
| 78 |
}
|
| 79 |
// return val of key from space
|
| 80 |
return isset($context[$namespace][$attribute]);
|
| 81 |
case CONTEXT_CLEAR:
|
| 82 |
$context = array();
|
| 83 |
return true;
|
| 84 |
}
|
| 85 |
return false;
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Sets a context by namespace + attribute.
|
| 90 |
*/
|
| 91 |
function context_set($namespace, $attribute = null, $value = null) {
|
| 92 |
return context_context(CONTEXT_SET, $namespace, $attribute, $value);
|
| 93 |
}
|
| 94 |
|
| 95 |
/**
|
| 96 |
* Retrieves a context by namespace + (optional) attribute.
|
| 97 |
*/
|
| 98 |
function context_get($namespace = null, $attribute = null) {
|
| 99 |
return context_context(CONTEXT_GET, $namespace, $attribute, null);
|
| 100 |
}
|
| 101 |
|
| 102 |
/**
|
| 103 |
* Returns a boolean for whether a context namespace + attribute have been set.
|
| 104 |
*/
|
| 105 |
function context_isset($namespace = null, $attribute = null) {
|
| 106 |
return context_context(CONTEXT_ISSET, $namespace, $attribute, null);
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Deprecated context_exists() function. Retained for backwards
|
| 111 |
* compatibility -- please use context_isset() instead.
|
| 112 |
*/
|
| 113 |
function context_exists($namespace = null, $attribute = null) {
|
| 114 |
return context_context(CONTEXT_ISSET, $namespace, $attribute, null);
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Clears static context array() -- meant only for testing
|
| 119 |
*/
|
| 120 |
function context_clear() {
|
| 121 |
return context_context(CONTEXT_CLEAR);
|
| 122 |
}
|