Commit 08ad5375 authored by Quentin Vrel's avatar Quentin Vrel

tp2 10-11

parent 6522fb03
<?php
class DatabaseConnector {
protected static $pdo = NULL;
public static function current(){
if(is_null(static::$pdo))
static::createPDO();
return static::$pdo;
}
protected static function createPDO() {
// $db = new PDO("sqlite::memory");
$connectionString = "mysql:host=". DB_HOST;
if(defined('DB_PORT'))
$connectionString .= ";port=". DB_PORT;
$connectionString .= ";dbname=" . DB_DATABASE;
static::$pdo = new PDO($connectionString,DB_USERNAME,DB_PASSWORD);
static::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
\ No newline at end of file
<?php
class UserModel
{
public static function getAllUsers() {
$DB = DatabaseConnector::current();
$request = $DB->prepare("select * from users");
$request->execute();
$request->setFetchMode(PDO::FETCH_CLASS, get_called_class());
$users = $request->fetchAll();
return $users;
}
}
\ No newline at end of file
<?php
class UsersController {
private $requestMethod;
public function __construct($requestMethod)
{
$this->requestMethod = $requestMethod;
}
public function processRequest()
{
switch ($this->requestMethod) {
case 'GET':
$response = $this->getAllUsers();
break;
default:
$response = $this->notFoundResponse();
break;
}
header($response['status_code_header']);
if ($response['body']) {
echo $response['body'];
}
}
private function getAllUsers()
{
$tab_users = UserModel::getAllUsers();
$response['status_code_header'] = http_response_code(200);
$response['body'] = json_encode($tab_users);
return $response;
}
private function notFoundResponse()
{
$response['status_code_header'] = 'HTTP/1.1 404 Not Found';
$response['body'] = null;
return $response;
}
}
<?php
require "bootstrap.php";
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
header("Access-Control-Allow-Methods: GET,POST,PUT,DELETE");
header("Access-Control-Max-Age: 3600"); // Maximum number of seconds the results can be cached.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
function getRoute($url){
$url = trim($url, '/');
$urlSegments = explode('/', $url);
$scheme = ['controller', 'params'];
$route = [];
foreach ($urlSegments as $index => $segment){
if ($scheme[$index] == 'params'){
$route['params'] = array_slice($urlSegments, $index);
break;
} else {
$route[$scheme[$index]] = $segment;
}
}
return $route;
}
// $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
$route = getRoute($uri);
$requestMethod = $_SERVER["REQUEST_METHOD"];
$controllerName = $route['controller'];
switch($controllerName) {
case 'users' :
// GET api.php?/users
// POST api.php?/users
$controller = new UsersController($requestMethod);
break;
default :
header("HTTP/1.1 404 Not Found");
exit();
}
$controller->processRequest();
\ No newline at end of file
<?php
require "config.php";
// simplest auto loader cf. doc PHP
// we will revisit that later
spl_autoload_register(function ($class_name) {
$classFile = $class_name . '.php';
include $classFile;
});
\ No newline at end of file
<?php
define('DB_HOST','127.0.0.1');
define('DB_PORT',3306);
define('DB_DATABASE','dbtest');
define('DB_USERNAME','root');
define('DB_PASSWORD','');
// define('__DEBUG', false);
define('__DEBUG', true);
// ================================================================================
// Debug utilities
// ================================================================================
if(__DEBUG) {
error_reporting(E_ALL);
ini_set("display_errors", E_ALL);
} else {
error_reporting(0);
ini_set("display_errors", 0);
}
function myLog($msg) {
if(__DEBUG) {
echo $msg;
}
}
function myDump($var) {
if(__DEBUG) {
var_dump($var);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment