Skip to content

Environment-based Dependency Injection #2081

@adragus-inviqa

Description

@adragus-inviqa

Since we now have the DI system creating the objects and the system being tight to the environment, do you think it's worth having an environment-aware DI system?

Is there something in the current codebase that handles such "conditionals"?

It would facilitate custom object creation along the pipeline. Some examples:

  • different log object for develoment (DEBUG, text), CI (info, text) and production (ERROR, 3rd party)
  • different email implementations for development (to file) and production (to email server)
  • payment mocks on development

The DI system could rely only on $_ENV for this and it will still be pretty awesome. Otherwise, some other sub-system would take care of this and it wouldn't be right, because it's the DI who should decide what object goes where and when. Something similar to the layout's ifconfig.

I'm not sure about the implementation though, as, for example, preference is a 1-to-1 relationship. Keeping BC, maybe something like this is possible to implement (assuming $_ENV['ENV_TYPE'] is set to dev, ci or prod):

<preferenceIf for="\My\LoggerInterface" switch="ENV_TYPE" default="dev">
    <dev>\My\FullLogger</dev>  <!-- or <case value="dev">\My\FullLogger</case> -->
    <ci>ciLoggerVirtualType</ci>
    <prod>\Some\ThirdPartyLogger</prod>
</preferenceIf>

For now I'm thinking that this could be done for preference only, not type or virtualType.

I think it would be a shame to let such a flexible DI become a... static thing.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions