1
0
mirror of https://github.com/flarum/core.git synced 2025-08-07 17:07:19 +02:00

chore: remove ExtenderInterface[] as a conditional option, only support callable or ::class invoke (#3904)

* chore: remove ExtenderInterface[] as a conditional option, only support callable or ::class invoke

* Apply fixes from StyleCI

* stan

* review

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
IanM
2023-10-21 17:37:07 +01:00
committed by GitHub
parent 94de8b42b4
commit e4e0fbff73
3 changed files with 228 additions and 85 deletions

View File

@@ -27,7 +27,6 @@ use Flarum\Post\Event\Revised;
use Flarum\Post\Filter\PostFilterer; use Flarum\Post\Filter\PostFilterer;
use Flarum\Post\Post; use Flarum\Post\Post;
use Flarum\Tags\Api\Serializer\TagSerializer; use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Tag;
use Flarum\User\User; use Flarum\User\User;
return [ return [
@@ -126,7 +125,7 @@ return [
// Tag mentions // Tag mentions
(new Extend\Conditional()) (new Extend\Conditional())
->whenExtensionEnabled('flarum-tags', [ ->whenExtensionEnabled('flarum-tags', fn () => [
(new Extend\Formatter) (new Extend\Formatter)
->render(Formatter\FormatTagMentions::class) ->render(Formatter\FormatTagMentions::class)
->unparse(Formatter\UnparseTagMentions::class), ->unparse(Formatter\UnparseTagMentions::class),

View File

@@ -13,17 +13,34 @@ use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager; use Flarum\Extension\ExtensionManager;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
/**
* The Conditional extender allows developers to conditionally apply other extenders
* based on either boolean values or results from callable functions.
*
* This is useful for applying extenders only if certain conditions are met,
* such as the presence of an enabled extension or a specific configuration setting.
*/
class Conditional implements ExtenderInterface class Conditional implements ExtenderInterface
{ {
/** /**
* @var array<array{condition: bool|callable, extenders: ExtenderInterface[]}> * An array of conditions and their associated extenders.
*
* Each entry should have:
* - 'condition': a boolean or callable that should return a boolean.
* - 'extenders': a callable returning an array of extenders, or an invokable class string.
*
* @var array<array{condition: bool|callable, extenders: callable|string}>
*/ */
protected array $conditions = []; protected array $conditions = [];
/** /**
* @param ExtenderInterface[] $extenders * Apply extenders only if a specific extension is enabled.
*
* @param string $extensionId The ID of the extension.
* @param callable|string $extenders A callable returning an array of extenders, or an invokable class string.
* @return self
*/ */
public function whenExtensionEnabled(string $extensionId, array $extenders): self public function whenExtensionEnabled(string $extensionId, callable|string $extenders): self
{ {
return $this->when(function (ExtensionManager $extensions) use ($extensionId) { return $this->when(function (ExtensionManager $extensions) use ($extensionId) {
return $extensions->isEnabled($extensionId); return $extensions->isEnabled($extensionId);
@@ -31,16 +48,28 @@ class Conditional implements ExtenderInterface
} }
/** /**
* @param ExtenderInterface[] $extenders * Apply extenders only if a specific extension is disabled.
*
* @param string $extensionId The ID of the extension.
* @param callable|string $extenders A callable returning an array of extenders, or an invokable class string.
* @return self
*/ */
public function whenExtensionDisabled(string $extensionId, array $extenders): self public function whenExtensionDisabled(string $extensionId, callable|string $extenders): self
{ {
return $this->when(function (ExtensionManager $extensions) use ($extensionId) { return $this->when(function (ExtensionManager $extensions) use ($extensionId) {
return ! $extensions->isEnabled($extensionId); return ! $extensions->isEnabled($extensionId);
}, $extenders); }, $extenders);
} }
public function when(callable|bool $condition, array $extenders): self /**
* Apply extenders based on a condition.
*
* @param bool|callable $condition A boolean or callable that should return a boolean.
* If this evaluates to true, the extenders will be applied.
* @param callable|string $extenders A callable returning an array of extenders, or an invokable class string.
* @return self
*/
public function when(callable|bool $condition, callable|string $extenders): self
{ {
$this->conditions[] = [ $this->conditions[] = [
'condition' => $condition, 'condition' => $condition,
@@ -50,6 +79,13 @@ class Conditional implements ExtenderInterface
return $this; return $this;
} }
/**
* Iterates over the conditions and applies the associated extenders if the conditions are met.
*
* @param Container $container
* @param Extension|null $extension
* @return void
*/
public function extend(Container $container, Extension $extension = null): void public function extend(Container $container, Extension $extension = null): void
{ {
foreach ($this->conditions as $condition) { foreach ($this->conditions as $condition) {
@@ -58,7 +94,11 @@ class Conditional implements ExtenderInterface
} }
if ($condition['condition']) { if ($condition['condition']) {
foreach ($condition['extenders'] as $extender) { $extenders = $condition['extenders'];
$extenders = $container->call($extenders);
foreach ($extenders as $extender) {
$extender->extend($container, $extension); $extender->extend($container, $extension);
} }
} }

View File

@@ -25,14 +25,14 @@ class ConditionalTest extends TestCase
{ {
$this->extend( $this->extend(
(new Extend\Conditional()) (new Extend\Conditional())
->when(true, [ ->when(true, function () {
return [
(new Extend\ApiSerializer(ForumSerializer::class)) (new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () { ->attributes(function () {
return [ return ['customConditionalAttribute' => true];
'customConditionalAttribute' => true })
]; ];
}) })
])
); );
$this->app(); $this->app();
@@ -53,14 +53,14 @@ class ConditionalTest extends TestCase
{ {
$this->extend( $this->extend(
(new Extend\Conditional()) (new Extend\Conditional())
->when(false, [ ->when(false, function () {
return [
(new Extend\ApiSerializer(ForumSerializer::class)) (new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () { ->attributes(function () {
return [ return ['customConditionalAttribute' => true];
'customConditionalAttribute' => true })
]; ];
}) })
])
); );
$this->app(); $this->app();
@@ -83,14 +83,14 @@ class ConditionalTest extends TestCase
(new Extend\Conditional()) (new Extend\Conditional())
->when(function () { ->when(function () {
return true; return true;
}, [ }, function () {
return [
(new Extend\ApiSerializer(ForumSerializer::class)) (new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () { ->attributes(function () {
return [ return ['customConditionalAttribute' => true];
'customConditionalAttribute' => true })
]; ];
}) })
])
); );
$this->app(); $this->app();
@@ -113,14 +113,14 @@ class ConditionalTest extends TestCase
(new Extend\Conditional()) (new Extend\Conditional())
->when(function () { ->when(function () {
return false; return false;
}, [ }, function () {
return [
(new Extend\ApiSerializer(ForumSerializer::class)) (new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () { ->attributes(function () {
return [ return ['customConditionalAttribute' => true];
'customConditionalAttribute' => true })
]; ];
}) })
])
); );
$this->app(); $this->app();
@@ -147,32 +147,25 @@ class ConditionalTest extends TestCase
if (! $extensions) { if (! $extensions) {
throw new Exception('ExtensionManager not injected'); throw new Exception('ExtensionManager not injected');
} }
}, [ }, function () {
return [
(new Extend\ApiSerializer(ForumSerializer::class)) (new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () { ->attributes(function () {
return [ return ['customConditionalAttribute' => true];
'customConditionalAttribute' => true })
]; ];
}) })
])
); );
$this->app(); $this->app();
} }
/** @test */ /** @test */
public function conditional_disabled_extension_not_enabled_applies_extender_array() public function conditional_disabled_extension_not_enabled_applies_invokable_class()
{ {
$this->extend( $this->extend(
(new Extend\Conditional()) (new Extend\Conditional())
->whenExtensionDisabled('flarum-dummy-extension', [ ->whenExtensionDisabled('flarum-dummy-extension', TestExtender::class)
(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () {
return [
'customConditionalAttribute' => true
];
})
])
); );
$this->app(); $this->app();
@@ -189,20 +182,13 @@ class ConditionalTest extends TestCase
} }
/** @test */ /** @test */
public function conditional_disabled_extension_enabled_does_not_apply_extender_array() public function conditional_disabled_extension_enabled_does_not_apply_invokable_class()
{ {
$this->extension('flarum-tags'); $this->extension('flarum-tags');
$this->extend( $this->extend(
(new Extend\Conditional()) (new Extend\Conditional())
->whenExtensionDisabled('flarum-tags', [ ->whenExtensionDisabled('flarum-tags', TestExtender::class)
(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () {
return [
'customConditionalAttribute' => true
];
})
])
); );
$this->app(); $this->app();
@@ -219,18 +205,11 @@ class ConditionalTest extends TestCase
} }
/** @test */ /** @test */
public function conditional_enabled_extension_disabled_does_not_apply_extender_array() public function conditional_enabled_extension_disabled_does_not_apply_invokable_class()
{ {
$this->extend( $this->extend(
(new Extend\Conditional()) (new Extend\Conditional())
->whenExtensionEnabled('flarum-dummy-extension', [ ->whenExtensionEnabled('flarum-dummy-extension', TestExtender::class)
(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () {
return [
'customConditionalAttribute' => true
];
})
])
); );
$this->app(); $this->app();
@@ -247,19 +226,12 @@ class ConditionalTest extends TestCase
} }
/** @test */ /** @test */
public function conditional_enabled_extension_enabled_applies_extender_array() public function conditional_enabled_extension_enabled_applies_invokable_class()
{ {
$this->extension('flarum-tags'); $this->extension('flarum-tags');
$this->extend( $this->extend(
(new Extend\Conditional()) (new Extend\Conditional())
->whenExtensionEnabled('flarum-tags', [ ->whenExtensionEnabled('flarum-tags', TestExtender::class)
(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () {
return [
'customConditionalAttribute' => true
];
})
])
); );
$this->app(); $this->app();
@@ -274,4 +246,136 @@ class ConditionalTest extends TestCase
$this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']); $this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']);
} }
/** @test */
public function conditional_does_not_instantiate_extender_if_condition_is_false_using_callable()
{
$this->extend(
(new Extend\Conditional())
->when(false, TestExtender::class)
);
$this->app();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);
$payload = json_decode($response->getBody()->getContents(), true);
$this->assertArrayNotHasKey('customConditionalAttribute', $payload['data']['attributes']);
}
/** @test */
public function conditional_does_instantiate_extender_if_condition_is_true_using_callable()
{
$this->extend(
(new Extend\Conditional())
->when(true, TestExtender::class)
);
$this->app();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);
$payload = json_decode($response->getBody()->getContents(), true);
$this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']);
}
/** @test */
public function conditional_does_not_instantiate_extender_if_condition_is_false_using_callback()
{
$this->extend(
(new Extend\Conditional())
->when(false, function () {
return [
(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () {
return ['customConditionalAttribute' => true];
})
];
})
);
$this->app();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);
$payload = json_decode($response->getBody()->getContents(), true);
$this->assertArrayNotHasKey('customConditionalAttribute', $payload['data']['attributes']);
}
/** @test */
public function conditional_does_instantiate_extender_if_condition_is_true_using_callback()
{
$this->extend(
(new Extend\Conditional())
->when(true, function () {
return [
(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () {
return ['customConditionalAttribute' => true];
})
];
})
);
$this->app();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);
$payload = json_decode($response->getBody()->getContents(), true);
$this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']);
}
/** @test */
public function conditional_does_not_work_if_extension_is_disabled()
{
$this->extend(
(new Extend\Conditional())
->whenExtensionEnabled('dummy-extension-id', TestExtender::class)
);
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);
$payload = json_decode($response->getBody()->getContents(), true);
$this->assertArrayNotHasKey('customConditionalAttribute', $payload['data']['attributes']);
}
}
class TestExtender
{
public function __invoke(): array
{
return [
(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function () {
return [
'customConditionalAttribute' => true
];
})
];
}
} }