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

How to use Service injection in Symfony 6 properly #87

Open
TheAFOGroup opened this issue Jun 26, 2022 · 1 comment
Open

How to use Service injection in Symfony 6 properly #87

TheAFOGroup opened this issue Jun 26, 2022 · 1 comment

Comments

@TheAFOGroup
Copy link

Hi, thanks for the library!

I`m having quite some trouble understanding what the heck is going on. Basically, I want a service that I can use in Controllers and EventListeners.

After checking the issues here and comments, you mentioned to declare:

GeoIp2\Database\Reader:
        alias: 'geoip2.reader'
        public: true

However, this is not needed apparently. The moment you pass use GeoIp2\Database\Reader into the constructor, the following gets injected:

[ContainerLx2Tlzg\Reader_5400306 - GeoIp2\Database\Reader@proxy](http://.../_profiler/open?file=var\cache\dev\ContainerLx2Tlzg\Reader_5400306.php&line=7#line7) {#643 ▼
  -valueHolder16a63: null
  -initializer876c2: [Closure(&$wrappedInstance, LazyLoadingInterface $proxy)](http://.../_profiler/open?file=var\cache\dev\ContainerLx2Tlzg\**getGeoip2_Database_CityReaderService**.php&line=22#line22) {#647 ▼
    class: "[ContainerLx2Tlzg\getGeoip2_Database_CityReaderService](http://.../_profiler/open?file=var\cache\dev\ContainerLx2Tlzg\getGeoip2_Database_CityReaderService.php&line=11#line11)"
    use: {▼
      $container: ContainerLx2Tlzg\App_KernelDevDebugContainer {#55 …}
    }
  }

As above the City reader gets injected automatically, and I have no way of getting the ASN reader for example. I have also commented out default_database but without any effect.

So back to my question, how to use your library properly and as intended?

The requirement is quite simple:

  • Be able to use any reader in Controllers, EventListeners without injecting the ReaderFactory, just use the already there service.

Many thanks!

@peter-gribanov
Copy link
Member

If you have several reader services, then Symfony dependency injector will not be able to automatically determine which one to inject, because for Symfony injector, these services are the same and only you know which one to use where.
You will have to explicitly describe dependency injection in configs or use a ReaderFactory or create a proxy classes for each databases.

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

1 participant