Skip to content

Method initialize() should be part of DI\Container life-cycle & interface #232

@jakubboucek

Description

@jakubboucek

Some Nette DI extensions are using the container's initialize() method to append some logic which must be executed at end of container's setup phase.

But initialize() is just popular convention, not interface. That produces few problems:

initialize() method is not called automatically before the first use of the container, neither from DI\ContainerLoader nor DI\Container internally. The app is here responsible to call this method externally.

Container instance is generated at the run-time phase. A developer during developing has no way how to guarantee how the geterated container will look because app can only use DI\Container class interface which doesn't contains initialize() method (please, let Reflection sleep).

That's mean, PhpStorm is yelling inspections the method is undefined.

image

App never should use anything out of known interface.

That was formally problem, now the serious problem:

Look to two examples of app's config.neon files which is using ConstantsExtension:

parameters:
constants:
    HELLO: "world"
parameters:
constants:

The first one is create container with initialize() method. If the app does not call them, app will be incorrectly booted.
The second one is create container without them. If the app try call them, app will be crash info fatal error.

That's create very fragile stability of app design.

My suggestions

  • Add empty initialize() to DI\Container to create stable interface.
  • Add to container's life-cycle fixed point to invoke initialize(). Especially to documentation, because currently is mentioned as optional convention, but actually is it very important milestone.

I am happy happy to send PR, but first let me know my idea is valid or not.

Thanks for great tool ❤️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions