mirror of
https://github.com/flarum/core.git
synced 2025-10-13 07:54:25 +02:00
- Use contextual namespaces within Flarum\Core - Clean up and docblock everything - Refactor Activity/Notification blueprint stuff - Refactor Formatter stuff - Refactor Search stuff - Upgrade to JSON-API 1.0 - Removed “addedPosts” and “removedPosts” relationships from discussion API. This was used for adding/removing event posts after renaming a discussion etc. Instead we should make an additional request to get all new posts Todo: - Fix Extenders and extensions - Get rid of repository interfaces - Fix other bugs I’ve inevitably introduced
97 lines
2.5 KiB
PHP
97 lines
2.5 KiB
PHP
<?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);
|
|
}
|
|
}
|
|
}
|