1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 23:47:32 +02:00

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

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