1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Geotools library. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\Geotools\CLI\Command\Geocoder; |
13
|
|
|
|
14
|
|
|
use Geocoder\ProviderAggregator; |
15
|
|
|
use Http\Discovery\HttpClientDiscovery; |
16
|
|
|
use League\Geotools\Batch\Batch; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Command-line geocoder:geocode class |
24
|
|
|
* |
25
|
|
|
* @author Antoine Corcy <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Geocode extends Command |
28
|
1 |
|
{ |
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
1 |
|
$this |
32
|
1 |
|
->setName('geocoder:geocode') |
33
|
1 |
|
->setDescription('Geocode a street-address, IPv4 or IPv6 against a provider with an adapter') |
34
|
1 |
|
->addArgument('value', InputArgument::REQUIRED, 'The street-address, IPv4 or IPv6 to geocode') |
35
|
1 |
|
->addOption('provider', null, InputOption::VALUE_REQUIRED, |
36
|
1 |
|
'If set, the name of the provider to use, Google Maps by default', 'google_maps') |
37
|
1 |
|
->addOption('cache', null, InputOption::VALUE_REQUIRED, |
38
|
1 |
|
'If set, the name of a factory method that will create a PSR-6 cache. "Example\Acme::create"') |
39
|
1 |
|
->addOption('raw', null, InputOption::VALUE_NONE, |
40
|
1 |
|
'If set, the raw format of the reverse geocoding result') |
41
|
1 |
|
->addOption('json', null, InputOption::VALUE_NONE, |
42
|
1 |
|
'If set, the json format of the reverse geocoding result') |
43
|
1 |
|
->addOption('dumper', null, InputOption::VALUE_REQUIRED, |
44
|
1 |
|
'If set, the name of the dumper to use, no dumper by default') |
45
|
1 |
|
->addOption('args', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
46
|
1 |
|
'If set, the provider constructor arguments like api key, locale, region, ssl, toponym and service') |
47
|
1 |
|
->setHelp(<<<EOT |
48
|
1 |
|
<info>Available providers</info>: {$this->getProviders()} <comment>(some providers need arguments)</comment> |
49
|
1 |
|
<info>Available dumpers</info>: {$this->getDumpers()} |
50
|
1 |
|
|
51
|
1 |
|
<info>Use the default provider with the socket adapter and dump the output in WKT standard</info>: |
52
|
|
|
|
53
|
|
|
%command.full_name% paris <comment>--adapter=socket --dumper=wkt</comment> |
54
|
|
|
|
55
|
|
|
<info>Use the OpenStreetMaps provider with the default adapter</info>: |
56
|
|
|
|
57
|
|
|
%command.full_name% paris <comment>--provider=openstreetmaps</comment> |
58
|
|
|
|
59
|
|
|
<info>Use the FreeGeoIp provider with the socket adapter</info> |
60
|
|
|
|
61
|
|
|
%command.full_name% 74.200.247.59 <comment>--provider="free_geo_ip" --adapter="socket"</comment> |
62
|
|
|
|
63
|
|
|
<info>Use the default provider with the french locale and region via SSL</info>: |
64
|
|
|
|
65
|
|
|
%command.full_name% "Tagensvej 47, Copenhagen" <comment>--args=da_DK --args=Denmark --args="true"</comment> |
66
|
|
|
EOT |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
71
|
|
|
{ |
72
|
|
|
$geocoder = new ProviderAggregator; |
73
|
|
|
$httpClient = HttpClientDiscovery::find(); |
74
|
|
|
$provider = $this->getProvider($input->getOption('provider')); |
75
|
|
|
|
76
|
|
|
if ($input->getOption('args')) { |
77
|
|
|
$args = is_array($input->getOption('args')) |
78
|
|
|
? implode(',', $input->getOption('args')) |
79
|
|
|
: $input->getOption('args'); |
80
|
|
|
$geocoder->registerProvider(new $provider($httpClient, $args)); |
81
|
|
|
} else { |
82
|
|
|
$geocoder->registerProvider(new $provider($httpClient)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$batch = new Batch($geocoder); |
86
|
|
|
if ($input->getOption('cache')) { |
87
|
|
|
$batch->setCache($this->getCache($input->getOption('cache'))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$geocoded = $batch->geocode($input->getArgument('value'))->parallel(); |
|
|
|
|
91
|
|
|
$address = $geocoded[0]->first(); |
92
|
|
|
|
93
|
|
|
if ($input->getOption('raw')) { |
94
|
|
|
$result = array(); |
95
|
|
|
$result[] = sprintf('<label>HttpClient</label>: <value>%s</value>', get_class($httpClient)); |
96
|
|
|
$result[] = sprintf('<label>Provider</label>: <value>%s</value>', $provider); |
97
|
|
|
$result[] = sprintf('<label>Cache</label>: <value>%s</value>', isset($cache) ? $cache : 'None'); |
|
|
|
|
98
|
|
|
if ($input->getOption('args')) { |
99
|
|
|
$result[] = sprintf('<label>Arguments</label>: <value>%s</value>', $args); |
100
|
|
|
} |
101
|
|
|
$result[] = '---'; |
102
|
|
|
$coordinates = $address->getCoordinates(); |
103
|
|
|
$result[] = sprintf('<label>Latitude</label>: <value>%s</value>', null !== $coordinates ? $coordinates->getLatitude() : ''); |
104
|
|
|
$result[] = sprintf('<label>Longitude</label>: <value>%s</value>', null !== $coordinates ? $coordinates->getLongitude() : ''); |
105
|
|
|
if ($address->getBounds()) { |
106
|
|
|
$bounds = $address->getBounds()->toArray(); |
107
|
|
|
$result[] = '<label>Bounds</label>'; |
108
|
|
|
$result[] = sprintf(' - <label>South</label>: <value>%s</value>', $bounds['south']); |
109
|
|
|
$result[] = sprintf(' - <label>West</label>: <value>%s</value>', $bounds['west']); |
110
|
|
|
$result[] = sprintf(' - <label>North</label>: <value>%s</value>', $bounds['north']); |
111
|
|
|
$result[] = sprintf(' - <label>East</label>: <value>%s</value>', $bounds['east']); |
112
|
|
|
} |
113
|
|
|
$result[] = sprintf('<label>Street Number</label>: <value>%s</value>', $address->getStreetNumber()); |
114
|
|
|
$result[] = sprintf('<label>Street Name</label>: <value>%s</value>', $address->getStreetName()); |
115
|
|
|
$result[] = sprintf('<label>Zipcode</label>: <value>%s</value>', $address->getPostalCode()); |
116
|
|
|
$result[] = sprintf('<label>City</label>: <value>%s</value>', $address->getLocality()); |
117
|
|
|
$result[] = sprintf('<label>City District</label>: <value>%s</value>', $address->getSublocality()); |
118
|
|
|
if (null !== $adminLevels = $address->getAdminLevels()) { |
119
|
|
|
$result[] = '<label>Admin Levels</label>'; |
120
|
|
|
foreach ($adminLevels as $adminLevel) { |
121
|
|
|
$result[] = sprintf(' - <label>%s</label>: <value>%s</value>', $adminLevel->getCode(), $adminLevel->getName()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
$country = $address->getCountry(); |
125
|
|
|
$result[] = sprintf('<label>Country</label>: <value>%s</value>', null !== $country ? $country->getName() : ''); |
126
|
|
|
$result[] = sprintf('<label>Country Code</label>: <value>%s</value>', null !== $country ? $country->getCode() : ''); |
127
|
|
|
$result[] = sprintf('<label>Timezone</label>: <value>%s</value>', $address->getTimezone()); |
128
|
|
|
} elseif ($input->getOption('json')) { |
129
|
|
|
$result = sprintf('<value>%s</value>', json_encode($address->toArray())); |
130
|
|
|
} elseif ($input->getOption('dumper')) { |
131
|
|
|
$dumper = $this->getDumper($input->getOption('dumper')); |
132
|
|
|
$dumper = new $dumper; |
133
|
|
|
$result = sprintf('<value>%s</value>', $dumper->dump($address)); |
134
|
|
|
} else { |
135
|
|
|
$coordinates = $address->getCoordinates(); |
136
|
|
|
$result = '<value>null, null</value>'; |
137
|
|
|
if (null !== $coordinates) { |
138
|
|
|
$result = sprintf('<value>%s, %s</value>', $coordinates->getLatitude(), $coordinates->getLongitude()); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$output->writeln($result); |
|
|
|
|
143
|
|
|
return 0; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.