-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Autowire Drupal container params and plain values #6061
base: 13.x
Are you sure you want to change the base?
Conversation
16b2076
to
89cd330
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is based on https://github.com/drupal/drupal/blob/11.x/core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php. Looks like this PR wants to expand its duties considerably. I'm not opposed to that, but I fear that this may get tricky to maintain as Symfony evolves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hoping that @greg-1-anderson can take a look at changes in this file.
Last commit deserves better test coverage |
League container doesn't even support container parameters and string so AutowireTrait how about we cheat and go right to the delegated container for those. If you have a compound container, you can do $container->getContainer() to get the Drupal container, and then ->getParameter() to get the param. |
811901b
to
0529870
Compare
0529870
to
02471ee
Compare
I think I have something but no idea how add a new League container service in tests, see AutowireArgumentsTest::testWithDrushContainer() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could not instantiate Custom\Library\Drush\Commands\AutowireTestCommands: Cannot autowire service "woot.autowire_test": argument "$argListContainerService" of method "Custom\Library\Drush\Commands\AutowireTestCommands::_construct()", you should configure its value explicitly. [1.06 sec, 8.13 MB]
I am seeing an error when I run drush st -vvv
. I'm not sure why this commandfile loaded when I just ddev start
, and am not running tests.
// argument. If the Drupal container has been initialized, it's already a delegate. | ||
return [$drushContainer]; | ||
} elseif ($drupalContainer && is_a($type, ContainerInterface::class, true)) { | ||
// The factory create() method expects a container of any type as st argument and the Drupal container has |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do we mean "has already been initialized"?
// Add Drush container as 2nd argument if the method expects one. | ||
if (isset($params[1])) { | ||
$type = ltrim((string) $params[1]->getType(), '?'); | ||
if (is_a($type, ContainerInterface::class, true)) { | ||
$args[] = $drushContainer; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont like commands and AutowireTrait having multiple numbers of params and type hints in create(). How about if we ask commands going forward to always type hint to Psr\Container\ContainerInterface
. That receives the compound container. AutowireTrait could pull the drupalContainer out and get container params as needed. We need to declare a service for the drupalContainer in the league container. We already store a reference to the league container inside itself. So this would be a similar service.
Autowiring works now for services but not for Drupal container parameters or even plain values.
It should be possible to autowire Drupal container params and plain values in his way: