From bdcdd70c3678d93d7e0e9ec0494d0bc56a9990e8 Mon Sep 17 00:00:00 2001 From: moinal Date: Wed, 16 Dec 2020 18:06:53 +0100 Subject: [PATCH] Routage et exceptions --- Controleurs/ControleurHelloworld.php | 10 +++- Noyau/ChargementAuto.php | 8 +++ Noyau/Constantes.php | 6 ++ Noyau/Controleur.php | 73 ++++++++++++++++++------ Noyau/Exceptions/ControleurException.php | 3 + Vues/helloworld/testform.php | 8 +++ Vues/helloworld/voir.php | 2 +- index.php | 24 +++++++- 8 files changed, 111 insertions(+), 23 deletions(-) create mode 100644 Noyau/Exceptions/ControleurException.php create mode 100644 Vues/helloworld/testform.php diff --git a/Controleurs/ControleurHelloworld.php b/Controleurs/ControleurHelloworld.php index 18261d9..68019a1 100644 --- a/Controleurs/ControleurHelloworld.php +++ b/Controleurs/ControleurHelloworld.php @@ -5,9 +5,15 @@ final class ControleurHelloworld public function defautAction() { $O_helloworld = new Helloworld(); - Vue::montrer('helloworld/voir', array('helloworld' => $O_helloworld->donneMessage())); } -} + public function testformAction(Array $A_parametres = null, Array $A_postParams = null) + { + + Vue::montrer('helloworld/testform', array('formData' => $A_postParams)); + + } + +} \ No newline at end of file diff --git a/Noyau/ChargementAuto.php b/Noyau/ChargementAuto.php index 6374e54..eaa78f0 100644 --- a/Noyau/ChargementAuto.php +++ b/Noyau/ChargementAuto.php @@ -10,6 +10,13 @@ final class ChargementAuto return static::_charger($S_fichier); } + public static function chargerClassesException ($S_nomDeClasse) + { + $S_fichier = Constantes::repertoireExceptions() . "$S_nomDeClasse.php"; + + return static::_charger($S_fichier); + } + public static function chargerClassesModele ($S_nomDeClasse) { $S_fichier = Constantes::repertoireModele() . "$S_nomDeClasse.php"; @@ -42,6 +49,7 @@ final class ChargementAuto // J'empile tout ce beau monde comme j'ai toujours appris à le faire... spl_autoload_register('ChargementAuto::chargerClassesNoyau'); +spl_autoload_register('ChargementAuto::chargerClassesException'); spl_autoload_register('ChargementAuto::chargerClassesModele'); spl_autoload_register('ChargementAuto::chargerClassesVue'); spl_autoload_register('ChargementAuto::chargerClassesControleur'); diff --git a/Noyau/Constantes.php b/Noyau/Constantes.php index a3594ca..53f5ff4 100644 --- a/Noyau/Constantes.php +++ b/Noyau/Constantes.php @@ -13,6 +13,8 @@ final class Constantes const REPERTOIRE_NOYAU = '/Noyau/'; + const REPERTOIRE_EXCEPTIONS = '/Noyau/Exceptions/'; + const REPERTOIRE_CONTROLEURS = '/Controleurs/'; @@ -24,6 +26,10 @@ final class Constantes return self::repertoireRacine() . self::REPERTOIRE_NOYAU; } + public static function repertoireExceptions() { + return self::repertoireRacine() . self::REPERTOIRE_EXCEPTIONS; + } + public static function repertoireVues() { return self::repertoireRacine() . self::REPERTOIRE_VUES; } diff --git a/Noyau/Controleur.php b/Noyau/Controleur.php index 2b50dbb..4fd05f9 100644 --- a/Noyau/Controleur.php +++ b/Noyau/Controleur.php @@ -4,32 +4,69 @@ final class Controleur { private $_A_urlDecortique; - public function __construct ($S_controleur, $S_action) - { + private $_A_urlParametres; - if (empty($S_controleur)) { - // Nous avons pris le parti de préfixer tous les controleurs par "Controleur" - $this->_A_urlDecortique['controleur'] = 'ControleurDefaut'; - } else { - $this->_A_urlDecortique['controleur'] = 'Controleur' . ucfirst($S_controleur); + private $_A_postParams; + + public function __construct ($S_url, $A_postParams) + { + // On élimine l'éventuel slash en fin d'URL sinon notre explode renverra une dernière entrée vide + if ('/' == substr($S_url, -1, 1)) { + $S_url = substr($S_url, 0, strlen($S_url) - 1); } - if (empty($S_action)) { + // On éclate l'URL, elle va prendre place dans un tableau + $A_urlDecortique = explode('/', $S_url); + + if (empty($A_urlDecortique[0])) { + // Nous avons pris le parti de préfixer tous les controleurs par "Controleur" + $A_urlDecortique[0] = 'ControleurDefaut'; + } else { + $A_urlDecortique[0] = 'Controleur' . ucfirst($A_urlDecortique[0]); + } + + if (empty($A_urlDecortique[1])) { // L'action est vide ! On la valorise par défaut - $this->_A_urlDecortique['action'] = 'defautAction'; + $A_urlDecortique[1] = 'defautAction'; } else { // On part du principe que toutes nos actions sont suffixées par 'Action'...à nous de le rajouter - $this->_A_urlDecortique['action'] = $S_action . 'Action'; + $A_urlDecortique[1] = $A_urlDecortique[1] . 'Action'; } - } - - // On exécute - public function executer() - { - //fonction de rappel de notre controleur cible (ControleurHelloworld pour notre premier exemple) - call_user_func_array(array(new $this->_A_urlDecortique['controleur'], - $this->_A_urlDecortique['action']), array()); + + // on dépile 2 fois de suite depuis le début, c'est à dire qu'on enlève de notre tableau le contrôleur et l'action + // il ne reste donc que les éventuels parametres (si nous en avons)... + $this->_A_urlDecortique['controleur'] = array_shift($A_urlDecortique); // on recupere le contrôleur + $this->_A_urlDecortique['action'] = array_shift($A_urlDecortique); // puis l'action + + // ...on stocke ces éventuels parametres dans la variable d'instance qui leur est réservée + $this->_A_urlParametres = $A_urlDecortique; + + // On s'occupe du tableau $A_postParams + $this->_A_postParams = $A_postParams; + } + + // On exécute notre triplet + + public function executer() + { + if (!class_exists($this->_A_urlDecortique['controleur'])) { + throw new ControleurException($this->_A_urlDecortique['controleur'] . " n'est pas un controleur valide."); + } + + if (!method_exists($this->_A_urlDecortique['controleur'], $this->_A_urlDecortique['action'])) { + throw new ControleurException($this->_A_urlDecortique['action'] . " du contrôleur " . + $this->_A_urlDecortique['controleur'] . " n'est pas une action valide."); + } + + $B_called = call_user_func_array(array(new $this->_A_urlDecortique['controleur'], + $this->_A_urlDecortique['action']), array($this->_A_urlParametres, $this->_A_postParams )); + + if (false === $B_called) { + throw new ControleurException("L'action " . $this->_A_urlDecortique['action'] . + " du contrôleur " . $this->_A_urlDecortique['controleur'] . " a rencontré une erreur."); + } + } } \ No newline at end of file diff --git a/Noyau/Exceptions/ControleurException.php b/Noyau/Exceptions/ControleurException.php new file mode 100644 index 0000000..045e712 --- /dev/null +++ b/Noyau/Exceptions/ControleurException.php @@ -0,0 +1,3 @@ +'; + echo ''; + echo ''; + echo ''; \ No newline at end of file diff --git a/Vues/helloworld/voir.php b/Vues/helloworld/voir.php index cde140c..b8fdb6b 100644 --- a/Vues/helloworld/voir.php +++ b/Vues/helloworld/voir.php @@ -1,3 +1,3 @@ " . $A_vue['helloworld'] . "

"; \ No newline at end of file +echo "

" . $A_vue['helloworld'] . "

"; diff --git a/index.php b/index.php index 8f38ce5..2bd3f33 100644 --- a/index.php +++ b/index.php @@ -5,14 +5,34 @@ /* url pour notre premier test MVC Hello World, nous n'avons pas d'action précisée on visera celle par défaut - index.php?ctrl=helloworld + /index.php?ctrl=helloworld + /helloworld + /controleur/nom_action/whatever/whatever2/ + */ +/* $S_controleur = isset($_GET['ctrl']) ? $_GET['ctrl'] : null; $S_action = isset($_GET['action']) ? $_GET['action'] : null; Vue::ouvrirTampon(); // /Noyau/Vue.php : on ouvre le tampon d'affichage, les contrôleurs qui appellent des vues les mettront dedans $O_controleur = new Controleur($S_controleur, $S_action); - $O_controleur->executer(); +*/ + + $S_urlADecortiquer = isset($_GET['url']) ? $_GET['url'] : null; + $A_postParams = isset($_POST) ? $_POST : null; + + Vue::ouvrirTampon(); // on ouvre le tampon d'affichage, les contrôleurs qui appellent des vues les mettront dedans + + try + { + $O_controleur = new Controleur($S_urlADecortiquer, $A_postParams); + $O_controleur->executer(); + } + catch (ControleurException $O_exception) + { + echo ('Une erreur s\'est produite : ' . $O_exception->getMessage()); + } + // Les différentes sous-vues ont été "crachées" dans le tampon d'affichage, on les récupère $contenuPourAffichage = Vue::recupererContenuTampon();