From edfd053fc21dc344837de8527b8c639d6c389027 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 13 Feb 2015 10:30:32 +1030 Subject: [PATCH] Explicit mapping of post type to class --- .../src/Flarum/Core/CoreServiceProvider.php | 4 ++++ framework/core/src/Flarum/Core/Posts/Post.php | 22 ++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/framework/core/src/Flarum/Core/CoreServiceProvider.php b/framework/core/src/Flarum/Core/CoreServiceProvider.php index 43cc86991..78976fcc3 100644 --- a/framework/core/src/Flarum/Core/CoreServiceProvider.php +++ b/framework/core/src/Flarum/Core/CoreServiceProvider.php @@ -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'); } /** diff --git a/framework/core/src/Flarum/Core/Posts/Post.php b/framework/core/src/Flarum/Core/Posts/Post.php index cf7504b19..33699b1c5 100755 --- a/framework/core/src/Flarum/Core/Posts/Post.php +++ b/framework/core/src/Flarum/Core/Posts/Post.php @@ -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; + } } }