Skip to content

Conversation

@clue
Copy link
Owner

@clue clue commented Dec 24, 2025

This changeset adds support for loading recursive environment variables from the Container. With these changes applied, factory functions may now explicitly reference their own variable name, which is especially useful for applying default values or casting or juggling string environment variables to their expected type like int or float:

$container = new Container([
    Fetcher::class => fn(string $URL, float $DELAY, Browser $browser) => new Fetcher($URL, $DELAY, $browser),
    'DELAY' => fn(float|string $DELAY = 10.0): float => filter_var($DELAY, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE) ?? throw new \ValueError('Invalid $DELAY given'),
]);

This includes a number of updated tests to verify correct behavior and avoid introducing any potential regressions, so this has 100% code coverage and should be safe to apply. On top of this, I've updated the documentation slightly to showcase how this could be used to apply a default for the X_LISTEN environment variable. This is the next step in adding better configuration support and support for environment variables and .env (dotenv) files as discussed in #101.

Builds on top of #287, #286, #284, #184, #38, and others

@clue clue added this to the v0.18.0 milestone Dec 24, 2025
@clue clue requested a review from Copilot December 24, 2025 13:44
@clue clue added the new feature New feature or request label Dec 24, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for recursive environment variable loading from the Container, enabling factory functions to explicitly reference their own variable name. This is particularly useful for type casting, validation, and applying defaults to environment variables. The implementation temporarily unsets the factory reference during parameter loading to allow recursive lookups to fall through to environment variables.

Key changes:

  • Modified loadVariable() to temporarily unset factory during parameter loading, enabling recursive self-references
  • Updated error message in test to reflect new behavior where recursive factory references attempt environment lookup
  • Added comprehensive test coverage for recursive factories with various scenarios (defaults, nullable types, error recovery)

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Container.php Core implementation: temporarily unsets factory during parameter loading to enable recursive environment variable references
tests/ContainerTest.php Updated error message for recursive reference test; added 6 new tests covering recursive factory scenarios
docs/best-practices/controllers.md Updated example to demonstrate recursive variable reference pattern

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@clue clue merged commit e0ca633 into clue:main Dec 24, 2025
74 checks passed
@clue clue deleted the container-recursive-variables branch December 24, 2025 13:53
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.

1 participant