mirror of
https://github.com/flarum/core.git
synced 2025-08-10 10:24:46 +02:00
Add README documentation to ExtensionPage (#3094)
Co-authored-by: Alexander Skvortsov <sasha.skvortsov109@gmail.com>
This commit is contained in:
47
src/Api/Controller/ShowExtensionReadmeController.php
Normal file
47
src/Api/Controller/ShowExtensionReadmeController.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Api\Serializer\ExtensionReadmeSerializer;
|
||||
use Flarum\Extension\ExtensionManager;
|
||||
use Flarum\Http\RequestUtil;
|
||||
use Illuminate\Support\Arr;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
class ShowExtensionReadmeController extends AbstractShowController
|
||||
{
|
||||
/**
|
||||
* @var ExtensionManager
|
||||
*/
|
||||
protected $extensions;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $serializer = ExtensionReadmeSerializer::class;
|
||||
|
||||
public function __construct(ExtensionManager $extensions)
|
||||
{
|
||||
$this->extensions = $extensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function data(ServerRequestInterface $request, Document $document)
|
||||
{
|
||||
$extensionName = Arr::get($request->getQueryParams(), 'name');
|
||||
|
||||
RequestUtil::getActor($request)->assertAdmin();
|
||||
|
||||
return $this->extensions->getExtension($extensionName);
|
||||
}
|
||||
}
|
35
src/Api/Serializer/ExtensionReadmeSerializer.php
Normal file
35
src/Api/Serializer/ExtensionReadmeSerializer.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Api\Serializer;
|
||||
|
||||
class ExtensionReadmeSerializer extends AbstractSerializer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getDefaultAttributes($extension)
|
||||
{
|
||||
$attributes = [
|
||||
'content' => $extension->getReadme()
|
||||
];
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
public function getId($extension)
|
||||
{
|
||||
return $extension->getId();
|
||||
}
|
||||
|
||||
public function getType($extension)
|
||||
{
|
||||
return 'extension-readmes';
|
||||
}
|
||||
}
|
@@ -265,6 +265,13 @@ return function (RouteCollection $map, RouteHandlerFactory $route) {
|
||||
$route->toController(Controller\UninstallExtensionController::class)
|
||||
);
|
||||
|
||||
// Get readme for an extension
|
||||
$map->get(
|
||||
'/extension-readmes/{name}',
|
||||
'extension-readmes.show',
|
||||
$route->toController(Controller\ShowExtensionReadmeController::class)
|
||||
);
|
||||
|
||||
// Update settings
|
||||
$map->post(
|
||||
'/settings',
|
||||
|
@@ -19,6 +19,7 @@ use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
use s9e\TextFormatter\Bundles\Fatdown;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
@@ -525,4 +526,28 @@ class Extension implements Arrayable
|
||||
'links' => $this->getLinks(),
|
||||
], $this->composerJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the rendered contents of the extension README file as a HTML string.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getReadme(): ?string
|
||||
{
|
||||
$content = null;
|
||||
|
||||
if (file_exists($file = "$this->path/README.md")) {
|
||||
$content = file_get_contents($file);
|
||||
} elseif (file_exists($file = "$this->path/README")) {
|
||||
$content = file_get_contents($file);
|
||||
}
|
||||
|
||||
if ($content) {
|
||||
$xml = Fatdown::parse($content);
|
||||
|
||||
return Fatdown::render($xml);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user