NxTIC/manager/Repo.php

197 lines
4.9 KiB
PHP
Raw Normal View History

<?php
namespace manager;
// FORMAT:
//
// path: "nomModule/nomMethode"
// data1: {donnee1}
// data2: {donnee2}
// ...
//
//
//
//
class Repo{
// Constantes
public static $config_path = 'f/json/repositories/conf';
// Attributs prives utiles (initialisation)
private $path;
private $data;
private $repositories;
// Contiendra la reponse a la requete
private $answer;
// Contiendra l'etat de la requete
public $error;
/* CONSTRUCTEUR D'UNE REQUETE DE MODULE
*
* @path<String> Chemin de delegation ("repo/methode")
* @data<Array> Tableau contenant les parametres utiles au traitement
*
* @return status<Boolean> Retourne si oui ou non tout s'est bien passe
*
*/
public function __construct($path=null, $data=null){
// Si pas parametre manquant, on quitte
if( $path == null ){
$this->error = ManagerError::MissingPath;
return false;
}
/* [0] On met a jour la configuration
=========================================================*/
// Modules specifies
$this->repositories = json_decode( ResourceDispatcher::getResource(self::$config_path), true );
// Gestion de l'erreur de parsage
if( $this->repositories == null ){
$this->error = ManagerError::ParsingFailed;
return false;
}
/* [1] Verification des types des parametres
=========================================================*/
// Type de @path
if( !is_string($path) ){ // Si le type est incorrect
$this->error = ManagerError::WrongPathRepo;
return false; // On retourne FALSE, si erreur
}
// Type de @data (optionnel)
$data = (is_array($data)) ? $data : array();
/* [2] Verification du chemin (existence repo+methode)
=========================================================*/
if( !$this->checkPath($path) ) // Verification de la coherence du chemin + attribution
return false;
// Gestion d'erreur interne
/* [3] Construction de l'objet
=========================================================*/
$this->data = $data;
$this->error = ManagerError::Success;
/* [4] Enregistrement de la reponse
=========================================================*/
$this->answer = $this->dispatch();
return true; // On retourne que tout s'est bien passe
}
public function answer(){
return $this->answer;
}
/* EXECUTE LE TRAITEMENT ASSOCIE ET REMPLIE LA REPONSE
*
* @return answer<mixed*> Retourne une reponse, si tout s'est bien passe
*
*/
public function dispatch(){
/* [1] On verifie qu'aucune erreur n'a ete signalee
=========================================================*/
if( $this->error != ManagerError::Success ) // si il y a une erreur
return false; // on la passe a la reponse
/* [2] On verifie que la methode est amorcable
=========================================================*/
if( !is_callable($this->getFunctionCaller()) ){
$this->error = ManagerError::UncallableMethod;
return false;
}
/* [3] On amorce la methode
=========================================================*/
return call_user_func_array( $this->getFunctionCaller(), $this->data );
}
/* VERIFICATION DU FORMAT ET DE LA COHERENCE DU CHEMIN SPECIFIE
*
* @path<String> String correspondant au chemin de delegation ("repo/methode")
*
* @return validity<Boolean> Retourne si oui ou non l'objet est correct
*
*/
private function checkPath($path){
/* [1] Verification format general
=========================================================*/
if( !preg_match('#^([\w_-]+)/([\w_-]+)$#i', $path, $matches) ){ // Si mauvais format
$this->error = ManagerError::WrongPathRepo;
return false;
}
// On recupere les donnes de la regex
$repository = $matches[1];
$method = $matches[2];
/* [2] Verification de l'existence du repo (conf)
=========================================================*/
if( !array_key_exists($repository, $this->repositories) ){ // Si le repo n'est pas specifie dans la conf
$this->error = ManagerError::UnknownRepo;
return false; // On retourne FALSE, si erreur
}
/* [3] Verification de l'existence de la methode (conf)
=========================================================*/
if( array_search($method, $this->repositories[$repository]) === false ){ // Si la methode n'est pas specifie dans la conf
$this->error = ManagerError::UnknownMethod;
return false; // On retourne FALSE, si erreur
}
/* [4] Enregistrement du chemin et renvoi de SUCCESS
=========================================================*/
$this->path = array(
'repo' => $repository,
'method' => $method
);
return true;
}
/* RENVOI LE CHEMIN D'AMORCAGE DE LA METHODE
*
* @return path<Array> Retourne le chemin d'amorcage de la requete
*
*/
private function getFunctionCaller(){
return array( '\\manager\\repo\\'.$this->path['repo'], $this->path['method'] );
}
}
?>