1
- <?php
1
+ <?php namespace Toin0u \ Geocoder ;
2
2
3
3
/**
4
4
* This file is part of the GeocoderLaravel library.
9
9
* file that was distributed with this source code.
10
10
*/
11
11
12
- namespace Toin0u \Geocoder ;
13
-
14
12
use Geocoder \ProviderAggregator ;
15
13
use Geocoder \Provider \Chain ;
16
14
use Illuminate \Support \Collection ;
17
15
use Illuminate \Support \ServiceProvider ;
18
16
use ReflectionClass ;
19
17
20
- /**
21
- * Geocoder service provider
22
- *
23
- * @author Antoine Corcy <contact@sbin.dk>
24
- */
25
18
class GeocoderServiceProvider extends ServiceProvider
26
19
{
27
20
/**
@@ -32,9 +25,7 @@ class GeocoderServiceProvider extends ServiceProvider
32
25
public function boot ()
33
26
{
34
27
$ source = realpath (__DIR__ . '/../config/geocoder.php ' );
35
-
36
28
$ this ->publishes ([$ source => config_path ('geocoder.php ' )], 'config ' );
37
-
38
29
$ this ->mergeConfigFrom ($ source , 'geocoder ' );
39
30
}
40
31
@@ -45,13 +36,7 @@ public function boot()
45
36
*/
46
37
public function register ()
47
38
{
48
- $ this ->app ->singleton ('geocoder.adapter ' , function ($ app ) {
49
- $ adapter = config ('geocoder.adapter ' );
50
-
51
- return new $ adapter ;
52
- });
53
-
54
- $ this ->app ->singleton ('geocoder ' , function ($ app ) {
39
+ $ this ->app ->singleton ('geocoder ' , function () {
55
40
$ geocoder = new ProviderAggregator ();
56
41
$ geocoder ->registerProviders (
57
42
$ this ->getProviders (collect (config ('geocoder.providers ' )))
@@ -61,10 +46,16 @@ public function register()
61
46
});
62
47
}
63
48
49
+ /**
50
+ * Instantiate the configured Providers, as well as the Chain Provider.
51
+ *
52
+ * @param Collection
53
+ * @return array
54
+ */
64
55
private function getProviders (Collection $ providers )
65
56
{
66
57
$ providers = $ providers ->map (function ($ arguments , $ provider ) {
67
- $ arguments = $ this ->prepArguments ($ arguments , $ provider );
58
+ $ arguments = $ this ->getArguments ($ arguments , $ provider );
68
59
$ reflection = new ReflectionClass ($ provider );
69
60
70
61
if ($ provider === 'Geocoder\Provider\Chain ' ) {
@@ -77,64 +68,51 @@ private function getProviders(Collection $providers)
77
68
return $ providers ->toArray ();
78
69
}
79
70
80
- private function prepArguments (array $ arguments , $ provider )
71
+ /**
72
+ * Insert the required Adapter instance (if required) as the first element
73
+ * of the arguments array.
74
+ *
75
+ * @param array
76
+ * @param string
77
+ * @return string
78
+ */
79
+ private function getArguments (array $ arguments , $ provider )
81
80
{
82
- $ specificAdapter = $ this ->providerRequiresSpecificAdapter ($ provider );
83
-
84
- if ($ specificAdapter ) {
85
- array_unshift ($ arguments , $ specificAdapter );
86
-
87
- return $ arguments ;
88
- }
89
-
90
- if ($ this ->providerRequiresAdapter ($ provider )) {
91
- array_unshift ($ arguments , app ('geocoder.adapter ' ));
92
-
93
- return $ arguments ;
94
- }
95
-
96
81
if ($ provider === 'Geocoder\Provider\Chain ' ) {
97
82
return $ this ->getProviders (
98
83
collect (config ('geocoder.providers.Geocoder\Provider\Chain ' ))
99
84
);
100
85
}
101
86
87
+ $ adapter = $ this ->getAdapterClass ($ provider );
88
+
89
+ if ($ adapter ) {
90
+ array_unshift ($ arguments , (new $ adapter ));
91
+ }
92
+
102
93
return $ arguments ;
103
94
}
104
95
105
- private function providerRequiresSpecificAdapter ($ provider )
96
+ /**
97
+ * Get the required Adapter class name for the current provider. It will
98
+ * select a specific adapter if required, handle the Chain provider, and
99
+ * return the default configured adapter if non of the above are true.
100
+ *
101
+ * @param string
102
+ * @return string
103
+ */
104
+ private function getAdapterClass ($ provider )
106
105
{
107
106
$ specificAdapters = collect ([
108
107
'Geocoder\Provider\GeoIP2 ' => 'Geocoder\Adapter\GeoIP2Adapter ' ,
108
+ 'Geocoder\Provider\MaxMindBinary ' => null ,
109
109
]);
110
110
111
- return $ specificAdapters ->get ($ provider );
112
- }
113
-
114
- private function providerRequiresAdapter ($ provider )
115
- {
116
- $ providersRequiringAdapter = collect ([
117
- 'Geocoder\Provider\ArcGISOnline ' ,
118
- 'Geocoder\Provider\BingMaps ' ,
119
- 'Geocoder\Provider\FreeGeoIp ' ,
120
- 'Geocoder\Provider\GeoIPs ' ,
121
- 'Geocoder\Provider\Geonames ' ,
122
- 'Geocoder\Provider\GeoPlugin ' ,
123
- 'Geocoder\Provider\GoogleMaps ' ,
124
- 'Geocoder\Provider\GoogleMapsBusiness ' ,
125
- 'Geocoder\Provider\HostIp ' ,
126
- 'Geocoder\Provider\IpInfoDb ' ,
127
- 'Geocoder\Provider\MapQuest ' ,
128
- 'Geocoder\Provider\MaxMind ' ,
129
- 'Geocoder\Provider\Nominatim ' ,
130
- 'Geocoder\Provider\OpenCage ' ,
131
- 'Geocoder\Provider\OpenStreetMap ' ,
132
- 'Geocoder\Provider\Provider ' ,
133
- 'Geocoder\Provider\TomTom ' ,
134
- 'Geocoder\Provider\Yandex ' ,
135
- ]);
111
+ if ($ specificAdapters ->has ($ provider )) {
112
+ return $ specificAdapters ->get ($ provider );
113
+ }
136
114
137
- return $ providersRequiringAdapter -> contains ( $ provider );
115
+ return config ( ' geocoder.adapter ' );
138
116
}
139
117
140
118
/**
@@ -144,6 +122,6 @@ private function providerRequiresAdapter($provider)
144
122
*/
145
123
public function provides ()
146
124
{
147
- return ['geocoder ' , ' geocoder.adapter ' , ' geocoder.chain ' ];
125
+ return ['geocoder ' ];
148
126
}
149
127
}
0 commit comments