replaced eloquent by redbeanphp
This commit is contained in:
19
src/Config/redbean.php
Normal file
19
src/Config/redbean.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use RedBeanPHP\R;
|
||||
|
||||
/**
|
||||
* Initialise RedBeanPHP avec SQLite.
|
||||
* Appelé depuis public/index.php avant le chargement des routes.
|
||||
*/
|
||||
function initRedBean(string $dbPath): void
|
||||
{
|
||||
// Le préfixe « sqlite: » indique le driver SQLite
|
||||
R::setup('sqlite:' . $dbPath);
|
||||
|
||||
// En mode production on désactive le « freeze » pour que RedBean
|
||||
// ne crée plus de tables automatiquement.
|
||||
// Ici on garde le freeze à false pendant le développement.
|
||||
R::freeze(false);
|
||||
}
|
||||
79
src/Controllers/PostController.php
Normal file
79
src/Controllers/PostController.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Views\Twig;
|
||||
use App\Models\Post;
|
||||
|
||||
/**
|
||||
* Contrôleur dédié à la gestion des articles.
|
||||
*/
|
||||
class PostController
|
||||
{
|
||||
private Twig $view;
|
||||
|
||||
public function __construct(Twig $view)
|
||||
{
|
||||
$this->view = $view;
|
||||
}
|
||||
|
||||
/** Page publique – liste des articles */
|
||||
public function index(Request $request, Response $response): Response
|
||||
{
|
||||
$posts = Post::allDesc();
|
||||
return $this->view->render($response, 'pages/home.twig', ['posts' => $posts]);
|
||||
}
|
||||
|
||||
/** Tableau de bord admin */
|
||||
public function admin(Request $request, Response $response): Response
|
||||
{
|
||||
$posts = Post::allDesc();
|
||||
return $this->view->render($response, 'pages/admin.twig', ['posts' => $posts]);
|
||||
}
|
||||
|
||||
/** Formulaire création / édition */
|
||||
public function form(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$id = (int)($args['id'] ?? 0);
|
||||
$post = $id ? Post::find($id) : null; // id=0 → création
|
||||
|
||||
$action = $id ? "/admin/edit/{$id}" : "/admin/create";
|
||||
|
||||
return $this->view->render($response, 'pages/post_form.twig', [
|
||||
'action' => $action,
|
||||
'post' => $post,
|
||||
]);
|
||||
}
|
||||
|
||||
/** Enregistrement d’un nouvel article */
|
||||
public function create(Request $request, Response $response): Response
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
$post = Post::make($data);
|
||||
$post->save();
|
||||
|
||||
return $response->withHeader('Location', '/admin')->withStatus(302);
|
||||
}
|
||||
|
||||
/** Mise à jour d’un article existant */
|
||||
public function update(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$post = Post::find((int)$args['id']);
|
||||
$post->updateFromArray($request->getParsedBody());
|
||||
$post->save();
|
||||
|
||||
return $response->withHeader('Location', '/admin')->withStatus(302);
|
||||
}
|
||||
|
||||
/** Suppression d’un article */
|
||||
public function delete(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$post = Post::find((int)$args['id']);
|
||||
$post->delete();
|
||||
|
||||
return $response->withHeader('Location', '/admin')->withStatus(302);
|
||||
}
|
||||
}
|
||||
2
src/Middleware/AuthMiddleware.php
Normal file
2
src/Middleware/AuthMiddleware.php
Normal file
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
// Non utilisé pour l'instant
|
||||
@@ -1,16 +1,112 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models; // ← namespace attendu par routes.php
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use RedBeanPHP\R;
|
||||
use RedBeanPHP\OODBBean;
|
||||
|
||||
/**
|
||||
* Modèle Eloquent représentant un article.
|
||||
* Modèle RedBeanPHP minimal pour l'entité « post ».
|
||||
*
|
||||
* Chaque instance encapsule un bean RedBean (`OODBBean`).
|
||||
* Les opérations CRUD sont déléguées à RedBean, tandis que les
|
||||
* getters/setters offrent une API typée.
|
||||
*/
|
||||
class Post extends Model
|
||||
class Post
|
||||
{
|
||||
protected $table = 'posts';
|
||||
protected $fillable = ['title', 'content'];
|
||||
public $timestamps = false;
|
||||
/** @var OODBBean le bean réel géré par RedBean */
|
||||
private OODBBean $bean;
|
||||
|
||||
/** Constructeur privé – on crée/charge les objets via les factories */
|
||||
private function __construct(OODBBean $bean)
|
||||
{
|
||||
$this->bean = $bean;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------
|
||||
FACTORIES
|
||||
------------------------------------------------- */
|
||||
|
||||
/** Crée un nouveau post à partir d’un tableau de données */
|
||||
public static function make(array $data): self
|
||||
{
|
||||
$bean = R::dispense('post');
|
||||
$bean->title = $data['title'] ?? '';
|
||||
$bean->content = $data['content'] ?? '';
|
||||
return new self($bean);
|
||||
}
|
||||
|
||||
/** Charge un post existant (lève une exception s’il n’existe pas) */
|
||||
public static function find(int $id): self
|
||||
{
|
||||
$bean = R::load('post', $id);
|
||||
if ($bean->id === 0) {
|
||||
throw new \RuntimeException("Post {$id} introuvable");
|
||||
}
|
||||
return new self($bean);
|
||||
}
|
||||
|
||||
/** Retourne tous les posts, triés par id décroissant */
|
||||
public static function allDesc(): array
|
||||
{
|
||||
$beans = R::findAll('post', ' ORDER BY id DESC ');
|
||||
return array_map(fn(OODBBean $b) => new self($b), $beans);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------
|
||||
PERSISTENCE
|
||||
------------------------------------------------- */
|
||||
|
||||
/** Enregistre (INSERT ou UPDATE) le bean */
|
||||
public function save(): void
|
||||
{
|
||||
R::store($this->bean);
|
||||
}
|
||||
|
||||
/** Supprime le bean de la base */
|
||||
public function delete(): void
|
||||
{
|
||||
R::trash($this->bean);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------
|
||||
ACCESSEURS / MUTATEURS
|
||||
------------------------------------------------- */
|
||||
|
||||
public function getId(): int { return (int) $this->bean->id; }
|
||||
public function getTitle(): string { return (string) $this->bean->title; }
|
||||
public function getContent(): string { return (string) $this->bean->content; }
|
||||
|
||||
public function setTitle(string $title): void { $this->bean->title = $title; }
|
||||
public function setContent(string $content): void { $this->bean->content = $content; }
|
||||
|
||||
/* -------------------------------------------------
|
||||
MISE À JOUR À PARTIR D'UN TABLEAU
|
||||
------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Met à jour le bean à partir d’un tableau de données.
|
||||
*
|
||||
* Seuls les champs connus sont pris en compte ; les clés inconnues
|
||||
* sont simplement ignorées. Cette méthode facilite l’appel depuis
|
||||
* le contrôleur : `$post->updateFromArray($request->getParsedBody());`
|
||||
*
|
||||
* @param array<string,mixed> $data Données du formulaire
|
||||
*/
|
||||
public function updateFromArray(array $data): void
|
||||
{
|
||||
// titre
|
||||
if (array_key_exists('title', $data)) {
|
||||
$this->setTitle((string) $data['title']);
|
||||
}
|
||||
|
||||
// contenu
|
||||
if (array_key_exists('content', $data)) {
|
||||
$this->setContent((string) $data['content']);
|
||||
}
|
||||
|
||||
// Si d’autres champs sont ajoutés à l’entité (ex. author, status),
|
||||
// il suffit de les gérer ici de la même façon.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Slim\App;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use App\Models\Post;
|
||||
use Slim\Views\Twig;
|
||||
|
||||
return function (App $app) {
|
||||
|
||||
// -------------------------------------------------
|
||||
// Page admin – tableau de bord
|
||||
// -------------------------------------------------
|
||||
$app->get('/admin', function (Request $request, Response $response) use ($app) {
|
||||
$posts = Post::orderByDesc('id')->get();
|
||||
|
||||
/** @var Twig $view */
|
||||
$view = $request->getAttribute('view');
|
||||
return $view->render($response, 'pages/admin.twig', ['posts' => $posts]);
|
||||
});
|
||||
|
||||
// -------------------------------------------------
|
||||
// Création d'un article (POST depuis admin)
|
||||
// -------------------------------------------------
|
||||
$app->post('/admin/create', function (Request $request, Response $response) {
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
Post::create([
|
||||
'title' => $data['title'],
|
||||
'content' => $data['content'],
|
||||
]);
|
||||
|
||||
return $response
|
||||
->withHeader('Location', '/admin')
|
||||
->withStatus(302);
|
||||
});
|
||||
|
||||
// -------------------------------------------------
|
||||
// Formulaire d'édition (GET depuis admin)
|
||||
// -------------------------------------------------
|
||||
$app->get('/admin/edit/{id}', function (Request $request, Response $response, $args) use ($app) {
|
||||
$id = (int)$args['id'];
|
||||
$post = $id ? Post::findOrFail($id) : null; // id=0 → création
|
||||
|
||||
/** @var Twig $view */
|
||||
$view = $request->getAttribute('view');
|
||||
|
||||
return $view->render($response, 'pages/post_form.twig', [
|
||||
'action' => $id ? "/admin/edit/{$id}" : "/admin/create",
|
||||
'post' => $post,
|
||||
]);
|
||||
});
|
||||
|
||||
// -------------------------------------------------
|
||||
// Enregistrement des modifications (POST depuis admin)
|
||||
// -------------------------------------------------
|
||||
$app->post('/admin/edit/{id}', function (Request $request, Response $response, $args) {
|
||||
$post = Post::findOrFail($args['id']);
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
$post->update([
|
||||
'title' => $data['title'],
|
||||
'content' => $data['content'],
|
||||
]);
|
||||
|
||||
return $response
|
||||
->withHeader('Location', '/admin')
|
||||
->withStatus(302);
|
||||
});
|
||||
|
||||
// -------------------------------------------------
|
||||
// Suppression d'un article (POST depuis admin)
|
||||
// -------------------------------------------------
|
||||
$app->post('/admin/delete/{id}', function (Request $request, Response $response, $args) {
|
||||
$post = Post::findOrFail($args['id']);
|
||||
$post->delete();
|
||||
|
||||
return $response
|
||||
->withHeader('Location', '/admin')
|
||||
->withStatus(302);
|
||||
});
|
||||
};
|
||||
24
src/Routes/admin.routes.php
Normal file
24
src/Routes/admin.routes.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Slim\App;
|
||||
use App\Controllers\PostController;
|
||||
|
||||
/**
|
||||
* Routes d'administration du blog.
|
||||
*/
|
||||
return function (App $app): void {
|
||||
// Tableau de bord admin – liste des posts
|
||||
$app->get('/admin', [PostController::class, 'admin']);
|
||||
|
||||
// Formulaire de création
|
||||
$app->get('/admin/create', [PostController::class, 'form']);
|
||||
$app->post('/admin/create', [PostController::class, 'create']);
|
||||
|
||||
// Formulaire d'édition – l'ID doit être fourni dans l'URL
|
||||
$app->get('/admin/edit/{id}', [PostController::class, 'form']);
|
||||
$app->post('/admin/edit/{id}', [PostController::class, 'update']);
|
||||
|
||||
// Suppression d'un post
|
||||
$app->post('/admin/delete/{id}', [PostController::class, 'delete']);
|
||||
};
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Slim\App;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use App\Models\Post;
|
||||
use Slim\Views\Twig;
|
||||
|
||||
return function (App $app) {
|
||||
|
||||
// -------------------------------------------------
|
||||
// Page publique – liste des articles
|
||||
// -------------------------------------------------
|
||||
$app->get('/', function (Request $request, Response $response) use ($app) {
|
||||
$posts = Post::orderByDesc('id')->get();
|
||||
|
||||
/** @var Twig $view */
|
||||
$view = $request->getAttribute('view'); // <-- récupération correcte
|
||||
return $view->render($response, 'pages/home.twig', ['posts' => $posts]);
|
||||
});
|
||||
};
|
||||
17
src/Routes/home.routes.php
Normal file
17
src/Routes/home.routes.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Slim\App;
|
||||
use App\Controllers\PostController;
|
||||
|
||||
/**
|
||||
* Routes publiques du blog.
|
||||
*
|
||||
* Chargé de la même façon que les autres fichiers *.routes.php
|
||||
* depuis public/index.php.
|
||||
*/
|
||||
|
||||
return function (App $app): void {
|
||||
// Page d'accueil – liste des articles
|
||||
$app->get('/', [PostController::class, 'index']);
|
||||
};
|
||||
Reference in New Issue
Block a user