mirror of
https://github.com/flarum/core.git
synced 2025-08-15 12:54:47 +02:00
[1.x] feat: conditional extend whenExtensionDisabled (#4107)
* feat: conditional extend whenExtensionDisabled * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
@@ -47,6 +47,20 @@ class Conditional implements ExtenderInterface
|
||||
}, $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.
|
||||
*
|
||||
|
@@ -160,6 +160,27 @@ class ConditionalTest extends TestCase
|
||||
$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()
|
||||
{
|
||||
|
Reference in New Issue
Block a user