From 7de8e4782b84ea9c2b367858768283aab5e7e335 Mon Sep 17 00:00:00 2001 From: gilles Date: Mon, 14 Sep 2026 18:04:05 +0200 Subject: [PATCH] refactor: suppression de la sauvegarde json (JsonExporter, data/) --- .dockerignore | 1 - .gitignore | 1 - README.md | 11 ++--------- docker-compose.yml | 4 +--- public/index.php | 2 -- src/JsonExporter.php | 24 ------------------------ src/LinkController.php | 4 ---- tests/curl.sh | 5 +---- 8 files changed, 4 insertions(+), 48 deletions(-) delete mode 100644 src/JsonExporter.php diff --git a/.dockerignore b/.dockerignore index 0a60e9e..5ee4e76 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,6 @@ .git *.sqlite *.sqlite-journal -data/*.json .DS_Store .idea/ .vscode/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1f48621..fd3fe6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -data/*.json *.sqlite *.sqlite-journal .DS_Store diff --git a/README.md b/README.md index 5b46e3f..b4ac1b3 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ API REST en PHP natif (POO) pour gérer des liens de vidéos YouTube. - PHP natif uniquement : aucun framework, aucune dépendance, aucun Composer - Stockage SQLite via PDO -- À chaque création / modification / suppression, un snapshot complet de la table est écrit dans `data/data.json` ## Prérequis @@ -66,10 +65,6 @@ php database/migrate.php `title` et `link_url` sont obligatoires ; `description` et `thumbnail` sont optionnels. `link_url` doit être une URL valide et unique. -## Fichier `data/data.json` - -Régénéré à chaque POST / PUT / DELETE : contient la liste complète des liens, formatée (pretty print). - ## Tests manuels ```bash @@ -94,7 +89,7 @@ docker compose down -v # supprime la base (reset complet) ### Persistance -La base SQLite est stockée dans le Docker volume `db-data` et le fichier `data/data.json` dans le volume `data-data`. Ils survivent aux `down`/`up` successifs. Un `down -v` les supprime ; le prochain démarrage recrée la base via les migrations. +La base SQLite est stockée dans le Docker volume `db-data` : elle survit aux `down`/`up` successifs. Un `down -v` la supprime ; le prochain démarrage recrée la base via les migrations. ### Rebuild après modification du code @@ -111,13 +106,11 @@ public/ src/ Database.php # connexion PDO SQLite + exécution des migrations LinkRepository.php # CRUD SQL - JsonExporter.php # écriture de data/data.json LinkController.php # routage, validation, orchestration database/ migrate.php # runner de migrations (CLI) migrations/ # fichiers SQL versionnés -data/ # data.json (volume Docker dédié) Dockerfile # image PHP 8.4 Apache + mod_rewrite -docker-compose.yml # service web + volumes db-data et data-data +docker-compose.yml # service web + volume db-data .dockerignore ``` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 07738d1..be11c99 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,8 +8,6 @@ services: - "8000:80" volumes: - db-data:/var/www/html/database - - data-data:/var/www/html/data volumes: - db-data: - data-data: \ No newline at end of file + db-data: \ No newline at end of file diff --git a/public/index.php b/public/index.php index 50a1350..44aecfc 100644 --- a/public/index.php +++ b/public/index.php @@ -4,7 +4,6 @@ declare(strict_types=1); require_once __DIR__ . '/../src/Database.php'; require_once __DIR__ . '/../src/LinkRepository.php'; -require_once __DIR__ . '/../src/JsonExporter.php'; require_once __DIR__ . '/../src/LinkController.php'; header('Content-Type: application/json; charset=utf-8'); @@ -22,7 +21,6 @@ try { $controller = new LinkController( new LinkRepository($pdo), - new JsonExporter($pdo, dirname(__DIR__) . '/data/data.json'), ); $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); diff --git a/src/JsonExporter.php b/src/JsonExporter.php deleted file mode 100644 index 04b536c..0000000 --- a/src/JsonExporter.php +++ /dev/null @@ -1,24 +0,0 @@ -pdo = $pdo; - $this->filePath = $filePath; - } - - public function export(): void - { - $links = $this->pdo->query('SELECT * FROM links ORDER BY id DESC')->fetchAll(); - - $json = json_encode($links, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); - - file_put_contents($this->filePath, $json . PHP_EOL, LOCK_EX); - } -} diff --git a/src/LinkController.php b/src/LinkController.php index d64a2fc..353d854 100644 --- a/src/LinkController.php +++ b/src/LinkController.php @@ -6,7 +6,6 @@ final class LinkController { public function __construct( private readonly LinkRepository $repository, - private readonly JsonExporter $exporter, ) { } @@ -80,7 +79,6 @@ final class LinkController return; } - $this->exporter->export(); $this->json($this->repository->find($id), 201); } @@ -115,7 +113,6 @@ final class LinkController return; } - $this->exporter->export(); $this->json($this->repository->find($id)); } @@ -128,7 +125,6 @@ final class LinkController } $this->repository->delete($id); - $this->exporter->export(); http_response_code(204); } diff --git a/tests/curl.sh b/tests/curl.sh index cf05268..55a268f 100755 --- a/tests/curl.sh +++ b/tests/curl.sh @@ -34,7 +34,4 @@ echo "== GET /links/999 (introuvable -> 404) ==" curl -s -w "\n[%{http_code}]\n" "$URL/999" echo "== DELETE /links/1 (suppression) ==" -curl -s -w "\n[%{http_code}]\n" -X DELETE "$URL/1" - -echo "== data.json ==" -cat data/data.json \ No newline at end of file +curl -s -w "\n[%{http_code}]\n" -X DELETE "$URL/1" \ No newline at end of file