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
  • Loading branch information
imorland committed Oct 18, 2023
1 parent db0d9cb commit 74a9e01
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 105 deletions.
22 changes: 1 addition & 21 deletions extensions/mentions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
use Flarum\Post\Event\Revised;
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,23 +124,5 @@

// Tag mentions
(new Extend\Conditional())
->whenExtensionEnabled('flarum-tags', [
(new Extend\Formatter)
->render(Formatter\FormatTagMentions::class)
->unparse(Formatter\UnparseTagMentions::class),

(new Extend\ApiSerializer(BasicPostSerializer::class))
->hasMany('mentionsTags', TagSerializer::class),

(new Extend\ApiController(Controller\ShowDiscussionController::class))
->load(['posts.mentionsTags']),

(new Extend\ApiController(Controller\ListDiscussionsController::class))
->load([
'firstPost.mentionsTags', 'lastPost.mentionsTags',
]),

(new Extend\ApiController(Controller\ListPostsController::class))
->load(['mentionsTags']),
]),
->whenExtensionEnabled('flarum-tags', TagExtender::class),
];
34 changes: 34 additions & 0 deletions extensions/mentions/src/TagExtender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Flarum\Mentions;

use Flarum\Api\Controller;
use Flarum\Api\Serializer\BasicPostSerializer;
use Flarum\Extend;
use Flarum\Tags\Api\Serializer\TagSerializer;

class TagExtender
{
public function __invoke(): array
{
return [
(new Extend\Formatter)
->render(Formatter\FormatTagMentions::class)
->unparse(Formatter\UnparseTagMentions::class),

(new Extend\ApiSerializer(BasicPostSerializer::class))
->hasMany('mentionsTags', TagSerializer::class),

(new Extend\ApiController(Controller\ShowDiscussionController::class))
->load(['posts.mentionsTags']),

(new Extend\ApiController(Controller\ListDiscussionsController::class))
->load([
'firstPost.mentionsTags', 'lastPost.mentionsTags',
]),

(new Extend\ApiController(Controller\ListPostsController::class))
->load(['mentionsTags']),
];
}
}
58 changes: 50 additions & 8 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);
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,13 @@ public function extend(Container $container, Extension $extension = null): void
}

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

if (is_string($extenders) || is_callable($extenders)) {

Check failure on line 99 in framework/core/src/Extend/Conditional.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Result of || is always true.

Check failure on line 99 in framework/core/src/Extend/Conditional.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Result of || is always true.
$extenders = $container->call($extenders);
}

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

0 comments on commit 74a9e01

Please sign in to comment.