feat(api pages/<name>/md): create

This commit is contained in:
Jérémy Dufraisse 2023-03-02 18:18:09 +01:00
parent da1b58fb05
commit eb364c285b
5 changed files with 147 additions and 6 deletions

View File

@ -10,6 +10,7 @@ namespace SeaCMS;
use Exception;
use Pico;
use SeacmsAppPlugin;
use SeaCMS\Api\SpecialOutputException;
use Throwable;
set_error_handler(function (
@ -94,12 +95,15 @@ class App
$app = new App($contentFolderFromRoot);
$output = $app->runPico();
self::appendErrorMessagesIfNeeded($output);
} catch (SpecialOutputException $th) {
$output = $th->getJsonResponse()->send();
} catch (Throwable $th) {
$output = <<<HTML
<div style="color:red;">
Exception : {$th->__toString()}
</div>
HTML;
self::appendErrorMessagesIfNeeded($output);
} finally {
echo $output;
}

View File

@ -2,13 +2,14 @@
// SPDX-License-Identifier: EUPL-1.2
// Authors: see README.md
use SeaCMS\Api\ApiAware;
use SeaCMS\Api\LateApiAware;
use SeaCMS\Api\JsonResponse;
use SeaCMS\App\MdResponse;
/**
* A plugin for SeaCMS-app.
*/
class SeacmsAppPlugin extends AbstractPicoPlugin implements ApiAware
class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware
{
/**
* Pico API version.
@ -23,7 +24,20 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements ApiAware
public function registerApiRoutes():array
{
return [
'pages/(.*)/create' => 'createPage', // TODO only define for POST
];
}
/**
* return api routes
* @return array
*/
public function registerLateApiRoutes():array
{
return [
'POST pages/(.*)/create' => 'createPage', // TODO only define for POST
'GET pages' => 'apiPagesBase',
'GET pages/(.*)' => 'apiPageBase',
'GET pages/(.*)/md' => 'apiPageAsMd'
];
}
@ -36,4 +50,61 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements ApiAware
return new JsonResponse(501,['code'=>501,'reason'=>"work in progress for '$pageName'"]);
}
/**
* method to see base of pages api
* @return JsonResponse
*/
public function apiPagesBase(): JsonResponse
{
return new JsonResponse(200,[
'title' => 'Base api route to see pages',
'routes' => [
'Base for one page' => urldecode($this->getPico()->getPageUrl('index',[
'api' => 'pages/<NameOfPage>'
])),
'page as Markdown' => urldecode($this->getPico()->getPageUrl('index',[
'api' => 'pages/<NameOfPage>/md'
])),
'page as Markdown Extra' => urldecode($this->getPico()->getPageUrl('index',[
'api' => 'pages/<NameOfPage>/mdx'
]))
]
]);
}
/**
* method to see base of page api
* @return JsonResponse
*/
public function apiPageBase(string $pageName): JsonResponse
{
return new JsonResponse(200,[
'title' => 'Base api route to see page',
'routes' => [
'page as Markdown' => $this->getPico()->getPageUrl('index',[
'api' => "pages/$pageName/md"
]),
'page as Markdown Extra' => $this->getPico()->getPageUrl('index',[
'api' => "pages/$pageName/mdx"
])
]
]);
}
/**
* method to see page as md
* @return JsonResponse
*/
public function apiPageAsMd(string $pageName): JsonResponse
{
$pages = $this->getPico()->getPages();
if (array_key_exists($pageName,$pages)){
return new MdResponse(200,$pages[$pageName]['raw_content']);
} else {
return new JsonResponse(404,['code'=>404,'reason'=>"Page '$pageName' has not been found !"]);
}
}
}

View File

@ -32,7 +32,10 @@
"picocms/pico-theme": "Default pico theme : revision ^3.0"
},
"autoload": {
"classmap": ["App.php","SeacmsAppPlugin.php"]
"classmap": ["App.php","SeacmsAppPlugin.php"],
"psr-4": {
"SeaCMS\\App\\": "src"
}
},
"extra": {
"pico-plugin-dir": "vendor/picocms/plugins/",

4
composer.lock generated
View File

@ -264,7 +264,7 @@
"source": {
"type": "git",
"url": "https://git.defis.info/SeaCMS/seacms-api",
"reference": "c0695eca2f95357f1b77d022c56d9bf8e395ed62"
"reference": "49055e46711590f78a6d72ecb389d65d0a28e7a8"
},
"require": {
"php": "^8.0"
@ -302,7 +302,7 @@
"issues": "https://git.defis.info/SeaCMS/seacms-api/issues",
"source": "https://git.defis.info/SeaCMS/seacms-api"
},
"time": "2023-02-28T00:58:35+00:00"
"time": "2023-03-02T15:44:25+00:00"
},
{
"name": "seacms/seacms-auth",

63
src/MdResponse.php Normal file
View File

@ -0,0 +1,63 @@
<?php
// SPDX-License-Identifier: EUPL-1.2
// Authors: see README.md
namespace SeaCMS\App;
use SeaCMS\Api\JsonResponse;
/**
* Response for http response
*/
class MdResponse extends JsonResponse
{
/**
* content as string
* @var string
*/
protected $stringContent;
public function __construct(int $code, string $content, array $headers = [],?Cookies $cookies = null){
parent::__construct($code,[],[],$cookies);
$this->stringContent = $content;
$this->headers = array_merge([
'Content-Type' => 'text/markdown',
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Allow-Headers' => 'X-Requested-With, Location, Slug, Accept, Content-Type',
'Access-Control-Expose-Headers' => 'Location, Slug, Accept, Content-Type',
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, DELETE, PUT, PATCH',
'Access-Control-Max-Age' => '86400'
], $headers);
}
/* === Getters === */
/**
* return stringContent
* @return string
*/
public function getStringContent(): string
{
return $this->stringContent;
}
/**
* return content as String
* @return string
*/
protected function returnContent(): string
{
return $this->getStringContent();
}
/**
* export class as array
* @return array
*/
public function jsonSerialize(): mixed
{
return array_merge(parent::jsonSerialize(),[
'stringContent' => $this->getStringContent()
]);
}
}