View source
<?php
namespace Drupal\geocoder\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\geocoder\DumperPluginManager;
use Drupal\geocoder\FormatterPluginManager;
use Geocoder\Model\Address;
use Geocoder\Model\AddressCollection;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\geocoder\Geocoder;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
class GeocoderApiEnpoints extends ControllerBase {
protected $config;
protected $entityTypeManager;
protected $geocoder;
protected $dumperPluginManager;
protected $geocoderFormatterPluginManager;
protected $response;
protected function getAddressFormatter($address_format = NULL) {
return $address_format ?: 'default_formatted_address';
}
protected function setGeocodersOptions(Request $request) : array {
$geocoders_configs = $this->config
->get('plugins_options') ?: [];
$geocoders_options = $request
->get('options') ?: [];
$options = NestedArray::mergeDeep($geocoders_configs, $geocoders_options);
return $options;
}
protected function addGeometryProperty(Address $address) {
$address_array = $address
->toArray();
return [
'location' => [
'lat' => $address_array['latitude'],
'lng' => $address_array['longitude'],
],
'viewport' => [
'northeast' => [
'lat' => $address_array['bounds']['north'],
'lng' => $address_array['bounds']['east'],
],
'southwest' => [
'lat' => $address_array['bounds']['south'],
'lng' => $address_array['bounds']['west'],
],
],
];
}
protected function getAddressCollectionResponse(AddressCollection $geo_collection, $dumper = NULL, $address_format = NULL) : void {
$result = [];
foreach ($geo_collection
->all() as $k => $geo_address) {
if (isset($dumper)) {
$result[$k] = $dumper
->dump($geo_address);
}
else {
$result[$k] = $geo_address
->toArray();
if (!isset($result[$k]['formatted_address'])) {
try {
$result[$k]['formatted_address'] = $this->geocoderFormatterPluginManager
->createInstance($this
->getAddressFormatter($address_format))
->format($geo_address);
} catch (\Exception $e) {
watchdog_exception('geocoder', $e);
}
}
if (!isset($result[$k]['geometry'])) {
$result[$k]['geometry'] = $this
->addGeometryProperty($geo_address);
}
}
}
$this->response = new CacheableJsonResponse($result, 200);
$this->response
->addCacheableDependency(CacheableMetadata::createFromObject($result));
}
protected function getDumper($format) {
$dumper = NULL;
if (isset($format)) {
try {
$dumper = $this->dumperPluginManager
->createInstance($format);
} catch (\Exception $e) {
$dumper = NULL;
}
}
return $dumper;
}
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, Geocoder $geocoder, DumperPluginManager $dumper_plugin_manager, FormatterPluginManager $geocoder_formatter_plugin_manager) {
$this->config = $config_factory
->get('geocoder.settings');
$this->entityTypeManager = $entity_type_manager;
$this->geocoder = $geocoder;
$this->dumperPluginManager = $dumper_plugin_manager;
$this->geocoderFormatterPluginManager = $geocoder_formatter_plugin_manager;
$this->response = new Response('', 204);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('geocoder'), $container
->get('plugin.manager.geocoder.dumper'), $container
->get('plugin.manager.geocoder.formatter'));
}
public function geocode(Request $request) {
$address = $request
->get('address');
$geocoders_ids = $request
->get('geocoder');
$format = $request
->get('format');
$geocoders = [];
try {
$geocoders = $this->entityTypeManager
->getStorage('geocoder_provider')
->loadMultiple(explode(',', str_replace(' ', '', $geocoders_ids)));
} catch (\Exception $e) {
watchdog_exception('geocoder', $e);
}
$address_format = $request
->get('address_format');
if (isset($address)) {
$options = $this
->setGeocodersOptions($request);
$dumper = $this
->getDumper($format);
$geo_collection = $this->geocoder
->geocode($address, $geocoders, $options);
if ($geo_collection && $geo_collection instanceof AddressCollection) {
$this
->getAddressCollectionResponse($geo_collection, $dumper, $address_format);
}
}
return $this->response;
}
public function reverseGeocode(Request $request) {
$latlng = $request
->get('latlng');
$geocoders_ids = $request
->get('geocoder');
$format = $request
->get('format');
$geocoders = [];
try {
$geocoders = $this->entityTypeManager
->getStorage('geocoder_provider')
->loadMultiple(explode(',', $geocoders_ids));
} catch (\Exception $e) {
watchdog_exception('geocoder', $e);
}
if (isset($latlng)) {
$latlng = explode(',', $request
->get('latlng'));
$options = $this
->setGeocodersOptions($request);
$dumper = $this
->getDumper($format);
$geo_collection = $this->geocoder
->reverse($latlng[0], $latlng[1], $geocoders, $options);
if ($geo_collection && $geo_collection instanceof AddressCollection) {
$this
->getAddressCollectionResponse($geo_collection, $dumper);
}
}
return $this->response;
}
}