diff --git a/framework/core/src/Event/ConfigurePostTypes.php b/framework/core/src/Event/ConfigurePostTypes.php index 577103a0f..768db2b48 100644 --- a/framework/core/src/Event/ConfigurePostTypes.php +++ b/framework/core/src/Event/ConfigurePostTypes.php @@ -9,6 +9,9 @@ namespace Flarum\Event; +/** + * @deprecated in beta 15, remove in beta 16. Use the Post extender instead. + */ class ConfigurePostTypes { private $models; diff --git a/framework/core/src/Extend/Post.php b/framework/core/src/Extend/Post.php new file mode 100644 index 000000000..f750dbe2a --- /dev/null +++ b/framework/core/src/Extend/Post.php @@ -0,0 +1,39 @@ +postTypes[] = $postType; + + return $this; + } + + public function extend(Container $container, Extension $extension = null) + { + foreach ($this->postTypes as $postType) { + PostModel::setModel($postType::$type, $postType); + } + } +} diff --git a/framework/core/src/Post/Post.php b/framework/core/src/Post/Post.php index 89bd87d43..fc9ef843b 100644 --- a/framework/core/src/Post/Post.php +++ b/framework/core/src/Post/Post.php @@ -218,7 +218,7 @@ class Post extends AbstractModel * @param string $model The class name of the model for that type. * @return void */ - public static function setModel($type, $model) + public static function setModel(string $type, string $model) { static::$models[$type] = $model; } diff --git a/framework/core/src/Post/PostServiceProvider.php b/framework/core/src/Post/PostServiceProvider.php index 5fda0c859..be1c9d7c7 100644 --- a/framework/core/src/Post/PostServiceProvider.php +++ b/framework/core/src/Post/PostServiceProvider.php @@ -21,19 +21,20 @@ class PostServiceProvider extends AbstractServiceProvider { CommentPost::setFormatter($this->app->make('flarum.formatter')); - $this->registerPostTypes(); + $this->setPostTypes(); $events = $this->app->make('events'); $events->subscribe(PostPolicy::class); } - public function registerPostTypes() + protected function setPostTypes() { $models = [ CommentPost::class, DiscussionRenamedPost::class ]; + // Deprecated in beta 15, remove in beta 16. $this->app->make('events')->dispatch( new ConfigurePostTypes($models) ); diff --git a/framework/core/tests/integration/extenders/ModelTest.php b/framework/core/tests/integration/extenders/ModelTest.php index 1dd4fc057..608c7aadc 100644 --- a/framework/core/tests/integration/extenders/ModelTest.php +++ b/framework/core/tests/integration/extenders/ModelTest.php @@ -326,7 +326,7 @@ class ModelTest extends TestCase $this->app(); - $post = new CustomPost; + $post = new ModelTestCustomPost; $this->assertEquals(42, $post->answer); @@ -416,7 +416,7 @@ class ModelTest extends TestCase } } -class CustomPost extends AbstractEventPost +class ModelTestCustomPost extends AbstractEventPost { /** * {@inheritdoc} diff --git a/framework/core/tests/integration/extenders/PostTest.php b/framework/core/tests/integration/extenders/PostTest.php new file mode 100644 index 000000000..a91f12d77 --- /dev/null +++ b/framework/core/tests/integration/extenders/PostTest.php @@ -0,0 +1,58 @@ +assertArrayNotHasKey('customPost', Post::getModels()); + } + + /** + * @test + */ + public function custom_post_type_exists_if_added() + { + $this->extend((new Extend\Post)->type(PostTestCustomPost::class)); + + // Needed for extenders to be booted + $this->app(); + + $this->assertArrayHasKey('customPost', Post::getModels()); + } +} + +class PostTestCustomPost extends AbstractEventPost implements MergeableInterface +{ + /** + * {@inheritdoc} + */ + public static $type = 'customPost'; + + /** + * {@inheritdoc} + */ + public function saveAfter(Post $previous = null) + { + $this->save(); + + return $this; + } +}