1
0
mirror of https://github.com/flarum/core.git synced 2025-07-28 20:20:34 +02:00

Use Formatter extender to avoid resolving the UrlGenerator too early

Refs flarum/core#1578.
Fixes flarum/core#1703.
This commit is contained in:
Franz Liedke
2018-12-15 12:06:08 +01:00
parent 875daf3ce2
commit 3656072ff4
4 changed files with 115 additions and 145 deletions

View File

@@ -10,6 +10,8 @@
*/
use Flarum\Extend;
use Flarum\Formatter\Event\Rendering;
use Flarum\Mentions\ConfigureMentions;
use Flarum\Mentions\Listener;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\View\Factory;
@@ -19,14 +21,18 @@ return [
->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/less/forum.less'),
(new Extend\Formatter)
->configure(ConfigureMentions::class),
function (Dispatcher $events, Factory $views) {
$events->subscribe(Listener\AddPostMentionedByRelationship::class);
$events->subscribe(Listener\FormatPostMentions::class);
$events->subscribe(Listener\FormatUserMentions::class);
$events->subscribe(Listener\UpdatePostMentionsMetadata::class);
$events->subscribe(Listener\UpdateUserMentionsMetadata::class);
$events->subscribe(Listener\AddFilterByMentions::class);
$events->listen(Rendering::class, Listener\FormatPostMentions::class);
$events->listen(Rendering::class, Listener\FormatUserMentions::class);
$views->addNamespace('flarum-mentions', __DIR__.'/views');
},
];

View File

@@ -0,0 +1,105 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Mentions;
use Flarum\Http\UrlGenerator;
use s9e\TextFormatter\Configurator;
class ConfigureMentions
{
/**
* @var UrlGenerator
*/
protected $url;
/**
* @param UrlGenerator $url
*/
public function __construct(UrlGenerator $url)
{
$this->url = $url;
}
public function __invoke(Configurator $config)
{
$this->configureUserMentions($config);
$this->configurePostMentions($config);
}
private function configureUserMentions(Configurator $config)
{
$config->rendering->parameters['PROFILE_URL'] = $this->url->to('forum')->route('user', ['username' => '']);
$tagName = 'USERMENTION';
$tag = $config->tags->add($tagName);
$tag->attributes->add('username');
$tag->attributes->add('displayname');
$tag->attributes->add('id')->filterChain->append('#uint');
$tag->template = '<a href="{$PROFILE_URL}{@username}" class="UserMention">@<xsl:value-of select="@displayname"/></a>';
$tag->filterChain->prepend([static::class, 'addUserId'])
->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterUserMentions(tag); }');
$config->Preg->match('/\B@(?<username>[a-z0-9_-]+)(?!#)/i', $tagName);
}
/**
* @param $tag
*
* @return bool
*/
public static function addUserId($tag)
{
if ($user = User::where('username', 'like', $tag->getAttribute('username'))->first()) {
$tag->setAttribute('id', $user->id);
$tag->setAttribute('displayname', $user->display_name);
return true;
}
}
private function configurePostMentions(Configurator $config)
{
$config->rendering->parameters['PROFILE_URL'] = $this->url->to('forum')->route('user', ['username' => '']);
$tagName = 'USERMENTION';
$tag = $config->tags->add($tagName);
$tag->attributes->add('username');
$tag->attributes->add('displayname');
$tag->attributes->add('id')->filterChain->append('#uint');
$tag->template = '<a href="{$PROFILE_URL}{@username}" class="UserMention">@<xsl:value-of select="@displayname"/></a>';
$tag->filterChain->prepend([static::class, 'addPostId'])
->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterUserMentions(tag); }');
$config->Preg->match('/\B@(?<username>[a-z0-9_-]+)(?!#)/i', $tagName);
}
/**
* @param $tag
* @return bool
*/
public static function addPostId($tag)
{
$post = CommentPost::find($tag->getAttribute('id'));
if ($post) {
$tag->setAttribute('discussionid', (int) $post->discussion_id);
$tag->setAttribute('number', (int) $post->number);
$tag->setAttribute('displayname', $post->user->display_name);
return true;
}
}
}

View File

@@ -11,69 +11,12 @@
namespace Flarum\Mentions\Listener;
use Flarum\Formatter\Event\Configuring;
use Flarum\Formatter\Event\Rendering;
use Flarum\Http\UrlGenerator;
use Flarum\Post\CommentPost;
use Illuminate\Contracts\Events\Dispatcher;
use s9e\TextFormatter\Utils;
class FormatPostMentions
{
/**
* @var UrlGenerator
*/
protected $url;
/**
* @param UrlGenerator $url
*/
public function __construct(UrlGenerator $url)
{
$this->url = $url;
}
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Configuring::class, [$this, 'configure']);
$events->listen(Rendering::class, [$this, 'render']);
}
/**
* @param Configuring $event
*/
public function configure(Configuring $event)
{
$configurator = $event->configurator;
$configurator->rendering->parameters['DISCUSSION_URL'] = $this->url->to('forum')->route('discussion', ['id' => '']);
$tagName = 'POSTMENTION';
$tag = $configurator->tags->add($tagName);
$tag->attributes->add('username');
$tag->attributes->add('displayname');
$tag->attributes->add('number')->filterChain->append('#uint');
$tag->attributes->add('discussionid')->filterChain->append('#uint');
$tag->attributes->add('id')->filterChain->append('#uint');
$tag->template = '<a href="{$DISCUSSION_URL}{@discussionid}/{@number}" class="PostMention" data-id="{@id}"><xsl:value-of select="@displayname"/></a>';
$tag->filterChain
->prepend([static::class, 'addId'])
->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterPostMentions(tag); }');
$configurator->Preg->match('/\B@(?<username>[a-z0-9_-]+)#(?<id>\d+)/i', $tagName);
}
/**
* @param Rendering $event
*/
public function render(Rendering $event)
public function handle(Rendering $event)
{
$post = $event->context;
@@ -86,21 +29,4 @@ class FormatPostMentions
return $attributes;
});
}
/**
* @param $tag
* @return bool
*/
public static function addId($tag)
{
$post = CommentPost::find($tag->getAttribute('id'));
if ($post) {
$tag->setAttribute('discussionid', (int) $post->discussion_id);
$tag->setAttribute('number', (int) $post->number);
$tag->setAttribute('displayname', $post->user->display_name);
return true;
}
}
}

View File

@@ -11,64 +11,12 @@
namespace Flarum\Mentions\Listener;
use Flarum\Formatter\Event\Configuring;
use Flarum\Formatter\Event\Rendering;
use Flarum\Http\UrlGenerator;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
use s9e\TextFormatter\Utils;
class FormatUserMentions
{
/**
* @var UrlGenerator
*/
protected $url;
/**
* @param UrlGenerator $url
*/
public function __construct(UrlGenerator $url)
{
$this->url = $url;
}
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Configuring::class, [$this, 'configure']);
$events->listen(Rendering::class, [$this, 'render']);
}
/**
* @param Configuring $event
*/
public function configure(Configuring $event)
{
$configurator = $event->configurator;
$configurator->rendering->parameters['PROFILE_URL'] = $this->url->to('forum')->route('user', ['username' => '']);
$tagName = 'USERMENTION';
$tag = $configurator->tags->add($tagName);
$tag->attributes->add('username');
$tag->attributes->add('displayname');
$tag->attributes->add('id')->filterChain->append('#uint');
$tag->template = '<a href="{$PROFILE_URL}{@username}" class="UserMention">@<xsl:value-of select="@displayname"/></a>';
$tag->filterChain->prepend([static::class, 'addId'])
->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterUserMentions(tag); }');
$configurator->Preg->match('/\B@(?<username>[a-z0-9_-]+)(?!#)/i', $tagName);
}
/**
* @param Rendering $event
*/
public function render(Rendering $event)
public function handle(Rendering $event)
{
$post = $event->context;
@@ -82,19 +30,4 @@ class FormatUserMentions
return $attributes;
});
}
/**
* @param $tag
*
* @return bool
*/
public static function addId($tag)
{
if ($user = User::where('username', 'like', $tag->getAttribute('username'))->first()) {
$tag->setAttribute('id', $user->id);
$tag->setAttribute('displayname', $user->display_name);
return true;
}
}
}