Skip to content

Commit

Permalink
chore: remove ExtenderInterface[] as a conditional option, only suppo…
Browse files Browse the repository at this point in the history
…rt 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 <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Oct 21, 2023
1 parent 94de8b4 commit e4e0fbf
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 85 deletions.
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

0 comments on commit e4e0fbf

Please sign in to comment.