Subversion Repositories eFlore/Applications.del

Rev

Rev 1820 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1820 Rev 1821
Line 16... Line 16...
16
*/
16
*/
17
class Syndication extends RestService {
17
class Syndication extends RestService {
Line 18... Line 18...
18
 
18
 
19
	private $parametres = array();
19
	private $parametres = array();
-
 
20
	private $ressources = array();
20
	private $ressources = array();
21
	private $conteneur;
21
	private $methode = null;
-
 
22
	private $projetNom = array();
22
	private $methode = null;
23
	private $serviceNom = array();
23
	private $sousServiceNom = null;
24
	private $cheminCourant = null;
-
 
25
	private $squelette_dossier = null;
-
 
Line -... Line 24...
-
 
24
	private $cheminCourant = null;
26
	private $formats_autorises = null;
25
 
-
 
26
	private $serviceNom = 'syndication';
Line 27... Line 27...
27
 
27
	private $format = 'atom';
28
	private $conteneur;
28
	private $squelette_dossier = null;
Line 29... Line 29...
29
 
29
 
-
 
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
31
	protected $utilisationParametresBruts = true;
31
	protected $utilisationParametresBruts = true;
32
 
-
 
33
	public function __construct() {
32
 
34
		$this->conteneur = new Conteneur();
Line 33... Line 35...
33
	public function __construct() {
35
		$this->cheminCourant = dirname(__FILE__).DS;
34
		$this->cheminCourant = dirname(__FILE__).DS;
36
		$this->squelette_dossier = dirname(__FILE__).DS.$this->serviceNom.DS.'squelettes'.DS;
35
		$this->squelette_dossier = dirname(__FILE__).DS.'syndication'.DS.'squelettes'.DS;
-
 
36
	}
-
 
37
 
-
 
38
	public function consulter($ressources, $parametres) {
-
 
39
		$this->methode = 'consulter';
37
		$this->formats_autorises = $this->conteneur->getParametreTableau('syndication.formats');
40
		$resultat = '';
-
 
41
		$reponseHttp = new ReponseHttp();
38
	}
42
		try {
-
 
43
			$this->initialiserProjet();
39
 
44
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
-
 
45
			$this->conteneur = new Conteneur($this->parametres);
-
 
46
			$this->verifierRessourcesEtParametres();
-
 
47
			$resultat = $this->traiterRessources();
40
	public function consulter($ressources, $parametres) {
48
			$reponseHttp->setResultatService($this->creerResultatService($resultat));
-
 
49
		} catch (Exception $e) {
-
 
50
			$reponseHttp->ajouterErreur($e);
41
		$this->methode = 'consulter';
Line 51... Line 42...
51
		}
42
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
52
		$reponseHttp->emettreLesEntetes();
43
		$this->verifierRessourcesEtParametres();
53
		$corps = $reponseHttp->getCorps();
44
		$this->format = isset($this->parametres['format']) ? $this->parametres['format'] : $this->format;
54
		return $corps;
45
		return $this->executerService();
Line 55... Line 46...
55
	}
46
	}
56
 
-
 
57
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
47
 
58
		$this->ressources = $ressources;
48
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
59
		$this->parametres = $parametres;
49
		$this->ressources = $ressources;
60
	}
-
 
61
 
50
		$this->parametres = $parametres;
-
 
51
	}
Line 62... Line 52...
62
	private function verifierRessourcesEtParametres() {
52
 
63
		$servicesDispos = Config::get('servicesDispo');
53
	private function verifierRessourcesEtParametres() {
-
 
54
		if (isset($this->parametres['format']) && !in_array($this->parametres['format'], $this->formats_autorises)) {
64
		if (!isset($this->ressources[0]) || !in_array($this->ressources[0], explode(',',$servicesDispos))) {
55
			$msg = "Vous devez indiquer un format de flux valide.\n".$this->getDoc();
65
			$message = "Vous devez indiquer un nom de service valide, les services disponibles sont ".$servicesDispos;
-
 
66
			$code = RestServeur::HTTP_CODE_ERREUR;
56
			throw new Exception($msg, RestServeur::HTTP_CODE_ERREUR);
67
			throw new Exception($message, $code);
57
		}
68
		}
58
	}
69
 
59
 
70
		$chaineFormatsAutorises = Config::get('formatsRss');
60
	private function executerService() {
-
 
61
		$reponseHttp = new ReponseHttp();
-
 
62
		try {
-
 
63
			$donnees = $this->traiterRessources();
71
		$this->formats_autorises = explode(',', $chaineFormatsAutorises);
64
			$resultat = $this->creerResultatService($donnees);
Line 72... Line 65...
72
		if (!isset($this->ressources[1]) || !in_array($this->ressources[1], $this->formats_autorises)) {
65
			$reponseHttp->setResultatService($resultat);
73
			$message = "Vous devez indiquer un format de flux valide, les formats acceptés sont ".$chaineFormatsAutorises;
66
		} catch (Exception $e) {
74
			$code = RestServeur::HTTP_CODE_ERREUR;
67
			$reponseHttp->ajouterErreur($e);
75
			throw new Exception($message, $code);
68
		}
76
		} else {
69
		$reponseHttp->emettreLesEntetes();
Line 77... Line 70...
77
			$this->format = $this->ressources[1];
70
		$corps = $reponseHttp->getCorps();
78
		}
71
		return $corps;
-
 
72
	}
79
	}
