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
Adding an instance of a class which has an __invoke method #113
Comments
|
@willemwollebrants yes this is a bug/oversight, the decision was made to handle |
|
I believe it should be marked as bug, because the behaviour right now is inconsistent when using autowiring. Trying to make object from class that has __invoke method from container with autowiring treats the constructed object as object. On the other hand if concrete definition is provided, then container treats it as callable, meaning result of get() method on such classes is nondeterministic. |
|
@hannesvdvreken @localheinz @khelle I'm developing My only clean solution is ignoring classes implementing |
|
Handled in 421d774 for release in Provided |
|
It's an exceptional case, and there is a simple way of resolving an invokable object. |
|
I think the solution I've implemented works for this, won't be changing default behaviour though. |
|
👍 |
|
FWIW, I was having this same issue in <?php
namespace Foo\Markdown;
use League\CommonMark\CommonMarkConverter;
use League\Container\ServiceProvider\AbstractServiceProvider;
class CommonMarkProvider extends AbstractServiceProvider
{
protected $provides = [
CommonMarkConverter::class,
];
public function register(): void
{
$converter = new CommonMarkConverter;
$this->getContainer()->share(CommonMarkConverter::class, $converter);
}
}Fails with error: My workaround was to make a wrapper for <?php
namespace Foo\Markdown;
use League\CommonMark\CommonMarkConverter;
class CommonMarkWrapper
{
private $converter;
public function __construct()
{
$this->converter = new CommonMarkConverter;
}
public function toHtml(string $markdown): string
{
return $this->converter->convertToHtml($markdown);
}
}I hope that helps others who run into this issue. :) |
|
I had exactly (!) the same issue a while ago. |
I'm not sure if this is expected behavior or not, but here goes:
When you add an instance of a class which has an __invoke method, the DefinitionFactory sees this as a callable, so it gets added as a CallableDefinition. As a consequence of this, when you try to get the $concrete it does not return the instance, but whatever the __invoke method returns.
Here's an example to make it a bit clearer
I think this might be a bug, because changing the innards of a class should probably not change what the container returns. If my thinking is correct, the fix would probably be handling objects which are not instances of Callable like any other undefineable item and just add them as an arbitrary value/instance (= like here.
I'm more than willing to PR this, but I'd rather make sure this is indeed a bug and not by design, because I can imagine being able to use __invoke has its uses too.
The text was updated successfully, but these errors were encountered: