Skip to content

[Lock] Renamed target for autowire #60349

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

Open
lyrixx opened this issue May 5, 2025 · 2 comments
Open

[Lock] Renamed target for autowire #60349

lyrixx opened this issue May 5, 2025 · 2 comments
Labels
Good first issue Ideal for your first contribution! (some Symfony experience may be required) Lock

Comments

@lyrixx
Copy link
Member

lyrixx commented May 5, 2025

Description

I have the following configuration

framework:
    lock:
        resources:
            shared:
                - 'redis://%redis_shared_host%:%redis_shared_port%'
            crawler:
                - 'redis://%redis_crawler_host%:%redis_crawler_port%'

And I just noticed the naming for the Target attribute:

I have no name! /app/server/backend bin/console debug:autowi lock

Autowirable Types
=================

 The following classes & interfaces can be used as type-hints when autowiring:
 (only showing classes/interfaces matching lock)
 
[...]

 Factory provides method to create locks.
 Symfony\Component\Lock\LockFactory $crawlerLockFactory - target:crawler.lock.factory - alias:lock.crawler.factory
 Symfony\Component\Lock\LockFactory $sharedLockFactory - target:shared.lock.factory - alias:lock.shared.factory

So I need to write the following code:

    public function __construct(
        #[Target('shared.lock.factory')]
        private readonly LockFactory $lockFactory,
    ) {

This is not optimal. I would prefer to write

    public function __construct(
        #[Target('shared')]
        private readonly LockFactory $lockFactory,
    ) {

I would be more in sync with the http client, and the workflow.

WDYT?

Example

No response

@carsonbot carsonbot added the Lock label May 5, 2025
@GromNaN
Copy link
Member

GromNaN commented May 9, 2025

You can have multiple names, by duplicating this line:

$container->registerAliasForArgument('lock.'.$resourceName.'.factory', LockFactory::class, $resourceName.'.lock.factory');

The alias also lets you skip the #[Target] attribute:

    public function __construct(
        private readonly LockFactory $shared,
    ) {

@GromNaN GromNaN added the Good first issue Ideal for your first contribution! (some Symfony experience may be required) label May 9, 2025
@stof
Copy link
Member

stof commented May 13, 2025

@GromNaN The Target attribute has a big benefit over relying on the argument name: it provides a clear intent of using a specific target, both to the DI component (allowing it to fail explicitly when this target does not exist instead of silently using the default autowiring target for that type) and to developers reading the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good first issue Ideal for your first contribution! (some Symfony experience may be required) Lock
Projects
None yet
Development

No branches or pull requests

4 participants