Authentication, http requests, internationalization, updating data and logging the changes... all those functions you need to rewrite each time you want to prototype a php application!
y_ handles these functions and lets you focus on important things.
$arg db name inclusive filepath, without .json$y_feedbackfunction y_dbload($arg){
global $y_feedback;
$dbfullpath=$arg.'.json';
if(is_file($dbfullpath)){
$res=json_decode(file_get_contents($dbfullpath),true);
return $res;
}else{
$y_feedback[]='base de données pas trouvée';
return $y_feedback;
}
}
y_dbload()$parameter to log in or out$type (optional) pw or token$request (optional) consisting of ['user'] the username or email entered by the user, ['pw'] the password entered by the user$y_feedback, $y_vary_dbload()$words text description$langif ($_REQUEST['lang']!=null) {$lang=$_REQUEST['lang'];} else {$lang='';}
function y_i18n($words){
$langdir='lang/';
global $lang;
if (!is_file($langdir.$lang.'.txt')){
$lang='fr';
}
$lang_array=json_decode(file_get_contents($langdir.$lang.'.txt'),true);
foreach($lang_array as $k=>$v){
if ($k==$words){
return $v;
}
}
}
$table (multidimensional safe)$path, null if the full array $table is expected as resultfunction y_array_read(array $table,array $path){
global $path_index, $result;
if($path[0]==null){
$result=$table;
}else{
$path_current=array_shift($path);
if ($path!=null){
y_array_read($table[$path_current],$path);
}else{
$result=$table[$path_current];
}
}
return $result;
}
$table is the original table (multidimensional safe)$path through the table$value is the new variable to writefunction y_array_write_inner(array $path, $value){
$result=array();
$path_current=array_shift($path);
if ($path!=null){
$result[$path_current]=y_array_write_inner($path, $value);
}else{
$result[$path_current]=$value;
}
return $result;
}
function y_array_write(array $table, array $path, $value){
return array_replace_recursive($table, y_array_write_inner($path,$value));
}
y_dbload(), y_array_read(), y_array_write()$db db name inclusive filepath, without .json$scope the inner path within the db file$to the new data$y_user, $y_varfunction y_dbcrud(string $db, array $scope, $to){
global $y_user, $y_var;
$db_data=y_dbload($db);
if(is_array($db_data)){
file_put_contents($db.'.json',json_encode(
y_array_write($db_data,$scope,$to)
));
$log_temp=y_dbload($y_var['log_db_path']);
if(is_array($log_temp)){
$from=y_array_read($db_data,$scope);
$log_temp[]=array(
'date'=>date('Y-m-d H:i:s'),
'user'=>$y_user,
'db'=>$db,
'scope'=>$scope,
'from'=>$from,
'to'=>$to
);
file_put_contents($y_var['log_db_path'].'.json',json_encode($log_temp));
unset($log_temp);
}
unset($db_data);
}
}
y_dbload()$name request name$y_feedback$y_varrequire('y_lib/config.php');$XX_db_path, the loaded data $XX_db_data