From 73c0626e722d2be2b82804eec5746646b64b0c44 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sun, 16 Dec 2018 15:13:19 +0100 Subject: [PATCH] Get rid of event subscribers that resolve services too early, part 1 Refs flarum/core#1578. --- extend.php | 7 +++- src/Listener/AddForumTagsRelationship.php | 29 ------------- .../PrepareForumTagsApiAttributes.php | 42 +++++++++++++++++++ src/Listener/SaveTagsToDatabase.php | 11 +---- 4 files changed, 49 insertions(+), 40 deletions(-) create mode 100755 src/Listener/PrepareForumTagsApiAttributes.php diff --git a/extend.php b/extend.php index cdec345f..3efe5aeb 100644 --- a/extend.php +++ b/extend.php @@ -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 @@ 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); diff --git a/src/Listener/AddForumTagsRelationship.php b/src/Listener/AddForumTagsRelationship.php index 884a7170..0473846c 100755 --- a/src/Listener/AddForumTagsRelationship.php +++ b/src/Listener/AddForumTagsRelationship.php @@ -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 @@ public function subscribe(Dispatcher $events) $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 @@ public function includeTagsRelationship(WillGetData $event) $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'); - } - } } diff --git a/src/Listener/PrepareForumTagsApiAttributes.php b/src/Listener/PrepareForumTagsApiAttributes.php new file mode 100755 index 00000000..7a430309 --- /dev/null +++ b/src/Listener/PrepareForumTagsApiAttributes.php @@ -0,0 +1,42 @@ + + * + * 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'); + } + } +} diff --git a/src/Listener/SaveTagsToDatabase.php b/src/Listener/SaveTagsToDatabase.php index 8db8d160..5c2bb6a6 100755 --- a/src/Listener/SaveTagsToDatabase.php +++ b/src/Listener/SaveTagsToDatabase.php @@ -17,7 +17,6 @@ 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 @@ public function __construct(SettingsRepositoryInterface $settings, Factory $vali $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;