- Issue created by @znerol
- Merge request !12640Issue #3534354: Add aliases for keyvalue and keyvalue.expirable to the container β (Open) created by znerol
- πΊπΈUnited States nicxvan
I don't see any reason not to do this, but two things I've seen.
You can still autowire using the attribute and service id.
Usually I see these conversions done in the issue that needs the autowiring.
If this is so contrib can do it a link to that issue would be helpful.
- π¨πSwitzerland znerol
Came here from π [PP-1] Add a way to capture mails sent through the mailer.transport service during tests Postponed . Autowiring is not a requirement over there, but I noticed that referencing
KeyValueFactoryInterface::class
did not work. I.e.:public function alter(ContainerBuilder $container): void { [...] $definition->addMethodCall('setKeyValueFactory', [new Reference(KeyValueFactoryInterface::class)]); [...] }
Regarding the process, we had a couple of these in the past (e.g., π Wrong alias for access_policy_processor service in core.services.yml Active and π Add Psr\EventDispatcher\EventDispatcherInterface alias to core services Needs review ).
- πΊπΈUnited States nicxvan
Oh are you in a service provider?
I was talking about this
public function __construct(
#[Autowire(service: 'monolog.logger.request')]
private LoggerInterface $logger,
) {https://symfony.com/doc/current/service_container/autowiring.html
- π¨πSwitzerland znerol
Retitled and made it into a task to match the previous issue.
- π¦πΊAustralia larowlan π¦πΊπ.au GMT+10
When autowiring went in originally, we added
core/tests/Drupal/KernelTests/Core/DependencyInjection/AutowireTest.php
that is supposed to error if a services is added without a FQCN alias. Can we do some digging to work out why these services weren't causing that test to fail, so we can be sure that test is doing what we expect? - πΊπΈUnited States nicxvan
Is it just new services? I swear I have had to add them too.
- π¨πSwitzerland znerol
Can we do some digging to work out why these services weren't causing that test to fail, so we can be sure that test is doing what we expect?
core.services.yml
contains the followingkeyvalue
related definitions:keyvalue: class: Drupal\Core\KeyValueStore\KeyValueFactory arguments: ['@service_container', '%factory.keyvalue%'] keyvalue.database: class: Drupal\Core\KeyValueStore\KeyValueDatabaseFactory arguments: ['@serialization.phpserialize', '@database'] keyvalue.expirable: class: Drupal\Core\KeyValueStore\KeyValueExpirableFactory arguments: ['@service_container', '%factory.keyvalue.expirable%'] keyvalue.expirable.database: class: Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory arguments: ['@serialization.phpserialize', '@database', '@datetime.time']
The services
keyvalue
andkeyvalue.database
both implementKeyValueFactoryInterface
. Likewise the serviceskeyvalue.expirable
andkeyvalue.expirable.database
both implementKeyValueExpirableFactoryInterface
. TheAutowireTest
only requires an alias for classes that are the only implementations of an interface. - π¦πΊAustralia larowlan π¦πΊπ.au GMT+10
Thanks for explaining!
- π¬π§United Kingdom longwave UK
I deliberately did not add this at the time we added the rest of the aliases because of the possible confusion between
keyvalue
andkeyvalue.database
as they share the interface, but you will (almost) always wantkeyvalue
to proxy to the selected implementation so adding the alias to that makes sense so it can be injected more easily. And exactly the same applies for the expirable services too.