1
0
mirror of https://github.com/flarum/core.git synced 2025-08-16 21:34:08 +02:00

Add unparse to Formatter extender (#2780)

This commit is contained in:
Sami Mazouz
2021-04-14 11:34:49 +01:00
committed by GitHub
parent 9e3699ea47
commit 39a6106854
4 changed files with 79 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ class Formatter implements ExtenderInterface, LifecycleInterface
{
private $configurationCallbacks = [];
private $parsingCallbacks = [];
private $unparsingCallbacks = [];
private $renderingCallbacks = [];
/**
@@ -58,6 +59,28 @@ class Formatter implements ExtenderInterface, LifecycleInterface
return $this;
}
/**
* Prepare the system for unparsing. This can be used to modify the text that was parsed.
* Please note that the parsed text 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:
* - mixed $context
* - string $xml: The parsed text.
*
* The callback should return:
* - string $xml: The text to be unparsed.
*
* @return self
*/
public function unparse($callback)
{
$this->unparsingCallbacks[] = $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.
@@ -91,6 +114,10 @@ class Formatter implements ExtenderInterface, LifecycleInterface
$formatter->addParsingCallback(ContainerUtil::wrapCallback($callback, $container));
}
foreach ($this->unparsingCallbacks as $callback) {
$formatter->addUnparsingCallback(ContainerUtil::wrapCallback($callback, $container));
}
foreach ($this->renderingCallbacks as $callback) {
$formatter->addRenderingCallback(ContainerUtil::wrapCallback($callback, $container));
}