miniMVC/sys/libraries/Data_Store.php

153 lines
2.9 KiB
PHP
Raw Normal View History

2012-05-15 10:23:31 -04:00
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
2012-05-23 12:01:13 -04:00
* @link https://github.com/aviat4ion/miniMVC
2012-07-12 10:23:55 -04:00
* @license http://philsturgeon.co.uk/code/dbad-license
2012-05-15 10:23:31 -04:00
*/
// --------------------------------------------------------------------------
2012-05-22 11:11:36 -04:00
namespace miniMVC;
2012-05-15 10:23:31 -04:00
/**
* Class for using JSON as a key->value data store
*
* @package miniMVC
* @subpackage Libraries
*/
2012-05-21 14:29:36 -04:00
class Data_Store {
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
/**
* Settings object represented by the currently loaded JSON file
*/
private $current;
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
/**
* Singleton instance
*/
private static $instance;
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
/**
* Create and/or load json file
*/
protected function __construct()
{
$path = MM_APP_PATH .'config/data_store.json';
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
if ( ! is_file($path))
{
touch($path);
2012-05-15 16:53:10 -04:00
$this->current = (object)[];
2012-05-15 10:23:31 -04:00
}
else
{
// Load the file
$json = file_get_contents($path);
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
// Load the object into the class
$this->current = json_decode($json);
}
}
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
// --------------------------------------------------------------------------
/**
* Output the data on destruct
*/
public function __destruct()
{
$file_string = json_encode($this->current, JSON_PRETTY_PRINT);
2012-05-17 16:22:39 -04:00
file_put_contents(MM_APP_PATH . 'config/data_store.json', $file_string);
2012-05-15 10:23:31 -04:00
}
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
// --------------------------------------------------------------------------
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
/**
* Magic function called when cloning an object
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
// --------------------------------------------------------------------------
/**
* Magic method to simplify isset checking for config options
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return (isset($this->current->{$key}))
? $this->current->{$key}
: NULL;
}
// --------------------------------------------------------------------------
/**
* Magic method to simplify setting config options
*
* @param string $key
* @param mixed
*/
public function __set($key, $val)
{
return $this->current->{$key} = $val;
}
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
// --------------------------------------------------------------------------
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
/**
* Static method to retreive current instance
* of the singleton
*
* @return self
*/
public static function &get_instance()
{
if( ! isset(self::$instance))
{
$name = __CLASS__;
self::$instance = new $name();
}
return self::$instance;
}
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
// --------------------------------------------------------------------------
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
/**
* Removes a key from the data store
*
* @param string $key
* @return void
*/
public function del($key)
{
unset($this->current->{$key});
}
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
// --------------------------------------------------------------------------
2012-07-12 10:23:55 -04:00
2012-05-15 10:23:31 -04:00
/**
* Return the entire data store object
*
* @return object
*/
public function get_all()
{
return $this->current;
}
}
2012-05-17 16:22:39 -04:00
2012-05-15 10:23:31 -04:00
// End of data store.php