Registry.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Basic application registry class;
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. class Staple_Registry
  25. {
  26. protected static $store = array();
  27. /**
  28. * Returns a booleon if the specified key exists in the store.
  29. * @param string $key
  30. * @return bool
  31. */
  32. public static function isValid($key)
  33. {
  34. if(!array_key_exists('Staple', $_SESSION))
  35. {
  36. $_SESSION['Staple'] = array();
  37. }
  38. if(!array_key_exists('Registry', $_SESSION['Staple']))
  39. {
  40. $_SESSION['Staple']['Registry'] = array();
  41. }
  42. if(array_key_exists($key, $_SESSION['Staple']['Registry']))
  43. {
  44. self::$store[$key] = $_SESSION['Staple']['Registry'][$key];
  45. return true;
  46. }
  47. return array_key_exists($key, self::$store);
  48. }
  49. /**
  50. * Store a value or object in the registry.
  51. * @param string $key
  52. * @return multitype:|NULL
  53. */
  54. public static function get($key)
  55. {
  56. if(!array_key_exists('Staple', $_SESSION))
  57. {
  58. $_SESSION['Staple'] = array();
  59. }
  60. if(!array_key_exists('Registry', $_SESSION['Staple']))
  61. {
  62. $_SESSION['Staple']['Registry'] = array();
  63. }
  64. if(array_key_exists($key, self::$store))
  65. return self::$store[$key];
  66. elseif(array_key_exists($key, $_SESSION['Staple']['Registry']))
  67. {
  68. self::$store[$key] = $_SESSION['Staple']['Registry'][$key];
  69. return $_SESSION['Staple']['Registry'][$key];
  70. }
  71. else
  72. return NULL;
  73. }
  74. /**
  75. * Store a value or object in the registry.
  76. * @param string $key
  77. * @param mixed $obj
  78. * @param bool $storeInSession
  79. */
  80. public static function set($key, $obj, $storeInSession = true)
  81. {
  82. if(!array_key_exists('Staple', $_SESSION))
  83. {
  84. $_SESSION['Staple'] = array();
  85. }
  86. if(!array_key_exists('Registry', $_SESSION['Staple']))
  87. {
  88. $_SESSION['Staple']['Registry'] = array();
  89. }
  90. //We can't store resources in the session.
  91. if(!is_resource($obj) && $storeInSession === true)
  92. {
  93. if(array_key_exists($key, $_SESSION['Staple']['Registry']))
  94. {
  95. if($_SESSION['Staple']['Registry'][$key] !== $obj)
  96. {
  97. $_SESSION['Staple']['Registry'][$key] = $obj;
  98. }
  99. }
  100. else
  101. {
  102. $_SESSION['Staple']['Registry'][$key] = $obj;
  103. }
  104. }
  105. //Store the value locally as well.
  106. self::$store[$key] = $obj;
  107. }
  108. public function __clone()
  109. {
  110. throw new Exception('Clone is not allowed.');
  111. }
  112. }
  113. ?>