New object-based extension APIs

This commit is contained in:
Toby Zerner 2015-05-17 10:19:54 +09:30
parent cd25c631db
commit 2be90d1caf
15 changed files with 322 additions and 81 deletions

View File

@ -15,6 +15,7 @@ use Flarum\Core\Search\GambitManager;
use League\Flysystem\Adapter\Local; use League\Flysystem\Adapter\Local;
use Flarum\Core\Events\RegisterDiscussionGambits; use Flarum\Core\Events\RegisterDiscussionGambits;
use Flarum\Core\Events\RegisterUserGambits; use Flarum\Core\Events\RegisterUserGambits;
use Flarum\Extend\Permission;
class CoreServiceProvider extends ServiceProvider class CoreServiceProvider extends ServiceProvider
{ {
@ -138,13 +139,15 @@ class CoreServiceProvider extends ServiceProvider
public function registerPermissions() public function registerPermissions()
{ {
$this->permission('forum.view'); $this->extend(
$this->permission('forum.startDiscussion'); new Permission('forum.view'),
$this->permission('discussion.rename'); new Permission('forum.startDiscussion'),
$this->permission('discussion.delete'); new Permission('discussion.rename'),
$this->permission('discussion.reply'); new Permission('discussion.delete'),
$this->permission('post.edit'); new Permission('discussion.reply'),
$this->permission('post.delete'); new Permission('post.edit'),
new Permission('post.delete')
);
Forum::grantPermission(function ($grant, $user, $permission) { Forum::grantPermission(function ($grant, $user, $permission) {
return $user->hasPermission('forum.'.$permission); return $user->hasPermission('forum.'.$permission);

View File

@ -4,6 +4,7 @@ use Flarum\Support\ServiceProvider;
use Flarum\Core\Models\User; use Flarum\Core\Models\User;
use Flarum\Core\Notifications\Notifier; use Flarum\Core\Notifications\Notifier;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Extend\NotificationType;
class NotificationServiceProvider extends ServiceProvider class NotificationServiceProvider extends ServiceProvider
{ {
@ -19,7 +20,9 @@ class NotificationServiceProvider extends ServiceProvider
$notifier->registerMethod('alert', 'Flarum\Core\Notifications\Senders\NotificationAlerter'); $notifier->registerMethod('alert', 'Flarum\Core\Notifications\Senders\NotificationAlerter');
$notifier->registerMethod('email', 'Flarum\Core\Notifications\Senders\NotificationEmailer'); $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() public function register()

View 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;
}
}
}
}
}

View 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);
});
}
}

View 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);
}
}
}

View File

@ -0,0 +1,8 @@
<?php namespace Flarum\Extend;
use Illuminate\Foundation\Application;
interface ExtenderInterface
{
public function extend(Application $app);
}

View 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);
}
}

View 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);
});
}
}

View 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]));
}
}
}
}

View 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);
}
}

View 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);
}
}

View 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
}
});
}
}

View 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);
}
});
}
}

View 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);
}
});
}
}

View File

@ -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) { foreach (func_get_args() as $extender) {
$event->assets->addFile($assets); $extender->extend($this->app);
});
}
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));
}
} }
} }
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);
}
} }