Commit initial

This commit is contained in:
2020-05-15 12:27:34 +02:00
commit 70f72f2e54
87 changed files with 12684 additions and 0 deletions

View File

@ -0,0 +1,30 @@
<?php
class Configuration {
private static $parameters;
public static function get($nom, $default = null) {
if (isset(self::getParameters()[$nom])) {
$value = self::getParameters()[$nom];
}
else {
$value = $default;
}
return $value;
}
private static function getParameters() {
if (self::$parameters == null) {
$pathFile = $GLOBALS['app_path']."Config/config.ini";
if (!file_exists($pathFile)) {
throw new Exception("Aucun fichier de configuration trouvé");
}
else {
self::$parameters = parse_ini_file($pathFile);
}
}
return self::$parameters;
}
}

View File

@ -0,0 +1,46 @@
<?php
require_once $GLOBALS['app_path'].'Framework/Request.php';
require_once $GLOBALS['app_path'].'Framework/View.php';
abstract class Controller {
private $action;
protected $request;
public function setRequest(Request $request) {
$this->request = $request;
}
public function executeAction($action) {
if (method_exists($this, $action)) {
$this->action = $action;
$this->{$this->action}();
}
else {
$classController = get_class($this);
throw new Exception("Action '$action' non définie dans la classe $classController");
}
}
public abstract function index();
protected function generateView($dataView = array(), $action = null) {
$actionView = $this->action;
if ($action != null)
$actionView = $action;
$classController = get_class($this);
$controller = str_replace("Controller", "", $classController);
$vue = new View($actionView, $controller);
$vue->generate($dataView);
}
protected function redirect($controller, $action = null)
{
$root = Configuration::get("root", "/");
header("Location:" . $root . $controller . "/" . $action);
}
}

View File

@ -0,0 +1,32 @@
<?php
require_once $GLOBALS['app_path'].'Framework/Configuration.php';
abstract class Model
{
private static $bdd;
protected function executeRequest($sql, $params = null)
{
if ($params == null) {
$result = self::getBdd()->query($sql);
}
else {
$result = self::getBdd()->prepare($sql);
$result->execute($params);
}
return $result;
}
private static function getBdd()
{
if (self::$bdd === null) {
$dsn = Configuration::get("dsn");
$login = Configuration::get("login");
$mdp = Configuration::get("mdp");
self::$bdd = new PDO($dsn, $login, $mdp,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
return self::$bdd;
}
}

View File

@ -0,0 +1,31 @@
<?php
require_once $GLOBALS['app_path'].'Framework/Session.php';
class Request {
private $parameters;
private $session;
public function __construct($parameters) {
$this->parameters = $parameters;
$this->session = new Session();
}
public function existParameter($nom) {
return (isset($this->parameters[$nom]) && $this->parameters[$nom] != "");
}
public function getParameter($nom) {
if ($this->existParameter($nom)) {
return $this->parameters[$nom];
}
else
throw new Exception("Paramètre '$nom' absent de la requête");
}
public function getSession(){
return $this->session;
}
}

View File

@ -0,0 +1,58 @@
<?php
require_once $GLOBALS['app_path'].'Framework/Request.php';
require_once $GLOBALS['app_path'].'Framework/View.php';
class Router {
public function routingRequest() {
try {
$request = new Request(array_merge($_GET, $_POST));
$controller = $this->createController($request);
$action = $this->createAction($request);
$controller->executeAction($action);
}
catch (Exception $e) {
$this->error($e);
}
}
private function createController($request) {
$controller = "Default";
if ($request->existParameter('controller')) {
$controller = $request->getParameter('controller');
$controller = ucfirst(strtolower($controller));
}
$classController = "Controller" . $controller;
$fileController = $GLOBALS['app_path']."Controller/" . $classController . ".php";
if (file_exists($fileController)) {
require($fileController);
$controller = new $classController();
$controller->setRequest($request);
return $controller;
}
else
{
throw new Exception("Fichier '$fileController' introuvable");
return null;
}
}
private function createAction(Request $request) {
$action = "index";
if ($request->existParameter('action')) {
$action = $request->getParameter('action');
}
return $action;
}
private function error($exception) {
$view = new View('error');
$view->generate(array('msgError' => $exception->getMessage()));
}
}

View File

@ -0,0 +1,35 @@
<?php
class Session
{
public function __construct()
{
session_start();
}
public function destruct()
{
session_destroy();
}
public function setAttribute($name, $value)
{
$_SESSION[$name] = $value;
}
public function existAttribute($name)
{
return (isset($_SESSION[$name]) && $_SESSION[$name] != "");
}
public function getAttribute($name)
{
if ($this->existAttribute($name)) {
return $_SESSION[$name];
}
else {
throw new Exception("Attribut '$name' absent de la session");
}
}
}

View File

@ -0,0 +1,49 @@
<?php
require_once $GLOBALS['app_path'].'Framework/Configuration.php';
class View {
private $file;
public function __construct($action, $controller = "") {
$file = $GLOBALS['app_path']."View/";
if ($controller != "") {
$file = $file . $controller . "/";
}
$this->file = $file . $action . ".php";
}
public function generate($data) {
$content = $this->generateFile($this->file, $data);
$title = "";
$headScript = "";
if(isset($data['title']))
$title = $data['title'];
if(isset($data['headScript']))
$headScript = $data['headScript'];
$root = Configuration::get("root", "/");
$vue = $this->generateFile($GLOBALS['app_path'].'View/template.php',
array('headScript'=> $headScript, 'title' => $title, 'content' => $content,
'root' => $root));
echo $vue;
}
private function generateFile($file, $data) {
if (file_exists($file)) {
extract($data);
ob_start();
require $file;
return ob_get_clean();
}
else {
throw new Exception("Fichier '$file' introuvable");
return null;
}
}
private function clean($value) {
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
}
}