Get rid of event subscribers that resolve services too early, part 1

Refs flarum/core#1578.
This commit is contained in:
Franz Liedke 2018-12-16 15:13:19 +01:00
parent 85bc128e4f
commit d4783f4ac5
4 changed files with 49 additions and 40 deletions

View File

@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/
use Flarum\Api\Event\Serializing;
use Flarum\Discussion\Event\Saving;
use Flarum\Extend;
use Flarum\Tags\Access;
use Flarum\Tags\Api\Controller;
@ -35,11 +37,14 @@ return [
function (Dispatcher $events) {
$events->subscribe(Listener\AddDiscussionTagsRelationship::class);
$events->subscribe(Listener\AddForumTagsRelationship::class);
$events->listen(Serializing::class, Listener\PrepareForumTagsApiAttributes::class);
$events->subscribe(Listener\CreatePostWhenTagsAreChanged::class);
$events->subscribe(Listener\FilterDiscussionListByTags::class);
$events->subscribe(Listener\FilterPostsQueryByTag::class);
$events->subscribe(Listener\SaveTagsToDatabase::class);
$events->listen(Saving::class, Listener\SaveTagsToDatabase::class);
$events->subscribe(Listener\UpdateTagMetadata::class);
$events->subscribe(Access\GlobalPolicy::class);

View File

@ -12,30 +12,15 @@
namespace Flarum\Tags\Listener;
use Flarum\Api\Controller\ShowForumController;
use Flarum\Api\Event\Serializing;
use Flarum\Api\Event\WillGetData;
use Flarum\Api\Event\WillSerializeData;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Event\GetApiRelationship;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\Tag;
use Illuminate\Contracts\Events\Dispatcher;
class AddForumTagsRelationship
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
/**
* @param Dispatcher $events
*/
@ -44,7 +29,6 @@ class AddForumTagsRelationship
$events->listen(GetApiRelationship::class, [$this, 'getApiRelationship']);
$events->listen(WillSerializeData::class, [$this, 'loadTagsRelationship']);
$events->listen(WillGetData::class, [$this, 'includeTagsRelationship']);
$events->listen(Serializing::class, [$this, 'prepareApiAttributes']);
}
/**
@ -84,17 +68,4 @@ class AddForumTagsRelationship
$event->addInclude(['tags', 'tags.lastPostedDiscussion', 'tags.parent']);
}
}
/**
* @param Serializing $event
*/
public function prepareApiAttributes(Serializing $event)
{
if ($event->isSerializer(ForumSerializer::class)) {
$event->attributes['minPrimaryTags'] = $this->settings->get('flarum-tags.min_primary_tags');
$event->attributes['maxPrimaryTags'] = $this->settings->get('flarum-tags.max_primary_tags');
$event->attributes['minSecondaryTags'] = $this->settings->get('flarum-tags.min_secondary_tags');
$event->attributes['maxSecondaryTags'] = $this->settings->get('flarum-tags.max_secondary_tags');
}
}
}

View File

@ -0,0 +1,42 @@
<?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\Tags\Listener;
use Flarum\Api\Event\Serializing;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Settings\SettingsRepositoryInterface;
class PrepareForumTagsApiAttributes
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function handle(Serializing $event)
{
if ($event->isSerializer(ForumSerializer::class)) {
$event->attributes['minPrimaryTags'] = $this->settings->get('flarum-tags.min_primary_tags');
$event->attributes['maxPrimaryTags'] = $this->settings->get('flarum-tags.max_primary_tags');
$event->attributes['minSecondaryTags'] = $this->settings->get('flarum-tags.min_secondary_tags');
$event->attributes['maxSecondaryTags'] = $this->settings->get('flarum-tags.max_secondary_tags');
}
}
}

View File

@ -17,7 +17,6 @@ use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\Event\DiscussionWasTagged;
use Flarum\Tags\Tag;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Validation\Factory;
use Symfony\Component\Translation\TranslatorInterface;
@ -50,20 +49,12 @@ class SaveTagsToDatabase
$this->translator = $translator;
}
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Saving::class, [$this, 'whenDiscussionIsSaving']);
}
/**
* @param Saving $event
* @throws PermissionDeniedException
* @throws ValidationException
*/
public function whenDiscussionIsSaving(Saving $event)
public function handle(Saving $event)
{
$discussion = $event->discussion;
$actor = $event->actor;