NxTIC/manager/lightdb.php

219 lines
5.7 KiB
PHP
Raw Normal View History

<?php
namespace manager;
2016-05-17 13:24:14 +00:00
class lightdb{
2016-05-17 13:24:14 +00:00
// REPERTOIRE RACINE DE TOUTES LES BDD
2016-05-17 14:00:57 +00:00
public static $root = __ROOT__.'/src/upload/';
2016-05-17 13:24:14 +00:00
// ATTRIBUTS
private $dbname;
private $dir;
private $index;
2016-05-17 13:24:14 +00:00
private $driver;
private $line;
private $tmp;
/* CONSTRUCTEUR -> CREER LES FICHIERS S'ILS N'EXISTENT PAS SINON, RECUPERE LES DONNES
*
* @dbname<String> Nom de la base de données
*
*/
public function __construct($dbname){
2016-05-17 13:24:14 +00:00
/* [0] On récupère les attributs
=========================================================*/
$this->dbname = $dbname;
$this->dir = self::$root.$dbname.'/';
2016-05-17 13:24:14 +00:00
/* [1] Création du répertoire s'il n'existe pas
=========================================================*/
if( !is_dir($this->dir) )
mkdir($this->dir);
/* [2] Création du fichier d'index ou récupération
=========================================================*/
/* (1) Si le fichier n'existe pas, on le crée */
if( !file_exists($this->dir.'index') )
2016-05-17 13:24:14 +00:00
file_put_contents($this->dir.'index', json_encode(array()) );
2016-05-17 13:24:14 +00:00
/* (2) On récupère le contenu du fichier */
$fIndex = new \SplFileObject($this->dir.'index');
$fIndex->seek(0);
2016-05-17 13:24:14 +00:00
$index = json_decode( $fIndex->fgets(), true );
2016-05-17 13:24:14 +00:00
// Si erreur de parsage, on retourne une erreur
if( is_null($index) ) return;
$this->index = $index;
2016-05-17 13:24:14 +00:00
/* [3] Initialisation du gestionnaire d'acces (SplFileObject)
=========================================================*/
/* (1) Si le fichier n'existe pas, on le crée */
if( !file_exists($this->dir.'data') )
file_put_contents($this->dir.'data', '' );
/* (2) On place un 'driver' sur le fichier */
$this->driver = new \SplFileObject($this->dir.'data', 'r+');
// $this->driver->setFlags( \SplFileObject::SKIP_EMPTY );
/* (3) On récupère le nombre de lignes */
$this->line = -1;
while( !$this->driver->eof() ){
$this->line++;
$this->driver->fgetcsv();
}
}
2016-05-17 13:24:14 +00:00
public function close(){ $this->driver = null; }
2016-05-17 13:24:14 +00:00
/* RETOURNE LA LISTE DES INDEX
*
* @i<String> Index pour lequel on veut la ligne et le hash
*
* @return Index<Array> Tableau associatif contenant le hash et la ligne
*
*/
public function index($i=null){
return is_numeric($i) ? $this->index : $this->index;
}
2016-05-17 13:24:14 +00:00
/* INSERTION D'UNE ENTREE DANS LA BASE DE DONNEES
*
* @key<String> Clé qui permettra l'accès direct
* @data<mixed*> Objet qui sera enregistré dans la base
*
* @return status<Boolean> Retourne TRUE si tout s'est bien passé, sinon FALSE
*
*/
public function insert($key, $data){
2016-05-17 13:24:14 +00:00
/* (1) On vérifie que la clé est unique */
if( array_key_exists($key, $this->index) )
return true;
2016-05-17 13:24:14 +00:00
/* (2) On ajoute les données aux fichier */
$json_data = json_encode($data);
$this->driver->seek($this->line);
$this->line++;
$written = $this->driver->fwrite( $json_data.PHP_EOL );
// Si erreur d'écriture, on retourne FALSE
if( is_null($written) )
return false;
/* (3) On enregistre l'index */
$this->index[$key] = array(
'line' => $this->line - 1,
'hash' => sha1($json_data)
);
/* (4) On enregistre le fichier index */
$fIndex = new \SplFileObject($this->dir.'index', 'w');
$fIndex->fwrite( json_encode($this->index) );
$fIndex = null;
return true;
}
/* RENVOIE LES DONNEES ASSOCIEES A UNE CLE DONNEE
*
* @key<String> Clé associée à la valeur à récupérer
*
* @return data<mixed*> Renvoie la valeur associée à la clé, FALSE si erreur
*
*/
2016-05-17 10:25:34 +00:00
public function fetch($key){
2016-05-17 13:24:14 +00:00
/* (1) On vérifie que la clé existe bien */
if( !array_key_exists($key, $this->index) )
return false;
2016-05-17 10:25:34 +00:00
2016-05-17 13:24:14 +00:00
/* (2) On récupère la ligne */
$line = $this->index[$key]['line'];
2016-05-17 10:25:34 +00:00
2016-05-17 13:24:14 +00:00
/* (3) On récupère le contenu */
$this->driver->seek($line);
$json = json_decode( $this->driver->fgets(), true );
2016-05-17 10:25:34 +00:00
2016-05-17 13:24:14 +00:00
// Si erreur de parsage
if( is_null($json) )
return false;
2016-05-17 10:25:34 +00:00
2016-05-17 13:24:14 +00:00
return $json;
}
2016-05-17 13:24:14 +00:00
/* SUPPRIME UNE ENTREE DE CLE DONNEE DE LA BASE DE DONNEES
*
* @key<String> Clé de l'entrée à supprimer
*
* @return status<Boolean> Retourne TRUE si tout s'est bien passé, sinon FALSE
*
*/
public function delete($key){
/* (1) On vérifie l'existence de la clé */
if( !array_key_exists($key, $this->index) )
return true; // On considère que l'action souhaitée est effectuée
2016-05-17 10:25:34 +00:00
2016-05-17 13:24:14 +00:00
/* (2) On supprime la ligne de l'index */
unset( $this->index[$key] );
return true;
}
/* RENVOIE LES DONNEES ASSOCIEES A UN CHAMP DE RECHERCHE
*
* @nomParam<typeParam> Description du param
*
* @return nomRetour<typeRetour> Description du retour
*
*/
public function filter($data){
/* (1) Si @data est un tableau associatif */
if( is_array($data) ){
$filtered = array();
foreach($this->index as $i=>$indexData){
$this->driver->seek( $indexData['line'] );
$dbData = json_decode( $this->driver->fgets(), true );
foreach($data as $key=>$value)
if( isset($dbData[$key]) && preg_match("#$value#", $dbData[$key]) ){
$filtered[$i] = $dbData;
break;
}
}
return $filtered;
2016-05-17 13:24:14 +00:00
/* (2) Sinon on compare @data en tant que valeur simple */
}else{
$this->tmp = sha1( json_encode($data) );
return array_filter($this->index, array($this, 'simpleFilter'));
2016-05-17 13:24:14 +00:00
}
}
protected function simpleFilter($e){ return $e['hash'] == $this->tmp; }
2016-05-17 10:25:34 +00:00
2016-05-17 13:24:14 +00:00
}