mirror of
https://github.com/flarum/framework.git
synced 2024-12-04 00:03:37 +08:00
New object-based extension APIs
This commit is contained in:
parent
cd25c631db
commit
2be90d1caf
|
@ -15,6 +15,7 @@ use Flarum\Core\Search\GambitManager;
|
|||
use League\Flysystem\Adapter\Local;
|
||||
use Flarum\Core\Events\RegisterDiscussionGambits;
|
||||
use Flarum\Core\Events\RegisterUserGambits;
|
||||
use Flarum\Extend\Permission;
|
||||
|
||||
class CoreServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
@ -138,13 +139,15 @@ class CoreServiceProvider extends ServiceProvider
|
|||
|
||||
public function registerPermissions()
|
||||
{
|
||||
$this->permission('forum.view');
|
||||
$this->permission('forum.startDiscussion');
|
||||
$this->permission('discussion.rename');
|
||||
$this->permission('discussion.delete');
|
||||
$this->permission('discussion.reply');
|
||||
$this->permission('post.edit');
|
||||
$this->permission('post.delete');
|
||||
$this->extend(
|
||||
new Permission('forum.view'),
|
||||
new Permission('forum.startDiscussion'),
|
||||
new Permission('discussion.rename'),
|
||||
new Permission('discussion.delete'),
|
||||
new Permission('discussion.reply'),
|
||||
new Permission('post.edit'),
|
||||
new Permission('post.delete')
|
||||
);
|
||||
|
||||
Forum::grantPermission(function ($grant, $user, $permission) {
|
||||
return $user->hasPermission('forum.'.$permission);
|
||||
|
|
|
@ -4,6 +4,7 @@ use Flarum\Support\ServiceProvider;
|
|||
use Flarum\Core\Models\User;
|
||||
use Flarum\Core\Notifications\Notifier;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Flarum\Extend\NotificationType;
|
||||
|
||||
class NotificationServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
@ -19,7 +20,9 @@ class NotificationServiceProvider extends ServiceProvider
|
|||
$notifier->registerMethod('alert', 'Flarum\Core\Notifications\Senders\NotificationAlerter');
|
||||
$notifier->registerMethod('email', 'Flarum\Core\Notifications\Senders\NotificationEmailer');
|
||||
|
||||
$this->notificationType('Flarum\Core\Notifications\Types\DiscussionRenamedNotification', ['alert' => true]);
|
||||
$this->extend(
|
||||
(new NotificationType('Flarum\Core\Notifications\Types\DiscussionRenamedNotification'))->enableByDefault('alert')
|
||||
);
|
||||
}
|
||||
|
||||
public function register()
|
||||
|
|
35
framework/core/src/Extend/ApiInclude.php
Normal file
35
framework/core/src/Extend/ApiInclude.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
|
||||
class ApiInclude implements ExtenderInterface
|
||||
{
|
||||
protected $actions;
|
||||
|
||||
protected $relationships;
|
||||
|
||||
protected $status;
|
||||
|
||||
public function __construct($actions, $relationships, $status = false)
|
||||
{
|
||||
$this->actions = $actions;
|
||||
$this->relationships = $relationships;
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
foreach ((array) $this->actions as $action) {
|
||||
$parts = explode('.', $action);
|
||||
$class = 'Flarum\Api\Actions\\'.ucfirst($parts[0]).'\\'.ucfirst($parts[1]).'Action';
|
||||
|
||||
foreach ((array) $this->relationships as $relationship) {
|
||||
if (is_null($this->status)) {
|
||||
unset($class::$include[$relationship]);
|
||||
} else {
|
||||
$class::$include[$relationship] = $this->status;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
20
framework/core/src/Extend/DiscussionGambit.php
Normal file
20
framework/core/src/Extend/DiscussionGambit.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
|
||||
class DiscussionGambit implements ExtenderInterface
|
||||
{
|
||||
protected $class;
|
||||
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
$app['events']->listen('Flarum\Core\Events\RegisterDiscussionGambits', function ($event) {
|
||||
$event->gambits->add($this->class);
|
||||
});
|
||||
}
|
||||
}
|
20
framework/core/src/Extend/EventSubscribers.php
Normal file
20
framework/core/src/Extend/EventSubscribers.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
|
||||
class EventSubscribers implements ExtenderInterface
|
||||
{
|
||||
protected $subscribers;
|
||||
|
||||
public function __construct($subscribers)
|
||||
{
|
||||
$this->subscribers = $subscribers;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
foreach ((array) $this->subscribers as $subscriber) {
|
||||
$app['events']->subscribe($subscriber);
|
||||
}
|
||||
}
|
||||
}
|
8
framework/core/src/Extend/ExtenderInterface.php
Normal file
8
framework/core/src/Extend/ExtenderInterface.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
|
||||
interface ExtenderInterface
|
||||
{
|
||||
public function extend(Application $app);
|
||||
}
|
24
framework/core/src/Extend/Formatter.php
Normal file
24
framework/core/src/Extend/Formatter.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
|
||||
class Formatter implements ExtenderInterface
|
||||
{
|
||||
protected $name;
|
||||
|
||||
protected $class;
|
||||
|
||||
protected $priority;
|
||||
|
||||
public function __construct($name, $class, $priority = 0)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->class = $class;
|
||||
$this->priority = $priority;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
$app['flarum.formatter']->add($this->name, $this->class, $this->priority);
|
||||
}
|
||||
}
|
20
framework/core/src/Extend/ForumAssets.php
Normal file
20
framework/core/src/Extend/ForumAssets.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
|
||||
class ForumAssets implements ExtenderInterface
|
||||
{
|
||||
protected $files;
|
||||
|
||||
public function __construct($files)
|
||||
{
|
||||
$this->files = $files;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
$app['events']->listen('Flarum\Forum\Events\RenderView', function ($event) {
|
||||
$event->assets->addFile($this->files);
|
||||
});
|
||||
}
|
||||
}
|
40
framework/core/src/Extend/NotificationType.php
Normal file
40
framework/core/src/Extend/NotificationType.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Flarum\Core\Models\Notification;
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
class NotificationType implements ExtenderInterface
|
||||
{
|
||||
protected $class;
|
||||
|
||||
protected $enabled = [];
|
||||
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function enableByDefault($method)
|
||||
{
|
||||
$this->enabled[] = $method;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
$notifier = $app['flarum.notifier'];
|
||||
$class = $this->class;
|
||||
|
||||
$notifier->registerType($class);
|
||||
|
||||
Notification::registerType($class);
|
||||
|
||||
foreach ($notifier->getMethods() as $method => $sender) {
|
||||
if ($sender::compatibleWith($class)) {
|
||||
User::registerPreference(User::notificationPreferenceKey($class::getType(), $method), 'boolval', isset($this->enabled[$method]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
framework/core/src/Extend/Permission.php
Normal file
19
framework/core/src/Extend/Permission.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Flarum\Core\Models\Permission as PermissionModel;
|
||||
|
||||
class Permission implements ExtenderInterface
|
||||
{
|
||||
protected $permission;
|
||||
|
||||
public function __construct($permission)
|
||||
{
|
||||
$this->permission = $permission;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
PermissionModel::addPermission($this->permission);
|
||||
}
|
||||
}
|
19
framework/core/src/Extend/PostType.php
Normal file
19
framework/core/src/Extend/PostType.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Flarum\Core\Models\Post;
|
||||
|
||||
class PostType implements ExtenderInterface
|
||||
{
|
||||
protected $class;
|
||||
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
Post::addType($this->class);
|
||||
}
|
||||
}
|
38
framework/core/src/Extend/Relationship.php
Normal file
38
framework/core/src/Extend/Relationship.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Closure;
|
||||
|
||||
class Relationship implements ExtenderInterface
|
||||
{
|
||||
protected $parent;
|
||||
|
||||
protected $type;
|
||||
|
||||
protected $name;
|
||||
|
||||
protected $child;
|
||||
|
||||
public function __construct($parent, $type, $name, $child = null)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
$this->type = $type;
|
||||
$this->name = $name;
|
||||
$this->child = $child;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
$parent = $this->parent;
|
||||
|
||||
$parent::addRelationship($this->name, function ($model) {
|
||||
if ($this->type instanceof Closure) {
|
||||
return $this->type($model);
|
||||
} elseif ($this->type === 'belongsTo') {
|
||||
return $model->belongsTo($this->child, null, null, $this->name);
|
||||
} else {
|
||||
// @todo
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
26
framework/core/src/Extend/SerializeAttributes.php
Normal file
26
framework/core/src/Extend/SerializeAttributes.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Closure;
|
||||
|
||||
class SerializeAttributes implements ExtenderInterface
|
||||
{
|
||||
protected $serializer;
|
||||
|
||||
protected $callback;
|
||||
|
||||
public function __construct($serializer, $callback)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
$app['events']->listen('Flarum\Api\Events\SerializeAttributes', function ($event) {
|
||||
if ($event->serializer instanceof $this->serializer) {
|
||||
call_user_func($this->callback, $event->attributes, $event->model, $event->serializer);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
36
framework/core/src/Extend/SerializeRelationship.php
Normal file
36
framework/core/src/Extend/SerializeRelationship.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Closure;
|
||||
|
||||
class SerializeRelationship implements ExtenderInterface
|
||||
{
|
||||
protected $parent;
|
||||
|
||||
protected $type;
|
||||
|
||||
protected $name;
|
||||
|
||||
protected $child;
|
||||
|
||||
public function __construct($parent, $type, $name, $child = null)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
$this->type = $type;
|
||||
$this->name = $name;
|
||||
$this->child = $child;
|
||||
}
|
||||
|
||||
public function extend(Application $app)
|
||||
{
|
||||
$parent = $this->parent;
|
||||
|
||||
$parent::addRelationship($this->name, function ($serializer) {
|
||||
if ($this->type instanceof Closure) {
|
||||
return $this->type();
|
||||
} else {
|
||||
return $serializer->{$this->type}($this->child, $this->name);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -20,80 +20,10 @@ class ServiceProvider extends IlluminateServiceProvider
|
|||
//
|
||||
}
|
||||
|
||||
protected function forumAssets($assets)
|
||||
public function extend()
|
||||
{
|
||||
$this->app['events']->listen('Flarum\Forum\Events\RenderView', function ($event) use ($assets) {
|
||||
$event->assets->addFile($assets);
|
||||
});
|
||||
}
|
||||
|
||||
protected function postType($class)
|
||||
{
|
||||
Post::addType($class);
|
||||
}
|
||||
|
||||
protected function discussionGambit($class)
|
||||
{
|
||||
$this->app['events']->listen('Flarum\Core\Events\RegisterDiscussionGambits', function ($event) use ($class) {
|
||||
$event->gambits->add($class);
|
||||
});
|
||||
}
|
||||
|
||||
protected function formatter($name, $class, $priority = 0)
|
||||
{
|
||||
$this->app['flarum.formatter']->add($name, $class, $priority);
|
||||
}
|
||||
|
||||
protected function notificationType($class, $defaultPreferences = [])
|
||||
{
|
||||
$notifier = $this->app['flarum.notifier'];
|
||||
|
||||
$notifier->registerType($class);
|
||||
|
||||
Notification::registerType($class);
|
||||
|
||||
foreach ($notifier->getMethods() as $method => $sender) {
|
||||
if ($sender::compatibleWith($class)) {
|
||||
User::registerPreference(User::notificationPreferenceKey($class::getType(), $method), 'boolval', array_get($defaultPreferences, $method, false));
|
||||
}
|
||||
foreach (func_get_args() as $extender) {
|
||||
$extender->extend($this->app);
|
||||
}
|
||||
}
|
||||
|
||||
protected function relationship($parent, $type, $name, $child = null)
|
||||
{
|
||||
$parent::addRelationship($name, function ($model) use ($type, $name, $child) {
|
||||
if ($type instanceof Closure) {
|
||||
return $type($model);
|
||||
} elseif ($type === 'belongsTo') {
|
||||
return $model->belongsTo($child, null, null, $name);
|
||||
} else {
|
||||
// @todo
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected function serializeRelationship($parent, $type, $name, $child = null)
|
||||
{
|
||||
$parent::addRelationship($name, function ($serializer) use ($type, $name, $child) {
|
||||
if ($type instanceof Closure) {
|
||||
return $type();
|
||||
} else {
|
||||
return $serializer->$type($child, $name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected function serializeAttributes($serializer, Closure $callback)
|
||||
{
|
||||
$this->app['events']->listen('Flarum\Api\Events\SerializeAttributes', function ($event) use ($serializer, $callback) {
|
||||
if ($event->serializer instanceof $serializer) {
|
||||
$callback($event->attributes, $event->model, $event->serializer);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected function permission($permission)
|
||||
{
|
||||
Permission::addPermission($permission);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user