switch code to english

This commit is contained in:
Thomas Rubini 2023-01-12 14:31:06 +01:00
parent 32b777897d
commit 94dafd735c
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
21 changed files with 239 additions and 271 deletions

View File

@ -1,19 +0,0 @@
<?php
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));
}
}

View File

@ -0,0 +1,19 @@
<?php
final class HelloworldController
{
public function defaultAction()
{
$O_helloworld = new Helloworld();
View::show('helloworld/view', array('helloworld' => $O_helloworld->donneMessage()));
}
public function testformAction(Array $A_urlParams = null, Array $A_postParams = null)
{
View::show('helloworld/testform', array('formData' => $A_postParams));
}
}

54
Kernel/AutoLoad.php Normal file
View File

@ -0,0 +1,54 @@
<?php
require 'Kernel/Constants.php';
final class AutoLoad
{
public static function loadKernelClass ($S_className)
{
$S_file = Constants::kernelDir() . "$S_className.php";
return static::_load($S_file);
}
public static function loadExceptionClass ($S_className)
{
$S_file = Constants::exceptionsDir() . "$S_className.php";
return static::_load($S_file);
}
public static function loadModelClass ($S_className)
{
$S_file = Constants::modelsDir() . "$S_className.php";
return static::_load($S_file);
}
public static function loadViewClass ($S_className)
{
$S_path = Constants::viewsDir() . "$S_className.php";
return static::_load($S_path);
}
public static function loadControllerClass ($S_className)
{
$S_path = Constants::controllersDir() . "$S_className.php";
return static::_load($S_path);
}
private static function _load($S_path)
{
if (is_readable($S_path))
{
require $S_path;
}
}
}
spl_autoload_register('AutoLoad::loadKernelClass');
spl_autoload_register('AutoLoad::loadExceptionClass');
spl_autoload_register('AutoLoad::loadModelClass');
spl_autoload_register('AutoLoad::loadViewClass');
spl_autoload_register('AutoLoad::loadControllerClass');

42
Kernel/Constants.php Normal file
View File

@ -0,0 +1,42 @@
<?php
final class Constants
{
const KERNEL_DIR = '/Kernel/';
const EXCEPTIONS_DIR = '/Kernel/Exceptions/';
const VIEWS_DIR = '/Views/';
const MODELS_DIR = '/Models/';
const CONTROLLERS_DIR = '/Controllers/';
public static function rootDir() {
return realpath(__DIR__ . '/../');
}
public static function kernelDir() {
return self::rootDir() . self::KERNEL_DIR;
}
public static function exceptionsDir() {
return self::rootDir() . self::EXCEPTIONS_DIR;
}
public static function viewsDir() {
return self::rootDir() . self::VIEWS_DIR;
}
public static function modelsDir() {
return self::rootDir() . self::MODELS_DIR;
}
public static function controllersDir() {
return self::rootDir() . self::CONTROLLERS_DIR;
}
}

71
Kernel/Controller.php Normal file
View File

@ -0,0 +1,71 @@
<?php
final class controller
{
private $_A_urlParts;
private $_A_urlParams;
private $_A_postParams;
public function __construct ($S_url, $A_postParams)
{
// Remove the trailing slash
if ('/' == substr($S_url, -1, 1)) {
$S_url = substr($S_url, 0, strlen($S_url) - 1);
}
// Remove the leading slash
if ('/' == substr($S_url, 0, 1)) {
$S_url = substr($S_url, 1, strlen($S_url));
}
// split the url
$_A_urlParts = explode('/', $S_url);
if (empty($_A_urlParts[0])) {
$_A_urlParts[0] = 'DefaultController';
} else {
$_A_urlParts[0] = ucfirst($_A_urlParts[0]) . "Controller";
}
if (empty($_A_urlParts[1])) {
$_A_urlParts[1] = 'defaultAction';
} else {
$_A_urlParts[1] = $_A_urlParts[1] . 'Action';
}
$this->_A_urlParts['controller'] = array_shift($_A_urlParts);
$this->_A_urlParts['action'] = array_shift($_A_urlParts);
$this->_A_urlParametres = $_A_urlParts;
$this->_A_postParams = $A_postParams;
}
// Execute the controller and action deduced
public function execute()
{
if (!class_exists($this->_A_urlParts['controller'])) {
throw new ControllerException("Controller " . $this->_A_urlParts['controller'] . " is not valid.");
}
if (!method_exists($this->_A_urlParts['controller'], $this->_A_urlParts['action'])) {
throw new ControllerException("Action " . $this->_A_urlParts['action'] . " of controller " .
$this->_A_urlParts['controller'] . " is not valid.");
}
$B_called = call_user_func_array(array(new $this->_A_urlParts['controller'],
$this->_A_urlParts['action']), array($this->_A_urlParams, $this->_A_postParams ));
if (false === $B_called) {
throw new ControllerException("Action " . $this->_A_urlParts['action'] .
" of controller " . $this->_A_urlParts['controller'] . " failed.");
}
}
}

View File

@ -0,0 +1,3 @@
<?php
class ControllerException extends Exception {}

24
Kernel/View.php Normal file
View File

@ -0,0 +1,24 @@
<?php
final class View
{
public static function openBuffer()
{
ob_start();
}
public static function closeBuffer()
{
return ob_get_clean();
}
public static function show ($S_path, $A_params = array())
{
$S_file = Constants::viewsDir() . $S_path . '.php';
$A_view = $A_params;
ob_start();
include $S_file;
ob_end_flush();
}
}

