1
0
mirror of https://github.com/flarum/core.git synced 2025-10-14 00:15:51 +02:00

Upgrade to L5 + huge refactor + more. closes #2

New stuff:
- Signup + email confirmation.
- Updated authentication strategy with remember cookies. closes #5
- New search system with some example gambits! This is cool - check out
the source. Fulltext drivers will be implemented as decorators
overriding the EloquentPostRepository’s findByContent method.
- Lay down the foundation for bootstrapping the Ember app.
- Update Web layer’s asset manager to properly publish CSS/JS files.
- Console commands to run installation migrations and seeds.

Refactoring:
- New structure: move models, repositories, commands, and events into
their own namespaces, rather than grouping by entity.
- All events are classes.
- Use L5 middleware and command bus implementations.
- Clearer use of repositories and the Active Record pattern.
Repositories are used only for retrieval of ActiveRecord objects, and
then save/delete operations are called directly on those ActiveRecords.
This way, we don’t over-abstract at the cost of Eloquent magic, but
testing is still easy.
- Refactor of Web layer so that it uses the Actions routing
architecture.
- “Actor” concept instead of depending on Laravel’s Auth.
- General cleanup!
This commit is contained in:
Toby Zerner
2015-02-24 20:33:18 +10:30
parent 0e4e44c358
commit 2c46888db5
266 changed files with 5562 additions and 4658 deletions

151
src/Core/Models/Post.php Executable file
View File

@@ -0,0 +1,151 @@
<?php namespace Flarum\Core\Models;
use Tobscure\Permissible\Permissible;
use Flarum\Core\Events\PostWasDeleted;
class Post extends Model
{
use Permissible;
/**
* The validation rules for this model.
*
* @var array
*/
public static $rules = [
'discussion_id' => 'required|integer',
'time' => 'required|date',
'content' => 'required',
'number' => 'integer',
'user_id' => 'integer',
'edit_time' => 'date',
'edit_user_id' => 'integer',
'hide_time' => 'date',
'hide_user_id' => 'integer',
];
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'posts';
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['time', 'edit_time', 'hide_time'];
/**
* A map of post types, as specified in the `type` column, to their
* classes.
*
* @var array
*/
protected static $types = [];
/**
* Raise an event when a post is deleted.
*
* @return void
*/
public static function boot()
{
parent::boot();
static::deleted(function ($post) {
$post->raise(new PostWasDeleted($post));
});
}
/**
* Define the relationship with the post's discussion.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function discussion()
{
return $this->belongsTo('Flarum\Core\Models\Discussion', 'discussion_id');
}
/**
* Define the relationship with the post's author.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('Flarum\Core\Models\User', 'user_id');
}
/**
* Define the relationship with the user who edited the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function editUser()
{
return $this->belongsTo('Flarum\Core\Models\User', 'edit_user_id');
}
/**
* Define the relationship with the user who hid the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function hideUser()
{
return $this->belongsTo('Flarum\Core\Models\User', 'hide_user_id');
}
/**
* Terminate the query and return an array of matching IDs.
* Example usage: `$ids = $discussion->posts()->ids()`
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return array
*/
public function scopeIds($query)
{
return array_map('intval', $query->get(['id'])->fetch('id')->all());
}
/**
* Create a new model instance according to the post's type.
*
* @param array $attributes
* @return static|object
*/
public function newFromBuilder($attributes = [], $connection = null)
{
if (!empty($attributes->type)) {
$type = $attributes->type;
if (isset(static::$types[$type])) {
$class = static::$types[$type];
if (class_exists($class)) {
$instance = new $class;
$instance->exists = true;
$instance->setRawAttributes((array) $attributes, true);
$instance->setConnection($connection ?: $this->connection);
return $instance;
}
}
}
return parent::newFromBuilder($attributes, $connection);
}
/**
* Register a post type and its model class.
*
* @param string $type
* @param string $class
* @return void
*/
public static function addType($type, $class)
{
static::$types[$type] = $class;
}
}