Started porting to Slim

This commit is contained in:
Belle Aerni 2023-11-10 01:36:11 -08:00
parent d73d3180b7
commit 17fbfdff94
7 changed files with 652 additions and 148 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@
node_modules
src/Cache/*
src/Config/Config.yaml
src/Config/Pages.yaml
src/Config/Pages.yaml
src/php_error.log

View file

@ -14,9 +14,12 @@
"elgigi/commonmark-emoji": "^2.0",
"embed/embed": "^4.4",
"league/commonmark": "^2.3",
"nyholm/psr7": "^1.5",
"nyholm/psr7": "^1.8",
"nyholm/psr7-server": "^1.1",
"psr/http-message": "2.0 as 1.1",
"shapecode/twig-string-loader": "^1.1",
"simonvomeyser/commonmark-ext-lazy-image": "^2.0",
"slim/slim": "^4.12.0",
"symfony/yaml": "^6.0",
"twig/twig": "^3.5"
},

665
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,24 +5,50 @@ namespace AntCMS;
use AntCMS\AntMarkdown;
use AntCMS\AntPages;
use AntCMS\AntConfig;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class AntCMS
{
protected $antTwig;
protected ?Response $response = null;
protected ?Request $request = null;
public function __construct()
{
$this->antTwig = new AntTwig();
}
public function SetResponse(?Response $response): void
{
$this->response = $response;
}
public function setRequest(?Request $request): void
{
$this->request = $request;
}
public function getResponse(): ?Response
{
return $this->response;
}
public function getRequest(): ?Request
{
return $this->request;
}
/**
* Renders a page based on the provided page name.
*
* @param string $page The name of the page to be rendered
* @return string The rendered HTML of the page
*/
public function renderPage(string $page)
public function renderPage(): Response
{
$page = $this->request->getUri()->getPath();
$start_time = hrtime(true);
$content = $this->getPage($page);
$themeConfig = Self::getThemeConfig();
@ -49,7 +75,9 @@ class AntCMS
$pageTemplate = str_replace('<!--AntCMS-Debug-->', '<p>Took ' . $elapsed_time . ' milliseconds to render the page. </p>', $pageTemplate);
}
return $pageTemplate;
$response = $this->getResponse();
$response->getBody()->write($pageTemplate);
return $response;
}
/**
@ -223,16 +251,16 @@ class AntCMS
/**
* @return void
*/
public function serveContent(string $path)
public function serveContent(): Response
{
$path = $this->request->getUri();
if (!file_exists($path)) {
$this->renderException('404');
} else {
$asset_mime_type = mime_content_type($path);
header('Content-Type: ' . $asset_mime_type);
readfile($path);
$response = $this->response->withHeader('Content-Type', mime_content_type($path) . '; charset=UTF-8');
$response->getBody()->write(file_get_contents($path));
return $response;
}
exit;
}
public static function redirect(string $url)

View file

@ -11,3 +11,11 @@ const antPluginPath = __DIR__ . DIRECTORY_SEPARATOR . 'Plugins';
if (in_array('xxh128', hash_algos())) {
define('HAS_XXH128', true);
}
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$classMapPath = __DIR__ . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR . 'classMap.php';
$loader = new \AntCMS\AntLoader(['path' => $classMapPath]);
$loader->addNamespace('AntCMS\\', __DIR__ . DIRECTORY_SEPARATOR . 'AntCMS');
$loader->checkClassMap();
$loader->register();

View file

@ -1,10 +1,5 @@
<?php
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$classMapPath = __DIR__ . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR . 'classMap.php';
$loader = new AntCMS\AntLoader(['path' => $classMapPath]);
$loader->addNamespace('AntCMS\\', __DIR__ . DIRECTORY_SEPARATOR . 'AntCMS');
$loader->checkClassMap();
$loader->register();
require_once __DIR__ . DIRECTORY_SEPARATOR . 'bootstrap.php';
AntCMS\AntCache::clearCache();

View file

@ -1,19 +1,16 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Constants.php';
$classMapPath = __DIR__ . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR . 'classMap.php';
$loader = new AntCMS\AntLoader(['path' => $classMapPath]);
$loader->addNamespace('AntCMS\\', __DIR__ . DIRECTORY_SEPARATOR . 'AntCMS');
$loader->checkClassMap();
$loader->register();
use AntCMS\AntCMS;
use AntCMS\AntConfig;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set("error_log", "php_error.log");
require_once __DIR__ . DIRECTORY_SEPARATOR . 'bootstrap.php';
if (!file_exists(antConfigFile)) {
AntConfig::generateConfig();
@ -23,24 +20,36 @@ if (!file_exists(antPagesList)) {
\AntCMS\AntPages::generatePages();
}
$antCms = new AntCMS();
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$baseUrl = AntConfig::currentConfig('baseURL');
$antRouting = new \AntCMS\AntRouting($baseUrl, $requestUri);
$antCMS = new AntCMS;
$app = AppFactory::create();
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);
if (AntConfig::currentConfig('forceHTTPS') && !\AntCMS\AntEnviroment::isCli()) {
$antRouting->redirectHttps();
}
if ($antRouting->checkMatch('/themes/*/assets')) {
$antCms->serveContent(AntDir . $requestUri);
}
$app->get('/themes/{theme}/assets', function (Request $request, Response $response) use ($antCMS) {
$antCMS->setRequest($request);
$antCMS->SetResponse($response);
return $antCMS->serveContent();
});
if ($antRouting->checkMatch('/.well-known/acme-challenge/*')) {
$antCms->serveContent(AntDir . $requestUri);
}
$app->get('/.well-known/acme-challenge/{path:.*}', function (Request $request, Response $response) use ($antCMS) {
$antCMS->setRequest($request);
$antCMS->SetResponse($response);
return $antCMS->serveContent();
});
/**
* TODO: Make these non-static and not hard-coded.
* This is also still relying on the custom routing implementation I am working to remove
*/
if ($antRouting->checkMatch('/sitemap.xml')) {
$antRouting->setRequestUri('/plugin/sitemap');
}
@ -61,15 +70,20 @@ if ($antRouting->checkMatch('/plugin/*')) {
$antRouting->routeToPlugin();
}
if ($antRouting->isIndex()) {
// If the users list hasn't been created, redirect to the first-time setup
$app->get('/', function (Request $request, Response $response) use ($antCMS) {
if (!file_exists(antUsersList)) {
AntCMS::redirect('/profile/firsttime');
}
echo $antCms->renderPage('/');
exit;
} else {
echo $antCms->renderPage($requestUri);
exit;
}
$antCMS->setRequest($request);
$antCMS->SetResponse($response);
return $antCMS->renderPage();
});
$app->get('/{path:.*}', function (Request $request, Response $response) use ($antCMS) {
$antCMS->setRequest($request);
$antCMS->SetResponse($response);
return $antCMS->renderPage();
});
$app->run();