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

Support #[AutowireLocator] #411

Open
mvhirsch opened this issue Oct 7, 2024 · 4 comments
Open

Support #[AutowireLocator] #411

mvhirsch opened this issue Oct 7, 2024 · 4 comments

Comments

@mvhirsch
Copy link

mvhirsch commented Oct 7, 2024

Symfony 6.4 introduced an attribute called #[AutowireLocator] which automatically creates and injects a ServiceLocator into a service. Looks like a similar feature was already implemented with #151.

Is it possible to add support for this feature, too?

final class TranscriptAdmin
{
   // ...

    #[Required]
    public function setContainer(
        #[AutowireLocator([ProfileRepository::class])] ContainerInterface $container
    ): void {
        $this->locator = $container;
    }
}
 ------ ---------------------------------------------------------------------- 
  Line   src/TranscriptBundle/Admin/TranscriptAdmin.php                        
 ------ ---------------------------------------------------------------------- 
  56     Service "App\ProfileBundle\Repository\ProfileRepository" is private.  
         ✏️  src/TranscriptBundle/Admin/TranscriptAdmin.php                    
 ------ ---------------------------------------------------------------------- 
@RafaelKr
Copy link

The current workaround is to use // @phpstan-ignore symfonyContainer.privateService above the line where we use $this->locator->get. Of course this is not a nice solution.

Looking at https://github.com/phpstan/phpstan-symfony/blob/1ef4dce2baabd464c2dd3109d051bad94efa1e79/src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php and #151 it shouldn't be very hard to implement a fix, unfortunately I currently don't have time for that.

@RafaelKr
Copy link

I just implemented support for the basic syntax as described in the OP (#[AutowireLocator([ProfileRepository::class])]). See #420

@RafaelKr
Copy link

RafaelKr commented Jan 3, 2025

I actually took the time and implemented AutowireLocator attribute support in #420
Currently there are some failing checks I can't resolve, but besides from that everything should be ready!

@byWulf
Copy link

byWulf commented Jan 16, 2025

I have the same problem, but instead of defining specific services, I use a tag instead, so the container contains all services with this tag. This is also allowed by Symfony and the services don't have to be public to access them this way through this limited container. Could you consider this kind of usage too when fixing the issue? Thanks a lot!

#[AutoconfigureTag('app.some_tag')]
interface SomeInterface { }

class SomeServiceA implements SomeInterface { }
class SomeServiceB implements SomeInterface { public function doSomething(): void { } }
class SomeServiceC implements SomeInterface { }

class MainService {
    public function __construct(
        #[AutowireLocator('app.some_tag')]
        private ContainerInterface $myServices,
    ) { }

    public function someMethod(): void {
        $this->myServices->get(SomeServiceB::class)->doSomething();
    }
}

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

No branches or pull requests

3 participants