Comments (22)
Not sure about this example, since EntityManager::getRepository()
actually requires an argument, the fully qualified class name of the entity, right?
from container.
Okay, bad example, but still a relevant issue. Okay, I changed it for something not Doctrine oriented.
from container.
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.
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.
They don't seem to work when using Auto Wiring. Any thought on that?
from container.
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.
Never mind, I need some more investigation. Will report back!
from container.
I'm working on it, I am definitely not getting the expected result, I am looking for the cause.
from container.
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.
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.
$container->inflector(\MyNamespace\ClassInterface::class);
That line isn't actually doing anything.
from container.
I must be drunk, give me a minute.
from container.
Okay, I updated it but still the same issue.
from container.
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.
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.
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.
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.
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.
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.
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.
Got it, thanks for being helpful.
from container.
👍
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.