73
 
-
 
74
	private function traiterRessources() {
-
 
75
		$this->analyserRessources();
80
 
76
		$retour = $this->initialiserService();
81
	private function traiterRessources() {
77
		return $retour;
-
 
78
	}
82
		$retour = '';
79
 
Line 83... Line -...
83
		$retour = $this->initialiserService();
-
 
84
		return $retour;
80
	private function analyserRessources() {
85
	}
81
		if ($this->methode == 'consulter') {
86
 
82
			if (isset($this->ressources[0])) {
-
 
83
				$this->sousServiceNom = $this->ressources[0];
87
	private function creerResultatService($donnees) {
84
			}
-
 
85
		}
88
		$resultat = new ResultatService();
86
		if ($this->sousServiceNom == null) {
89
		$resultat->mime = $this->getTypeMime();
87
			$this->lancerMessageErreurRessource();
90
		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
88
		}
91
		return $resultat;
89
	}
92
	}
90
 
93
 
91
	private function lancerMessageErreurRessource() {
94
	/*------------------------------------------------------------------------------------------------------------------
92
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
95
										CONFIGURATION DU PROJET
-
 
96
	------------------------------------------------------------------------------------------------------------------*/
-
 
97
	private function initialiserProjet() {
93
		$message = "La ressource demandée '$ressource' ".
98
		$this->projetNom = 'syndication';
94
			"n'est pas disponible pour le service {$this->serviceNom} !\n".
99
		$this->chargerConfigProjet();
-
 
100
	}
95
			$this->getDoc();
-
 
96
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
101
 
97
		throw new Exception($message, $code);
-
 
98
	}
Line -... Line 99...
-
 
99
 
102
	private function chargerConfigProjet() {
100
	public function getDoc() {
103
		$projet = $this->projetNom;
101
		$formatsAutorises = implode(', ', $this->formats_autorises);
104
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
102
		return "Les URLs disponibles pour ce service sont :\n".
105
		Config::charger($chemin);
103
			" * en GET :\n".
106
	}
104
			" - syndication/commentaires\n".
107
 
105
			" - syndication/tags\n".
-
 
106
			" - syndication/votes-par-protocole\n".
108
	/*------------------------------------------------------------------------------------------------------------------
107
			" Paramètres : \n".
109
								CONFIGURATION DU SERVICE
108
			"  - format : $formatsAutorises";
110
	------------------------------------------------------------------------------------------------------------------*/
-
 
111
	private function initialiserService() {
109
	}
112
		$this->chargerNomService();
110
 
113
 
111
	private function initialiserService() {
114
		$classe = $this->obtenirNomClasseService($this->serviceNom);
112
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
-
 
113
		$chemins = array();
-
 
114
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
-
 
115
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
-
 
116
		$retour = '';
-
 
117
		$service = null;
115
		$chemins = array();
118
 
116
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
119
		foreach ($chemins as $chemin) {
117
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
120
			if (file_exists($chemin)) {
Line 118... Line 121...
118
		$retour = '';
121
				require_once $chemin;
119
		$service = null;
122
				$service = new $classe($this->conteneur);
-
 
123
				if ($this->methode == 'consulter') {
-
 
124
					$retour = $service->consulter();
-
 
125
				} else {
120
		foreach ($chemins as $chemin) {
126
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
121
			if (file_exists($chemin)) {
127
						"ne possède pas de méthode '{$this->methode}' !";
122
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
128
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
123
				require_once $chemin;
129
					throw new Exception($message, $code);
124
				$service = new $classe($this->conteneur);
130
				}
Line 125... Line -...
125
				if ($this->methode == 'consulter') {
-
 
126
					$retour = $service->consulter($this->ressources, $this->parametres);
-
 
127
				}
-
 
128
			}
-
 
129
		}
-
 
130
 
-
 
131
		if (is_null($service)) {
-
 
132
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
-
 
133
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
-
 
134
			throw new Exception($message, $code);
-
 
135
		}
131
			}
136
		return $retour;
132
		}
137
	}
133
 
138
 
134
		if (is_null($service)) {
Line -... Line 135...
-
 
135
			$ressource = $this->serviceNom.'/'.implode('/', $this->ressources);
-
 
136
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
-
 
137
				"est introuvable par le service '{$this->serviceNom}' !\n".
-
 
138
				$this->getDoc();
-
 
139
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
-
 
140
			throw new Exception($message, $code);
-
 
141
		}
139
	private function chargerNomService() {
142
		return $retour;
140
		if (!isset($this->ressources[0])) {
143
	}
141
			$message = "Vous devez indiquer un nom de service";
144
 
142
			$code = RestServeur::HTTP_CODE_ERREUR;
145
	private function obtenirNomClasseService($mot) {
143
			throw new Exception($message, $code);
146
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
Line 167... Line 170...
167
			default:
170
			default:
168
				$mime = 'text/html';
171
				$mime = 'text/html';
169
		}
172
		}
170
		return $mime;
173
		return $mime;
171
	}
174
	}
172
}
-
 
173
175
}
-
 
176