Added post extender with type method, deprecated ConfigurePostTypes (#2101)

This commit is contained in:
Alexander Skvortsov 2020-11-03 10:43:49 -05:00 committed by GitHub
parent 459d4b63b0
commit 9149489405
6 changed files with 106 additions and 5 deletions

View File

@ -9,6 +9,9 @@
namespace Flarum\Event; namespace Flarum\Event;
/**
* @deprecated in beta 15, remove in beta 16. Use the Post extender instead.
*/
class ConfigurePostTypes class ConfigurePostTypes
{ {
private $models; private $models;

View File

@ -0,0 +1,39 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\Post\Post as PostModel;
use Illuminate\Contracts\Container\Container;
class Post implements ExtenderInterface
{
private $postTypes = [];
/**
* Register a new post type. This is generally done for custom 'event posts',
* such as those that appear when a discussion is renamed.
*
* @param string $postType: The ::class attribute of the custom Post type that is being added.
*/
public function type(string $postType)
{
$this->postTypes[] = $postType;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
foreach ($this->postTypes as $postType) {
PostModel::setModel($postType::$type, $postType);
}
}
}

View File

@ -218,7 +218,7 @@ class Post extends AbstractModel
* @param string $model The class name of the model for that type. * @param string $model The class name of the model for that type.
* @return void * @return void
*/ */
public static function setModel($type, $model) public static function setModel(string $type, string $model)
{ {
static::$models[$type] = $model; static::$models[$type] = $model;
} }

View File

@ -21,19 +21,20 @@ class PostServiceProvider extends AbstractServiceProvider
{ {
CommentPost::setFormatter($this->app->make('flarum.formatter')); CommentPost::setFormatter($this->app->make('flarum.formatter'));
$this->registerPostTypes(); $this->setPostTypes();
$events = $this->app->make('events'); $events = $this->app->make('events');
$events->subscribe(PostPolicy::class); $events->subscribe(PostPolicy::class);
} }
public function registerPostTypes() protected function setPostTypes()
{ {
$models = [ $models = [
CommentPost::class, CommentPost::class,
DiscussionRenamedPost::class DiscussionRenamedPost::class
]; ];
// Deprecated in beta 15, remove in beta 16.
$this->app->make('events')->dispatch( $this->app->make('events')->dispatch(
new ConfigurePostTypes($models) new ConfigurePostTypes($models)
); );

View File

@ -326,7 +326,7 @@ class ModelTest extends TestCase
$this->app(); $this->app();
$post = new CustomPost; $post = new ModelTestCustomPost;
$this->assertEquals(42, $post->answer); $this->assertEquals(42, $post->answer);
@ -416,7 +416,7 @@ class ModelTest extends TestCase
} }
} }
class CustomPost extends AbstractEventPost class ModelTestCustomPost extends AbstractEventPost
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}

View File

@ -0,0 +1,58 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\extenders;
use Flarum\Extend;
use Flarum\Post\AbstractEventPost;
use Flarum\Post\MergeableInterface;
use Flarum\Post\Post;
use Flarum\Tests\integration\TestCase;
class PostTest extends TestCase
{
/**
* @test
*/
public function custom_post_type_doesnt_exist_by_default()
{
$this->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;
}
}