You are here

function geoip_instance in GeoIP API 7.2

Same name and namespace in other branches
  1. 5 geoip.module \geoip_instance()
  2. 6 geoip.module \geoip_instance()
  3. 7 geoip.module \geoip_instance()

Returns the instance to handle the configured file.

Return value

\Drupal\geoip\GeoIpHandlerInterface The handler to use.

8 calls to geoip_instance()
geoip_city_name in ./geoip.module
Returns the city data for a given IP.
geoip_continent_code in ./geoip.module
Returns the continent code for a given IP.
geoip_continent_name in ./geoip.module
Returns the continent name for a given IP.
geoip_country_code in ./geoip.module
Returns the ISO 3166-2 country code for a given IP.
geoip_country_name in ./geoip.module
Returns the country name for a given IP.

... See full list

File

./geoip.module, line 132
API for using the MaxMind GeoLite Country database.

Code

function geoip_instance() {
  $instances =& drupal_static(__FUNCTION__, array());

  // Check if a proper data file is given.
  if (!($data_file = variable_get('geoip_data_file')) || !file_exists($data_file)) {
    return FALSE;
  }
  $cid = $data_file;
  if (!isset($instances[$cid])) {
    try {

      // As this isn't D8 we can't rely on a PSR-4 loader and thus we load the
      // files on our own.
      require_once dirname(__FILE__) . '/src/GeoIpHandlerInterface.php';
      switch (geoip_get_api_version()) {
        case 1:
          require_once dirname(__FILE__) . '/src/GeoIpHandlerV1.php';
          $instances[$cid] = new \Drupal\geoip\GeoIpHandlerV1();
          break;
        case 2:
        default:
          require_once dirname(__FILE__) . '/src/GeoIpHandlerV2.php';
          $instances[$cid] = new \Drupal\geoip\GeoIpHandlerV2();
      }

      // Configure the instance.
      $instances[$cid]
        ->setDbFile($data_file);
    } catch (Exception $e) {
      return FALSE;
    }
  }
  return $instances[$cid];
}