class
BtstoreRoot
{
static
$root
;
}
class
BtstoreElement
implements
ArrayAccess, Iterator
{
private
$dataDir
;
private
$arrData
;
function
__construct(
$dataDir
,
$arrData
)
{
$this
->dataDir =
''
;
$this
->arrData =
array
();
if
(!
empty
(
$dataDir
) &&
is_dir
(
$dataDir
))
{
$this
->dataDir =
$dataDir
;
}
if
(!
empty
(
$arrData
))
{
$this
->arrData =
$arrData
;
}
}
function
__get(
$key
)
{
if
(isset (
$this
->arrData [
$key
] ))
{
$data
=
$this
->arrData [
$key
];
if
(
is_array
(
$data
) && !
is_object
(
$data
))
{
$data
=
new
BtstoreElement (
''
,
$data
);
}
return
$data
;
}
if
(!
empty
(
$this
->dataDir ))
{
$path
=
$this
->dataDir .
'/'
.
$key
;
if
(
is_dir
(
$path
))
{
$data
=
new
BtstoreElement (
$path
, null );
$this
->arrData [
$key
] =
$data
;
return
$data
;
}
if
(
is_file
(
$path
))
{
$content
=
file_get_contents
(
$path
);
$arrData
= unserialize (
$content
);
$data
=
new
BtstoreElement (
''
,
$arrData
);
$this
->arrData [
$key
] =
$data
;
return
$data
;
}
}
trigger_error (
"undefined index:$key"
);
}
function
__isset(
$key
)
{
if
(isset (
$this
->arrData [
$key
] ))
{
return
true;
}
if
(
file_exists
(
$this
->dataDir .
'/'
.
$key
))
{
return
true;
}
return
false;
}
function
toArray()
{
return
$this
->arrData;
}
public
function
offsetExists(
$offset
)
{
return
$this
->__isset (
$offset
);
}
public
function
offsetGet(
$offset
)
{
return
$this
->__get (
$offset
);
}
public
function
offsetSet(
$offset
,
$value
)
{
trigger_error (
'offsetSet not implemented by BtstoreElement'
);
}
public
function
offsetUnset(
$offset
)
{
trigger_error (
'offsetUnset not implemented by BtstoreElement'
);
}
public
function
current()
{
return
current (
$this
->arrData );
}
public
function
next()
{
return
next (
$this
->arrData );
}
public
function
key()
{
return
key (
$this
->arrData );
}
public
function
valid()
{
$data
= current (
$this
->arrData );
return
!
empty
(
$data
);
}
public
function
rewind
()
{
reset (
$this
->arrData );
}
}
function
btstore_get()
{
if
(
empty
( BtstoreRoot::
$root
))
{
BtstoreRoot::
$root
=
new
BtstoreElement ( ScriptConf::BTSTORE_ROOT, null );
}
return
BtstoreRoot::
$root
;
}