Skip to content

Support factory functions returning Closure for container variables - #318

Merged
clue merged 1 commit into
clue:mainfrom
clue-labs:container-factory-closure
Oct 29, 2026
Merged

clue merged 1 commit into
clue:mainfrom
clue-labs:container-factory-closure

Conversation

@clue

@clue clue commented Oct 29, 2026

Copy link
Copy Markdown
Owner

This changeset adds support for factory functions returning a Closure for container variables. The factory function is still invoked only once and every consumer receives the same Closure, just like with any other container variable:

$container = new FrameworkX\Container([
    'greeter' => function () {
        return function (string $name) {
            return 'Hello ' . $name . '!';
        };
    },
    Acme\Todo\UserController::class => function (\Closure $greeter) {
        return new Acme\Todo\UserController($greeter);
    }
]);

A Closure in the container configuration is always treated as a factory function, so returning one from a factory function is the only way to provide it as a value. Until now, this was not supported because the cached result would be invoked as a factory again on the next access, which #302 turned into an explicit TypeError. The cache now wraps it in a new Closure instead. Factory functions for class names are unchanged and must still return an object.

This includes a new test case covering repeated access to the same Closure. This has 100% code coverage and should be safe to apply.

Builds on top of #302, #178, #307, #288 and others

@clue clue added the new feature New feature or request label Oct 29, 2026
@clue clue added this to the v0.18.0 milestone Oct 29, 2026
@clue
clue requested a balanced review from Copilot October 29, 2026 11:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the addition of closure-valued container-variable support.

No open issues 🎉

What changed in this PR

Adds support for container-variable factories that return a Closure, preserving the returned instance across consumers while invoking the original factory only once. Class-name factory requirements remain unchanged.

Changes:

  • Wraps cached closure values so later accesses do not invoke them as factories.
  • Adds a regression test for shared closure identity and a single factory invocation.
  • Updates the expected error when getEnv() receives a closure instead of string|null.
File Description
tests/​ContainerTest.php Tests closure caching and updates the getEnv() type-error expectation.
src/​Container.php Allows closure-valued variables by wrapping cached closures.

Review effort: Balanced

@clue
clue merged commit e158e3b into clue:main Oct 29, 2026
75 checks passed
@clue
clue deleted the container-factory-closure branch October 29, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants