Arnapou simple site

Php

On peut personnaliser le comportement du site à l'aide de PHP.
Pour cela on met des fichiers php dans le dossier de config path_php et on respecte quelques principes de programmation PHP.

Les fichiers php intégrés avec un comportement doivent retourner un objet, cet objet peut être de deux types.

L'objet retourné implémente l'interface Arnapou\SimpleSite\PhpCode

interface Arnapou\SimpleSite\PhpCode
{
    public function init(): void;
}

Comme dans l'exemple php/twig_globals.php qui injecte en variable global twig des données d'une table yaml :

<?php

declare(strict_types=1);

/*
 * This file is part of the Arnapou Simple Site package.
 *
 * (c) Arnaud Buathier <arnaud@arnapou.net>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use Arnapou\SimpleSite;
use Arnapou\SimpleSite\PhpCode;

return new class() implements PhpCode {
    public function init(): void
    {
        $twigEnvironment = SimpleSite::twigEnvironment();
        $database = SimpleSite::database();

        $parameters = $database->getTable('twig_globals');
        foreach ($parameters as $key => $data) {
            $twigEnvironment->addGlobal($key, $data['value'] ?? '');
        }
    }
};

L'objet retourné étend la classe Arnapou\SimpleSite\Controller

Obligation d'implémenter la méthode abstraite configure (pour les routes) :

abstract class Arnapou\SimpleSite\Controller
{
    abstract public function configure(): void;
}

Comme dans l'exemple php/controller.php qui déclare une route hello pour afficher une page bidon :

<?php

declare(strict_types=1);

/*
 * This file is part of the Arnapou Simple Site package.
 *
 * (c) Arnaud Buathier <arnaud@arnapou.net>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use Arnapou\Psr\Psr7HttpMessage\Response;
use Arnapou\SimpleSite\Controller;

return new class() extends Controller {
    public function configure(): void
    {
        $this->addRoute('hello-{name}', [$this, 'hello'], 'hello')
            ->setRequirement('name', '[a-zA-Z]+');
    }

    public function hello(string $name): Response
    {
        $this->logger()->debug("Hello $name");
        $this->logger()->info("Hello $name");
        $this->logger()->notice("Hello $name");
        $this->logger()->warning("Hello $name");
        $this->logger()->error("Hello $name");
        $this->logger()->critical("Hello $name");
        $this->logger()->alert("Hello $name");
        $this->logger()->emergency("Hello $name");

        return $this->render('@templates/demo/hello.twig', ['name' => $name]);
    }
};

Exemple de page : lien produit avec {{ path('hello', { name: 'world'}) }}

Custom listener sur les routes

On peut ajouter des listeners custom pour changer le comportement du site : changer la réponse par exemple et/ou court-circuiter le routing.

Exemple php/event_hook.php qui court-circuite le routing pour envoyer une autre page :

<?php

declare(strict_types=1);

/*
 * This file is part of the Arnapou Simple Site package.
 *
 * (c) Arnaud Buathier <arnaud@arnapou.net>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use Arnapou\Psr\Psr15HttpHandlers\Routing\Event\ServerRequestEvent;
use Arnapou\Psr\Psr15HttpHandlers\Routing\Listener\ServerRequestListenerInterface;
use Arnapou\Psr\Psr7HttpMessage\HtmlResponse;
use Arnapou\SimpleSite;
use Arnapou\SimpleSite\PhpCode;
use Arnapou\SimpleSite\Controller;

return new class() implements PhpCode {
    public function init(): void
    {
        SimpleSite::router()->addListener($this->getHackListener(), Controller::PRIORITY_HIGHEST);
    }

    private function getHackListener(): ServerRequestListenerInterface
    {
        return new class() implements ServerRequestListenerInterface {
            public function __invoke(ServerRequestEvent $event): void
            {
                if ($event->request->getQueryParams()['killme'] ?? false) {
                    $event->response = new HtmlResponse('<h1>Arrrgghh .... I am killed ...</h1>', 500);
                    $event->stoppedPropagation = true;
                }
            }
        };
    }
};

Tester la page :