Trailing slash removal + route caching

This commit is contained in:
Belle Aerni 2023-11-10 03:22:45 -08:00
parent cf6d13d1ad
commit 5a1e17beea
5 changed files with 69 additions and 12 deletions

View file

@ -15,6 +15,7 @@
"embed/embed": "^4.4",
"league/commonmark": "^2.3",
"middlewares/https": "^2.0",
"middlewares/trailing-slash": "^2.0",
"nyholm/psr7": "^1.8",
"nyholm/psr7-server": "^1.1",
"psr/http-message": "2.0 as 1.1",

56
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "03371ff18f92e62d0da1155ec5876278",
"content-hash": "e4fa5187f135275ae659690c48f1572b",
"packages": [
{
"name": "antcms/antloader",
@ -714,6 +714,60 @@
},
"time": "2020-12-02T00:06:18+00:00"
},
{
"name": "middlewares/trailing-slash",
"version": "v2.0.1",
"source": {
"type": "git",
"url": "https://github.com/middlewares/trailing-slash.git",
"reference": "1bedcedbc89be78595c5a7a86776fe5ed003e819"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/middlewares/trailing-slash/zipball/1bedcedbc89be78595c5a7a86776fe5ed003e819",
"reference": "1bedcedbc89be78595c5a7a86776fe5ed003e819",
"shasum": ""
},
"require": {
"middlewares/utils": "^3.0",
"php": "^7.2 || ^8.0",
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.0",
"laminas/laminas-diactoros": "^2.2",
"oscarotero/php-cs-fixer-config": "^1.0",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^8|^9",
"squizlabs/php_codesniffer": "^3.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Middlewares\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Middleware to normalize the trailing slash of the uri path",
"homepage": "https://github.com/middlewares/trailing-slash",
"keywords": [
"http",
"middleware",
"normalize",
"path",
"psr-15",
"psr-7",
"slash"
],
"support": {
"issues": "https://github.com/middlewares/trailing-slash/issues",
"source": "https://github.com/middlewares/trailing-slash/tree/v2.0.1"
},
"time": "2020-12-02T00:06:55+00:00"
},
{
"name": "middlewares/utils",
"version": "v3.3.0",

View file

@ -7,7 +7,6 @@ use AntCMS\AntPages;
use AntCMS\AntConfig;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Psr\Http\Message\UriInterface;
class AntCMS
@ -15,12 +14,10 @@ class AntCMS
protected $antTwig;
protected ?Response $response = null;
protected ?Request $request = null;
protected ?App $app = null;
public function __construct(?App $app)
public function __construct()
{
$this->antTwig = new AntTwig();
$this->app = $app;
}
public function SetResponse(?Response $response): void
@ -43,11 +40,6 @@ class AntCMS
return $this->request;
}
public function getApp()
{
return $this->app;
}
/**
* Renders the page based on the request URI
*/

View file

@ -14,7 +14,7 @@ if (in_array('xxh128', hash_algos())) {
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$classMapPath = __DIR__ . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR . 'classMap.php';
$classMapPath = AntCachePath . DIRECTORY_SEPARATOR . 'classMap.php';
$loader = new \AntCMS\AntLoader(['path' => $classMapPath]);
$loader->addNamespace('AntCMS\\', __DIR__ . DIRECTORY_SEPARATOR . 'AntCMS');
$loader->checkClassMap();

View file

@ -28,12 +28,22 @@ $app = AppFactory::create();
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);
$antCMS = new AntCMS($app);
$trailingSlash = new Middlewares\TrailingSlash();
$app->addMiddleware($trailingSlash->redirect());
if (AntConfig::currentConfig('forceHTTPS') && !\AntCMS\AntEnviroment::isCli()) {
$app->addMiddleware(new Middlewares\Https());
}
if (AntConfig::currentConfig('cacheMode') !== 'none') {
$routeCollector = $app->getRouteCollector();
$routeCollector->setCacheFile(AntCachePath . DIRECTORY_SEPARATOR . 'routes.cache');
}
$routeCollector = $app->getRouteCollector();
$antCMS = new AntCMS($app);
$app->get('/themes/{theme}/assets', function (Request $request, Response $response) use ($antCMS) {
$antCMS->setRequest($request);
$antCMS->SetResponse($response);