error = new Error(Err::Success); /* (1) Access file content ---------------------------------------------------------*/ { /* (1) Vérification existence fichier config */ if( !file_exists($path) ) { $this->error->set(Err::UnreachableResource); return; } /* (2) Lecture fichier config */ $conf = @file_get_contents($path); /* (3) Si erreur lecture */ if( $conf === false ) { $this->error->set(Err::UnreachableResource); return; } /* (4) Parsage json */ $this->raw = json_decode( $conf, true ); /* (5) Gestion de l'erreur de parsage */ if( $this->raw == null ) { $this->error->set(Err::ParsingFailed, 'json'); return; } } /* (2) Construction de l'index des chemins ---------------------------------------------------------*/ { /* (1) Initialisation */ $this->index = []; $ref = [ '/' => $this->raw ]; /* (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]); } } } } /** (3) Static singleton 'get-or-create' * * @return Config Configuration singleton * ---------------------------------------------------------*/ public static function get() : Config{ /* (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; } }