Skip to content

Issue with autoconfigure bindings and PHP compatibility #46420

Closed
@fritzmg

Description

@fritzmg

Symfony version(s) affected

5.4, 6.x

Description

If you are not using symfony/flex, but you want to dump the .env.local.php you will need to register the command manually like so:

services:
    Symfony\Component\Dotenv\Command\DotenvDumpCommand:
        autoconfigure: true

However, due to the usage of the Autoconfigure attribute this will only work in PHP 8, otherwise the bindings will be missing.

But, if you instead register the service like this:

services:
    Symfony\Component\Dotenv\Command\DotenvDumpCommand:
        autoconfigure: true
        arguments:
            - '%kernel.project_dir%'
            - '%kernel.environment%'

so that it works in PHP 7 - it won't work in PHP 8 as the following error will occur:

In ResolveBindingsPass.php line 84:
                                                                                                                                                      
  [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]                                                                          
  A binding is configured for an argument named "$projectDir" under "_instanceof" in file "vendor\symfony\dotenv\Command\DotenvDumpCommand.php", but no corresponding argument has been found. It may be unused and should be removed, or it may have a typo.

So essentially there is no way to properly register a service that happens to use the

#[Autoconfigure(bind: …)]

attribute so that it still works both with PHP 7.4 and 8.x.

Shouldn't the bindings be ignored instead, if the arguments were specifically set in the service configuration?

How to reproduce

In a non-flex Symfony project, register the dotenv:dump command like so:

services:
    Symfony\Component\Dotenv\Command\DotenvDumpCommand:
        autoconfigure: true
        arguments:
            - '%kernel.project_dir%'
            - '%kernel.environment%'

Then execute cache:warmup with a PHP 8 CLI. This will reproduce the aforementioned error.

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions