mirror of
https://github.com/flarum/framework.git
synced 2025-02-25 07:18:55 +08:00
Added post extender with type method, deprecated ConfigurePostTypes (#2101)
This commit is contained in:
parent
459d4b63b0
commit
9149489405
@ -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;
|
||||||
|
39
framework/core/src/Extend/Post.php
Normal file
39
framework/core/src/Extend/Post.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
);
|
);
|
||||||
|
@ -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}
|
||||||
|
58
framework/core/tests/integration/extenders/PostTest.php
Normal file
58
framework/core/tests/integration/extenders/PostTest.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user