fork download
  1. <?php
  2.  
  3. final class Registry {
  4. private $registry;
  5.  
  6. /**
  7.   * Class constructor
  8.   */
  9. private function __construct() {
  10. $this->registry = array();
  11. }
  12.  
  13. /**
  14.   * Adds an element to the registry
  15.   *
  16.   * @param string location in the registry, in which to store the value
  17.   * @param mixed value to be stored in the registry
  18.   * @param boolean specifies whether the existing element should be overwritten or not
  19.   * @return boolean TRUE on success, FALSE otherwise
  20.   */
  21. public function addValue($index, $value, $overwrite = false) {
  22. if($this->isRegistered($index) && !$overwrite) {
  23. return false;
  24. }
  25. $this->registry[$index] = $value;
  26. return true;
  27. }
  28.  
  29. /**
  30.   * Adds multiple elements to the registry
  31.   *
  32.   * @param array associative array containing elements to add to the registry
  33.   * @param boolean specified whether the existing elements should be overwritten or not
  34.   * @return boolean TRUE on success, FALSE otherwise
  35.   */
  36. public function addValues($values, $overwrite = false) {
  37. if(!is_array($values)) {
  38. return false;
  39. }
  40. foreach($values as $index => $value) {
  41. if(!$this->addValue($index, $value, $overwrite)) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47.  
  48. /**
  49.   * Flushes (removes all elements from) the registry
  50.   *
  51.   * @return void does not return any value
  52.   */
  53. public function flushRegistry() {
  54. $this->registry = array();
  55. }
  56.  
  57. /**
  58.   * Returns the global QRegistry object, creating it only if it does not exist already
  59.   *
  60.   * @param string instance name
  61.   * @return object the QRegistry object
  62.   */
  63. public static function getInstance($name = 'main') {
  64. static $instance;
  65. if(!isset($instance[$name])) {
  66. $instance[$name] = new Registry();
  67. }
  68. return $instance[$name];
  69. }
  70.  
  71. /**
  72.   * Returns all elements stored in registry
  73.   *
  74.   * @return array associative array containing all registered values
  75.   */
  76. public function getRegistry() {
  77. return $this->registry;
  78. }
  79.  
  80. /**
  81.   * Retrieves a value from the registry
  82.   *
  83.   * @param string location in the registry, in which the value is stored
  84.   * @param mixed the default value to return when element is not found
  85.   * @return mixed the value of requested item
  86.   */
  87. public function getValue($index, $default = NULL) {
  88. if(!$this->isRegistered($index)) {
  89. return $default;
  90. }
  91. return $this->registry[$index];
  92. }
  93.  
  94. /**
  95.   * Retrieves multiple values from the registry
  96.   *
  97.   * @param array array containing the names of the requested elements
  98.   * @param mixed the default value to return when element is not found
  99.   * @return array associative array containing values of the requested items
  100.   */
  101. public function getValues($indexes, $default = NULL) {
  102. $results = array();
  103. if(!is_array($indexes)) {
  104. return $results;
  105. }
  106. foreach($indexes as $index) {
  107. $results[$index] = $this->getValue($index, $default);
  108. }
  109. return $results;
  110. }
  111.  
  112. /**
  113.   * Checks if container contains an item with the specified index
  114.   *
  115.   * @param string location in the registry
  116.   * @return boolean TRUE if index is a named value in the registry, FALSE otherwise
  117.   */
  118. public function isRegistered($index) {
  119. return array_key_exists($index, $this->registry);
  120. }
  121.  
  122. /**
  123.   * Removes given registered element
  124.   *
  125.   * @param string location in the registry
  126.   * @return boolean TRUE on success, FALSE otherwise
  127.   */
  128. public function removeValue($index) {
  129. if(!$this->isRegistered($index)) {
  130. return false;
  131. }
  132. unset($this->registry[$index]);
  133. return true;
  134. }
  135.  
  136. /**
  137.   * Removes multiple given registered elements
  138.   *
  139.   * @param array array containing the names of the requested elements
  140.   * @return boolean TRUE on success, FALSE otherwise
  141.   */
  142. public function removeValues($indexes) {
  143. if(!is_array($indexes)) {
  144. return false;
  145. }
  146. foreach($indexes as $index) {
  147. if(!$this->removeValue($index)) {
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153.  
  154. }
  155.  
  156. $rega = Registry::getInstance();
  157. $regb = Registry::getInstance();
  158. var_dump($rega === $regb);
  159. $rega->addValue(0,'test');
  160. var_dump($rega->getRegistry(), $regb->getRegistry());
Success #stdin #stdout 0.02s 52480KB
stdin
Standard input is empty
stdout
bool(true)
array(1) {
  [0]=>
  string(4) "test"
}
array(1) {
  [0]=>
  string(4) "test"
}