mirror of
https://github.com/flarum/framework.git
synced 2024-11-23 01:23:08 +08:00
Explicit mapping of post type to class
This commit is contained in:
parent
011ae3603e
commit
5e09fd7cd8
|
@ -5,6 +5,7 @@ use Config;
|
|||
use Event;
|
||||
|
||||
use Flarum\Core\Formatter\FormatterManager;
|
||||
use Flarum\Core\Posts\Post;
|
||||
|
||||
class CoreServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
@ -31,6 +32,9 @@ class CoreServiceProvider extends ServiceProvider
|
|||
Event::listen('Flarum.Core.*', 'Flarum\Core\Listeners\DiscussionMetadataUpdater');
|
||||
Event::listen('Flarum.Core.*', 'Flarum\Core\Listeners\UserMetadataUpdater');
|
||||
Event::listen('Flarum.Core.*', 'Flarum\Core\Listeners\RenamedPostCreator');
|
||||
|
||||
Post::addType('comment', 'Flarum\Core\Posts\CommentPost');
|
||||
Post::addType('renamed', 'Flarum\Core\Posts\RenamedPost');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -27,6 +27,8 @@ class Post extends Entity
|
|||
'hide_user_id' => 'integer',
|
||||
];
|
||||
|
||||
protected static $types = [];
|
||||
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
@ -111,15 +113,23 @@ class Post extends Entity
|
|||
}
|
||||
}
|
||||
|
||||
public static function addType($type, $class)
|
||||
{
|
||||
static::$types[$type] = $class;
|
||||
}
|
||||
|
||||
public function newFromBuilder($attributes = [])
|
||||
{
|
||||
if (!empty($attributes->type)) {
|
||||
$class = 'Flarum\Core\Posts\\'.ucfirst($attributes->type).'Post';
|
||||
if (class_exists($class)) {
|
||||
$instance = new $class;
|
||||
$instance->exists = true;
|
||||
$instance->setRawAttributes((array) $attributes, true);
|
||||
return $instance;
|
||||
$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);
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user