mirror of
https://github.com/flarum/core.git
synced 2025-02-26 12:15:32 +01:00
Allow formatter to be used for things other than post formatting
This commit is contained in:
parent
c4dc1a5ee2
commit
8f387bbd52
@ -69,17 +69,18 @@ class Formatter
|
||||
});
|
||||
}
|
||||
|
||||
protected function getParser(CommentPost $post)
|
||||
protected function getParser($context = null)
|
||||
{
|
||||
$parser = $this->getComponent('parser');
|
||||
$parser->registeredVars['post'] = $post;
|
||||
|
||||
event(new FormatterParser($parser, $post));
|
||||
$parser->registeredVars['context'] = $context;
|
||||
|
||||
event(new FormatterParser($parser, $context));
|
||||
|
||||
return $parser;
|
||||
}
|
||||
|
||||
protected function getRenderer(CommentPost $post)
|
||||
protected function getRenderer($context = null)
|
||||
{
|
||||
spl_autoload_register(function ($class) {
|
||||
if (file_exists($file = storage_path() . '/app/' . $class . '.php')) {
|
||||
@ -89,7 +90,7 @@ class Formatter
|
||||
|
||||
$renderer = $this->getComponent('renderer');
|
||||
|
||||
event(new FormatterRenderer($renderer, $post));
|
||||
event(new FormatterRenderer($renderer, $context));
|
||||
|
||||
return $renderer;
|
||||
}
|
||||
@ -109,16 +110,16 @@ class Formatter
|
||||
])['js'];
|
||||
}
|
||||
|
||||
public function parse($text, CommentPost $post)
|
||||
public function parse($text, $context = null)
|
||||
{
|
||||
$parser = $this->getParser($post);
|
||||
$parser = $this->getParser($context);
|
||||
|
||||
return $parser->parse($text);
|
||||
}
|
||||
|
||||
public function render($xml, CommentPost $post)
|
||||
public function render($xml, $context = null)
|
||||
{
|
||||
$renderer = $this->getRenderer($post);
|
||||
$renderer = $this->getRenderer($context);
|
||||
|
||||
return $renderer->render($xml);
|
||||
}
|
||||
|
@ -11,7 +11,6 @@
|
||||
namespace Flarum\Events;
|
||||
|
||||
use s9e\TextFormatter\Parser;
|
||||
use Flarum\Core\Posts\CommentPost;
|
||||
|
||||
class FormatterParser
|
||||
{
|
||||
@ -21,17 +20,17 @@ class FormatterParser
|
||||
public $parser;
|
||||
|
||||
/**
|
||||
* @var CommentPost
|
||||
* @var mixed
|
||||
*/
|
||||
public $post;
|
||||
public $context;
|
||||
|
||||
/**
|
||||
* @param Parser $parser
|
||||
* @param CommentPost $post
|
||||
* @param mixed $context
|
||||
*/
|
||||
public function __construct(Parser $parser, CommentPost $post)
|
||||
public function __construct(Parser $parser, $context)
|
||||
{
|
||||
$this->parser = $parser;
|
||||
$this->post = $post;
|
||||
$this->context = $context;
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@
|
||||
namespace Flarum\Events;
|
||||
|
||||
use s9e\TextFormatter\Renderer;
|
||||
use Flarum\Core\Posts\CommentPost;
|
||||
|
||||
class FormatterRenderer
|
||||
{
|
||||
@ -21,17 +20,17 @@ class FormatterRenderer
|
||||
public $renderer;
|
||||
|
||||
/**
|
||||
* @var CommentPost
|
||||
* @var mixed
|
||||
*/
|
||||
public $post;
|
||||
public $context;
|
||||
|
||||
/**
|
||||
* @param Renderer $renderer
|
||||
* @param CommentPost $post
|
||||
* @param mixed $context
|
||||
*/
|
||||
public function __construct(Renderer $renderer, CommentPost $post)
|
||||
public function __construct(Renderer $renderer, $context)
|
||||
{
|
||||
$this->renderer = $renderer;
|
||||
$this->post = $post;
|
||||
$this->context = $context;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user