flarum / framework

Simple forum software for building great communities.
http://flarum.org/
6.27k stars 826 forks source link

[1.x] `Conditional` extender instantiates the extenders array even when the conditional is false #3897

Closed imorland closed 10 months ago

imorland commented 11 months ago

Current Behavior

The Conditional extender instantiates provided extenders even if the associated condition is false. This can lead to unintended side effects, especially if the constructors of these extenders perform actions. More critically, if the extender class doesn't even exist, it will result in a fatal error due to class not being found, regardless of the condition's value.

Steps to Reproduce

Reference a nonexistent extender class within the Conditional extender.

(new Extend\Conditional())
    ->when(false, [new NonexistentExtender()]);

Upon execution, PHP will throw a fatal error stating that the NonexistentExtender class was not found, even though the condition was set to false.

Expected Behavior

The expected behavior is that the Conditional extender should only attempt to instantiate and use provided extenders when the associated condition evaluates to true, and should completely ignore them when the condition is false.

Screenshots

No response

Environment

Output of php flarum info

Output of "php flarum info", run this in terminal in your Flarum directory.

Possible Solution

Instead of directly providing an array of extenders, wrap them in a callback, ie invokable class

Additional Context

No response