10
10
11
11
namespace Bazinga \GeocoderBundle \DependencyInjection ;
12
12
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 ;
14
17
use 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 ;
15
23
use Geocoder \Provider \Cache \ProviderCache ;
16
- use Geocoder \Provider \Provider ;
17
24
use Symfony \Component \Config \Definition \Processor ;
18
25
use Symfony \Component \Config \FileLocator ;
19
26
use Symfony \Component \DependencyInjection \ContainerBuilder ;
@@ -38,41 +45,111 @@ public function load(array $configs, ContainerBuilder $container)
38
45
if (true === $ config ['profiling ' ]['enabled ' ]) {
39
46
$ loader ->load ('profiling.yml ' );
40
47
}
41
- $ this ->loadProviders ($ container , $ config );
42
48
43
49
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 ' ]);
46
52
} else {
47
- $ container ->removeDefinition (FakeRequestListener ::class);
53
+ $ container ->removeDefinition (FakeIpPlugin ::class);
48
54
}
55
+
56
+ $ this ->loadProviders ($ container , $ config );
49
57
}
50
58
51
59
private function loadProviders (ContainerBuilder $ container , array $ config )
52
60
{
53
61
foreach ($ config ['providers ' ] as $ providerName => $ providerConfig ) {
54
62
$ factoryService = $ container ->getDefinition ($ providerConfig ['factory ' ]);
55
63
$ 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 ' ]));
58
66
}
59
67
$ factoryClass ::validate ($ providerConfig ['options ' ], $ providerName );
60
68
61
69
// See if any option has a service reference
62
70
$ providerConfig ['options ' ] = $ this ->findReferences ($ providerConfig ['options ' ]);
63
71
64
72
$ 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 ' ]))
67
79
->addArgument ($ providerConfig ['options ' ]);
68
80
69
81
$ def ->addTag ('bazinga_geocoder.provider ' );
70
82
foreach ($ providerConfig ['aliases ' ] as $ alias ) {
71
83
$ container ->setAlias ($ alias , $ serviceId );
72
84
}
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
+ }
73
116
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 ' ]]);
75
122
}
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 );
76
153
}
77
154
78
155
/**
0 commit comments