Skip to content

Commit 8eb56b1

Browse files
imorlandStyleCIBot
andauthored
[1.x] feat: conditional extend whenExtensionDisabled (#4107)
* feat: conditional extend whenExtensionDisabled * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 634132e commit 8eb56b1

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

framework/core/src/Extend/Conditional.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ public function whenExtensionEnabled(string $extensionId, $extenders): self
4747
}, $extenders);
4848
}
4949

50+
/**
51+
* Apply extenders only if a specific extension is disabled/not installed.
52+
*
53+
* @param string $extensionId The ID of the extension.
54+
* @param ExtenderInterface[]|callable|string $extenders A callable returning an array of extenders, or an invokable class string.
55+
* @return self
56+
*/
57+
public function whenExtensionDisabled(string $extensionId, $extenders): self
58+
{
59+
return $this->when(function (ExtensionManager $extensions) use ($extensionId) {
60+
return ! $extensions->isEnabled($extensionId);
61+
}, $extenders);
62+
}
63+
5064
/**
5165
* Apply extenders based on a condition.
5266
*

framework/core/tests/integration/extenders/ConditionalTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,27 @@ public function conditional_injects_dependencies_to_condition_callable()
160160
$this->app();
161161
}
162162

163+
/** @test */
164+
public function conditional_disabled_extension_not_enabled_applies_extender()
165+
{
166+
$this->extend(
167+
(new Extend\Conditional())
168+
->whenExtensionDisabled('flarum-dummy-extension', TestExtender::class)
169+
);
170+
171+
$this->app();
172+
173+
$response = $this->send(
174+
$this->request('GET', '/api', [
175+
'authenticatedAs' => 1,
176+
])
177+
);
178+
179+
$payload = json_decode($response->getBody()->getContents(), true);
180+
181+
$this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']);
182+
}
183+
163184
/** @test */
164185
public function conditional_does_not_instantiate_extender_if_condition_is_false_using_callable()
165186
{

0 commit comments

Comments
 (0)