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

Autowire Drupal container params and plain values #6061

Open
wants to merge 2 commits into
base: 13.x
Choose a base branch
from

Conversation

claudiu-cristea
Copy link
Member

@claudiu-cristea claudiu-cristea commented Jul 14, 2024

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:

public function __construct(
    #[Autowire(param: 'container.modules')]
    protected array $installedModules,
    #[Autowire(value: 'a plain string')]
    protected string $string,
)
{
    parent:: __construct();
}

@claudiu-cristea claudiu-cristea force-pushed the autowire-params branch 2 times, most recently from 16b2076 to 89cd330 Compare July 14, 2024 12:12
@claudiu-cristea claudiu-cristea changed the title Autowire Drupal container params or plain values Autowire Drupal container params and plain values Jul 14, 2024
Copy link
Member

@weitzman weitzman left a 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

Copy link
Member

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.

Copy link
Member

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.

@claudiu-cristea
Copy link
Member Author

Last commit deserves better test coverage

@weitzman
Copy link
Member

weitzman commented Nov 2, 2024

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.

@claudiu-cristea claudiu-cristea force-pushed the autowire-params branch 2 times, most recently from 811901b to 0529870 Compare November 3, 2024 18:59
@claudiu-cristea
Copy link
Member Author

I think I have something but no idea how add a new League container service in tests, see AutowireArgumentsTest::testWithDrushContainer()

Copy link
Member

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
Copy link
Member

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"?

Comment on lines +403 to +409
// 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;
}
}
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants