1
0
mirror of https://github.com/flarum/core.git synced 2025-10-11 23:14:29 +02:00

Refactor and improve formatter extender (#2098)

- Deprecated all events involved with Formatter
- Refactor ->configure() method on extender not to use events
- Add extender methods for ->render() and ->parse()
- Add integration tests
This commit is contained in:
Alexander Skvortsov
2020-11-03 13:05:33 -05:00
committed by GitHub
parent cee87848fe
commit 87c258b2f8
6 changed files with 271 additions and 15 deletions

View File

@@ -10,38 +10,104 @@
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\Formatter\Event\Configuring;
use Flarum\Formatter\Formatter as ActualFormatter;
use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher;
class Formatter implements ExtenderInterface, LifecycleInterface
{
private $callback;
private $configurationCallbacks = [];
private $parsingCallbacks = [];
private $renderingCallbacks = [];
/**
* Configure the formatter. This can be used to add support for custom markdown/bbcode/etc tags,
* or otherwise change the formatter. Please see documentation for the s9e text formatter library for more
* information on how to use this.
*
* @param callable|string $callback
*
* The callback can be a closure or invokable class, and should accept:
* - \s9e\TextFormatter\Configurator $configurator
*/
public function configure($callback)
{
$this->callback = $callback;
$this->configurationCallbacks[] = $callback;
return $this;
}
/**
* Prepare the system for parsing. This can be used to modify the text that will be parsed, or to modify the parser.
* Please note that the text to be parsed must be returned, regardless of whether it's changed.
*
* @param callable|string $callback
*
* The callback can be a closure or invokable class, and should accept:
* - \s9e\TextFormatter\Parser $parser
* - mixed $context
* - string $text: The text to be parsed.
*
* The callback should return:
* - string $text: The text to be parsed.
*/
public function parse($callback)
{
$this->parsingCallbacks[] = $callback;
return $this;
}
/**
* Prepare the system for rendering. This can be used to modify the xml that will be rendered, or to modify the renderer.
* Please note that the xml to be rendered must be returned, regardless of whether it's changed.
*
* @param callable|string $callback
*
* The callback can be a closure or invokable class, and should accept:
* - \s9e\TextFormatter\Rendered $renderer
* - mixed $context
* - string $xml: The xml to be rendered.
* - ServerRequestInterface $request
*
* The callback should return:
* - string $xml: The xml to be rendered.
*/
public function render($callback)
{
$this->renderingCallbacks[] = $callback;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
$events = $container->make(Dispatcher::class);
$events->listen(
Configuring::class,
function (Configuring $event) use ($container) {
if (is_string($this->callback)) {
$callback = $container->make($this->callback);
} else {
$callback = $this->callback;
$container->extend('flarum.formatter', function ($formatter, $container) {
foreach ($this->configurationCallbacks as $callback) {
if (is_string($callback)) {
$callback = $container->make($callback);
}
$callback($event->configurator);
$formatter->addConfigurationCallback($callback);
}
);
foreach ($this->parsingCallbacks as $callback) {
if (is_string($callback)) {
$callback = $container->make($callback);
}
$formatter->addParsingCallback($callback);
}
foreach ($this->renderingCallbacks as $callback) {
if (is_string($callback)) {
$callback = $container->make($callback);
}
$formatter->addRenderingCallback($callback);
}
return $formatter;
});
}
public function onEnable(Container $container, Extension $extension)