Skip to content

Commit

Permalink
feat: conditional extend whenExtensionDisabled
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 9, 2024
1 parent 634132e commit 023af44
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions framework/core/src/Extend/Conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public function whenExtensionEnabled(string $extensionId, $extenders): self
}, $extenders);
}

/**
* Apply extenders only if a specific extension is disabled/not installed.
*
* @param string $extensionId The ID of the extension.
* @param ExtenderInterface[]|callable|string $extenders A callable returning an array of extenders, or an invokable class string.
* @return self
*/
public function whenExtensionDisabled(string $extensionId, $extenders): self
{
return $this->when(function (ExtensionManager $extensions) use ($extensionId) {
return ! $extensions->isEnabled($extensionId);
}, $extenders);
}

/**
* Apply extenders based on a condition.
*
Expand Down
22 changes: 22 additions & 0 deletions framework/core/tests/integration/extenders/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flarum\Extension\ExtensionManager;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use s9e\TextFormatter\Configurator\TemplateNormalizations\Custom;

class ConditionalTest extends TestCase
{
Expand Down Expand Up @@ -160,6 +161,27 @@ public function conditional_injects_dependencies_to_condition_callable()
$this->app();
}

/** @test */
public function conditional_disabled_extension_not_enabled_applies_extender()
{
$this->extend(
(new Extend\Conditional())
->whenExtensionDisabled('flarum-dummy-extension', 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_callable()
{
Expand Down

0 comments on commit 023af44

Please sign in to comment.