Fix problem where stacked AttrTransforms clobber each other.
[htmlpurifier.git] / library / HTMLPurifier / Context.php
blob9ddf0c5476a186d9bb694dee319e674f79ad868c
1 <?php
3 /**
4 * Registry object that contains information about the current context.
5 * @warning Is a bit buggy when variables are set to null: it thinks
6 * they don't exist! So use false instead, please.
7 * @note Since the variables Context deals with may not be objects,
8 * references are very important here! Do not remove!
9 */
10 class HTMLPurifier_Context
13 /**
14 * Private array that stores the references.
16 private $_storage = array();
18 /**
19 * Registers a variable into the context.
20 * @param $name String name
21 * @param $ref Reference to variable to be registered
23 public function register($name, &$ref) {
24 if (isset($this->_storage[$name])) {
25 trigger_error("Name $name produces collision, cannot re-register",
26 E_USER_ERROR);
27 return;
29 $this->_storage[$name] =& $ref;
32 /**
33 * Retrieves a variable reference from the context.
34 * @param $name String name
35 * @param $ignore_error Boolean whether or not to ignore error
37 public function &get($name, $ignore_error = false) {
38 if (!isset($this->_storage[$name])) {
39 if (!$ignore_error) {
40 trigger_error("Attempted to retrieve non-existent variable $name",
41 E_USER_ERROR);
43 $var = null; // so we can return by reference
44 return $var;
46 return $this->_storage[$name];
49 /**
50 * Destorys a variable in the context.
51 * @param $name String name
53 public function destroy($name) {
54 if (!isset($this->_storage[$name])) {
55 trigger_error("Attempted to destroy non-existent variable $name",
56 E_USER_ERROR);
57 return;
59 unset($this->_storage[$name]);
62 /**
63 * Checks whether or not the variable exists.
64 * @param $name String name
66 public function exists($name) {
67 return isset($this->_storage[$name]);
70 /**
71 * Loads a series of variables from an associative array
72 * @param $context_array Assoc array of variables to load
74 public function loadArray($context_array) {
75 foreach ($context_array as $key => $discard) {
76 $this->register($key, $context_array[$key]);
82 // vim: et sw=4 sts=4