Application.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PEAR :: Cache |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Richard Heyes <richard@phpguru.org> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Application.php,v 1.5 2003/01/04 11:54:45 mj Exp $
  20. require_once 'Cache.php';
  21. // Application level variables
  22. //
  23. // Purpose
  24. // Variables that are persisent across all user sessions,
  25. // not just a specific user ala normal sessions.
  26. //
  27. // Usage:
  28. //
  29. // Example 1:
  30. //
  31. // $app =& new Cache_Application();
  32. // $_APP =& $app->getData();
  33. //
  34. // In this case the $_APP variable is akin to the $_SESSION variable.
  35. // If you add/remove stuff, it will be reflected in the next request
  36. // (of any user).
  37. //
  38. // Example 2:
  39. //
  40. // $foo = 'Some data';
  41. // $bar = 'Some other data';
  42. //
  43. // $app =& new Cache_Application();
  44. // $app->register('foo');
  45. // $app->register('bar', $bar);
  46. //
  47. // $foo = 'Different data';
  48. //
  49. // In this case the variables are registered with the register() function.
  50. // This is akin to session_register().
  51. //
  52. // As with session_register(), the contents of the variable at the *end* of the
  53. // request is registered and not at the point of registration. Therefore in this
  54. // example, for the $foo variable, the string 'Different data' is stored and not
  55. // 'Some data'. The exception to this rule is if you use the second argument to
  56. // register() as in the second call to it above. This will cause the data supplied
  57. // in the second argument to be stored and not the contents at the end of the request.
  58. //
  59. // Note: If you use this method with register_globals turned on, the variables will be
  60. // automatically globalled upon startup, (ie. when you create the object).
  61. //
  62. // Note: If you register a variable that is not set when the script finishes, it will
  63. // registered as NULL.
  64. //
  65. //
  66. // *** You are strongly recommended to use only one method of the two above. ***
  67. //
  68. // (In fact if you use the register() function with register_globals Off, you have to
  69. // use the $_APP method to get at the data).
  70. class Cache_Application extends Cache {
  71. var $data;
  72. var $id;
  73. var $group;
  74. var $registered_vars;
  75. /**
  76. * Constructor
  77. *
  78. * @param string Name of container class
  79. * @param array Array with container class options
  80. */
  81. function Cache_Application($container = 'file', $container_options = array('cache_dir' => '/tmp/', 'filename_prefix' => 'cache_'), $id = 'application_var', $group = 'application_cache')
  82. {
  83. $this->id = $id;
  84. $this->group = $group;
  85. $this->registered_vars = array();
  86. $this->Cache($container, $container_options);
  87. $this->data = $this->isCached($this->id, $this->group) ? unserialize($this->get($this->id, $this->group)) : array();
  88. // If register_globals on, global all registered variables
  89. if (ini_get('register_globals') AND is_array($this->data)) {
  90. foreach ($this->data as $key => $value) {
  91. global $$key;
  92. $$key = $value;
  93. }
  94. }
  95. }
  96. /**
  97. * Destructor
  98. *
  99. * Gets values of all registered variables and stores them. Then calls save() to
  100. * write data away.
  101. */
  102. function _Cache_Application()
  103. {
  104. // Get contents of all registered variables
  105. if (is_array($this->registered_vars) AND !empty($this->registered_vars)) {
  106. foreach ($this->registered_vars as $varname) {
  107. global $$varname;
  108. $this->data[$varname] = $$varname;
  109. }
  110. }
  111. // Save the data
  112. $this->save($this->id, serialize($this->data), 0, $this->group);
  113. }
  114. /**
  115. * register()
  116. *
  117. * Registers a variable to be stored.
  118. *
  119. * @param string Name of variable to register
  120. * @param mixed Optional data to store
  121. */
  122. function register($varname, $data = null)
  123. {
  124. if (isset($data)) {
  125. $this->data[$varname] = $data;
  126. } else {
  127. $this->registered_vars[] = $varname;
  128. }
  129. }
  130. /**
  131. * unregister()
  132. *
  133. * Unregisters a variable from being stored.
  134. *
  135. * @param string Name of variable to unregister
  136. */
  137. function unregister($varname)
  138. {
  139. if (isset($this->data[$varname])) {
  140. unset($this->data[$varname]);
  141. }
  142. }
  143. /**
  144. * clear()
  145. *
  146. * Removes all stored data
  147. */
  148. function clear()
  149. {
  150. $this->data = array();
  151. }
  152. /**
  153. * getData()
  154. *
  155. * Use this to get a reference to the data to manipulate
  156. * in calling script. Eg. $_APP =& $obj->getData();
  157. *
  158. * @return mixed A reference to the data
  159. */
  160. function &getData()
  161. {
  162. return $this->data;
  163. }
  164. }
  165. ?>