Skip to content

Commit

Permalink
[1.x] feat: conditional extend whenExtensionDisabled (#4107)
Browse files Browse the repository at this point in the history
* feat: conditional extend whenExtensionDisabled

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
imorland and StyleCIBot authored Nov 12, 2024
1 parent 634132e commit 8eb56b1
Show file tree
Hide file tree
Showing 2 changed files with 35 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
21 changes: 21 additions & 0 deletions framework/core/tests/integration/extenders/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,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 8eb56b1

Please sign in to comment.