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

Source for file Memory.php

Documentation is available at Memory.php

  1. <?php
  2. /**
  3.  * Mai_Memory
  4.  * 
  5.  * Copyright (C) 2009  Mai (Me Artificial Intelligence)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  * 
  20.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  21.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  22.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  23.  * @link http://www.launchpad.net/mai
  24.  * 
  25.  * @category Mai
  26.  * @package Memory
  27.  * 
  28.  */
  29.  
  30. /** Required files */
  31. {
  32.     //Zend_Session_Namespace is needed for storage
  33.     require_once 'Zend/Session/Namespace.php';
  34. }
  35.  
  36. /**
  37.  * Mai_Memory, the memory of Mai (session based).
  38.  * 
  39.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  40.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  41.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  42.  * @link http://www.launchpad.net/mai
  43.  * 
  44.  * @category Mai
  45.  * @package Memory
  46.  * 
  47.  */
  48.  
  49. class Mai_Memory
  50. {
  51.     /**
  52.      * @var Mai_Memory Singleton instance
  53.      */
  54.     protected static $_instance;
  55.  
  56.     /**
  57.      * @var Zend_Session_Namespace 
  58.      */
  59.     protected $_memory;
  60.  
  61.     /**
  62.      * @var array Items that can be stored in $_memory.
  63.      */
  64.     protected $_memoryItems = array(
  65.         'unknown-words'
  66.     );
  67.  
  68.     /**
  69.      * Private constructor to prevent creation of this object.
  70.      * Store instance of Zend_Session_Namespace
  71.      * @return void 
  72.      */
  73.     private function __construct()
  74.     {
  75.         //Create a new namespace and prevent multiple instances
  76.         self::$_memory new Zend_Session_Namespace('mai-memory'true);
  77.  
  78.         //XSS prevention
  79.         if (!isset(self::$_memory->initialized))
  80.         {
  81.             Zend_Session::regenerateId();
  82.         }
  83.         self::$_memory->initialized true;
  84.  
  85.     }
  86.  
  87.     /**
  88.      * Retrieve singleton instance
  89.      *
  90.      * @return Mai_Memory 
  91.      */
  92.     public static function getInstance()
  93.     {
  94.         if(!Mai::isInit())
  95.         {
  96.             trigger_error('Mai has not been setup yet. Please use Mai::init().'E_USER_ERROR);
  97.         }
  98.  
  99.         if (null === self::$_instance{
  100.             self::$_instance new self();
  101.         }
  102.         return self::$_instance;
  103.     }
  104.  
  105.     /**
  106.      * Set a value in the memory.
  107.      * @param string $var 
  108.      * @param mixed $value 
  109.      */
  110.     public function __set($var$value)
  111.     {
  112.         if(!in_array($var$this->_memoryItems))
  113.         {
  114.             throw new Exception("Variable with name ".$var." may not be stored in Mai_Memory.");
  115.         }
  116.  
  117.         //Store in memory
  118.         $this->_memoryItems->$var $value;
  119.     }
  120.  
  121.     /**
  122.      * Get a variable from the memory.
  123.      * @param string $var 
  124.      * @return mixed 
  125.      */
  126.     public function __get($var)
  127.     {
  128.         if(isset($this->_memory->$var))
  129.         {
  130.             return $this->_memory->$var;
  131.         }
  132.         else
  133.         {
  134.             throw new Exception("Variable with name ".$var." does not excist in Mai_Memory.");
  135.         }
  136.     }
  137.     
  138.     /**
  139.      * Cloning of a singleton class is not allowed.
  140.      * @return void 
  141.      */
  142.     final public function __clone()
  143.     {
  144.         trigger_error('Clone is not allowed.'E_USER_ERROR);
  145.     }
  146.  
  147.     /**
  148.      * Function to prevent deserializing
  149.      * @return void 
  150.      */
  151.     public function __wakeup()
  152.     {
  153.         trigger_error('Deserializing is not allowed.'E_USER_ERROR);
  154.     }
  155. }

Documentation generated on Mon, 27 Jul 2009 19:55:10 +0200 by phpDocumentor 1.4.1