View File

@ -1,55 +0,0 @@
<?php
require 'Noyau/Constantes.php';
final class ChargementAuto
{
public static function chargerClassesNoyau ($S_nomDeClasse)
{
$S_fichier = Constantes::repertoireNoyau() . "$S_nomDeClasse.php";
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";
return static::_charger($S_fichier);
}
public static function chargerClassesVue ($S_nomDeClasse)
{
$S_fichier = Constantes::repertoireVues() . "$S_nomDeClasse.php";
return static::_charger($S_fichier);
}
public static function chargerClassesControleur ($S_nomDeClasse)
{
$S_fichier = Constantes::repertoireControleurs() . "$S_nomDeClasse.php";
return static::_charger($S_fichier);
}
private static function _charger ($S_fichierACharger)
{
if (is_readable($S_fichierACharger))
{
require $S_fichierACharger;
}
}
}
// 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');

View File

@ -1,46 +0,0 @@
<?php
// Rappel : nous sommes dans le répertoire Core, voilà pourquoi dans realpath je "remonte d'un cran" pour faire référence
// à la VRAIE racine de mon application
final class Constantes
{
// Les constantes relatives aux chemins
const REPERTOIRE_VUES = '/Vues/';
const REPERTOIRE_MODELE = '/Modele/';
const REPERTOIRE_NOYAU = '/Noyau/';
const REPERTOIRE_EXCEPTIONS = '/Noyau/Exceptions/';
const REPERTOIRE_CONTROLEURS = '/Controleurs/';
public static function repertoireRacine() {
return realpath(__DIR__ . '/../');
}
public static function repertoireNoyau() {
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;
}
public static function repertoireModele() {
return self::repertoireRacine() . self::REPERTOIRE_MODELE;
}
public static function repertoireControleurs() {
return self::repertoireRacine() . self::REPERTOIRE_CONTROLEURS;
}
}

View File

@ -1,77 +0,0 @@
<?php
final class Controleur
{
private $_A_urlDecortique;
private $_A_urlParametres;
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);
}
// On élimine l'éventuel slash en début d'URL sinon notre explode renverra une première entrée vide
if ('/' == substr($S_url, 0, 1)) {
$S_url = substr($S_url, 1, strlen($S_url));
}
// 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
$A_urlDecortique[1] = 'defautAction';
} else {
// On part du principe que toutes nos actions sont suffixées par 'Action'...à nous de le rajouter
$A_urlDecortique[1] = $A_urlDecortique[1] . 'Action';
}
// 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.");
}
}
}

View File

@ -1,3 +0,0 @@
<?php
class ControleurException extends Exception {}

View File

@ -1,27 +0,0 @@
<?php
final class Vue
{
public static function ouvrirTampon()
{
// On démarre le tampon de sortie, on va l'appeler "tampon principal"
ob_start();
}
public static function recupererContenuTampon()
{
// On retourne le contenu du tampon principal
return ob_get_clean();
}
public static function montrer ($S_localisation, $A_parametres = array())
{
$S_fichier = Constantes::repertoireVues() . $S_localisation . '.php';
$A_vue = $A_parametres;
// Démarrage d'un sous-tampon
ob_start();
include $S_fichier; // c'est dans ce fichier que sera utilisé A_vue, la vue est inclue dans le sous-tampon
ob_end_flush();
}
}

View File

@ -1,2 +1,2 @@
<?php
echo '<header> <h1>Titre</h1></header>';
<?php
echo '<header> <h1>Titre</h1></header>';

View File

@ -0,0 +1,3 @@
<?php
echo "<p>" . $A_view['helloworld'] . "</p>";

View File

@ -1,12 +1,12 @@
<!doctype html>
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My sweet MVC</title>
</head>
<body>
<?php Vue::montrer('standard/entete'); ?>
<?php echo $A_vue['body'] ?>
<?php Vue::montrer('standard/pied'); ?>
</body>
<!doctype html>
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My sweet MVC</title>
</head>
<body>
<?php View::show('common/header'); ?>
<?php echo $A_view['body'] ?>
<?php View::show('common/footer'); ?>
</body>
</html>

View File

@ -1,3 +0,0 @@
<?php
echo "<p>" . $A_vue['helloworld'] . "</p>";

View File

@ -1,46 +1,28 @@
<?php
// Ce fichier est le point d'entrée de votre application
// Application entry point
require 'vendor/autoload.php';
require 'Noyau/ChargementAuto.php';
/*
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
/helloworld
/controleur/nom_action/whatever/whatever2/
require 'Kernel/AutoLoad.php';
*/
/*
$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);
*/
// load .env file in $_ENV
$dotenv = Dotenv\Dotenv::createImmutable(Constantes::repertoireRacine());
$dotenv = Dotenv\Dotenv::createImmutable(Constants::rootDir());
$dotenv->load();
$S_urlADecortiquer = isset($_GET['url']) ? $_GET['url'] : null;
$S_url = 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
View::openBuffer();
try
{
$O_controleur = new Controleur($S_urlADecortiquer, $A_postParams);
$O_controleur->executer();
$O_controller = new Controller($S_url, $A_postParams);
$O_controller->execute();
}
catch (ControleurException $O_exception)
{
echo ('Une erreur s\'est produite : ' . $O_exception->getMessage());
echo ('An error occured: ' . $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();
$content = View::closeBuffer();
// On affiche le contenu dans la partie body du gabarit général
Vue::montrer('gabarit', array('body' => $contenuPourAffichage));
View::show('html', array('body' => $content));