Cache
[ class tree: Cache ] [ index: Cache ] [ all elements ]

Source for file Application.php

Documentation is available at Application.php

  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 315102 2011-08-17 19:38:20Z cweiske $
  20.  
  21. require_once 'Cache.php';
  22.  
  23. // Application level variables
  24. //
  25. // Purpose
  26. // Variables that are persisent across all user sessions,
  27. // not just a specific user ala normal sessions.
  28. //
  29. // Usage:
  30. //
  31. // Example 1:
  32. //
  33. // $app  =& new Cache_Application();
  34. // $_APP =& $app->getData();
  35. //
  36. // In this case the $_APP variable is akin to the $_SESSION variable.
  37. // If you add/remove stuff, it will be reflected in the next request
  38. // (of any user).
  39. //
  40. // Example 2:
  41. //
  42. // $foo = 'Some data';
  43. // $bar = 'Some other data';
  44. //
  45. // $app =& new Cache_Application();
  46. // $app->register('foo');
  47. // $app->register('bar', $bar); 
  48. //
  49. // $foo = 'Different data';
  50. //
  51. // In this case the variables are registered with the register() function.
  52. // This is akin to session_register().
  53. //
  54. // As with session_register(), the contents of the variable at the *end* of the
  55. // request is registered and not at the point of registration. Therefore in this
  56. // example, for the $foo variable, the string 'Different data' is stored and not 
  57. // 'Some data'. The exception to this rule is if you use the second argument to
  58. // register() as in the second call to it above. This will cause the data supplied
  59. // in the second argument to be stored and not the contents at the end of the request.
  60. //
  61. // Note: If you use this method with register_globals turned on, the variables will be
  62. //       automatically globalled upon startup, (ie. when you create the object).
  63. //
  64. // Note: If you register a variable that is not set when the script finishes, it will
  65. //       registered as null.
  66. //
  67. //
  68. // *** You are strongly recommended to use only one method of the two above. ***
  69. //
  70. // (In fact if you use the register() function with register_globals Off, you have to
  71. //  use the $_APP method to get at the data).
  72.  
  73. class Cache_Application extends Cache
  74. {
  75.  
  76.     var $data;
  77.     var $id;
  78.     var $group;
  79.     var $registered_vars;
  80.  
  81.     /**
  82.     * Constructor
  83.     *
  84.     * @param    string  Name of container class
  85.     * @param    array   Array with container class options
  86.     */
  87.     function Cache_Application($container 'file'$container_options = array('cache_dir' => '/tmp/''filename_prefix' => 'cache_')$id 'application_var'$group 'application_cache')
  88.     {
  89.         $this->id    = $id;
  90.         $this->group = $group;
  91.         $this->registered_vars = array();
  92.  
  93.         $this->Cache($container$container_options);
  94.         $this->data = $this->isCached($this->id$this->groupunserialize($this->get($this->id$this->group)) : array();
  95.  
  96.         // If register_globals on, global all registered variables
  97.         if (ini_get('register_globals'&& is_array($this->data)) {
  98.             foreach ($this->data as $key => $value{
  99.                 global $$key;
  100.                 $$key $value;
  101.             }
  102.         }
  103.     }
  104.  
  105.     /**
  106.     * Destructor
  107.     *
  108.     * Gets values of all registered variables and stores them. Then calls save() to
  109.     * write data away.
  110.     */
  111.     function _Cache_Application()
  112.     {
  113.         // Get contents of all registered variables
  114.         if (is_array($this->registered_vars&& !empty($this->registered_vars)) {
  115.             foreach ($this->registered_vars as $varname{
  116.                 global $$varname;
  117.                 $this->data[$varname= $$varname;
  118.             }
  119.         }
  120.  
  121.         // Save the data
  122.         $this->save($this->idserialize($this->data)0$this->group);
  123.     }
  124.  
  125.     /**
  126.     * register()
  127.     *
  128.     * Registers a variable to be stored.
  129.     *
  130.     * @param    string  Name of variable to register
  131.     * @param    mixed   Optional data to store
  132.     */
  133.     function register($varname$data = null)
  134.     {
  135.         if (isset($data)) {
  136.             $this->data[$varname$data;
  137.         else {
  138.             $this->registered_vars[$varname;
  139.         }
  140.     }
  141.  
  142.     /**
  143.     * unregister()
  144.     *
  145.     * Unregisters a variable from being stored.
  146.     *
  147.     * @param    string  Name of variable to unregister
  148.     */
  149.     function unregister($varname)
  150.     {
  151.         if (isset($this->data[$varname])) {
  152.             unset($this->data[$varname]);
  153.         }
  154.     }
  155.  
  156.     /**
  157.     * clear()
  158.     *
  159.     * Removes all stored data
  160.     */
  161.     function clear()
  162.     {
  163.         $this->data = array();
  164.     }
  165.  
  166.     /**
  167.     * getData()
  168.     *
  169.     * Use this to get a reference to the data to manipulate
  170.     * in calling script. Eg. $_APP =& $obj->getData();
  171.     *
  172.     * @return mixed   A reference to the data
  173.     */
  174.     function &getData()
  175.     {
  176.         return $this->data;
  177.     }
  178. }
  179. ?>

Documentation generated on Mon, 11 Mar 2019 15:44:49 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.