| Line 92... |
Line 92... |
| 92 |
* L'utiliser quand le serveur ou un service soulève une erreur ou une exception. */
|
92 |
* L'utiliser quand le serveur ou un service soulève une erreur ou une exception. */
|
| 93 |
const HTTP_CODE_ERREUR = '500';
|
93 |
const HTTP_CODE_ERREUR = '500';
|
| Line 94... |
Line 94... |
| 94 |
|
94 |
|
| 95 |
/** Mettre à true pour activer l'affichage des messages d'erreurs et de débogage.
|
95 |
/** Mettre à true pour activer l'affichage des messages d'erreurs et de débogage.
|
| 96 |
* @var boolean */
|
96 |
* @var boolean */
|
| Line 97... |
Line 97... |
| 97 |
private $debogageActivation = false;
|
97 |
private static $debogageActivation = false;
|
| 98 |
|
98 |
|
| 99 |
/** Indiquer le mode de débogage à utiliser (@see Debug).
|
99 |
/** Indiquer le mode de débogage à utiliser (@see Debug).
|
| Line 100... |
Line 100... |
| 100 |
* @var string */
|
100 |
* @var string */
|
| 101 |
private $debogageMode = '';
|
101 |
private static $debogageMode = '';
|
| 102 |
|
102 |
|
| Line 134... |
Line 134... |
| 134 |
* Ex. http://www.mondomaine.org/services/apiVersion/mon-service?monParametre1=maValeur1&monParametre2=maValeur2
|
134 |
* Ex. http://www.mondomaine.org/services/apiVersion/mon-service?monParametre1=maValeur1&monParametre2=maValeur2
|
| 135 |
* @var array
|
135 |
* @var array
|
| 136 |
*/
|
136 |
*/
|
| 137 |
private $parametres = array();
|
137 |
private $parametres = array();
|
| Line -... |
Line 138... |
| - |
|
138 |
|
| - |
|
139 |
/** Tableau contenant les paramètres de configuration du serveur.
|
| - |
|
140 |
* @var array
|
| - |
|
141 |
*/
|
| - |
|
142 |
private static $config = array();
|
| 138 |
|
143 |
|
| 139 |
/** Codes HTTP. */
|
144 |
/** Codes HTTP. */
|
| 140 |
private static $http10 = array(
|
145 |
private static $http10 = array(
|
| 141 |
self::HTTP_CODE_OK => 'OK',
|
146 |
self::HTTP_CODE_OK => 'OK',
|
| 142 |
self::HTTP_CODE_CREATION_OK => 'Created',
|
147 |
self::HTTP_CODE_CREATION_OK => 'Created',
|
| Line 158... |
Line 163... |
| 158 |
* l'url appelée pour trouver le service demandé.
|
163 |
* l'url appelée pour trouver le service demandé.
|
| 159 |
*/
|
164 |
*/
|
| 160 |
public function __construct() {
|
165 |
public function __construct() {
|
| 161 |
Config::verifierPresenceParametres($this->parametres_obligatoires);
|
166 |
Config::verifierPresenceParametres($this->parametres_obligatoires);
|
| Line 162... |
Line 167... |
| 162 |
|
167 |
|
| 163 |
$this->debogageActivation = Config::get('fw_debogage');
|
168 |
self::$debogageActivation = Config::get('fw_debogage');
|
| Line 164... |
Line 169... |
| 164 |
$this->debogageMode = Config::get('fw_debogage_mode');
|
169 |
self::$debogageMode = Config::get('fw_debogage_mode');
|
| 165 |
|
170 |
|
| 166 |
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && isset($_SERVER['QUERY_STRING'])) {
|
171 |
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && isset($_SERVER['QUERY_STRING'])) {
|
| Line 311... |
Line 316... |
| 311 |
* when a resource name is given, or a resource element when a resource and resource unique identifier are given. It does not change the
|
316 |
* when a resource name is given, or a resource element when a resource and resource unique identifier are given. It does not change the
|
| 312 |
* database contents.
|
317 |
* database contents.
|
| 313 |
*/
|
318 |
*/
|
| 314 |
private function get() {
|
319 |
private function get() {
|
| 315 |
if ($this->service != null) {
|
320 |
if ($this->service != null) {
|
| 316 |
$Service = new $this->service();
|
321 |
$Service = new $this->service(self::$config);
|
| 317 |
if (method_exists($Service, self::METHODE_GET)) {
|
322 |
if (method_exists($Service, self::METHODE_GET)) {
|
| 318 |
$methodeGet = self::METHODE_GET;
|
323 |
$methodeGet = self::METHODE_GET;
|
| 319 |
$Service->$methodeGet($this->ressources, $this->parametres);
|
324 |
$Service->$methodeGet($this->ressources, $this->parametres);
|
| 320 |
} else {
|
325 |
} else {
|
| 321 |
$e = "Le service '{$this->service}' ne contient pas la méthode '".self::METHODE_GET."' nécessaire ".
|
326 |
$e = "Le service '{$this->service}' ne contient pas la méthode '".self::METHODE_GET."' nécessaire ".
|
| Line 333... |
Line 338... |
| 333 |
$this->delete();
|
338 |
$this->delete();
|
| 334 |
} else if (count($this->ressources) == 0) {// Altnative à l'utilisation de PUT
|
339 |
} else if (count($this->ressources) == 0) {// Altnative à l'utilisation de PUT
|
| 335 |
$this->add();
|
340 |
$this->add();
|
| 336 |
} else {
|
341 |
} else {
|
| 337 |
if ($this->service != null) {
|
342 |
if ($this->service != null) {
|
| 338 |
$Service = new $this->service();
|
343 |
$Service = new $this->service(self::$config);
|
| 339 |
if (method_exists($Service, self::METHODE_POST)) {
|
344 |
if (method_exists($Service, self::METHODE_POST)) {
|
| 340 |
$methodePost = self::METHODE_POST;
|
345 |
$methodePost = self::METHODE_POST;
|
| 341 |
if ($Service->$methodePost($this->ressources, $paires)) {
|
346 |
if ($Service->$methodePost($this->ressources, $paires)) {
|
| 342 |
$this->envoyerEnteteStatutHttp(self::HTTP_CODE_CREATION_OK);
|
347 |
$this->envoyerEnteteStatutHttp(self::HTTP_CODE_CREATION_OK);
|
| 343 |
}
|
348 |
}
|
| Line 356... |
Line 361... |
| 356 |
private function put() {
|
361 |
private function put() {
|
| 357 |
$paires = $this->parserDonneesRequete();
|
362 |
$paires = $this->parserDonneesRequete();
|
| Line 358... |
Line 363... |
| 358 |
|
363 |
|
| 359 |
if (count($paires) != 0) {
|
364 |
if (count($paires) != 0) {
|
| 360 |
if ($this->service != null) {
|
365 |
if ($this->service != null) {
|
| 361 |
$Service = new $this->service();
|
366 |
$Service = new $this->service(self::$config);
|
| 362 |
if (method_exists($Service, self::METHODE_PUT)) {
|
367 |
if (method_exists($Service, self::METHODE_PUT)) {
|
| 363 |
$methodePut = self::METHODE_PUT;
|
368 |
$methodePut = self::METHODE_PUT;
|
| 364 |
if ($Service->$methodePut($this->ressources, $paires)) {
|
369 |
if ($Service->$methodePut($this->ressources, $paires)) {
|
| 365 |
$this->envoyerEnteteStatutHttp(self::HTTP_CODE_CREATION_OK);
|
370 |
$this->envoyerEnteteStatutHttp(self::HTTP_CODE_CREATION_OK);
|
| Line 376... |
Line 381... |
| 376 |
}
|
381 |
}
|
| Line 377... |
Line 382... |
| 377 |
|
382 |
|
| 378 |
private function delete() {
|
383 |
private function delete() {
|
| 379 |
if (count($this->ressources) != 0) {
|
384 |
if (count($this->ressources) != 0) {
|
| 380 |
if ($this->service != null) {
|
385 |
if ($this->service != null) {
|
| 381 |
$Service = new $this->service();
|
386 |
$Service = new $this->service(self::$config);
|
| 382 |
if (method_exists($Service, self::METHODE_DELETE)) {
|
387 |
if (method_exists($Service, self::METHODE_DELETE)) {
|
| 383 |
$methodeDelete = self::METHODE_DELETE;
|
388 |
$methodeDelete = self::METHODE_DELETE;
|
| 384 |
if ($Service->$methodeDelete($this->ressources, $paires)) {
|
389 |
if ($Service->$methodeDelete($this->ressources, $paires)) {
|
| 385 |
$this->envoyerEnteteStatutHttp(self::HTTP_CODE_SUPPRESSION_OK);
|
390 |
$this->envoyerEnteteStatutHttp(self::HTTP_CODE_SUPPRESSION_OK);
|
| Line 445... |
Line 450... |
| 445 |
* - Debug::MODE_ENTETE_HTTP : les message sont stockés par le gestionnaire d'exception sous forme d'erreur de type
|
450 |
* - Debug::MODE_ENTETE_HTTP : les message sont stockés par le gestionnaire d'exception sous forme d'erreur de type
|
| 446 |
* E_USER_NOTICE et sont renvoyés dans un entête HTTP (X_REST_DEBOGAGE_MESSAGES) à la fin de l'execution du programme.
|
451 |
* E_USER_NOTICE et sont renvoyés dans un entête HTTP (X_REST_DEBOGAGE_MESSAGES) à la fin de l'execution du programme.
|
| 447 |
* - Autre valeur : les messages sont formatés puis retournés par la fonction de débogage (à vous de les afficher).
|
452 |
* - Autre valeur : les messages sont formatés puis retournés par la fonction de débogage (à vous de les afficher).
|
| 448 |
*/
|
453 |
*/
|
| 449 |
public static function gererErreurs() {
|
454 |
public static function gererErreurs() {
|
| 450 |
if ($this->debogageActivation && GestionnaireException::getExceptionsNbre() > 0) {
|
455 |
if (self::$debogageActivation && GestionnaireException::getExceptionsNbre() > 0) {
|
| 451 |
$exceptionsTriees = GestionnaireException::getExceptionsTriees();
|
456 |
$exceptionsTriees = GestionnaireException::getExceptionsTriees();
|
| 452 |
reset($exceptionsTriees);
|
457 |
reset($exceptionsTriees);
|
| 453 |
$debogageSeulement = true;
|
458 |
$debogageSeulement = true;
|
| 454 |
if (! (count($exceptionsTriees) == 1 && key($exceptionsTriees) == E_USER_NOTICE)) {
|
459 |
if (! (count($exceptionsTriees) == 1 && key($exceptionsTriees) == E_USER_NOTICE)) {
|
| 455 |
self::envoyerEnteteStatutHttp(self::HTTP_CODE_ERREUR);
|
460 |
self::envoyerEnteteStatutHttp(self::HTTP_CODE_ERREUR);
|
| Line 457... |
Line 462... |
| 457 |
}
|
462 |
}
|
| Line 458... |
Line 463... |
| 458 |
|
463 |
|
| 459 |
$exceptionsFormatees = array();
|
464 |
$exceptionsFormatees = array();
|
| 460 |
foreach ($exceptionsTriees as $exceptions) {
|
465 |
foreach ($exceptionsTriees as $exceptions) {
|
| 461 |
foreach ($exceptions as $e) {
|
466 |
foreach ($exceptions as $e) {
|
| 462 |
if ($debogageSeulement && $this->debogageMode == Debug::MODE_ENTETE_HTTP) {
|
467 |
if ($debogageSeulement && self::$debogageMode == Debug::MODE_ENTETE_HTTP) {
|
| 463 |
$exceptionsFormatees[] = GestionnaireException::formaterExceptionTxt($e);
|
468 |
$exceptionsFormatees[] = GestionnaireException::formaterExceptionTxt($e);
|
| 464 |
} else {
|
469 |
} else {
|
| 465 |
echo GestionnaireException::formaterExceptionXhtml($e);
|
470 |
echo GestionnaireException::formaterExceptionXhtml($e);
|
| 466 |
}
|
471 |
}
|
| 467 |
}
|
472 |
}
|
| Line 468... |
Line 473... |
| 468 |
}
|
473 |
}
|
| 469 |
|
474 |
|
| 470 |
if ($debogageSeulement && $this->debogageMode == Debug::MODE_ENTETE_HTTP) {
|
475 |
if ($debogageSeulement && self::$debogageMode == Debug::MODE_ENTETE_HTTP) {
|
| 471 |
header('X_REST_DEBOGAGE_MESSAGES: '.json_encode($exceptionsFormatees));
|
476 |
header('X_REST_DEBOGAGE_MESSAGES: '.json_encode($exceptionsFormatees));
|
| 472 |
}
|
477 |
}
|
| 473 |
}
|
478 |
}
|