Skip to content

Commit

Permalink
Use Extend\Event
Browse files Browse the repository at this point in the history
  • Loading branch information
karaok491 committed Dec 5, 2020
1 parent 509b120 commit 4f334dc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 51 deletions.
14 changes: 7 additions & 7 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

namespace FoF\Split;

use Flarum\Api\Event\Serializing;
use Flarum\Discussion\Event\Renamed;
use Flarum\Event\ConfigurePostTypes;
use Flarum\Extend;
use Illuminate\Contracts\Events\Dispatcher;
use FoF\Split\Events\DiscussionWasSplit;

return [
(new Extend\Frontend('admin'))
Expand All @@ -29,10 +31,8 @@
->post('/split', 'fof.split.run', Api\Controllers\SplitController::class),

(new Extend\Event())
->listen(Renamed::class, Listeners\UpdateSplitTitleAfterDiscussionWasRenamed::class),

function (Dispatcher $events) {
$events->subscribe(Listeners\AddSplitApi::class);
$events->subscribe(Listeners\CreatePostWhenSplit::class);
},
->listen(Renamed::class, Listeners\UpdateSplitTitleAfterDiscussionWasRenamed::class)
->listen(Serializing::class, Listeners\AddSplitApi::class)
->listen(ConfigurePostTypes::class, Listeners\AddPostType::class)
->listen(DiscussionWasSplit::class, Listeners\CreatePostWhenSplit::class),
];
23 changes: 23 additions & 0 deletions src/Listeners/AddPostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of fof/split.
*
* Copyright (c) 2020 FriendsOfFlarum
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace FoF\Split\Listeners;

use Flarum\Event\ConfigurePostTypes;
use FoF\Split\Posts\DiscussionSplitPost;

class AddPostType
{
public function handle(ConfigurePostTypes $event)
{
$event->add(DiscussionSplitPost::class);
}
}
25 changes: 1 addition & 24 deletions src/Listeners/AddSplitApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,13 @@

use Flarum\Api\Event\Serializing;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;

class AddSplitApi
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;

/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}

/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Serializing::class, [$this, 'prepareApiAttributes']);
}

/**
* @param Serializing $event
*/
public function prepareApiAttributes(Serializing $event)
public function handle(Serializing $event)
{
if ($event->isSerializer(DiscussionSerializer::class)) {
$event->attributes['canSplit'] = $event->actor->can('split', $event->model);
Expand Down
21 changes: 1 addition & 20 deletions src/Listeners/CreatePostWhenSplit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,13 @@

use FoF\Split\Events\DiscussionWasSplit;
use FoF\Split\Posts\DiscussionSplitPost;
use Flarum\Event\ConfigurePostTypes;
use Illuminate\Events\Dispatcher;

class CreatePostWhenSplit
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigurePostTypes::class, [$this, 'addPostType']);
$events->listen(DiscussionWasSplit::class, [$this, 'whenDiscussionWasSplit']);
}

/**
* @param ConfigurePostTypes $event
*/
public function addPostType(ConfigurePostTypes $event)
{
$event->add(DiscussionSplitPost::class);
}

/**
* @param DiscussionWasSplit $event
*/
public function whenDiscussionWasSplit(DiscussionWasSplit $event)
public function handle(DiscussionWasSplit $event)
{
foreach (['to', 'from'] as $direction) {
forward_static_call_array(
Expand Down

0 comments on commit 4f334dc

Please sign in to comment.