1
0
mirror of https://github.com/flarum/core.git synced 2025-08-15 04:44:08 +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:
IanM
2024-11-12 09:40:05 +00:00
committed by GitHub
parent 634132e06e
commit 8eb56b16cf
2 changed files with 35 additions and 0 deletions

View File

@@ -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.
*

View File

@@ -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()
{