mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 09:11:40 +08:00
Refactor some APIs
This commit is contained in:
parent
359f56a71b
commit
e6c3ace0e7
@ -13,7 +13,6 @@ use Flarum\Core\Models\Discussion;
|
||||
use Flarum\Core\Search\GambitManager;
|
||||
use Flarum\Core\Events\RegisterDiscussionGambits;
|
||||
use Flarum\Core\Events\RegisterUserGambits;
|
||||
use Flarum\Extend\Permission;
|
||||
use Flarum\Extend\ActivityType;
|
||||
use Flarum\Extend\NotificationType;
|
||||
use Flarum\Extend\Locale;
|
||||
|
34
framework/core/src/Extend/AdminClient.php
Normal file
34
framework/core/src/Extend/AdminClient.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Flarum\Admin\Actions\IndexAction;
|
||||
|
||||
class AdminClient implements ExtenderInterface
|
||||
{
|
||||
protected $assets = [];
|
||||
|
||||
protected $translations = [];
|
||||
|
||||
public function assets($assets)
|
||||
{
|
||||
$this->assets = array_merge($this->assets, $assets);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function translations($keys)
|
||||
{
|
||||
$this->translations = array_merge($this->translations, $keys);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$container->make('events')->listen('Flarum\Admin\Events\RenderView', function ($event) {
|
||||
$event->assets->addFiles($this->assets);
|
||||
});
|
||||
|
||||
IndexAction::$translations = array_merge(IndexAction::$translations, $this->translations);
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class AdminTranslations implements ExtenderInterface
|
||||
{
|
||||
protected $keys;
|
||||
|
||||
public function __construct($keys)
|
||||
{
|
||||
$this->keys = $keys;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
143
framework/core/src/Extend/ApiAction.php
Normal file
143
framework/core/src/Extend/ApiAction.php
Normal file
@ -0,0 +1,143 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class ApiAction implements ExtenderInterface
|
||||
{
|
||||
protected $action;
|
||||
|
||||
protected $serializer;
|
||||
|
||||
protected $addInclude = [];
|
||||
|
||||
protected $removeInclude = [];
|
||||
|
||||
protected $addLink = [];
|
||||
|
||||
protected $removeLink = [];
|
||||
|
||||
protected $limitMax;
|
||||
|
||||
protected $limit;
|
||||
|
||||
protected $addSortFields = [];
|
||||
|
||||
protected $removeSortFields = [];
|
||||
|
||||
protected $sort;
|
||||
|
||||
public function __construct($action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
public function serializer($serializer)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addInclude($relation, $default = true)
|
||||
{
|
||||
$this->addInclude[] = compact('relation', 'default');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeInclude($relation)
|
||||
{
|
||||
$this->removeInclude[] = $relation;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addLink($relation)
|
||||
{
|
||||
$this->addLink[] = $relation;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeLink($relation)
|
||||
{
|
||||
$this->removeLink[] = $relation;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function limitMax($limitMax)
|
||||
{
|
||||
$this->limitMax = $limitMax;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function limit($limit)
|
||||
{
|
||||
$this->limit = $limit;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addSortField($field)
|
||||
{
|
||||
$this->addSortFields[] = $field;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeSortField($field)
|
||||
{
|
||||
$this->removeSortFields[] = $field;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sort($sort)
|
||||
{
|
||||
$this->sort = $sort;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
foreach ((array) $this->action as $action) {
|
||||
if ($this->serializer) {
|
||||
$action::$serializer = $this->serializer;
|
||||
}
|
||||
foreach ($this->addInclude as $include) {
|
||||
$action::$include[$include['relation']] = $include['default'];
|
||||
}
|
||||
foreach ($this->removeInclude as $relation) {
|
||||
unset($action::$include[$relation]);
|
||||
}
|
||||
foreach ($this->addLink as $relation) {
|
||||
$action::$link[] = $relation;
|
||||
}
|
||||
foreach ($this->removeLink as $relation) {
|
||||
if (($k = array_search($relation, $action::$link)) !== false) {
|
||||
unset($action::$link[$k]);
|
||||
}
|
||||
}
|
||||
if ($this->limitMax) {
|
||||
$action::$limitMax = $this->limitMax;
|
||||
}
|
||||
if ($this->limit) {
|
||||
$action::$limit = $this->limit;
|
||||
}
|
||||
foreach ($this->addSortFields as $field) {
|
||||
$action::$sortFields[] = $field;
|
||||
}
|
||||
foreach ($this->removeSortFields as $field) {
|
||||
if (($k = array_search($field, $action::$sortFields)) !== false) {
|
||||
unset($action::$sortFields[$k]);
|
||||
}
|
||||
}
|
||||
if ($this->sort) {
|
||||
$action::$sort = $this->sort;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
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(Container $container)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class ApiLink implements ExtenderInterface
|
||||
{
|
||||
protected $actions;
|
||||
|
||||
protected $relationships;
|
||||
|
||||
public function __construct($actions, $relationships)
|
||||
{
|
||||
$this->actions = $actions;
|
||||
$this->relationships = $relationships;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
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) {
|
||||
$class::$link[] = $relationship;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
61
framework/core/src/Extend/ApiSerializer.php
Normal file
61
framework/core/src/Extend/ApiSerializer.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class ApiSerializer implements ExtenderInterface
|
||||
{
|
||||
protected $serializer;
|
||||
|
||||
protected $attributes = [];
|
||||
|
||||
protected $relations = [];
|
||||
|
||||
public function __construct($serializer)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
|
||||
public function attributes($callback)
|
||||
{
|
||||
$this->attributes[] = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasOne($relation, $related)
|
||||
{
|
||||
$this->relations[$relation] = function ($serializer) use ($relation, $related) {
|
||||
return $serializer->hasOne($related, $relation);
|
||||
};
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasMany($relation, $related)
|
||||
{
|
||||
$this->relations[$relation] = function ($serializer) use ($relation, $related) {
|
||||
return $serializer->hasMany($related, $relation);
|
||||
};
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$serializer = $this->serializer;
|
||||
|
||||
if (count($this->attributes)) {
|
||||
$container->make('events')->listen('Flarum\Api\Events\SerializeAttributes', function ($event) use ($serializer) {
|
||||
if ($event->serializer instanceof $serializer) {
|
||||
foreach ($this->attributes as $callback) {
|
||||
$callback($event->attributes, $event->model, $event->serializer->actor->getUser());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
foreach ($this->relations as $relation => $callback) {
|
||||
$serializer::addRelationship($relation, $callback);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,18 +2,18 @@
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class EventSubscribers implements ExtenderInterface
|
||||
class EventSubscriber implements ExtenderInterface
|
||||
{
|
||||
protected $subscribers;
|
||||
protected $subscriber;
|
||||
|
||||
public function __construct($subscribers)
|
||||
public function __construct($subscriber)
|
||||
{
|
||||
$this->subscribers = $subscribers;
|
||||
$this->subscriber = $subscriber;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
foreach ((array) $this->subscribers as $subscriber) {
|
||||
foreach ((array) $this->subscriber as $subscriber) {
|
||||
$container->make('events')->subscribe($subscriber);
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class ForumAssets implements ExtenderInterface
|
||||
{
|
||||
protected $files;
|
||||
|
||||
public function __construct($files)
|
||||
{
|
||||
$this->files = $files;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$container->make('events')->listen('Flarum\Forum\Events\RenderView', function ($event) {
|
||||
$event->assets->addFiles($this->files);
|
||||
});
|
||||
}
|
||||
}
|
34
framework/core/src/Extend/ForumClient.php
Normal file
34
framework/core/src/Extend/ForumClient.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Flarum\Forum\Actions\IndexAction;
|
||||
|
||||
class ForumClient implements ExtenderInterface
|
||||
{
|
||||
protected $assets = [];
|
||||
|
||||
protected $translations = [];
|
||||
|
||||
public function assets($assets)
|
||||
{
|
||||
$this->assets = array_merge($this->assets, $assets);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function translations($keys)
|
||||
{
|
||||
$this->translations = array_merge($this->translations, $keys);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$container->make('events')->listen('Flarum\Forum\Events\RenderView', function ($event) {
|
||||
$event->assets->addFiles($this->assets);
|
||||
});
|
||||
|
||||
IndexAction::$translations = array_merge(IndexAction::$translations, $this->translations);
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Flarum\Forum\Actions\IndexAction;
|
||||
|
||||
class ForumTranslations implements ExtenderInterface
|
||||
{
|
||||
protected $keys;
|
||||
|
||||
public function __construct($keys)
|
||||
{
|
||||
$this->keys = $keys;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
IndexAction::$translations = array_merge(IndexAction::$translations, $this->keys);
|
||||
}
|
||||
}
|
87
framework/core/src/Extend/Model.php
Normal file
87
framework/core/src/Extend/Model.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Closure;
|
||||
|
||||
class Model implements ExtenderInterface
|
||||
{
|
||||
protected $model;
|
||||
|
||||
protected $scopeVisible = [];
|
||||
|
||||
protected $allow = [];
|
||||
|
||||
protected $relations = [];
|
||||
|
||||
public function __construct($model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
public function scopeVisible(Closure $callback)
|
||||
{
|
||||
$this->scopeVisible[] = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function allow($action, Closure $callback)
|
||||
{
|
||||
$this->allow[] = compact('action', 'callback');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasOne($relation, $related, $foreignKey = null, $localKey = null)
|
||||
{
|
||||
$this->relations[$relation] = function ($model) use ($relation, $related, $foreignKey, $localKey) {
|
||||
return $model->hasOne($related, $foreignKey, $localKey, $relation);
|
||||
};
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function belongsTo($relation, $related, $foreignKey = null, $otherKey = null)
|
||||
{
|
||||
$this->relations[$relation] = function ($model) use ($relation, $related, $foreignKey, $otherKey) {
|
||||
return $model->belongsTo($related, $foreignKey, $otherKey, $relation);
|
||||
};
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasMany($relation, $related, $foreignKey = null, $localKey = null)
|
||||
{
|
||||
$this->relations[$relation] = function ($model) use ($relation, $related, $foreignKey, $localKey) {
|
||||
return $model->hasMany($related, $foreignKey, $localKey, $relation);
|
||||
};
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function belongsToMany($relation, $related, $table = null, $foreignKey = null, $otherKey = null)
|
||||
{
|
||||
$this->relations[$relation] = function ($model) use ($relation, $related, $table, $foreignKey, $otherKey) {
|
||||
return $model->belongsToMany($related, $table, $foreignKey, $otherKey, $relation);
|
||||
};
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$model = $this->model;
|
||||
|
||||
foreach ($this->relations as $relation => $callback) {
|
||||
$model::addRelationship($relation, $callback);
|
||||
}
|
||||
|
||||
foreach ($this->scopeVisible as $callback) {
|
||||
$model::scopeVisible($callback);
|
||||
}
|
||||
|
||||
foreach ($this->allow as $info) {
|
||||
$model::allow($info['action'], $info['callback']);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Closure;
|
||||
|
||||
class Relationship implements ExtenderInterface
|
||||
{
|
||||
protected $parent;
|
||||
|
||||
protected $name;
|
||||
|
||||
protected $type;
|
||||
|
||||
protected $child;
|
||||
|
||||
protected $table;
|
||||
|
||||
public function __construct($parent, $name, $type, $child = null)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
$this->name = $name;
|
||||
$this->type = $type;
|
||||
$this->child = $child;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$parent = $this->parent;
|
||||
|
||||
$parent::addRelationship($this->name, function ($model) {
|
||||
if ($this->type instanceof Closure) {
|
||||
return call_user_func($this->type, $model);
|
||||
} elseif ($this->type === 'belongsTo') {
|
||||
return $model->belongsTo($this->child, null, null, $this->name);
|
||||
} elseif ($this->type === 'belongsToMany') {
|
||||
return $model->belongsToMany($this->child, $this->table, null, null, $this->name);
|
||||
} else {
|
||||
// @todo
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class SerializeAttributes implements ExtenderInterface
|
||||
{
|
||||
protected $serializer;
|
||||
|
||||
protected $callback;
|
||||
|
||||
public function __construct($serializer, $callback)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$container->make('events')->listen('Flarum\Api\Events\SerializeAttributes', function ($event) {
|
||||
if ($event->serializer instanceof $this->serializer) {
|
||||
call_user_func_array($this->callback, [&$event->attributes, $event->model, $event->serializer]);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
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(Container $container)
|
||||
{
|
||||
$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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -60,7 +60,7 @@ class IndexAction extends HtmlAction
|
||||
// manually?
|
||||
$response = $this->apiClient->send('Flarum\Api\Actions\Users\ShowAction', ['id' => $user->id]);
|
||||
|
||||
$data = [$response->data];
|
||||
$data[] = $response->data;
|
||||
if (isset($response->included)) {
|
||||
$data = array_merge($data, $response->included);
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php namespace Flarum\Forum;
|
||||
|
||||
use Flarum\Extend\ForumTranslations;
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Support\AssetManager;
|
||||
@ -44,12 +43,6 @@ class ForumServiceProvider extends ServiceProvider
|
||||
]);
|
||||
|
||||
$this->routes();
|
||||
|
||||
$this->extend(
|
||||
new ForumTranslations([
|
||||
//
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
protected function routes()
|
||||
|
Loading…
x
Reference in New Issue
Block a user