Skip to content

Conversation

@clue
Copy link
Owner

@clue clue commented Jul 29, 2022

This changeset adds support for optional values in container configuration and support for nullable arguments and default values in factory functions:

<?php

require __DIR__ . '/../vendor/autoload.php';

$container = new FrameworkX\Container([
    Acme\Todo\UserController::class => function (bool $debug = false) {
        // example UserController class uses $debug, apply default if not set
        return new Acme\Todo\UserController($debug);
    },
    'debug' => true
]);

// …
<?php

require __DIR__ . '/../vendor/autoload.php';

$container = new FrameworkX\Container([
    Acme\Todo\UserController::class => function (?string $name) {
        // example UserController class uses $name, defaults to null if not set
        return new Acme\Todo\UserController($name ?? 'ACME');
    },
    'name' => 'Demo'
]);

// …

The previous version would always omit parameters with default values and nullable values without checking if an explicit container variable is set. We now first check to see if an explicit container variable is set and use this value if available. If this value is not available, we continue using the default value or a null value. This is the next step in adding better configuration support and support for (optional) environment variables and .env (dotenv) files as discussed in #101.

Builds on top of #180, #179, #178, #163, #97, #95 and others

@clue clue added the new feature New feature or request label Jul 29, 2022
@clue clue force-pushed the container-nullable branch from c821421 to 9fe083d Compare July 29, 2022 19:54
@clue clue merged commit 3cb7373 into clue:main Jul 29, 2022
@clue clue deleted the container-nullable branch July 29, 2022 22:10
@clue clue changed the title Support explicit container configuration for factory functions with nullable arguments and defaults values Support nullable arguments and default values for DI container configuration Aug 2, 2022
@SimonFrings SimonFrings added this to the v0.12.0 milestone Mar 5, 2024
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