197 lines
5.6 KiB
PHP
197 lines
5.6 KiB
PHP
<?php
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
// Authors: see README.md
|
|
|
|
use SeaCMS\Api\LateApiAware;
|
|
use SeaCMS\Api\JsonResponse;
|
|
use SeaCMS\App\MdResponse;
|
|
use SeaCMS\App\TestInterface;
|
|
use SeaCMS\App\TestOnPageRenderingInterface;
|
|
|
|
/**
|
|
* A plugin for SeaCMS-app.
|
|
*/
|
|
class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware
|
|
{
|
|
/**
|
|
* Pico API version.
|
|
* @var int
|
|
*/
|
|
const API_VERSION = 4;
|
|
|
|
/**
|
|
* define if test output should be defined
|
|
* @var bool
|
|
*/
|
|
protected $triggerTest;
|
|
|
|
/**
|
|
* define if test output should be defined onPageRendering
|
|
* @var bool
|
|
*/
|
|
protected $triggerTestOnPageRendering;
|
|
|
|
/**
|
|
* define test
|
|
* @var TestInterface
|
|
*/
|
|
protected $testRunner;
|
|
|
|
/**
|
|
* construct
|
|
* @param Pico $pico current instance of Pico
|
|
* @param ?TestInterface $testRunner optional
|
|
*/
|
|
public function __construct(Pico $pico, $testRunner = null)
|
|
{
|
|
parent::__construct($pico);
|
|
$this->triggerTest = !empty($testRunner) && ($testRunner instanceof TestInterface);
|
|
$this->triggerTestOnPageRendering = !empty($testRunner) && ($testRunner instanceof TestOnPageRenderingInterface);
|
|
$this->testRunner = ($this->triggerTest) ? $testRunner : null;
|
|
}
|
|
|
|
/**
|
|
* return api routes
|
|
* @return array
|
|
*/
|
|
public function registerApiRoutes():array
|
|
{
|
|
return [
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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',
|
|
'GET pages/(.*)/(.*)/md' => 'apiPageAsMd2Levels',
|
|
'GET pages/(.*)/(.*)/(.*)/md' => 'apiPageAsMd3Levels',
|
|
'GET pages/(.*)/(.*)/(.*)/(.*)/md' => 'apiPageAsMd4Levels',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* method to create a page
|
|
* @return JsonResponse
|
|
*/
|
|
public function createPage(string $pageName): JsonResponse
|
|
{
|
|
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'],[
|
|
'Content-Disposition' => 'inline; filename="'.str_replace('/','-',$pageName).'.md"'
|
|
]);
|
|
} else {
|
|
return new JsonResponse(404,['code'=>404,'reason'=>"Page '$pageName' has not been found !"]);
|
|
}
|
|
|
|
}
|
|
/**
|
|
* method to see page as md
|
|
* @return JsonResponse
|
|
*/
|
|
public function apiPageAsMd2Levels(string $folder1,string $pageName): JsonResponse
|
|
{
|
|
return $this->apiPageAsMd("$folder1/$pageName");
|
|
}
|
|
/**
|
|
* method to see page as md
|
|
* @return JsonResponse
|
|
*/
|
|
public function apiPageAsMd3Levels(string $folder1,string $folder2,string $pageName): JsonResponse
|
|
{
|
|
return $this->apiPageAsMd("$folder1/$folder2/$pageName");
|
|
}
|
|
/**
|
|
* method to see page as md
|
|
* @return JsonResponse
|
|
*/
|
|
public function apiPageAsMd4Levels(string $folder1,string $folder2,string $folder3,string $pageName): JsonResponse
|
|
{
|
|
return $this->apiPageAsMd("$folder1/$folder2/$folder3/$pageName");
|
|
}
|
|
|
|
/**
|
|
* Triggered after Pico has rendered the page
|
|
*
|
|
* @param string &$output contents which will be sent to the user
|
|
*/
|
|
public function onPageRendered(&$output)
|
|
{
|
|
if ($this->triggerTest){
|
|
$this->testRunner->run($this,$output);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Triggered before Pico renders the page
|
|
*
|
|
* @param string &$templateName file name of the template
|
|
* @param array &$twigVariables template variables
|
|
*/
|
|
public function onPageRendering(&$templateName, array &$twigVariables)
|
|
{
|
|
if ($this->triggerTestOnPageRendering){
|
|
$this->testRunner->runOnPageRendering($this,$templateName,$twigVariables);
|
|
}
|
|
}
|
|
|
|
} |