abstract class ProviderBase in Geocoder 8.3
Same name and namespace in other branches
- 8.2 src/ProviderBase.php \Drupal\geocoder\ProviderBase
Provides a base class for providers using handlers.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\geocoder\ProviderBase implements ContainerFactoryPluginInterface, ProviderInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ProviderBase
1 file declares its use of ProviderBase
- Random.php in src/
Plugin/ Geocoder/ Provider/ Random.php
File
- src/
ProviderBase.php, line 15
Namespace
Drupal\geocoderView source
abstract class ProviderBase extends PluginBase implements ProviderInterface, ContainerFactoryPluginInterface {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The cache backend used to cache geocoding data.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* The configurable language manager.
*
* @var \Drupal\language\ConfigurableLanguageManager
*/
protected $languageManager;
/**
* Constructs a geocoder provider plugin object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend used to cache geocoding data.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The Drupal language manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->cacheBackend = $cache_backend;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('cache.geocoder'), $container
->get('language_manager'));
}
/**
* {@inheritdoc}
*/
public function geocode($source) {
return $this
->process(__FUNCTION__, \func_get_args());
}
/**
* {@inheritdoc}
*/
public function reverse($latitude, $longitude) {
return $this
->process(__FUNCTION__, \func_get_args());
}
/**
* Provides a helper callback for geocode() and reverse().
*
* @param string $method
* The method: 'geocode' or 'reverse'.
* @param array $data
* An array with data to be processed. When geocoding, it contains only one
* item with the string. When reversing, contains 2 items: the latitude and
* the longitude.
*
* @return \Geocoder\Model\Address|null
* The Address, NULL otherwise.
*/
protected function process($method, array $data) {
if ($caching = $this->configFactory
->get('geocoder.settings')
->get('cache')) {
// Try to retrieve from cache first.
$cid = $this
->getCacheId($method, $data);
if ($cache = $this->cacheBackend
->get($cid)) {
return $cache->data;
}
}
// Call the processor.
$processor = $method == 'geocode' ? 'doGeocode' : 'doReverse';
$value = \call_user_func_array([
$this,
$processor,
], $data);
if ($caching) {
// Cache the result.
$this->cacheBackend
->set($cid, $value);
}
return $value;
}
/**
* Performs the geocoding.
*
* @param string $source
* The data to be geocoded.
*
* @return \Geocoder\Model\AddressCollection|\Geometry|null
* The address collection, or the geometry, or NULL.
*/
protected abstract function doGeocode($source);
/**
* Performs the reverse geocode.
*
* @param float $latitude
* The latitude.
* @param float $longitude
* The longitude.
*
* @return \Geocoder\Model\AddressCollection|null
* The AddressCollection, NULL otherwise.
*/
protected abstract function doReverse($latitude, $longitude);
/**
* Builds a cached id.
*
* @param string $method
* The method: 'geocode' or 'reverse'.
* @param array $data
* An array with data to be processed. When geocoding, it contains only one
* item with the string. When reversing, contains 2 items: the latitude and
* the longitude.
*
* @return string
* An unique cache id.
*/
protected function getCacheId($method, array $data) : string {
$cid = [
$method,
$this
->getPluginId(),
];
$cid[] = sha1(serialize($this->configuration) . serialize($data));
return implode(':', $cid);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ProviderBase:: |
protected | property | The cache backend used to cache geocoding data. | |
ProviderBase:: |
protected | property | The config factory service. | |
ProviderBase:: |
protected | property | The configurable language manager. | |
ProviderBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
2 |
ProviderBase:: |
abstract protected | function | Performs the geocoding. | 2 |
ProviderBase:: |
abstract protected | function | Performs the reverse geocode. | 2 |
ProviderBase:: |
public | function |
Geocode a source string. Overrides ProviderInterface:: |
|
ProviderBase:: |
protected | function | Builds a cached id. | |
ProviderBase:: |
protected | function | Provides a helper callback for geocode() and reverse(). | |
ProviderBase:: |
public | function |
Reverse geocode latitude and longitude. Overrides ProviderInterface:: |
|
ProviderBase:: |
public | function |
Constructs a geocoder provider plugin object. Overrides PluginBase:: |
1 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |