diff --git a/Exercice.md b/Exercice.md index 04de2cba4c6fd68d9362ced03732fd95bf88d7a2..2c50f9acdda1834457adb8f3d455e011a19c1e91 100644 --- a/Exercice.md +++ b/Exercice.md @@ -20,7 +20,7 @@ Idéalement, un fork du projet avec vos notes et les modifications que vous avez ## Deuxième étape - La prise en main - [x] Faire marcher l'application -- [ ] Créer un mode d'emploi pour faire marcher l'application (un docker-compose par exemple ?) +- [x] Créer un mode d'emploi pour faire marcher l'application (un docker-compose par exemple ?) - [ ] Trouver les dépendances non maintenu ## Troisième étape - La maintenance diff --git a/composer.json b/composer.json index 9654d2f7e4bfc0280c26ac9b4dbf5d3a9a15223a..3b49acc0f8d750eb17e39b456f0bb9a3519a041a 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,9 @@ { "require": { - "slim/slim": "2.*", - "twig/twig": "~1.0", - "illuminate/database": "4.2.9" + "slim/slim": "4.*", + "slim/psr7" : "^1.6", + "twig/twig": "^3.0", + "illuminate/database": "^10.0" }, "autoload":{ "psr-0":{ diff --git a/controller/Search.php b/controller/Search.php index fc0b01edbf55d4e682dd19d539e44ba824c2d48c..af9b087e02f529fcfb36374c90caa597159b2cab 100644 --- a/controller/Search.php +++ b/controller/Search.php @@ -5,26 +5,42 @@ namespace controller; use model\Annonce; use model\Categorie; -class Search { +class Search +{ - function show($twig, $menu, $chemin, $cat) { - $template = $twig->loadTemplate("search.html.twig"); + function show($twig, $menu, $chemin, $cat) + { $menu = array( - array('href' => $chemin, - 'text' => 'Acceuil'), - array('href' => $chemin."/search", - 'text' => "Recherche") + array( + 'href' => $chemin, + 'text' => 'Acceuil' + ), + array( + 'href' => $chemin . "/search", + 'text' => "Recherche" + ) ); - echo $template->render(array("breadcrumb" => $menu, "chemin" => $chemin, "categories" => $cat)); + + $content = $twig->render('search.html.twig', [ + "breadcrumb" => $menu, + "chemin" => $chemin, + "categories" => $cat + ]); + + return $content; } - function research($array, $twig, $menu, $chemin, $cat) { - $template = $twig->loadTemplate("index.html.twig"); + function research($array, $twig, $menu, $chemin, $cat) + { $menu = array( - array('href' => $chemin, - 'text' => 'Acceuil'), - array('href' => $chemin."/search", - 'text' => "Résultats de la recherche") + array( + 'href' => $chemin, + 'text' => 'Acceuil' + ), + array( + 'href' => $chemin . "/search", + 'text' => "Résultats de la recherche" + ) ); $nospace_mc = str_replace(' ', '', $array['motclef']); @@ -33,45 +49,53 @@ class Search { $query = Annonce::select(); - if( ($nospace_mc === "") && + if ( + ($nospace_mc === "") && ($nospace_cp === "") && (($array['categorie'] === "Toutes catégories" || $array['categorie'] === "-----")) && ($array['prix-min'] === "Min") && - ( ($array['prix-max'] === "Max") || ($array['prix-max'] === "nolimit") ) ) { + (($array['prix-max'] === "Max") || ($array['prix-max'] === "nolimit")) + ) { $annonce = Annonce::all(); } else { // A REFAIRE SEPARER LES TRUCS - if( ($nospace_mc !== "") ) { - $query->where('description', 'like', '%'.$array['motclef'].'%'); + if (($nospace_mc !== "")) { + $query->where('description', 'like', '%' . $array['motclef'] . '%'); } - if( ($nospace_cp !== "") ) { + if (($nospace_cp !== "")) { $query->where('ville', '=', $array['codepostal']); } - if ( ($array['categorie'] !== "Toutes catégories" && $array['categorie'] !== "-----") ) { + if (($array['categorie'] !== "Toutes catégories" && $array['categorie'] !== "-----")) { $categ = Categorie::select('id_categorie')->where('id_categorie', '=', $array['categorie'])->first()->id_categorie; $query->where('id_categorie', '=', $categ); } - if ( $array['prix-min'] !== "Min" && $array['prix-max'] !== "Max") { - if($array['prix-max'] !== "nolimit") { + if ($array['prix-min'] !== "Min" && $array['prix-max'] !== "Max") { + if ($array['prix-max'] !== "nolimit") { $query->whereBetween('prix', array($array['prix-min'], $array['prix-max'])); } else { $query->where('prix', '>=', $array['prix-min']); } - } elseif ( $array['prix-max'] !== "Max" && $array['prix-max'] !== "nolimit") { + } elseif ($array['prix-max'] !== "Max" && $array['prix-max'] !== "nolimit") { $query->where('prix', '<=', $array['prix-max']); - } elseif ( $array['prix-min'] !== "Min" ) { + } elseif ($array['prix-min'] !== "Min") { $query->where('prix', '>=', $array['prix-min']); } $annonce = $query->get(); } - echo $template->render(array("breadcrumb" => $menu, "chemin" => $chemin, "annonces" => $annonce, "categories" => $cat)); - + $content = $twig->render('index.html.twig', [ + "breadcrumb" => $menu, + "chemin" => $chemin, + "annonces" => $annonce, + "categories" => $cat + ]); + + return $content; } } diff --git a/controller/index.php b/controller/index.php index 075971a0beb083ce905e69802eff3ed11b403db8..bc783287e683c8eff1de1cf8d5dadeae5af4f773 100644 --- a/controller/index.php +++ b/controller/index.php @@ -33,17 +33,17 @@ class index { } public function displayAllAnnonce($twig, $menu, $chemin, $cat) { - $template = $twig->loadTemplate("index.html.twig"); $menu = array( array('href' => $chemin, 'text' => 'Acceuil'), ); $this->getAll($chemin); - echo $template->render(array( + echo $twig->render("index.html.twig", array( "breadcrumb" => $menu, "chemin" => $chemin, "categories" => $cat, - "annonces" => $this->annonce)); + "annonces" => $this->annonce + )); } } \ No newline at end of file diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile index f5a8e9f23585f116bd5ec7f4975ca9f0838de11b..41087ad8782c3e31c72caa151d4de68a7f769cd6 100644 --- a/docker/php/Dockerfile +++ b/docker/php/Dockerfile @@ -1,4 +1,4 @@ -FROM php:7.4 +FROM php:8.4 RUN apt update RUN apt install git zip unzip libxml2-dev -y diff --git a/index.php b/index.php index 9913d6c73e8705c6955aed17d363b1af37cc72f7..1ebc5b271ab8187bd8cf360eb634cf1fdec95925 100644 --- a/index.php +++ b/index.php @@ -9,14 +9,15 @@ use model\Annonce; use model\Categorie; use model\Annonceur; use model\Departement; - +use Slim\Factory\AppFactory; +use Slim\Psr7\Factory\ResponseFactory; +use Twig\Loader\FilesystemLoader; +use Twig\Environment; connection::createConn(); - -$app = new \Slim\Slim(array( - 'mode' => 'development' -)); +$responseFactory = new ResponseFactory(); +$app = AppFactory::create($responseFactory); if (!isset($_SESSION)) { session_start(); @@ -33,8 +34,8 @@ if (!isset($_SESSION['token'])) { //$app->add(new CsrfGuard()); -$loader = new Twig_Loader_Filesystem('template'); -$twig = new Twig_Environment($loader); +$loader = new FilesystemLoader('template'); +$twig = new Environment($loader); $menu = array( array('href' => "./index.php", @@ -46,185 +47,165 @@ $chemin = dirname($_SERVER['SCRIPT_NAME']); $cat = new \controller\getCategorie(); $dpt = new \controller\getDepartment(); -$app->get('/', function () use ($twig, $menu, $chemin, $cat) { +$app->get('/', function ($request, $response, $args) use ($twig, $menu, $chemin, $cat) { $index = new \controller\index(); $index->displayAllAnnonce($twig, $menu, $chemin, $cat->getCategories()); + return $response; }); -$app->get('/item/:n', function ($n) use ($twig, $menu, $chemin, $cat) { +$app->get('/item/{n}', function ($request, $response, $args) use ($twig, $menu, $chemin, $cat) { + $n = $args['n']; $item = new \controller\item(); $item->afficherItem($twig, $menu, $chemin, $n, $cat->getCategories()); + return $response; }); -$app->get('/add/', function () use ($twig, $app, $menu, $chemin, $cat, $dpt) { - +$app->get('/add', function ($request, $response, $args) use ($twig, $app, $menu, $chemin, $cat, $dpt) { $ajout = new controller\addItem(); $ajout->addItemView($twig, $menu, $chemin, $cat->getCategories(), $dpt->getAllDepartments()); - + return $response; }); -$app->post('/add/', function () use ($twig, $app, $menu, $chemin) { - - $allPostVars = $app->request->post(); +$app->post('/add', function ($request, $response, $args) use ($twig, $menu, $chemin) { + $allPostVars = $request->getParsedBody(); $ajout = new controller\addItem(); $ajout->addNewItem($twig, $menu, $chemin, $allPostVars); + return $response; }); -$app->get('/item/:id/edit', function ($id) use ($twig, $menu, $chemin) { +$app->get('/item/{id}/edit', function ($request, $response, $args) use ($twig, $menu, $chemin) { + $id = $args['id']; $item = new \controller\item(); - $item->modifyGet($twig,$menu,$chemin, $id); + $item->modifyGet($twig, $menu, $chemin, $id); + return $response; }); -$app->post('/item/:id/edit', function ($id) use ($twig, $app, $menu, $chemin, $cat, $dpt) { - $allPostVars = $app->request->post(); - $item= new \controller\item(); - $item->modifyPost($twig,$menu,$chemin, $id, $allPostVars, $cat->getCategories(), $dpt->getAllDepartments()); +$app->post('/item/{id}/edit', function ($request, $response, $args) use ($twig, $menu, $chemin, $cat, $dpt) { + $id = $args['id']; + $allPostVars = $request->getParsedBody(); + $item = new \controller\item(); + $item->modifyPost($twig, $menu, $chemin, $id, $allPostVars, $cat->getCategories(), $dpt->getAllDepartments()); + return $response; }); -$app->map('/item/:id/confirm', function ($id) use ($twig, $app, $menu, $chemin) { - $allPostVars = $app->request->post(); +$app->any('/item/{id}/confirm', function ($request, $response, $args) use ($twig, $app, $menu, $chemin) { + $id = $args['id']; + $allPostVars = $request->getParsedBody(); $item = new \controller\item(); - $item->edit($twig,$menu,$chemin, $id, $allPostVars); -})->name('confirm')->via('GET', 'POST'); - -$app->get('/search/', function () use ($twig, $menu, $chemin, $cat) { - $s = new controller\Search(); - $s->show($twig, $menu, $chemin, $cat->getCategories()); + $item->edit($twig, $menu, $chemin, $id, $allPostVars); + return $response; +})->setName('confirm'); + +$app->get('/search', function ($request, $response) use ($twig, $menu, $chemin, $cat) { + $search = new \controller\Search(); + $content = $search->show($twig, $menu, $chemin, $cat->getCategories()); + $response->getBody()->write($content); + return $response; }); - -$app->post('/search/', function () use ($app, $twig, $menu, $chemin, $cat) { - $array = $app->request->post(); - - $s = new controller\Search(); - $s->research($array, $twig, $menu, $chemin, $cat->getCategories()); - +$app->post('/search', function ($request, $response) use ($twig, $menu, $chemin, $cat) { + $allPostVars = $request->getParsedBody(); + $search = new \controller\Search(); + $content = $search->research($allPostVars, $twig, $menu, $chemin, $cat->getCategories()); + $response->getBody()->write($content); + return $response; }); -$app->get('/annonceur/:n', function ($n) use ($twig, $menu, $chemin, $cat) { - $annonceur = new controller\viewAnnonceur(); +$app->get('/annonceur/{n}', function ($request, $response, $args) use ($twig, $menu, $chemin, $cat) { + $n = $args['n']; + $annonceur = new \controller\viewAnnonceur(); $annonceur->afficherAnnonceur($twig, $menu, $chemin, $n, $cat->getCategories()); + return $response; }); -$app->get('/del/:n', function ($n) use ($twig, $menu, $chemin) { - $item = new controller\item(); +$app->get('/del/{n}', function ($request, $response, $args) use ($twig, $menu, $chemin) { + $n = $args['n']; + $item = new \controller\item(); $item->supprimerItemGet($twig, $menu, $chemin, $n); + return $response; }); -$app->post('/del/:n', function ($n) use ($twig, $menu, $chemin, $cat) { +$app->post('/del/{n}', function ($request, $response, $args) use ($twig, $menu, $chemin, $cat) { + $n = $args['n']; $item = new controller\item(); $item->supprimerItemPost($twig, $menu, $chemin, $n, $cat->getCategories()); + return $response; }); -$app->get('/cat/:n', function ($n) use ($twig, $menu, $chemin, $cat) { +$app->get('/cat/{n}', function ($request, $response, $args) use ($twig, $menu, $chemin, $cat) { + $n = $args['n']; $categorie = new controller\getCategorie(); $categorie->displayCategorie($twig, $menu, $chemin, $cat->getCategories(), $n); + return $response; }); -$app->get('/api(/)', function () use ($twig, $menu, $chemin, $cat) { - $template = $twig->loadTemplate("api.html.twig"); - $menu = array( - array('href' => $chemin, - 'text' => 'Acceuil'), - array('href' => $chemin . '/api', - 'text' => 'Api') - ); - echo $template->render(array("breadcrumb" => $menu, "chemin" => $chemin)); -}); - -$app->group('/api', function () use ($app, $twig, $menu, $chemin, $cat) { - - $app->group('/annonce', function () use ($app) { - - $app->get('/:id', function ($id) use ($app) { - $annonceList = ['id_annonce', 'id_categorie as categorie', 'id_annonceur as annonceur', 'id_departement as departement', 'prix', 'date', 'titre', 'description', 'ville']; - $return = Annonce::select($annonceList)->find($id); - - if (isset($return)) { - $app->response->headers->set('Content-Type', 'application/json'); - $return->categorie = Categorie::find($return->categorie); - $return->annonceur = Annonceur::select('email', 'nom_annonceur', 'telephone') - ->find($return->annonceur); - $return->departement = Departement::select('id_departement', 'nom_departement')->find($return->departement); - $links = []; - $links["self"]["href"] = "/api/annonce/" . $return->id_annonce; - $return->links = $links; - echo $return->toJson(); - } else { - $app->notFound(); - } - }); +$app->group('/api', function ($group) use ($twig, $menu, $chemin, $cat) { + + // Route API racine + $group->get('', function ($request, $response) use ($twig, $menu, $chemin) { + $template = $twig->load("api.html.twig"); + $menu = array( + array('href' => $chemin, 'text' => 'Accueil'), + array('href' => $chemin . '/api', 'text' => 'Api') + ); + $body = $template->render(array("breadcrumb" => $menu, "chemin" => $chemin)); + $response->getBody()->write($body); + return $response; }); - $app->group('/annonces(/)', function () use ($app) { - - $app->get('/', function () use ($app) { - $annonceList = ['id_annonce', 'prix', 'titre', 'ville']; - $app->response->headers->set('Content-Type', 'application/json'); - $a = Annonce::all($annonceList); + // Routes pour /api/categories + $group->group('/categories', function ($group) { + $group->get('', function ($request, $response) { + $c = Categorie::get(); $links = []; - foreach ($a as $ann) { - $links["self"]["href"] = "/api/annonce/" . $ann->id_annonce; - $ann->links = $links; + foreach ($c as $cat) { + $links["self"]["href"] = "/api/categorie/" . $cat->id_categorie; + $cat->links = $links; } - $links["self"]["href"] = "/api/annonces/"; - $a->links = $links; - echo $a->toJson(); + $links["self"]["href"] = "/api/categories/"; + $c->links = $links; + + $response->getBody()->write($c->toJson()); + return $response->withHeader('Content-Type', 'application/json'); }); - }); - - $app->group('/categorie', function () use ($app) { - - $app->get('/:id', function ($id) use ($app) { - $app->response->headers->set('Content-Type', 'application/json'); + // Route pour une catégorie spécifique + $group->get('/{id}', function ($request, $response, $args) { + $id = $args['id']; $a = Annonce::select('id_annonce', 'prix', 'titre', 'ville') ->where("id_categorie", "=", $id) ->get(); $links = []; - foreach ($a as $ann) { $links["self"]["href"] = "/api/annonce/" . $ann->id_annonce; $ann->links = $links; } - + $c = Categorie::find($id); - $links["self"]["href"] = "/api/categorie/" . $id; $c->links = $links; $c->annonces = $a; - echo $c->toJson(); - }); - }); - - $app->group('/categories(/)', function () use ($app) { - $app->get('/', function () use ($app) { - $app->response->headers->set('Content-Type', 'application/json'); -// $c = Categorie::all(["id_categorie", "nom_categorie"]); - $c = Categorie::get(); - $links = []; - foreach ($c as $cat) { - $links["self"]["href"] = "/api/categorie/" . $cat->id_categorie; - $cat->links = $links; - } - $links["self"]["href"] = "/api/categories/"; - $c->links = $links; - echo $c->toJson(); + + $response->getBody()->write($c->toJson()); + return $response->withHeader('Content-Type', 'application/json'); }); }); - $app->get('/key', function() use ($app, $twig, $menu, $chemin, $cat) { - $kg = new controller\KeyGenerator(); + // Routes pour /api/key + $group->get('/key', function ($request, $response) use ($twig, $menu, $chemin, $cat) { + $kg = new \controller\KeyGenerator(); $kg->show($twig, $menu, $chemin, $cat->getCategories()); + return $response; }); - $app->post('/key', function() use ($app, $twig, $menu, $chemin, $cat) { - $nom = $_POST['nom']; - - $kg = new controller\KeyGenerator(); + $group->post('/key', function ($request, $response) use ($twig, $menu, $chemin, $cat) { + $data = $request->getParsedBody(); + $nom = $data['nom']; + + $kg = new \controller\KeyGenerator(); $kg->generateKey($twig, $menu, $chemin, $cat->getCategories(), $nom); + return $response; }); }); - $app->run(); diff --git a/notes.md b/notes.md index 828fdfb0825ea8b7c43ce512107cd9484bbd7f70..1a44b1a156924d6c55d571b4787c5d4b59576170 100644 --- a/notes.md +++ b/notes.md @@ -21,7 +21,7 @@ Application de petites annonces entre particuliers permettant de : ### Étapes d'installation 1. Lancer les conteneurs : ```bash -docker-compose up -d +docker compose up -d ``` 2. Installer les dépendances : ```bash @@ -35,5 +35,12 @@ composer install - [x] Configuration Docker fonctionnelle ## Deuxième étape - Prise en main (En cours) -- [ ] Documentation à compléter -- [ ] Audit des dépendances à réaliser \ No newline at end of file +- [x] Documentation à compléter +- [x] Audit des dépendances à réaliser + +Avec la commande : +```bash +composer show -l +``` +Il n'y a aucune MAJ majeure à faire. +Cependant, PHP n'est pas à jour (7.4 au lieu de 8.4) \ No newline at end of file