Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions extensions/mentions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Flarum\Post\Filter\PostFilterer;
use Flarum\Post\Post;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Tag;
use Flarum\User\User;

return [
Expand Down Expand Up @@ -126,7 +125,7 @@

// Tag mentions
(new Extend\Conditional())
->whenExtensionEnabled('flarum-tags', [
->whenExtensionEnabled('flarum-tags', fn () => [
(new Extend\Formatter)
->render(Formatter\FormatTagMentions::class)
->unparse(Formatter\UnparseTagMentions::class),
Expand Down
54 changes: 47 additions & 7 deletions framework/core/src/Extend/Conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,63 @@
use Flarum\Extension\ExtensionManager;
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
{
/**
* @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 = [];

/**
* @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 $extensions->isEnabled($extensionId);
}, $extenders);
}

/**
* @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 ! $extensions->isEnabled($extensionId);
}, $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[] = [
'condition' => $condition,
Expand All @@ -50,6 +79,13 @@ public function when(callable|bool $condition, array $extenders): self
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
{
foreach ($this->conditions as $condition) {
Expand All @@ -58,7 +94,11 @@ public function extend(Container $container, Extension $extension = null): void
}

if ($condition['condition']) {
foreach ($condition['extenders'] as $extender) {
$extenders = $condition['extenders'];

$extenders = $container->call($extenders);

foreach ($extenders as $extender) {
$extender->extend($container, $extension);
}
}
Expand Down
Loading
Loading