mirror of
https://github.com/flarum/core.git
synced 2025-07-23 09:41:26 +02:00
Rework public API based on events
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Flarum\Core\Activity\Activity;
|
||||
use Flarum\Api\Serializers\ActivitySerializer;
|
||||
|
||||
class ActivityType implements ExtenderInterface
|
||||
{
|
||||
protected $class;
|
||||
|
||||
protected $serializer;
|
||||
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function subjectSerializer($serializer)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$class = $this->class;
|
||||
$type = $class::getType();
|
||||
|
||||
Activity::setSubjectModel($type, $class::getSubjectModel());
|
||||
|
||||
if ($this->serializer) {
|
||||
ActivitySerializer::setSubjectSerializer($type, $this->serializer);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
<?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,35 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class Api implements ExtenderInterface
|
||||
{
|
||||
protected $routes = [];
|
||||
|
||||
public function route($method, $url, $name, $action)
|
||||
{
|
||||
$this->routes[] = compact('method', 'url', 'name', 'action');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
if ($container->make('type') !== 'api') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (count($this->routes)) {
|
||||
$routes = $container->make('flarum.api.routes');
|
||||
|
||||
foreach ($this->routes as $route) {
|
||||
$method = $route['method'];
|
||||
$routes->$method($route['url'], $route['name'], function (ServerRequestInterface $httpRequest, $routeParams) use ($container, $route) {
|
||||
$action = $container->make($route['action']);
|
||||
|
||||
return $action->handle($httpRequest, $routeParams);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,143 +0,0 @@
|
||||
<?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,61 +0,0 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class DiscussionGambit implements ExtenderInterface
|
||||
{
|
||||
protected $class;
|
||||
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$container->make('flarum.discussionGambits')[] = $this->class;
|
||||
}
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class EventSubscriber implements ExtenderInterface
|
||||
{
|
||||
protected $subscriber;
|
||||
|
||||
public function __construct($subscriber)
|
||||
{
|
||||
$this->subscriber = $subscriber;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
foreach ((array) $this->subscriber as $subscriber) {
|
||||
$container->make('events')->subscribe($subscriber);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
interface ExtenderInterface
|
||||
{
|
||||
public function extend(Container $container);
|
||||
}
|
@@ -1,61 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Flarum\Forum\Actions\IndexAction;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class ForumClient implements ExtenderInterface
|
||||
{
|
||||
protected $assets = [];
|
||||
|
||||
protected $translations = [];
|
||||
|
||||
protected $routes = [];
|
||||
|
||||
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 route($method, $url, $name, $action = 'Flarum\Forum\Actions\IndexAction')
|
||||
{
|
||||
$this->routes[] = compact('method', 'url', 'name', 'action');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
if ($container->make('type') !== 'forum') {
|
||||
return;
|
||||
}
|
||||
|
||||
$container->make('events')->listen('Flarum\Forum\Events\RenderView', function ($event) {
|
||||
$event->assets->addFiles($this->assets);
|
||||
});
|
||||
|
||||
IndexAction::$translations = array_merge(IndexAction::$translations, $this->translations);
|
||||
|
||||
if (count($this->routes)) {
|
||||
$routes = $container->make('flarum.forum.routes');
|
||||
|
||||
foreach ($this->routes as $route) {
|
||||
$method = $route['method'];
|
||||
$routes->$method($route['url'], $route['name'], function (ServerRequestInterface $httpRequest, $routeParams) use ($container, $route) {
|
||||
$action = $container->make($route['action']);
|
||||
|
||||
return $action->handle($httpRequest, $routeParams);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,57 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class Locale implements ExtenderInterface
|
||||
{
|
||||
protected $locale;
|
||||
|
||||
protected $translations;
|
||||
|
||||
protected $config;
|
||||
|
||||
protected $js;
|
||||
|
||||
public function __construct($locale)
|
||||
{
|
||||
$this->locale = $locale;
|
||||
}
|
||||
|
||||
public function translations($translations)
|
||||
{
|
||||
$this->translations = $translations;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function config($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function js($js)
|
||||
{
|
||||
$this->js = $js;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$manager = $container->make('flarum.localeManager');
|
||||
|
||||
if ($this->translations) {
|
||||
$manager->addTranslations($this->locale, $this->translations);
|
||||
}
|
||||
|
||||
if ($this->config) {
|
||||
$manager->addConfig($this->locale, $this->config);
|
||||
}
|
||||
|
||||
if ($this->js) {
|
||||
$manager->addJsFile($this->locale, $this->js);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,96 +0,0 @@
|
||||
<?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 date($attribute)
|
||||
{
|
||||
$this->dates[] = $attribute;
|
||||
}
|
||||
|
||||
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::setRelationMethod($relation, $callback);
|
||||
}
|
||||
|
||||
foreach ($this->scopeVisible as $callback) {
|
||||
$model::addVisibleScope($callback);
|
||||
}
|
||||
|
||||
foreach ($this->allow as $info) {
|
||||
$model::allow($info['action'], $info['callback']);
|
||||
}
|
||||
|
||||
foreach ($this->dates as $attribute) {
|
||||
$model::addDateAttribute($attribute);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,51 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Flarum\Core\Notifications\Notification;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Api\Serializers\NotificationSerializer;
|
||||
use ReflectionClass;
|
||||
|
||||
class NotificationType implements ExtenderInterface
|
||||
{
|
||||
protected $class;
|
||||
|
||||
protected $serializer;
|
||||
|
||||
protected $enabled = [];
|
||||
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function subjectSerializer($serializer)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function enableByDefault($method)
|
||||
{
|
||||
$this->enabled[] = $method;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$class = $this->class;
|
||||
$type = $class::getType();
|
||||
|
||||
Notification::setSubjectModel($type, $class::getSubjectModel());
|
||||
|
||||
User::addPreference(User::getNotificationPreferenceKey($type, 'alert'), 'boolval', in_array('alert', $this->enabled));
|
||||
|
||||
if ((new ReflectionClass($class))->implementsInterface('Flarum\Core\Notifications\MailableBlueprint')) {
|
||||
User::addPreference(User::getNotificationPreferenceKey($type, 'email'), 'boolval', in_array('email', $this->enabled));
|
||||
}
|
||||
|
||||
NotificationSerializer::setSubjectSerializer($type, $this->serializer);
|
||||
}
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class PostFormatter implements ExtenderInterface
|
||||
{
|
||||
protected $class;
|
||||
|
||||
protected $priority;
|
||||
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$container->make('flarum.formatter')->add($this->class);
|
||||
}
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
<?php namespace Flarum\Extend;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Flarum\Core\Posts\Post;
|
||||
|
||||
class PostType implements ExtenderInterface
|
||||
{
|
||||
protected $class;
|
||||
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function extend(Container $container)
|
||||
{
|
||||
$class = $this->class;
|
||||
|
||||
Post::setModel($class::$type, $class);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user