Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the Request available to the Formatter\Rendering event #1721

Merged
merged 1 commit into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Api/Controller/AbstractSerializeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
);

$serializer = static::$container->make($this->serializer);
$serializer->setActor($request->getAttribute('actor'));
$serializer->setRequest($request);

$element = $this->createElement($data, $serializer)
->with($this->extractInclude($request))
Expand Down
29 changes: 22 additions & 7 deletions src/Api/Serializer/AbstractSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Contracts\Events\Dispatcher;
use InvalidArgumentException;
use LogicException;
use Psr\Http\Message\ServerRequestInterface as Request;
use Tobscure\JsonApi\AbstractSerializer as BaseAbstractSerializer;
use Tobscure\JsonApi\Collection;
use Tobscure\JsonApi\Relationship;
Expand All @@ -28,6 +29,11 @@

abstract class AbstractSerializer extends BaseAbstractSerializer
{
/**
* @var Request
*/
protected $request;

/**
* @var User
*/
Expand All @@ -44,19 +50,28 @@ abstract class AbstractSerializer extends BaseAbstractSerializer
protected static $container;

/**
* @return User
* @return Request
*/
public function getActor()
public function getRequest()
{
return $this->actor;
return $this->request;
}

/**
* @param Request $request
*/
public function setRequest(Request $request)
{
$this->request = $request;
$this->actor = $request->getAttribute('actor');
}

/**
* @param User $actor
* @return User
*/
public function setActor(User $actor)
public function getActor()
{
$this->actor = $actor;
return $this->actor;
}

/**
Expand Down Expand Up @@ -231,7 +246,7 @@ protected function resolveSerializerClass($class)
{
$serializer = static::$container->make($class);

$serializer->setActor($this->actor);
$serializer->setRequest($this->request);

return $serializer;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Serializer/BasicPostSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function getDefaultAttributes($post)
];

if ($post instanceof CommentPost) {
$attributes['contentHtml'] = $post->content_html;
$attributes['contentHtml'] = $post->formatContent($this->request);
} else {
$attributes['content'] = $post->content;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Api/Serializer/PostSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ protected function getDefaultAttributes($post)
$canEdit = $gate->allows('edit', $post);

if ($post instanceof CommentPost) {
$attributes['contentHtml'] = $post->content_html;

if ($canEdit) {
$attributes['content'] = $post->content;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Formatter/Event/Rendering.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Flarum\Formatter\Event;

use Psr\Http\Message\ServerRequestInterface;
use s9e\TextFormatter\Renderer;

class Rendering
Expand All @@ -30,15 +31,22 @@ class Rendering
*/
public $xml;

/**
* @var ServerRequestInterface
*/
public $request;

/**
* @param Renderer $renderer
* @param mixed $context
* @param string $xml
* @param ServerRequestInterface|null $request
*/
public function __construct(Renderer $renderer, $context, &$xml)
public function __construct(Renderer $renderer, $context, &$xml, ServerRequestInterface $request = null)
{
$this->renderer = $renderer;
$this->context = $context;
$this->xml = &$xml;
$this->request = $request;
}
}
6 changes: 4 additions & 2 deletions src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Flarum\Formatter\Event\Rendering;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Events\Dispatcher;
use Psr\Http\Message\ServerRequestInterface;
use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Unparser;

Expand Down Expand Up @@ -69,13 +70,14 @@ public function parse($text, $context = null)
*
* @param string $xml
* @param mixed $context
* @param ServerRequestInterface|null $request
* @return string
*/
public function render($xml, $context = null)
public function render($xml, $context = null, ServerRequestInterface $request = null)
{
$renderer = $this->getRenderer();

$this->events->dispatch(new Rendering($renderer, $context, $xml));
$this->events->dispatch(new Rendering($renderer, $context, $xml, $request));

return $renderer->render($xml);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Post/CommentPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
use Flarum\Post\Event\Restored;
use Flarum\Post\Event\Revised;
use Flarum\User\User;
use Psr\Http\Message\ServerRequestInterface;

/**
* A standard comment in a discussion.
*
* @property string $parsed_content
* @property string $content_html
*/
class CommentPost extends Post
{
Expand Down Expand Up @@ -166,11 +166,12 @@ public function setParsedContentAttribute($value)
/**
* Get the content rendered as HTML.
*
* @param ServerRequestInterface $request
* @return string
*/
public function getContentHtmlAttribute()
public function formatContent(ServerRequestInterface $request)
{
return static::$formatter->render($this->attributes['content'], $this);
return static::$formatter->render($this->attributes['content'], $this, $request);
}

/**
Expand Down