Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@quimcalpe
Copy link
Contributor

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 4 commits September 21, 2015 23:05
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.
@quimcalpe quimcalpe force-pushed the factory_closure_injection branch from 7665610 to 8867688 Compare November 16, 2015 12:42
@quimcalpe quimcalpe force-pushed the factory_closure_injection branch from 8867688 to c7020df Compare November 16, 2015 13:01
@mnapoli
Copy link
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
mnapoli added a commit that referenced this pull request Nov 29, 2015
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants