Refactor object() to create() and autowire() #449

Merged
merged 20 commits into from Apr 15, 2017

Conversation

Projects
1 participant
@mnapoli
Member

mnapoli commented Jan 1, 2017

Fixes #294, see #395 for more explanations.

The main goal is to simplify DI\object() by replacing it with DI\autowire() and DI\create().

autowire() and create() will not extend the previous definition (when using multiple configuration files). That will be much clearer.

Because of that, we need to have an autowire() helper that will explicitly autowire the service (and let us override just some parameters). It has the benefit of being much more explicit.

  • add DI\create() with the same signature as DI\object()
  • DI\create() must not extend the previous entry with the same name
    • add test for that
  • DI\create() must not go through autowiring
    • add test for that
  • add DI\autowire() with the same signature as DI\object() but calls autowiring
  • remove DI\object() entirely
  • update the documentation
  • changelog
  • upgrade guide

Will fix:


Extract of the migration guide:

The DI\object() function helper has been removed. You should use DI\create() or DI\autowire() instead.

What should you do with your DI\object() definitions:

  • if you disabled autowiring:
    • replace it with DI\create() and everything should work
  • if you use autowiring:
    • replace it with DI\create() for definitions that replace autowiring completely (i.e. those that redefine all the parameters)
    • replace it with DI\autowire() for definitions that just define some parameters and let autowiring guess the rest of the parameters

If you have a single configuration file, that's it.

If you have multiple configuration files, for example if you have built a module system, then there is one thing to be aware of: DI\object() used to extend previous definitions. create() and autowire() do not extend previous definitions, they completely override them, and this is intended. The behavior of object() was confusing and hard to understand, the new helpers are more predictable and simple.

@mnapoli mnapoli added the enhancement label Jan 1, 2017

@mnapoli mnapoli added this to the 6.0 milestone Jan 1, 2017

@mnapoli mnapoli self-assigned this Jan 1, 2017

mnapoli added some commits Jan 1, 2017

Test that create() overrides the previous entry
`object()` extended the previous entry, `create()` doesn't.
Test that create() overrides autowiring (and does not inherit it)
`object()` extended the autowired definition, `create()` doesn't.
`DI\create()` does not have `constructorParameter()` and `methodParam…
…eter()` methods...

... because they extend nothing. Only `constructor()` and `method()` can be used.
Merge branch '6.0' into refactor-object-to-create
# Conflicts:
#	tests/UnitTest/Definition/Helper/AutowireDefinitionHelperTest.php

@mnapoli mnapoli changed the title from WIP: Refactor DI\object() to DI\create() to Refactor DI\object() to DI\create() Apr 15, 2017

@mnapoli mnapoli changed the title from Refactor DI\object() to DI\create() to Refactor DI\object() to DI\create() and DI\autowire() Apr 15, 2017

@mnapoli mnapoli changed the title from Refactor DI\object() to DI\create() and DI\autowire() to Refactor object() to create() and autowire() Apr 15, 2017

@mnapoli mnapoli merged commit ff03746 into 6.0 Apr 15, 2017

2 of 6 checks passed

Scrutinizer Created
Details
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
continuous-integration/travis-ci/push The Travis CI build is in progress
Details
coverage/coveralls Coverage pending from Coveralls.io
Details
continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/styleci/push The StyleCI analysis has passed
Details

@mnapoli mnapoli deleted the refactor-object-to-create branch Apr 15, 2017

@mnapoli mnapoli moved this from In Progress to Done in PHP-DI v6 Apr 15, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment