Context.php 2.3 KB

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