Allow formatter to be used for things other than post formatting

This commit is contained in:
Toby Zerner
2015-09-18 13:29:43 +09:30
parent c4dc1a5ee2
commit 8f387bbd52
3 changed files with 20 additions and 21 deletions

View File

@ -69,17 +69,18 @@ class Formatter
}); });
} }
protected function getParser(CommentPost $post) protected function getParser($context = null)
{ {
$parser = $this->getComponent('parser'); $parser = $this->getComponent('parser');
$parser->registeredVars['post'] = $post;
event(new FormatterParser($parser, $post)); $parser->registeredVars['context'] = $context;
event(new FormatterParser($parser, $context));
return $parser; return $parser;
} }
protected function getRenderer(CommentPost $post) protected function getRenderer($context = null)
{ {
spl_autoload_register(function ($class) { spl_autoload_register(function ($class) {
if (file_exists($file = storage_path() . '/app/' . $class . '.php')) { if (file_exists($file = storage_path() . '/app/' . $class . '.php')) {
@ -89,7 +90,7 @@ class Formatter
$renderer = $this->getComponent('renderer'); $renderer = $this->getComponent('renderer');
event(new FormatterRenderer($renderer, $post)); event(new FormatterRenderer($renderer, $context));
return $renderer; return $renderer;
} }
@ -109,16 +110,16 @@ class Formatter
])['js']; ])['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); 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); return $renderer->render($xml);
} }

View File

@ -11,7 +11,6 @@
namespace Flarum\Events; namespace Flarum\Events;
use s9e\TextFormatter\Parser; use s9e\TextFormatter\Parser;
use Flarum\Core\Posts\CommentPost;
class FormatterParser class FormatterParser
{ {
@ -21,17 +20,17 @@ class FormatterParser
public $parser; public $parser;
/** /**
* @var CommentPost * @var mixed
*/ */
public $post; public $context;
/** /**
* @param Parser $parser * @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->parser = $parser;
$this->post = $post; $this->context = $context;
} }
} }

View File

@ -11,7 +11,6 @@
namespace Flarum\Events; namespace Flarum\Events;
use s9e\TextFormatter\Renderer; use s9e\TextFormatter\Renderer;
use Flarum\Core\Posts\CommentPost;
class FormatterRenderer class FormatterRenderer
{ {
@ -21,17 +20,17 @@ class FormatterRenderer
public $renderer; public $renderer;
/** /**
* @var CommentPost * @var mixed
*/ */
public $post; public $context;
/** /**
* @param Renderer $renderer * @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->renderer = $renderer;
$this->post = $post; $this->context = $context;
} }
} }