mirror of
https://github.com/flarum/core.git
synced 2025-07-29 12:40:40 +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 $configurationCallbacks = [];
|
||||||
private $parsingCallbacks = [];
|
private $parsingCallbacks = [];
|
||||||
|
private $unparsingCallbacks = [];
|
||||||
private $renderingCallbacks = [];
|
private $renderingCallbacks = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,6 +59,28 @@ class Formatter implements ExtenderInterface, LifecycleInterface
|
|||||||
return $this;
|
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.
|
* 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.
|
* 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));
|
$formatter->addParsingCallback(ContainerUtil::wrapCallback($callback, $container));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ($this->unparsingCallbacks as $callback) {
|
||||||
|
$formatter->addUnparsingCallback(ContainerUtil::wrapCallback($callback, $container));
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($this->renderingCallbacks as $callback) {
|
foreach ($this->renderingCallbacks as $callback) {
|
||||||
$formatter->addRenderingCallback(ContainerUtil::wrapCallback($callback, $container));
|
$formatter->addRenderingCallback(ContainerUtil::wrapCallback($callback, $container));
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,8 @@ class Formatter
|
|||||||
|
|
||||||
protected $parsingCallbacks = [];
|
protected $parsingCallbacks = [];
|
||||||
|
|
||||||
|
protected $unparsingCallbacks = [];
|
||||||
|
|
||||||
protected $renderingCallbacks = [];
|
protected $renderingCallbacks = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,6 +54,11 @@ class Formatter
|
|||||||
$this->parsingCallbacks[] = $callback;
|
$this->parsingCallbacks[] = $callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addUnparsingCallback($callback)
|
||||||
|
{
|
||||||
|
$this->unparsingCallbacks[] = $callback;
|
||||||
|
}
|
||||||
|
|
||||||
public function addRenderingCallback($callback)
|
public function addRenderingCallback($callback)
|
||||||
{
|
{
|
||||||
$this->renderingCallbacks[] = $callback;
|
$this->renderingCallbacks[] = $callback;
|
||||||
@@ -98,10 +105,15 @@ class Formatter
|
|||||||
* Unparse XML.
|
* Unparse XML.
|
||||||
*
|
*
|
||||||
* @param string $xml
|
* @param string $xml
|
||||||
|
* @param mixed $context
|
||||||
* @return string
|
* @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);
|
return Unparser::unparse($xml);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -128,7 +128,7 @@ class CommentPost extends Post
|
|||||||
*/
|
*/
|
||||||
public function getContentAttribute($value)
|
public function getContentAttribute($value)
|
||||||
{
|
{
|
||||||
return static::$formatter->unparse($value);
|
return static::$formatter->unparse($value, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -89,6 +89,36 @@ class FormatterTest extends TestCase
|
|||||||
$this->assertEquals('<t>ReplacedText<a></t>', $this->getFormatter()->parse('Text<a>'));
|
$this->assertEquals('<t>ReplacedText<a></t>', $this->getFormatter()->parse('Text<a>'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function custom_formatter_unparsing_doesnt_work_by_default()
|
||||||
|
{
|
||||||
|
$this->assertEquals('Text<a>', $this->getFormatter()->unparse('<t>Text<a></t>'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function custom_formatter_unparsing_works_if_added_with_closure()
|
||||||
|
{
|
||||||
|
$this->extend((new Extend\Formatter)->unparse(function ($context, $xml) {
|
||||||
|
return '<t>ReplacedText<a></t>';
|
||||||
|
}));
|
||||||
|
|
||||||
|
$this->assertEquals('ReplacedText<a>', $this->getFormatter()->unparse('<t>Text<a></t>'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function custom_formatter_unparsing_works_if_added_with_invokable_class()
|
||||||
|
{
|
||||||
|
$this->extend((new Extend\Formatter)->unparse(InvokableUnparsing::class));
|
||||||
|
|
||||||
|
$this->assertEquals('ReplacedText<a>', $this->getFormatter()->unparse('<t>Text<a></t>'));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
@@ -136,6 +166,14 @@ class InvokableParsing
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class InvokableUnparsing
|
||||||
|
{
|
||||||
|
public function __invoke($context, $xml)
|
||||||
|
{
|
||||||
|
return '<t>ReplacedText<a></t>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class InvokableRendering
|
class InvokableRendering
|
||||||
{
|
{
|
||||||
public function __invoke($renderer, $context, $xml, $request)
|
public function __invoke($renderer, $context, $xml, $request)
|
||||||
|
Reference in New Issue
Block a user