ptut-vhost/build/api/core/Config.php

155 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2018-02-17 17:18:58 +00:00
<?php
namespace api\core;
use \error\core\Error;
use \error\core\Err;
class Config{
/* (1) Attributes
---------------------------------------------------------*/
/* (1) Static */
2018-03-11 15:14:12 +00:00
/** @var Config? */
2018-02-17 17:18:58 +00:00
private static $inst = null; // singleton instance
2018-03-11 15:14:12 +00:00
private static function config_path() : String { return __ROOT__.'/config/modules.json'; }
/** @var array[String] */
2018-02-17 17:18:58 +00:00
public static $allowed_http_methods = [ "GET", "POST", "PUT", "DELETE" ];
/* (2) Instance */
2018-03-11 15:14:12 +00:00
/** @var array */
2018-02-17 17:18:58 +00:00
private $raw;
2018-03-11 15:14:12 +00:00
/** @var array */
2018-02-17 17:18:58 +00:00
public $index;
2018-03-11 15:14:12 +00:00
/** @var Error */
2018-02-17 17:18:58 +00:00
public $error;
2018-03-11 15:14:12 +00:00
/** (2) Private constructor
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @param String|null $path Configuration path
2018-02-17 17:18:58 +00:00
*
---------------------------------------------------------*/
2018-03-11 15:14:12 +00:00
private function __construct(?String $path=null){
2018-02-17 17:18:58 +00:00
// Set default error
$this->error = new Error(Err::Success);
/* (1) Access file content
---------------------------------------------------------*/ {
/* (1) Vérification existence fichier config */
2018-02-27 13:48:07 +00:00
if( !file_exists($path) ) {
$this->error->set(Err::UnreachableResource);
return;
}
2018-02-17 17:18:58 +00:00
/* (2) Lecture fichier config */
$conf = @file_get_contents($path);
/* (3) Si erreur lecture */
2018-02-27 13:48:07 +00:00
if( $conf === false ) {
$this->error->set(Err::UnreachableResource);
return;
}
2018-02-17 17:18:58 +00:00
/* (4) Parsage json */
$this->raw = json_decode( $conf, true );
/* (5) Gestion de l'erreur de parsage */
2018-02-27 13:48:07 +00:00
if( $this->raw == null ) {
$this->error->set(Err::ParsingFailed, 'json');
return;
}
2018-02-17 17:18:58 +00:00
}
/* (2) Construction de l'index des chemins
---------------------------------------------------------*/ {
/* (1) Initialisation */
$this->index = [];
2018-02-27 13:48:07 +00:00
$ref = [ '/' => $this->raw ];
2018-02-17 17:18:58 +00:00
/* (2) Tant qu'il reste des @ref à traiter */
while( count($ref) > 0 ){
/* (2.1) For each ref */
foreach($ref as $ref_path=>$ref_children){
/* (2.2) For each children */
foreach($ref_children as $path=>$method_or_subpath){
/* (2.2.1) If is an HTTP method -> add to index */
if( in_array($path, self::$allowed_http_methods) ){
/* (2.2.1.1) If no index for this path -> add it */
if( !isset($this->index[$ref_path]) )
$this->index[$ref_path] = [];
/* (2.2.1.2) Add the HTTP method definition */
$this->index[$ref_path][$path] = $method_or_subpath;
/* (2.2.2) If a sub path -> add it to next refs to process */
}else{
if( $ref_path == '/' )
$ref["$ref_path$path"] = $method_or_subpath;
else
$ref["$ref_path/$path"] = $method_or_subpath;
}
}
/* (2.3) In all cases -> remove current from ref */
unset($ref[$ref_path]);
}
}
}
}
2018-03-11 15:14:12 +00:00
/** (3) Static singleton 'get-or-create'
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @return Config Configuration singleton
2018-02-17 17:18:58 +00:00
*
---------------------------------------------------------*/
2018-02-27 13:48:07 +00:00
public static function get() : Config{
2018-02-17 17:18:58 +00:00
/* (1) If @inst already exists -> return singleton */
if( self::$inst instanceof Config )
return self::$inst;
/* (2) If @inst not set -> create singleton and return it */
self::$inst = new self( self::config_path() );
return self::$inst;
}
}