1010
1111namespace Bazinga \GeocoderBundle \DependencyInjection ;
1212
13- use Bazinga \GeocoderBundle \EventListener \FakeRequestListener ;
13+ use Bazinga \GeocoderBundle \DataCollector \GeocoderDataCollector ;
14+ use Bazinga \GeocoderBundle \Plugin \FakeIpPlugin ;
15+ use Bazinga \GeocoderBundle \Plugin \ProfilingPlugin ;
16+ use Bazinga \GeocoderBundle \ProviderFactory \PluginProviderFactory ;
1417use Bazinga \GeocoderBundle \ProviderFactory \ProviderFactoryInterface ;
18+ use Geocoder \Plugin \Plugin \CachePlugin ;
19+ use Geocoder \Plugin \Plugin \LimitPlugin ;
20+ use Geocoder \Plugin \Plugin \LocalePlugin ;
21+ use Geocoder \Plugin \Plugin \LoggerPlugin ;
22+ use Geocoder \Plugin \PluginProvider ;
1523use Geocoder \Provider \Cache \ProviderCache ;
16- use Geocoder \Provider \Provider ;
1724use Symfony \Component \Config \Definition \Processor ;
1825use Symfony \Component \Config \FileLocator ;
1926use Symfony \Component \DependencyInjection \ContainerBuilder ;
@@ -38,41 +45,111 @@ public function load(array $configs, ContainerBuilder $container)
3845 if (true === $ config ['profiling ' ]['enabled ' ]) {
3946 $ loader ->load ('profiling.yml ' );
4047 }
41- $ this ->loadProviders ($ container , $ config );
4248
4349 if ($ config ['fake_ip ' ]['enabled ' ]) {
44- $ definition = $ container ->getDefinition (FakeRequestListener ::class);
45- $ definition ->replaceArgument (0 , $ config ['fake_ip ' ]['ip ' ]);
50+ $ definition = $ container ->getDefinition (FakeIpPlugin ::class);
51+ $ definition ->replaceArgument (1 , $ config ['fake_ip ' ]['ip ' ]);
4652 } else {
47- $ container ->removeDefinition (FakeRequestListener ::class);
53+ $ container ->removeDefinition (FakeIpPlugin ::class);
4854 }
55+
56+ $ this ->loadProviders ($ container , $ config );
4957 }
5058
5159 private function loadProviders (ContainerBuilder $ container , array $ config )
5260 {
5361 foreach ($ config ['providers ' ] as $ providerName => $ providerConfig ) {
5462 $ factoryService = $ container ->getDefinition ($ providerConfig ['factory ' ]);
5563 $ factoryClass = $ factoryService ->getClass () ?: $ providerConfig ['factory ' ];
56- if (!( is_a ( $ factoryClass , ProviderFactoryInterface::class) )) {
57- // throw new \LogicException(sprintf('Provider factory "%s" must implement ProviderFactoryInterface', $providerConfig['factory']));
64+ if (!class_implements ( $ factoryClass , ProviderFactoryInterface::class)) {
65+ throw new \LogicException (sprintf ('Provider factory "%s" must implement ProviderFactoryInterface ' , $ providerConfig ['factory ' ]));
5866 }
5967 $ factoryClass ::validate ($ providerConfig ['options ' ], $ providerName );
6068
6169 // See if any option has a service reference
6270 $ providerConfig ['options ' ] = $ this ->findReferences ($ providerConfig ['options ' ]);
6371
6472 $ serviceId = 'bazinga_geocoder.provider. ' .$ providerName ;
65- $ def = $ container ->register ($ serviceId , Provider::class);
66- $ def ->setFactory ([new Reference ($ providerConfig ['factory ' ]), 'createProvider ' ])
73+ $ plugins = $ this ->configureProviderPlugins ($ container , $ providerConfig , $ serviceId );
74+
75+ $ def = $ container ->register ($ serviceId , PluginProvider::class)
76+ ->setFactory ([PluginProviderFactory::class, 'createPluginProvider ' ])
77+ ->addArgument ($ plugins )
78+ ->addArgument (new Reference ($ providerConfig ['factory ' ]))
6779 ->addArgument ($ providerConfig ['options ' ]);
6880
6981 $ def ->addTag ('bazinga_geocoder.provider ' );
7082 foreach ($ providerConfig ['aliases ' ] as $ alias ) {
7183 $ container ->setAlias ($ alias , $ serviceId );
7284 }
85+ }
86+ }
87+
88+ /**
89+ * Configure plugins for a client.
90+ *
91+ * @param ContainerBuilder $container
92+ * @param array $config
93+ * @param string $providerServiceId
94+ *
95+ * @return array
96+ */
97+ public function configureProviderPlugins (ContainerBuilder $ container , array $ config , string $ providerServiceId ): array
98+ {
99+ $ plugins = [];
100+ foreach ($ config ['plugins ' ] as $ plugin ) {
101+ $ plugins [] = $ plugin ['id ' ];
102+ }
103+
104+ if (isset ($ config ['cache ' ]) || isset ($ config ['cache_lifetime ' ])) {
105+ if (null === $ cacheServiceId = $ config ['cache ' ]) {
106+ if (!$ container ->has ('app.cache ' )) {
107+ throw new \LogicException ('You need to specify a service for cache. ' );
108+ }
109+ $ cacheServiceId = 'app.cache ' ;
110+ }
111+ $ plugins [] = $ providerServiceId .'.cache ' ;
112+ $ container ->register ($ providerServiceId .'.cache ' , CachePlugin::class)
113+ ->setPublic (false )
114+ ->setArguments ([new Reference ($ cacheServiceId ), (int ) $ config ['cache_lifetime ' ]]);
115+ }
73116
74- $ this ->configureCache ($ container , $ serviceId , $ providerConfig );
117+ if (isset ($ config ['limit ' ])) {
118+ $ plugins [] = $ providerServiceId .'.limit ' ;
119+ $ container ->register ($ providerServiceId .'.limit ' , LimitPlugin::class)
120+ ->setPublic (false )
121+ ->setArguments ([(int ) $ config ['limit ' ]]);
75122 }
123+
124+ if (isset ($ config ['locale ' ])) {
125+ $ plugins [] = $ providerServiceId .'.locale ' ;
126+ $ container ->register ($ providerServiceId .'.locale ' , LocalePlugin::class)
127+ ->setPublic (false )
128+ ->setArguments ([$ config ['locale ' ]]);
129+ }
130+
131+ if (isset ($ config ['logger ' ])) {
132+ $ plugins [] = $ providerServiceId .'.logger ' ;
133+ $ container ->register ($ providerServiceId .'.logger ' , LoggerPlugin::class)
134+ ->setPublic (false )
135+ ->setArguments ([new Reference ($ config ['logger ' ])]);
136+ }
137+
138+ if ($ container ->has (FakeIpPlugin::class)) {
139+ $ plugins [] = FakeIpPlugin::class;
140+ }
141+
142+ if ($ container ->has (GeocoderDataCollector::class)) {
143+ $ plugins [] = $ providerServiceId .'.profiler ' ;
144+ $ container ->register ($ providerServiceId .'.profiler ' , ProfilingPlugin::class)
145+ ->setPublic (false )
146+ ->setArguments ([substr ($ providerServiceId , strlen ('bazinga_geocoder.provider. ' ))])
147+ ->addTag ('bazinga_geocoder.profiling_plugin ' );
148+ }
149+
150+ return array_map (function (string $ id ) {
151+ return new Reference ($ id );
152+ }, $ plugins );
76153 }
77154
78155 /**
0 commit comments