Memory
[
class tree: Memory
] [
index: Memory
] [
all elements
]
Packages:
Analyzer
Base
Cache
Config
Container
Creator
Log
Memory
Pattern
Registry
Stat
Word
Source for file Memory.php
Documentation is available at
Memory.php
<?php
/**
* Mai_Memory
*
* Copyright (C) 2009 Mai (Me Artificial Intelligence)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
@author
Wouter Bulten (wouterbulten@mai-ai.org)
*
@copyright
Copyright (C) 2009 Mai (Me Artificial Intelligence)
*
@license
http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
*
@link
http://www.launchpad.net/mai
*
*
@category
Mai
*
@package
Memory
*
*/
/** Required files */
if
(
!
Mai
::
getAutoloader
(
))
{
//Zend_Session_Namespace is needed for storage
require_once
'Zend/Session/Namespace.php'
;
}
/**
* Mai_Memory, the memory of Mai (session based).
*
*
@author
Wouter Bulten (wouterbulten@mai-ai.org)
*
@copyright
Copyright (C) 2009 Mai (Me Artificial Intelligence)
*
@license
http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
*
@link
http://www.launchpad.net/mai
*
*
@category
Mai
*
@package
Memory
*
*/
class
Mai_Memory
{
/**
*
@var
Mai_Memory
Singleton instance
*/
protected
static
$_instance
;
/**
*
@var
Zend_Session_Namespace
*/
protected
$_memory
;
/**
*
@var
array
Items that can be stored in $_memory.
*/
protected
$_memoryItems
=
array
(
'unknown-words'
)
;
/**
* Private constructor to prevent creation of this object.
* Store instance of Zend_Session_Namespace
*
@return
void
*/
private
function
__construct
(
)
{
//Create a new namespace and prevent multiple instances
self
::
$_memory
=
new
Zend_Session_Namespace
(
'mai-memory'
,
true
)
;
//XSS prevention
if
(
!
isset
(
self
::
$_memory
->
initialized
))
{
Zend_Session
::
regenerateId
(
)
;
}
self
::
$_memory
->
initialized
=
true
;
}
/**
* Retrieve singleton instance
*
*
@return
Mai_Memory
*/
public
static
function
getInstance
(
)
{
if
(
!
Mai
::
isInit
(
))
{
trigger_error
(
'Mai has not been setup yet. Please use Mai::init().'
,
E_USER_ERROR
)
;
}
if
(
null
===
self
::
$_instance
)
{
self
::
$_instance
=
new
self
(
)
;
}
return
self
::
$_instance
;
}
/**
* Set a value in the memory.
*
@param
string
$var
*
@param
mixed
$value
*/
public
function
__set
(
$var
,
$value
)
{
if
(
!
in_array
(
$var
,
$this
->
_memoryItems
))
{
throw
new
Exception
(
"Variable with name "
.
$var
.
" may not be stored in Mai_Memory."
)
;
}
//Store in memory
$this
->
_memoryItems
->
$var
=
$value
;
}
/**
* Get a variable from the memory.
*
@param
string
$var
*
@return
mixed
*/
public
function
__get
(
$var
)
{
if
(
isset
(
$this
->
_memory
->
$var
))
{
return
$this
->
_memory
->
$var
;
}
else
{
throw
new
Exception
(
"Variable with name "
.
$var
.
" does not excist in Mai_Memory."
)
;
}
}
/**
* Cloning of a singleton class is not allowed.
*
@return
void
*/
final
public
function
__clone
(
)
{
trigger_error
(
'Clone is not allowed.'
,
E_USER_ERROR
)
;
}
/**
* Function to prevent deserializing
*
@return
void
*/
public
function
__wakeup
(
)
{
trigger_error
(
'Deserializing is not allowed.'
,
E_USER_ERROR
)
;
}
}
Documentation generated on Mon, 27 Jul 2009 19:55:10 +0200 by
phpDocumentor 1.4.1