Git Product home page Git Product logo

Comments (22)

localheinz avatar localheinz commented on August 26, 2024

@gabrielbull

Not sure about this example, since EntityManager::getRepository() actually requires an argument, the fully qualified class name of the entity, right?

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

Okay, bad example, but still a relevant issue. Okay, I changed it for something not Doctrine oriented.

from container.

philipobenito avatar philipobenito commented on August 26, 2024

Already possible https://github.com/thephpleague/container/blob/master/src/Container.php#L211

If you pass a callable as the second argument, that takes precedence, the callable is passed the object in question and you can manipulate as you wish.

from container.

localheinz avatar localheinz commented on August 26, 2024

@gabrielbull

Hehe, no worries!

Just wondering, though, have you seen this:

public function inflector($type, callable $callback = null)
{
    return $this->inflectors->add($type, $callback);
}

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

@philipobenito @localheinz

They don't seem to work when using Auto Wiring. Any thought on that?

from container.

philipobenito avatar philipobenito commented on August 26, 2024

https://github.com/thephpleague/container/blob/master/src/Container.php#L105 it should be, can you provide a bigger context? A gist or something?

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

Never mind, I need some more investigation. Will report back!

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

I'm working on it, I am definitely not getting the expected result, I am looking for the cause.

from container.

philipobenito avatar philipobenito commented on August 26, 2024

Happy to take a look if you provide a gist of the full context.

Essentially we do the following if this helps.

if ($class instanceof $inflector) {
    // apply inflectors
}

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

https://gist.github.com/gabrielbull/e79d651d0655a152e184

I think the problem is that adding an inflector to the container is not enough, I need to add the class to the container as well.

Fatal error: Uncaught League\Container\Exception\NotFoundException: Alias (MyNamespace\ClassTest) is not being managed by the container

Now, it would be ideal to have the container be able to understand that these classes all use the same factory since this is what I specify. Do you guys agree?

from container.

philipobenito avatar philipobenito commented on August 26, 2024
$container->inflector(\MyNamespace\ClassInterface::class);

That line isn't actually doing anything.

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

I must be drunk, give me a minute.

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

Okay, I updated it but still the same issue.

from container.

philipobenito avatar philipobenito commented on August 26, 2024

You're aware autowiring is not enabled by default anymore?

http://container.thephpleague.com/auto-wiring/

The last code snippet at the bottom of that page shows how to enable it. Then the whole thing will work.

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

Thanks Phil, you're very helpful. I was able to pin-point the problem now. Here's a new Gist with that reproduces my original issue: https://gist.github.com/gabrielbull/d3674bb6b2cc81e09a97

Here's the error thrown by Container:

Fatal error: Uncaught League\Container\Exception\NotFoundException: Unable to resolve a value for parameter (param2) in the function/method (__construct) in

from container.

philipobenito avatar philipobenito commented on August 26, 2024

The problem there is that when using Auto Wiring the container has to know what is needed by the constructor. Unfortunately it can't resolve what $param2 is as there is no type hint or default value. I'd always recommend being explicit and defining your objects in your container whenever you can.

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

Still, shouldn't it try my factory first and then fail? I am very explicit in my own code but when trying to glue other libraries in my project I cannot control that.

from container.

philipobenito avatar philipobenito commented on August 26, 2024

That's not really what the inflectors are for, they inflect and change state on already instantiated objects through invoking methods and changing properties etc. We can't evaluate the type/interface without an instantiated object, and we can't instantiate without passing in required constructor arguments.

Auto wiring will allow that process to be automated but it can only be automated to a certain point, if it has no information on what to inject, it can't inject anything.

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

That's true, but if you agree that it would be good for the container auto-wiring to be able to auto-wire my example, it can be done with the ReflectionClass like this:

class Container implements ContainerInterface
{
    private $container = [];

    public function get($id)
    {
        foreach ($this->container as $alias => $concrete) {
            $class = new ReflectionClass($id);
            if (false === $class) {
                return null;
            }
            do {
                $name = $class->getName();
                if ($alias == $name) {
                    return $concrete($this);
                }
                $interfaces = $class->getInterfaceNames();
                if (is_array($interfaces) && in_array($alias, $interfaces)) {
                    return $concrete($this);
                }
                $class = $class->getParentClass();
            } while (false !== $class);
            return null;
        }
        return null;
    }
}

from container.

philipobenito avatar philipobenito commented on August 26, 2024

This takes us in to the realm of slower reflection though for what I consider to be an edge case as I'd rather push people towards being explicit with their definitions instead of relying on the container to handle it for them.

from container.

gabrielbull avatar gabrielbull commented on August 26, 2024

Got it, thanks for being helpful.

from container.

philipobenito avatar philipobenito commented on August 26, 2024

👍

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.