diff --git a/src/Core/CoreServiceProvider.php b/src/Core/CoreServiceProvider.php index 8f71be596..6f63cd6de 100644 --- a/src/Core/CoreServiceProvider.php +++ b/src/Core/CoreServiceProvider.php @@ -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; diff --git a/src/Extend/AdminClient.php b/src/Extend/AdminClient.php new file mode 100644 index 000000000..b12e82ae5 --- /dev/null +++ b/src/Extend/AdminClient.php @@ -0,0 +1,34 @@ +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); + } +} diff --git a/src/Extend/AdminTranslations.php b/src/Extend/AdminTranslations.php deleted file mode 100644 index bea1f8fbb..000000000 --- a/src/Extend/AdminTranslations.php +++ /dev/null @@ -1,18 +0,0 @@ -keys = $keys; - } - - public function extend(Container $container) - { - - } -} diff --git a/src/Extend/ApiAction.php b/src/Extend/ApiAction.php new file mode 100644 index 000000000..cd2f25951 --- /dev/null +++ b/src/Extend/ApiAction.php @@ -0,0 +1,143 @@ +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; + } + } + } +} diff --git a/src/Extend/ApiInclude.php b/src/Extend/ApiInclude.php deleted file mode 100644 index 4336f69f8..000000000 --- a/src/Extend/ApiInclude.php +++ /dev/null @@ -1,35 +0,0 @@ -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; - } - } - } - } -} diff --git a/src/Extend/ApiLink.php b/src/Extend/ApiLink.php deleted file mode 100644 index cc592d262..000000000 --- a/src/Extend/ApiLink.php +++ /dev/null @@ -1,28 +0,0 @@ -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; - } - } - } -} diff --git a/src/Extend/ApiSerializer.php b/src/Extend/ApiSerializer.php new file mode 100644 index 000000000..14010c745 --- /dev/null +++ b/src/Extend/ApiSerializer.php @@ -0,0 +1,61 @@ +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); + } + } +} diff --git a/src/Extend/EventSubscribers.php b/src/Extend/EventSubscriber.php similarity index 50% rename from src/Extend/EventSubscribers.php rename to src/Extend/EventSubscriber.php index c8c391a1c..27b129afc 100644 --- a/src/Extend/EventSubscribers.php +++ b/src/Extend/EventSubscriber.php @@ -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); } } diff --git a/src/Extend/ForumAssets.php b/src/Extend/ForumAssets.php deleted file mode 100644 index 1294d94fd..000000000 --- a/src/Extend/ForumAssets.php +++ /dev/null @@ -1,20 +0,0 @@ -files = $files; - } - - public function extend(Container $container) - { - $container->make('events')->listen('Flarum\Forum\Events\RenderView', function ($event) { - $event->assets->addFiles($this->files); - }); - } -} diff --git a/src/Extend/ForumClient.php b/src/Extend/ForumClient.php new file mode 100644 index 000000000..fba5642ea --- /dev/null +++ b/src/Extend/ForumClient.php @@ -0,0 +1,34 @@ +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); + } +} diff --git a/src/Extend/ForumTranslations.php b/src/Extend/ForumTranslations.php deleted file mode 100644 index 1e56c2a14..000000000 --- a/src/Extend/ForumTranslations.php +++ /dev/null @@ -1,19 +0,0 @@ -keys = $keys; - } - - public function extend(Container $container) - { - IndexAction::$translations = array_merge(IndexAction::$translations, $this->keys); - } -} diff --git a/src/Extend/Model.php b/src/Extend/Model.php new file mode 100644 index 000000000..7cf2fa6af --- /dev/null +++ b/src/Extend/Model.php @@ -0,0 +1,87 @@ +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']); + } + } +} diff --git a/src/Extend/Relationship.php b/src/Extend/Relationship.php deleted file mode 100644 index a5e817f82..000000000 --- a/src/Extend/Relationship.php +++ /dev/null @@ -1,42 +0,0 @@ -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 - } - }); - } -} diff --git a/src/Extend/SerializeAttributes.php b/src/Extend/SerializeAttributes.php deleted file mode 100644 index e3d314889..000000000 --- a/src/Extend/SerializeAttributes.php +++ /dev/null @@ -1,25 +0,0 @@ -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]); - } - }); - } -} diff --git a/src/Extend/SerializeRelationship.php b/src/Extend/SerializeRelationship.php deleted file mode 100644 index 3d52e0ea7..000000000 --- a/src/Extend/SerializeRelationship.php +++ /dev/null @@ -1,36 +0,0 @@ -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); - } - }); - } -} diff --git a/src/Forum/Actions/IndexAction.php b/src/Forum/Actions/IndexAction.php index 215ce902f..53ec01873 100644 --- a/src/Forum/Actions/IndexAction.php +++ b/src/Forum/Actions/IndexAction.php @@ -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); } diff --git a/src/Forum/ForumServiceProvider.php b/src/Forum/ForumServiceProvider.php index f6e64a1e5..f79fa639d 100644 --- a/src/Forum/ForumServiceProvider.php +++ b/src/Forum/ForumServiceProvider.php @@ -1,6 +1,5 @@ routes(); - - $this->extend( - new ForumTranslations([ - // - ]) - ); } protected function routes()