Skip to content

Starting to explore Maxmind API decoupling (Signature clashes) #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ function geoip_detect2_get_info_from_current_ip($locales = null, $options = arra
* @since 2.0.0
* @since 2.5.0 new parameter $options
* @since 2.7.0 Parameter $options['source'] has been introduced
* @since 4.1.0 This does not return a Maxmind Reader anymore, as the plugin readers have been decoupled from the Maxmind Readers (API incompability). But the method names stay the same.
*
* @return \YellowTree\GeoipDetect\DataSources\ReaderInterface $reader
*/
function geoip_detect2_get_reader($locales = null, $options = array()) {
_geoip_maybe_disable_pagecache();
Expand Down
20 changes: 19 additions & 1 deletion data-sources/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,29 @@ class ExtraInformation extends \GeoIp2\Record\AbstractRecord {
protected $validAttributes = array('source', 'cached', 'error', 'original', 'flag', 'tel', 'countryIsoCode3', 'currencyCode');
}

interface ReaderInterface extends \GeoIp2\ProviderInterface {
/**
* This interface is basically extending \GeoIp2\ProviderInterface ,
* but due to signature changes in the Maxmind lib, extending it would not be wise (as it breaks as soon as other plugins are using a different version of the Maxmind lib)
*/
interface ReaderInterface {
/**
* Closes the database and returns the resources to the system.
*/
public function close();

/**
* @param string $ipAddress an IPv4 or IPv6 address to lookup
*
* @return \GeoIp2\Model\Country a Country model for the requested IP address
*/
public function country($ipAddress);

/**
* @param string $ipAddress an IPv4 or IPv6 address to lookup
*
* @return \GeoIp2\Model\City a City model for the requested IP address
*/
public function city($ipAddress);
}

abstract class AbstractReader implements \YellowTree\GeoipDetect\DataSources\ReaderInterface {
Expand Down
2 changes: 1 addition & 1 deletion data-sources/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function country($ip) {

$r['traits']['ip_address'] = $ip;

$record = new \GeoIp2\Model\City($r, array('en'));
$record = new \YellowTree\GeoipDetect\DataSources\City($r, array('en'));

return $record;
}
Expand Down
1 change: 1 addition & 0 deletions data-sources/manual.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public function getReader($locales = array('en'), $options = array()) {
$data_file = $this->maxmindGetFilename();
if ($data_file) {
try {
// ToDo this needs to be wrapped?
$reader = new \GeoIp2\Database\Reader ( $data_file, $locales );
} catch ( \Exception $e ) {
if (WP_DEBUG)
Expand Down
4 changes: 2 additions & 2 deletions geoip-detect-lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function _geoip_detect2_process_options($options) {
* @param array(string) List of locale codes to use in name property
* from most preferred to least preferred. (Default: Site language, en)
* @param boolean If locale filter should be skipped (default: No)
* @return GeoIp2\Database\Reader The reader, ready to do its work. Don't forget to `close()` it afterwards. NULL if file not found (or other problems).
* @return \YellowTree\GeoipDetect\DataSources\ReaderInterface The reader, ready to do its work. Don't forget to `close()` it afterwards. NULL if file not found (or other problems).
* NULL if initialization went wrong (e.g., File not found.)
*/
function _geoip_detect2_get_reader($locales = null, $skipLocaleFilter = false, &$sourceId = '', $options = array()) {
Expand Down Expand Up @@ -211,7 +211,7 @@ function _geoip_detect2_get_record_from_reader($reader, $ip, &$error) {

function _geoip_detect2_get_new_empty_record($ip = '') {
$data = array('traits' => array('ip_address' => $ip), 'is_empty' => true);
return new \GeoIp2\Model\City($data);
return new \YellowTree\GeoipDetect\DataSources\City($data);
}

function _geoip_detect2_record_enrich_data($record, $ip, $sourceId, $error) : array {
Expand Down