Skip to content
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

Suggest packages on missing autowiring aliases #30521

Open
nicolas-grekas opened this Issue Mar 11, 2019 · 5 comments

Comments

Projects
None yet
2 participants
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Mar 11, 2019

When I use a type-hint for an interface that is not installed, autowiring fails with a generic message, e.g.: Cannot autowire argument $client of "App\Controller\HelloController::index()": argument "$responseFactory" of method "Symfony\Component\HttpClient\Psr18Client::__construct()" has type "Psr\Http\Message\ResponseFactoryInterface" but this class was not found.

But in this example, we could suggest a solution: compose require nyholm/psr7 will install recipes for the interface. Maybe we could display this as a suggestion when we detect that flex is installed? We would do this for packages with official recipes only to reduce the scope.

Another example is Cannot determine controller argument for "App\Controller\HelloController::index()": the $client argument is type-hinted with the non-existent class or interface: "Psr\Http\Client\ClientInterface". when psr/http-client is missing.

@moynzzz

This comment has been minimized.

Copy link
Contributor

moynzzz commented Mar 13, 2019

I would like to work on this issue @nicolas-grekas how do you suggest implementing the desired functionality in terms of which packages interfaces should be suggested. Should we extract all possible interfaces from the packages that exist in the Symfony recipes repository or some more robust way of doing that?

@nicolas-grekas

This comment has been minimized.

Copy link
Member Author

nicolas-grekas commented Mar 18, 2019

From what I read, these two messages are found:

  1. in AutowirePass::createTypeNotFoundMessage()
  2. in RegisterControllerArgumentLocatorsPass::process()

I think we could add a static class in the DI component that would hard-code a few packages - let's start with only the ones mentionned in the description and see later if/how to grow the list?

@moynzzz

This comment has been minimized.

Copy link
Contributor

moynzzz commented Mar 18, 2019

Okay I'm working on a PR for this feature.

@moynzzz

This comment has been minimized.

Copy link
Contributor

moynzzz commented Mar 21, 2019

I have a qustion about how to check if flex is installed in a symfony application. I tried to check if flex class exists (Symfony\Flex\Flex) but it depends on Composer\Plugin\PluginInterface which is not autoloaded (because composer autoloader doesn't add it to the list of loaded classes which is expected we don't need it in our applications) and so the check will fail. A solution will be to check for a class that exists in the flex source like Symfony\Flex\Lock but I don't think this is the best one.

Does anybody have an idea how this can be solved?

@nicolas-grekas

This comment has been minimized.

Copy link
Member Author

nicolas-grekas commented Mar 21, 2019

A solution will be to check for a class that exists in the flex source like Symfony\Flex\Lock

I think that's the best solution actually :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.