From eb364c285bacb81204e04dbbc8eba9924db0a19c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Thu, 2 Mar 2023 18:18:09 +0100 Subject: [PATCH] feat(api pages//md): create --- App.php | 4 +++ SeacmsAppPlugin.php | 77 +++++++++++++++++++++++++++++++++++++++++++-- composer.json | 5 ++- composer.lock | 4 +-- src/MdResponse.php | 63 +++++++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 src/MdResponse.php diff --git a/App.php b/App.php index 2c441df..824e541 100644 --- a/App.php +++ b/App.php @@ -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 = << Exception : {$th->__toString()} HTML; + self::appendErrorMessagesIfNeeded($output); } finally { echo $output; } diff --git a/SeacmsAppPlugin.php b/SeacmsAppPlugin.php index 2c052b6..722bc47 100644 --- a/SeacmsAppPlugin.php +++ b/SeacmsAppPlugin.php @@ -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/' + ])), + 'page as Markdown' => urldecode($this->getPico()->getPageUrl('index',[ + 'api' => 'pages//md' + ])), + 'page as Markdown Extra' => urldecode($this->getPico()->getPageUrl('index',[ + 'api' => 'pages//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 !"]); + } + + } + } \ No newline at end of file diff --git a/composer.json b/composer.json index a9d0149..e63e85e 100644 --- a/composer.json +++ b/composer.json @@ -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/", diff --git a/composer.lock b/composer.lock index 7ffd6a1..c60b6bb 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/src/MdResponse.php b/src/MdResponse.php new file mode 100644 index 0000000..e284b22 --- /dev/null +++ b/src/MdResponse.php @@ -0,0 +1,63 @@ +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() + ]); + } +}