refactor: suppression de la sauvegarde json (JsonExporter, data/)
This commit is contained in:
1 parent
56244d2e95
commit
7de8e4782b
8 files changed
+4
-48
No files matched your search
@@ -1,7 +1,6 @@
|
||||
.git
|
||||
*.sqlite
|
||||
*.sqlite-journal
|
||||
data/*.json
|
||||
.DS_Store
|
||||
.idea/
|
||||
.vscode/
|
||||
@@ -1,4 +1,3 @@
|
||||
data/*.json
|
||||
*.sqlite
|
||||
*.sqlite-journal
|
||||
.DS_Store
|
||||
|
||||
@@ -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
|
||||
```
|
||||
+1
-3
@@ -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:
|
||||
db-data:
|
||||
@@ -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);
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
final class JsonExporter
|
||||
{
|
||||
private PDO $pdo;
|
||||
private string $filePath;
|
||||
|
||||
public function __construct(PDO $pdo, string $filePath)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+1
-4
@@ -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
|
||||
curl -s -w "\n[%{http_code}]\n" -X DELETE "$URL/1"
|
||||
Reference in new issue
Block a user