mirror of
https://github.com/flarum/core.git
synced 2025-07-31 13:40:20 +02:00
Add unparse to Formatter extender (#2780)
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
@@ -20,6 +20,8 @@ class Formatter
|
||||
|
||||
protected $parsingCallbacks = [];
|
||||
|
||||
protected $unparsingCallbacks = [];
|
||||
|
||||
protected $renderingCallbacks = [];
|
||||
|
||||
/**
|
||||
@@ -52,6 +54,11 @@ class Formatter
|
||||
$this->parsingCallbacks[] = $callback;
|
||||
}
|
||||
|
||||
public function addUnparsingCallback($callback)
|
||||
{
|
||||
$this->unparsingCallbacks[] = $callback;
|
||||
}
|
||||
|
||||
public function addRenderingCallback($callback)
|
||||
{
|
||||
$this->renderingCallbacks[] = $callback;
|
||||
@@ -98,10 +105,15 @@ class Formatter
|
||||
* Unparse XML.
|
||||
*
|
||||
* @param string $xml
|
||||
* @param mixed $context
|
||||
* @return string
|
||||
*/
|
||||
public function unparse($xml)
|
||||
public function unparse($xml, $context = null)
|
||||
{
|
||||
foreach ($this->unparsingCallbacks as $callback) {
|
||||
$xml = $callback($context, $xml);
|
||||
}
|
||||
|
||||
return Unparser::unparse($xml);
|
||||
}
|
||||
|
||||
|
@@ -128,7 +128,7 @@ class CommentPost extends Post
|
||||
*/
|
||||
public function getContentAttribute($value)
|
||||
{
|
||||
return static::$formatter->unparse($value);
|
||||
return static::$formatter->unparse($value, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user