Context.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * @type array
  14. */
  15. private $_storage = array();
  16. /**
  17. * Registers a variable into the context.
  18. * @param string $name String name
  19. * @param mixed $ref Reference to variable to be registered
  20. */
  21. public function register($name, &$ref)
  22. {
  23. if (array_key_exists($name, $this->_storage)) {
  24. trigger_error(
  25. "Name $name produces collision, cannot re-register",
  26. E_USER_ERROR
  27. );
  28. return;
  29. }
  30. $this->_storage[$name] =& $ref;
  31. }
  32. /**
  33. * Retrieves a variable reference from the context.
  34. * @param string $name String name
  35. * @param bool $ignore_error Boolean whether or not to ignore error
  36. * @return mixed
  37. */
  38. public function &get($name, $ignore_error = false)
  39. {
  40. if (!array_key_exists($name, $this->_storage)) {
  41. if (!$ignore_error) {
  42. trigger_error(
  43. "Attempted to retrieve non-existent variable $name",
  44. E_USER_ERROR
  45. );
  46. }
  47. $var = null; // so we can return by reference
  48. return $var;
  49. }
  50. return $this->_storage[$name];
  51. }
  52. /**
  53. * Destroys a variable in the context.
  54. * @param string $name String name
  55. */
  56. public function destroy($name)
  57. {
  58. if (!array_key_exists($name, $this->_storage)) {
  59. trigger_error(
  60. "Attempted to destroy non-existent variable $name",
  61. E_USER_ERROR
  62. );
  63. return;
  64. }
  65. unset($this->_storage[$name]);
  66. }
  67. /**
  68. * Checks whether or not the variable exists.
  69. * @param string $name String name
  70. * @return bool
  71. */
  72. public function exists($name)
  73. {
  74. return array_key_exists($name, $this->_storage);
  75. }
  76. /**
  77. * Loads a series of variables from an associative array
  78. * @param array $context_array Assoc array of variables to load
  79. */
  80. public function loadArray($context_array)
  81. {
  82. foreach ($context_array as $key => $discard) {
  83. $this->register($key, $context_array[$key]);
  84. }
  85. }
  86. }
  87. // vim: et sw=4 sts=4