class GeoLocation in GeoIP API 8.2
Service to interact with the default geolocator plugin for geolocation.
Hierarchy
- class \Drupal\geoip\GeoLocation uses UseCacheBackendTrait
Expanded class hierarchy of GeoLocation
1 file declares its use of GeoLocation
- GeoLocationTest.php in tests/
src/ Unit/ GeoLocationTest.php
1 string reference to 'GeoLocation'
1 service uses GeoLocation
File
- src/
GeoLocation.php, line 14
Namespace
Drupal\geoipView source
class GeoLocation {
use UseCacheBackendTrait;
/**
* Plugin manager for GeoLocator plugins.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $geoLocatorManager;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
protected $cacheKey = 'geolocated_ips';
protected $cacheTags = [
'geoip',
];
protected $locatedAddresses = [];
protected $config = [];
/**
* Constructs a new GeoLocation object.
*
* @param \Drupal\Component\Plugin\PluginManagerInterface $geolocators_manager
* The geolocation locator plugin manager service to use.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
*/
public function __construct(PluginManagerInterface $geolocators_manager, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache_backend) {
$this->geoLocatorManager = $geolocators_manager;
$this->configFactory = $config_factory;
$this->cacheBackend = $cache_backend;
$this->config = $this->configFactory
->get('geoip.geolocation');
}
/**
* Gets the identifier of the default geolocator plugin.
*
* @return string
* Identifier of the default geolocator plugin.
*/
public function getGeoLocatorId() {
return $this->config
->get('plugin_id');
}
/**
* Gets an instance of the default geolocator plugin.
*
* @return \Drupal\geoip\Plugin\GeoLocator\GeoLocatorInterface
* Instance of the default geolocator plugin.
*/
public function getGeoLocator() {
return $this->geoLocatorManager
->createInstance($this->config
->get('plugin_id'));
}
/**
* Geolocate an IP address.
*
* @param string $ip_address
* The IP address to geo locate.
*
* @return string|null
* The geolocated country code, or NULL if not found.
*/
public function geolocate($ip_address) {
if (!isset($this->locatedAddresses[$ip_address])) {
if ($cache = $this->cacheBackend
->get($this->cacheKey . ':' . $ip_address)) {
$this->locatedAddresses[$ip_address] = $cache->data;
}
else {
$geolocator = $this
->getGeoLocator();
$result = $geolocator
->geolocate($ip_address);
$this->locatedAddresses[$ip_address] = $result;
$this->cacheBackend
->set($this->cacheKey . ':' . $ip_address, $this->locatedAddresses[$ip_address], Cache::PERMANENT, $this->cacheTags);
}
}
return $this->locatedAddresses[$ip_address];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GeoLocation:: |
protected | property | ||
GeoLocation:: |
protected | property | ||
GeoLocation:: |
protected | property | ||
GeoLocation:: |
protected | property | The configuration factory. | |
GeoLocation:: |
protected | property | Plugin manager for GeoLocator plugins. | |
GeoLocation:: |
protected | property | ||
GeoLocation:: |
public | function | Geolocate an IP address. | |
GeoLocation:: |
public | function | Gets an instance of the default geolocator plugin. | |
GeoLocation:: |
public | function | Gets the identifier of the default geolocator plugin. | |
GeoLocation:: |
public | function | Constructs a new GeoLocation object. | |
UseCacheBackendTrait:: |
protected | property | Cache backend instance. | |
UseCacheBackendTrait:: |
protected | property | Flag whether caches should be used or skipped. | |
UseCacheBackendTrait:: |
protected | function | Fetches from the cache backend, respecting the use caches flag. | 1 |
UseCacheBackendTrait:: |
protected | function | Stores data in the persistent cache, respecting the use caches flag. |