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

[1.x] feat: conditional extend whenExtensionDisabled #4107

Merged
merged 2 commits into from
Nov 12, 2024
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
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);
}
imorland marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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
Loading