Comments (13)
You're actually trying to alias an interface to an implementation. I do the following:
$container->add('App\Authentication\UserMapperInterface', function () {
$container->get('App\Users\UserMapper');
});
from container.
So I register both the UserMapper
and UserMapperInterface
in the same service provider.
from container.
The issue here isn't with the definition of UserMapperInterface
but with the dependencies of the concrete UserMapper
it has a constructor dependency of Spot\Locator
so you need to make sure that is injected, something like the following.
$container->add('App\Authentication\UserMapperInterface', 'App\Users\UserMapper')
->withArgument('Spot\Locator');
from container.
Then I usually do this:
$this->container->add('App\Authentication\UserMapper')
->withArgument('Spot\Locator');
$this->container->add('App\Authentication\UserMapperInterface', function () {
return $this->container->get('App\Users\UserMapper');
});
edit used $this->container
instead of $container
edit from philipobenito good point on the scoping, also added a return for the benefit of the OP.
from container.
@hannesvdvreken that's probably the best solution yes, you'll need a use ($container)
in there but that's how I'd do it then you're keeping your concrete seperate from the abstraction. 👍
@nebulousGirl the example in @hannesvdvreken comment above will work perfectly, if Spot\Locator
also has dependencies you will also need to define them in a similar way.
from container.
What I find confusing is that $container->get('App\Users\UserMapper')
does reflection and works without me having to add it to the container, but the following doesn't work?
$container->add('App\Authentication\UserMapperInterface', 'App\Users\UserMapper');
I was expecting the same reflection behavior in both scenarios.
@philipobenito I can settle with your code example, just thought it could improve usability from a developer's point of view.
from container.
@nebulousGirl it was a conscious decision as reflection is slow so the idea is that if you define something, you can be aware that it's not going to reflect, that way the user has a clear choice and there's no real ambiguity about what's happening in the background.
from container.
Also, you can use the container for something like this:
$container->add('key', 'value');
$container->get ('key');
So the container can't know when you're trying to alias something or not. I suggest an alias method that adds a new closure to the container. As I described here before.
from container.
@philipobenito Ok, I understand your point. Is it possible to turn off reflection completely, at least from the get
method?
I would prefer to have the same behavior all the time, it would be less confusing.
from container.
It's something I'm playing about with on v2 (due in the next week or so), possibly having 2 separate methods, get
and reflect
to make it clear what is happening.
from container.
That would be great. Keep up the good work, I'm eagerly awaiting v2 of Container and Route.
from container.
I got caught out by this limitation on the weekend. I'm interested to hear how v2 is going.
I'd also suggest adding something to the documentation if it does change. An example on binding a concrete to an interface in the Auto Dependency Resolution section would be useful!
from container.
I don't think it would be prudent to backport any of this to v1 but v2 has been built around the idea of being much more explicit about when and where reflection will happen, this is mainly to allow for compilation of an immutable container with no abiguity of what is being resolved. I'll close this now as my development copy will be pushed to master hopefully over the weekend with a period of time set aside for discussion and improvements before tagging the release.
from container.
Related Issues (20)
- References are unallowed HOT 1
- Can't override a definition.
- Setting concrete does not reset arguments. HOT 4
- [2.x] Add support for PHP 8 HOT 2
- Extends causing an infinite loop HOT 1
- Inconsistent Behavior HOT 12
- Question: Possible to use tags in Service Providers? HOT 3
- Fatal error: incompatible declaration of get method against PSR interface, in some PHP versions HOT 5
- Returning from an inflector HOT 7
- Support for non-object concrete values HOT 3
- Question: How to "inflect once" for shared services? HOT 13
- Preceeding slash confusing fully qualified class names HOT 1
- Extends causing an infinite loop HOT 2
- Question: Can Container Change an Alias? HOT 1
- Huge performance hits when having a bigger application with a lot of service definitions HOT 6
- How swap added definition HOT 1
- Auto wiring a shared dependency HOT 1
- Infinate loop in resolving non-existant class HOT 1
- Question: How to achieve autowiring inside the service provider? HOT 1
- Is this package still maintained? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from container.