Allow injection of container and current definition instance as factory parameters #345

Merged
merged 8 commits into from Nov 29, 2015

Conversation

Projects
None yet
3 participants
@quimcalpe
Contributor

quimcalpe commented Nov 13, 2015

This PR supersedes #333 and #269. Actually, It's done on top of #333 to preserve commits.

You can get now the container and current definition injected into factory closures type-hinted like this:

factory(function(ContainerInterface $c, Definition $d) {
    return new Thing($c->get($d->getName()));
});

You can mix also any dependency the container can resolve, parameter order is irrelevant with type-hinting:

factory(function(ContainerInterface $c, Service $s, Definition $d) {
    return new Thing($s, $d->getName(), $c->get("whatever"));
});

This is also backwards compatible, so if you don't type hint the first parameter, an instance of the ContainerInterface will be injected, if you don't type hint the second parameter, an instance of the current Definition will be also injected, although that's not recommended anyway.

factory(function($c, $d) {
    $c; // => container instance
    $d->getName(); // => requested name, without wildcards
    ....
});

Todo:

  • test case for FactoryParameterResolver
  • update change-log.md

jdreesen and others added some commits Sep 21, 2015

Allow injection of current definition instance as factory parameter
Current Definition can be injected via type-hinting: DI\Definition\Definition
or as a second parameter if no type-hinting is present.

Docs and tests updated accordingly.
@mnapoli

This comment has been minimized.

Show comment
Hide comment
@mnapoli

mnapoli Nov 29, 2015

Member

I finally got to dive into this! This is a very good addition thanks, I only had a few changes to make. Could you review #347 if you have time? The changes I did are detailed in the PR.

Member

mnapoli commented Nov 29, 2015

I finally got to dive into this! This is a very good addition thanks, I only had a few changes to make. Could you review #347 if you have time? The changes I did are detailed in the PR.

@mnapoli mnapoli merged commit c17949f into PHP-DI:master Nov 29, 2015

4 checks passed

Scrutinizer 2 new issues, 2 updated code elements
Details
StyleCI The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage decreased (-0.2%) to 84.79%
Details

mnapoli added a commit that referenced this pull request Nov 29, 2015

Merge pull request #347 from PHP-DI/345
Inject dependencies and requested entry name in factory parameters [retake of #345]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment