mirror of
https://github.com/flarum/framework.git
synced 2024-11-26 02:10:09 +08:00
Refactor using new base ServiceProvider
This commit is contained in:
parent
360402c5b1
commit
840cc84724
|
@ -1,66 +0,0 @@
|
|||
<?php namespace Flarum\Categories;
|
||||
|
||||
use Flarum\Categories\Events\DiscussionWasMoved;
|
||||
use Flarum\Core\Events\ModelCall;
|
||||
use Flarum\Core\Events\RegisterDiscussionGambits;
|
||||
use Flarum\Core\Events\DiscussionWillBeSaved;
|
||||
use Flarum\Core\Models\Discussion;
|
||||
use Flarum\Api\Events\SerializeRelationship;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
use Flarum\Forum\Events\RenderView;
|
||||
|
||||
class CategoriesHandler
|
||||
{
|
||||
public function subscribe($events)
|
||||
{
|
||||
$events->listen('Flarum\Forum\Events\RenderView', __CLASS__.'@renderForum');
|
||||
$events->listen('Flarum\Api\Events\SerializeRelationship', __CLASS__.'@serializeRelationship');
|
||||
$events->listen('Flarum\Core\Events\RegisterDiscussionGambits', __CLASS__.'@registerDiscussionGambits');
|
||||
$events->listen('Flarum\Core\Events\DiscussionWillBeSaved', __CLASS__.'@whenDiscussionWillBeSaved');
|
||||
}
|
||||
|
||||
public function renderForum(RenderView $event)
|
||||
{
|
||||
$root = __DIR__.'/..';
|
||||
|
||||
$event->assets->addFile([
|
||||
$root.'/js/dist/extension.js',
|
||||
$root.'/less/categories.less'
|
||||
]);
|
||||
|
||||
$serializer = new CategorySerializer($event->action->actor);
|
||||
$event->view->data = array_merge($event->view->data, $serializer->collection(Category::orderBy('position')->get())->toArray());
|
||||
}
|
||||
|
||||
public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
|
||||
{
|
||||
if (isset($event->command->data['links']['category']['linkage'])) {
|
||||
$linkage = $event->command->data['links']['category']['linkage'];
|
||||
|
||||
$categoryId = (int) $linkage['id'];
|
||||
$discussion = $event->discussion;
|
||||
$user = $event->command->user;
|
||||
|
||||
$oldCategoryId = (int) $discussion->category_id;
|
||||
|
||||
if ($oldCategoryId === $categoryId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$discussion->category_id = $categoryId;
|
||||
$discussion->raise(new DiscussionWasMoved($discussion, $user, $oldCategoryId));
|
||||
}
|
||||
}
|
||||
|
||||
public function registerDiscussionGambits(RegisterDiscussionGambits $event)
|
||||
{
|
||||
$event->gambits->add('Flarum\Categories\CategoryGambit');
|
||||
}
|
||||
|
||||
public function serializeRelationship(SerializeRelationship $event)
|
||||
{
|
||||
if ($event->serializer instanceof DiscussionSerializer && $event->name === 'category') {
|
||||
return $event->serializer->hasOne('Flarum\Categories\CategorySerializer', 'category');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,9 @@
|
|||
<?php namespace Flarum\Categories;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Models\Discussion;
|
||||
use Flarum\Core\Notifications\Notifier;
|
||||
use Flarum\Support\ServiceProvider;
|
||||
use Flarum\Api\Actions\Discussions\IndexAction as DiscussionsIndexAction;
|
||||
use Flarum\Api\Actions\Discussions\ShowAction as DiscussionsShowAction;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class CategoriesServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
@ -15,26 +12,36 @@ class CategoriesServiceProvider extends ServiceProvider
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot(Dispatcher $events, Notifier $notifier)
|
||||
public function boot(Dispatcher $events)
|
||||
{
|
||||
$events->subscribe('Flarum\Categories\CategoriesHandler');
|
||||
$events->subscribe('Flarum\Categories\DiscussionMovedNotifier');
|
||||
$events->subscribe('Flarum\Categories\Handlers\DiscussionMovedNotifier');
|
||||
$events->subscribe('Flarum\Categories\Handlers\CategoryPreloader');
|
||||
$events->subscribe('Flarum\Categories\Handlers\CategorySaver');
|
||||
|
||||
$this->forumAssets([
|
||||
__DIR__.'/../js/dist/extension.js',
|
||||
__DIR__.'/../less/categories.less'
|
||||
]);
|
||||
|
||||
$this->postType('Flarum\Categories\DiscussionMovedPost');
|
||||
|
||||
$this->discussionGambit('Flarum\Categories\CategoryGambit');
|
||||
|
||||
$this->notificationType('Flarum\Categories\DiscussionMovedNotification', ['alert' => true]);
|
||||
|
||||
$this->relationship('Flarum\Core\Models\Discussion', 'belongsTo', 'category', 'Flarum\Categories\Category');
|
||||
|
||||
$this->serializeRelationship('Flarum\Api\Serializers\DiscussionSerializer', 'hasOne', 'category', 'Flarum\Categories\CategorySerializer');
|
||||
|
||||
// Add the category relationship to the Discussion model, and include
|
||||
// it in discussion-related API actions by default.
|
||||
Discussion::addRelationship('category', function ($model) {
|
||||
return $model->belongsTo('Flarum\Categories\Category', null, null, 'category');
|
||||
});
|
||||
DiscussionsIndexAction::$include['category'] = true;
|
||||
DiscussionsShowAction::$include['category'] = true;
|
||||
|
||||
// Add a new post and notification type to represent a discussion
|
||||
// being moved from one category to another.
|
||||
Post::addType('Flarum\Categories\DiscussionMovedPost');
|
||||
|
||||
$notifier->registerType('Flarum\Categories\DiscussionMovedNotification', ['alert' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind(
|
||||
|
|
19
extensions/tags/src/Handlers/CategoryPreloader.php
Executable file
19
extensions/tags/src/Handlers/CategoryPreloader.php
Executable file
|
@ -0,0 +1,19 @@
|
|||
<?php namespace Flarum\Categories\Handlers;
|
||||
|
||||
use Flarum\Categories\Category;
|
||||
use Flarum\Categories\CategorySerializer;
|
||||
use Flarum\Forum\Events\RenderView;
|
||||
|
||||
class CategoryPreloader
|
||||
{
|
||||
public function subscribe($events)
|
||||
{
|
||||
$events->listen('Flarum\Forum\Events\RenderView', __CLASS__.'@renderForum');
|
||||
}
|
||||
|
||||
public function renderForum(RenderView $event)
|
||||
{
|
||||
$serializer = new CategorySerializer($event->action->actor);
|
||||
$event->view->data = array_merge($event->view->data, $serializer->collection(Category::orderBy('position')->get())->toArray());
|
||||
}
|
||||
}
|
32
extensions/tags/src/Handlers/CategorySaver.php
Executable file
32
extensions/tags/src/Handlers/CategorySaver.php
Executable file
|
@ -0,0 +1,32 @@
|
|||
<?php namespace Flarum\Categories\Handlers;
|
||||
|
||||
use Flarum\Categories\Events\DiscussionWasMoved;
|
||||
use Flarum\Core\Events\DiscussionWillBeSaved;
|
||||
|
||||
class CategorySaver
|
||||
{
|
||||
public function subscribe($events)
|
||||
{
|
||||
$events->listen('Flarum\Core\Events\DiscussionWillBeSaved', __CLASS__.'@whenDiscussionWillBeSaved');
|
||||
}
|
||||
|
||||
public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
|
||||
{
|
||||
if (isset($event->command->data['links']['category']['linkage'])) {
|
||||
$linkage = $event->command->data['links']['category']['linkage'];
|
||||
|
||||
$categoryId = (int) $linkage['id'];
|
||||
$discussion = $event->discussion;
|
||||
$user = $event->command->user;
|
||||
|
||||
$oldCategoryId = (int) $discussion->category_id;
|
||||
|
||||
if ($oldCategoryId === $categoryId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$discussion->category_id = $categoryId;
|
||||
$discussion->raise(new DiscussionWasMoved($discussion, $user, $oldCategoryId));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
<?php namespace Flarum\Categories;
|
||||
<?php namespace Flarum\Categories\Handlers;
|
||||
|
||||
use Flarum\Categories\DiscussionMovedPost;
|
||||
use Flarum\Categories\DiscussionMovedNotification;
|
||||
use Flarum\Categories\Events\DiscussionWasMoved;
|
||||
use Flarum\Core\Notifications\Notifier;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
Loading…
Reference in New Issue
Block a user