PHP Class JSONStorage

Provides a very simple way to persistent JSON storage. Acts as a registry key saver. Example of use: $j = new JSONStorage(); $j->addRegistry('custom'); echo $j->registryExists('custom'); $j->setData('mydata','super','custom'); $j->save(); $j->load(); echo $j->getData('super','custom');
Inheritance: extends CComponent
ファイルを表示 Open project: yinhe/yincart

Protected Properties

Property Type Description
$data the data of the registry
$default the name of the default registry
$dirty whether the registry has changed or not
$filename the filename to save the values to
$path the full path to the directory with read/write access to save the registry to. If none set, the component will used the application's runtime folder

Public Methods

Method Description
__construct ( null $registry = null ) class constructor
__destruct ( ) class destructor - flush data
addRegistry ( string $registry ) : boolean Add new collection name
getData ( string $key, null $registry = null ) : mixed Retrieves a data value from the registry
getFile ( ) : string Property get file
getLength ( null $registry = null ) : integer Retrieves the number of keys in registry
getPath ( ) : string Property get path
getRegistry ( string $registry ) : mixed | null Retrieves a registry collection based on its name
load ( ) Loads registry data into memory
onAfterSave ( CEvent $event ) Fires after the registry has been saved
onBeforeSave ( CEvent $event ) Fires before registry has been saved
registryExists ( string $registry ) : boolean Checkes whether a collection exists (registry)
removeData ( string $key, null $registry = null ) : boolean Removes data from a key in the registry
removeRegistry ( string $registry ) : boolean Remove an existing collection and all associated data
save ( ) Saves registry data to the file
setData ( string $key, array $data, null $registry = null ) : boolean Saves data to the registry
setFile ( string $file ) Property set file
setPath ( string $path ) : boolean Property set path
verify ( ) : boolean Verifies data integrity

Private Methods

Method Description
decode ( string $data ) : mixed JSON decodes the data
encode ( string $data ) : string JSON encodes the data
flush ( ) : boolean Saves the global registry to the file

Method Details

__construct() public method

class constructor
public __construct ( null $registry = null )
$registry null

__destruct() public method

class destructor - flush data
public __destruct ( )

addRegistry() public method

Add new collection name
public addRegistry ( string $registry ) : boolean
$registry string the name of the registry (collection) to create
return boolean

getData() public method

Retrieves a data value from the registry
public getData ( string $key, null $registry = null ) : mixed
$key string the name of the key that holds the data
$registry null the registry name
return mixed the data in the key value, null otherwise

getFile() public method

Property get file
public getFile ( ) : string
return string filename

getLength() public method

Retrieves the number of keys in registry
public getLength ( null $registry = null ) : integer
$registry null the registry name
return integer the data length

getPath() public method

Property get path
public getPath ( ) : string
return string

getRegistry() public method

Retrieves a registry collection based on its name
public getRegistry ( string $registry ) : mixed | null
$registry string the name of the registry to retrieve
return mixed | null the registry, null if none found

load() public method

Loads registry data into memory
public load ( )

onAfterSave() public method

Fires after the registry has been saved
public onAfterSave ( CEvent $event )
$event CEvent

onBeforeSave() public method

Fires before registry has been saved
public onBeforeSave ( CEvent $event )
$event CEvent

registryExists() public method

Checkes whether a collection exists (registry)
public registryExists ( string $registry ) : boolean
$registry string the name of the registry to check existence
return boolean

removeData() public method

Removes data from a key in the registry
public removeData ( string $key, null $registry = null ) : boolean
$key string the key name that holds the data to remove
$registry null the registry name
return boolean true if successful, false otherwise

removeRegistry() public method

Remove an existing collection and all associated data
public removeRegistry ( string $registry ) : boolean
$registry string the name of the registry to remove
return boolean

save() public method

Saves registry data to the file
public save ( )

setData() public method

Saves data to the registry
public setData ( string $key, array $data, null $registry = null ) : boolean
$key string the name of the key that will hold the data
$data array the data to save
$registry null the name of the registry
return boolean

setFile() public method

Property set file
public setFile ( string $file )
$file string the filename to save the registry to

setPath() public method

Property set path
public setPath ( string $path ) : boolean
$path string the full path of the directory with read/write access to save the registry file to
return boolean

verify() public method

Verifies data integrity
public verify ( ) : boolean
return boolean

Property Details

$data protected_oe property

the data of the registry
protected $data

$default protected_oe property

the name of the default registry
protected $default

$dirty protected_oe property

whether the registry has changed or not
protected $dirty

$filename protected_oe property

the filename to save the values to
protected $filename

$path protected_oe property

the full path to the directory with read/write access to save the registry to. If none set, the component will used the application's runtime folder
protected $path