Giter Club home page Giter Club logo

Comments (13)

hannesvdvreken avatar hannesvdvreken commented on September 2, 2024

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.

hannesvdvreken avatar hannesvdvreken commented on September 2, 2024

So I register both the UserMapper and UserMapperInterface in the same service provider.

from container.

philipobenito avatar philipobenito commented on September 2, 2024

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.

hannesvdvreken avatar hannesvdvreken commented on September 2, 2024

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.

philipobenito avatar philipobenito commented on September 2, 2024

@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.

nebulousGirl avatar nebulousGirl commented on September 2, 2024

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.

philipobenito avatar philipobenito commented on September 2, 2024

@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.

hannesvdvreken avatar hannesvdvreken commented on September 2, 2024

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.

nebulousGirl avatar nebulousGirl commented on September 2, 2024

@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.

philipobenito avatar philipobenito commented on September 2, 2024

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.

nebulousGirl avatar nebulousGirl commented on September 2, 2024

That would be great. Keep up the good work, I'm eagerly awaiting v2 of Container and Route.

from container.

sjparkinson avatar sjparkinson commented on September 2, 2024

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.

philipobenito avatar philipobenito commented on September 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.