[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file vars_manager.class.php
Documentation is available at
vars_manager.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Vars_Manager
*
@package
tangra_lib
*
@subpackage
core
*/
/**
* Loading exception
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_var_already_exists.class.php'
)
;
/**
* Loading exception
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_var_not_exists.class.php'
)
;
/**
* Container for values that provides unified access.
*
* Class Vars_Manager and it's descendants are used to provide container for variables.
* Objects of class Vars_Manager are intended to be stored in $_SESSION that is way they have garbage_collector() - to clean up old data.
*
*
@see
Session_Vars_Manager, Thread_Vars_Manager
*
@package
tangra_lib
*
@subpackage
core
*/
class
Vars_Manager
extends
Tangra_Class
{
/**
* Storage for the values
*
*
@var
array
simple associative array
*/
private
$vars
=
array
(
)
;
/**
* Holds last access time for each variable
*
*
@var
array
*/
private
$atimes
=
array
(
)
;
/**
* Maximum life time for the variables. If (time() - $atime) > $gc_maxlifetime variable will be seen as garbage.
*
*
@var
unknown_type
*/
private
$gc_maxlifetime
;
// maximum time (in seconds) that var will "live" if not used
/**
* Constructor
*
*
@param
integer
$gc_maxlifetime
Maximum life time for variables after which will be seen as garbage.
*/
function
__construct
(
$gc_maxlifetime
=
1440
)
{
tangra_if_not_int_throw_e
(
$gc_maxlifetime
)
;
$this
->
gc_maxlifetime
=
$gc_maxlifetime
;
}
/**
* Registers variable. You have first to register variable before using Vars_Manager::set_var()
*
*
@param
string
$var_name
*
@param
boolean
$expires
- if false variable will be never seen as garbage. Default is true.
*/
public
function
register_var
(
$var_name
,
$expires
=
false
)
{
$this
->
garbage_collector
(
)
;
if
(
!
array_key_exists
(
$var_name
,
$this
->
vars
))
{
$this
->
vars
[
$var_name
]
=
NULL
;
if
(
$expires
)
{
$this
->
atimes
[
$var_name
]
=
time
(
)
;
}
else
{
// 0 means - never expire
$this
->
atimes
[
$var_name
]
=
0
;
}
}
}
/**
* Adds new variable. Shortcut for register_var() and set_var()
*
*
@param
string
$var_name
*
@param
any
$value
must be a variable not reference. Use temporary variables if needed.
*/
public
function
add_var
(
$var_name
,
&
$value
)
{
$this
->
register_var
(
$var_name
)
;
$this
->
set_var
(
$var_name
,
$value
)
;
}
/**
* Sets variable.
* Variable must be already registered - if not - throws exception.
*
*
@param
string
$var_name
*
@param
any
$value
*
@throws
TE_Var_Not_Exists
*/
public
function
set_var
(
$var_name
,
&
$value
)
{
$this
->
garbage_collector
(
)
;
if
(
array_key_exists
(
$var_name
,
$this
->
vars
))
{
$this
->
vars
[
$var_name
]
=
&
$value
;
if
(
$this
->
atimes
[
$var_name
]
)
{
$this
->
atimes
[
$var_name
]
=
time
(
)
;
}
}
else
{
throw
new
TE_Var_Not_Exists
(
$var_name
)
;
}
}
/**
* Gets variable as reference. Use this function if you intend to change the value.
* If variable is not found - throws exception.
*
*
@param
string
$var_name
*
@return
any
rvalue is eturned as reference
*
@throws
TE_Var_Not_Exists
*/
public
function
&
get_var
(
$var_name
)
{
$this
->
garbage_collector
(
)
;
if
(
array_key_exists
(
$var_name
,
$this
->
vars
))
{
$ret
=
&
$this
->
vars
[
$var_name
]
;
if
(
$this
->
atimes
[
$var_name
]
)
{
$this
->
atimes
[
$var_name
]
=
time
(
)
;
}
return
$ret
;
}
else
{
throw
new
TE_Var_Not_Exists
(
$var_name
)
;
}
}
/**
* Query variable. Similar to
{@link get_var}
() but returns copy of the variable, not reference. Use this function instead of get_var() if you are not intending to change the value.
* If variable is not found - throws exception.
*
@param
string
$var_name
*
@return
any
*
@throws
TE_Var_Not_Exists
*/
public
function
query_var
(
$var_name
)
{
$this
->
garbage_collector
(
)
;
if
(
array_key_exists
(
$var_name
,
$this
->
vars
))
{
$ret
=
$this
->
vars
[
$var_name
]
;
if
(
$this
->
atimes
[
$var_name
]
)
{
$this
->
atimes
[
$var_name
]
=
time
(
)
;
}
return
$ret
;
}
else
{
throw
new
TE_Var_Not_Exists
(
$var_name
)
;
}
}
/**
* Removes/unregisters variable.
* If variable is not found - throws exception.
*
@param
string
$var_name
*
@throws
TE_Var_Not_Exists
*/
public
function
remove_var
(
$var_name
)
{
if
(
array_key_exists
(
$var_name
,
$this
->
vars
))
{
unset
(
$this
->
vars
[
$var_name
]
)
;
unset
(
$this
->
atimes
[
$var_name
]
)
;
}
else
{
throw
new
TE_Var_Not_Exists
(
$var_name
)
;
}
}
/**
* Checks if variable with name provided by $var_name exists.
*
*
@param
string
$var_name
*
@return
boolean
*/
public
function
is_var_registered
(
$var_name
)
{
$this
->
garbage_collector
(
)
;
return
array_key_exists
(
$var_name
,
$this
->
vars
)
;
}
/**
* Collects the garbage.
* Normally this function is not called directly by the user.
*
*/
public
function
garbage_collector
(
)
{
$time
=
time
(
)
;
foreach
(
$this
->
atimes
as
$key
=>
$value
)
{
if
(
$value
)
{
//procces only expirable, e.g. <> 0
if
((
$value
+
$this
->
gc_maxlifetime
)
<
$time
)
{
$this
->
remove_var
(
$key
)
;
}
}
}
}
}