Skip to content

Instantly share code, notes, and snippets.

@magnusbonnevier
Last active May 18, 2017 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magnusbonnevier/5c73f96d6ec09850aea8b1ad605aac1f to your computer and use it in GitHub Desktop.
Save magnusbonnevier/5c73f96d6ec09850aea8b1ad605aac1f to your computer and use it in GitHub Desktop.
Simple PHP class that handles a simple key value store, It uses flat textfiles for this. You can store values of string, Array, Objects. Etc.
<?PHP
namespace EyeDentify\Tools;
class EyeKeyValueStore {
/*
EyeKeyValueStore class v1.1
author: Magnus Bonnevier.
e-mail: magnus.bonnevier@gmail.com
website: www.magnusbonnevier.se
Simple class that handles simple key value store.
It uses flat textfiles for this.
A default path to a directory for the files are provided,
change this for your needs.
The directory needs to be writable ofcourse.
default key value files is created like this:
_keyname.php
You can change Prefix and Suffix to your needs.
Code is free to use, you are not obligated to
reference me, but it would be appreciated.
*/
/* init vars */
protected $storePath = 'key_value_store/';
protected $filePrefix = '_';
protected $fileSuffix = '.php';
/* init class constructor */
public function __construct($path = null) {
/* if key value store path supplied then override the default one */
if(!$path == '') {
if(!is_string($path)) {
exit('Error __construct(), you must supply a path string!');
}
$this->storePath = $path;
}
/* return this for chainability */
return $this;
}
/* set the filename prefix */
public function setFilePrefix ($prefixStr = null) {
if(!$prefixStr == '') {
if(!is_string($prefixStr)) {
exit('Error setFilePrefix(), you must supply a string!');
}
$this->filePrefix = $prefixStr;
}
return $this;
}
/* set the filename suffix */
public function setFileSuffix ($suffixStr = null) {
if(!$suffixStr == '') {
if(!is_string($suffixStr)) {
exit('Error setFileSuffix(), you must supply a string!');
}
$this->fileSuffix = $suffixStr;
}
return $this;
}
/* check if key exists */
public function keyExists($key = null) {
/* construct the file name to get the value from */
$keyValueFile = $this->filePrefix . $key . $this->fileSuffix;
/* do the associated key value file exist? */
if(!file_exists($this->storePath . $keyValueFile)) {
/* key do not exist return false */
return FALSE;
} else {
/* Yes!!! key exists so return true */
return TRUE;
}
}
/* add data to a new key or overwrite existing */
public function add($key = null, $value = null) {
if(!is_string($key)) {
exit('Error add(), you must supply a key string to value: ' . $value . '' . PHP_EOL);
}
/* construct the file name to store the value in */
$keyValueFile = $this->filePrefix . $key . $this->fileSuffix;
/* encode the data to JSON */
$data = json_encode($value);
/* save the json data to a flat file */
$result = file_put_contents($this->storePath . $keyValueFile, $data);
if($result === FALSE) {
exit('Error key could not be saved and written to file!');
}
return $this;
}
/* update a key value (overwrite existing value) */
public function update($key = null, $value) {
if(!is_string($key)) {
exit('Error update(), you must supply a key string to value: ' . $value . '' . PHP_EOL);
}
/* use the add() method, because it simply overwrites existing value if key exists */
$this->add($key, $value);
}
/* get data from the stored key value file and return it */
public function get($key = null) {
if(!is_string($key)) {
exit('Error get(), you must supply a key string!');
}
/* construct the file name to get the value from */
$keyValueFile = $this->filePrefix . $key . $this->fileSuffix;
/* read the json data from flat file */
if(file_exists($this->storePath . $keyValueFile)) {
$data = file_get_contents($this->storePath . $keyValueFile);
} else {
exit('Error key does not exist in store.');
}
/* decode JSON data into a Array, String or object depending of what was saved */
$data = json_decode($data);
/* return key value data */
return $data;
}
/* get a list of keys stored */
public function getKeys() {
/* init vars */
$path = $this->storePath;
$prefix = $this->filePrefix;
$suffix = $this->fileSuffix;
/* list all key value files in store directory */
foreach (glob("{$path}{$prefix}*{$suffix}") as $fileName) {
/* get path info filename */
$pathInfo = pathinfo($fileName);
/* do replace */
$keyValue = ltrim($pathInfo['basename'], $this->filePrefix);
$keyValue = rtrim($keyValue, $this->fileSuffix);
/* save the key value */
$keyValues[] = $keyValue;
}
return $keyValues;
}
/* get a list of the filenames used for key values */
public function getKeyFiles() {
/* init vars */
$path = $this->storePath;
$prefix = $this->filePrefix;
$suffix = $this->fileSuffix;
/* list all key value files in store directory */
foreach (glob("{$path}{$prefix}*{$suffix}") as $fileName) {
/* get path info filename */
$pathInfo = pathinfo($fileName);
/* save the key value filename */
$keyValues[] = $pathInfo['basename'];
}
return $keyValues;
}
/* remove key value file based on key name */
public function remove($key = null) {
if(!is_string($key)) {
exit('Error remove(), you must supply a key string!');
}
/* construct the file name to remove */
$keyValueFile = $this->filePrefix . $key . $this->fileSuffix;
/* remove key value file if it exists */
if(file_exists($this->storePath . $keyValueFile)) {
/* returns TRUE if successfull or FALSE if not */
$removeResult = unlink($this->storePath . $keyValueFile);
return $removeResult;
} else {
/* if file does not exist then give a error also return FALSE */
exit('Error key does not exist in store.');
return FALSE;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment