Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject dependencies and requested entry name in factory parameters [retake of #345] #347

merged 12 commits into from Nov 29, 2015


Copy link

@mnapoli mnapoli commented Nov 29, 2015

This PR contains #345 and #333.

The differences with #345:

  • optimizations: from my profilings we went from a 25% slowdown on factories to 0%, so 5.2 should not be slower than 5.1 (that was the main blocker for me)
  • factories can inject a new object (DI\Factory\RequestedEntry) to know the requested definition name: that replaces the injection of Definition instances. The reason for that is to avoid coupling user factories with definitions which are an implementation detail of PHP-DI. If definitions ever change, it will not break factories.

Example to get the requested entry name:

factory(function(ContainerInterface $c, RequestedEntry $e) {
    return new Thing($c->get($e->getName()));

Example to inject other services:

factory(function(Logger $logger, Mailer $mailer) {
    return new Thing($logger, $mailer);


  • update changelog
  • documentation

This is the last feature planned for 5.2, after that it's release time!

jdreesen and others added 10 commits Sep 21, 2015
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.
…into factories

That allows to keep definitions an internal implementation details. If definitions are refactored later on, factories will not be broken. Also factories have no reason to know about definitions, they just want the request entry name (ISP).
Copy link

Nice optimization passing container in constructor and resolving directly, then you can avoid TypeHintContainerResolver in resolver chain 👍
RequestedEntry is also cleaner... so 2 big thumbs up! 👍 👍

Copy link
Member Author

mnapoli commented Nov 29, 2015

Thanks! Time to merge then :)

mnapoli added a commit that referenced this pull request Nov 29, 2015
Inject dependencies and requested entry name in factory parameters [retake of #345]
@mnapoli mnapoli merged commit 773145e into master Nov 29, 2015
@mnapoli mnapoli deleted the 345 branch Nov 29, 2015
Copy link

Looks very nice 👍

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

Successfully merging this pull request may close these issues.

None yet

3 participants