Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

Commit

Permalink
Added method for manual cache update
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Nov 24, 2015
1 parent 88454b0 commit df542c5
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ class Extract
{

/**
* If $fetch is TRUE (the default) and no cached TLD set is found, the
* extractor will fetch the Public Suffix List live over HTTP on first use.
* If $fetch is TRUE and no cached TLD set is found, the extractor will fetch the Public Suffix List live over
* HTTP on first use.
*
* Set to FALSE to disable this behaviour.
*
* @var bool
*/
private static $fetch = true;
private static $fetch = false;
/**
* Specifying $cacheFile will override the location of the cached TLD set.
* Defaults to /path/to/tldextract/cache/.tld_set.
* Defaults to /path/to/TLDExtract/cache/.tld_set.
*
* @var string
*/
private static $cacheFile = '../cache/.tld_set';
private static $cacheFile = null;

/**
* Specifying $suffixFileUrl will override the URL from suffix list will be
Expand All @@ -45,7 +46,7 @@ class Extract
private static $suffixFileUrl = 'https://publicsuffix.org/list/effective_tld_names.dat';

/**
* Gets states of $_fetch.
* Gets states of $fetch.
*
* @return boolean
*/
Expand All @@ -55,9 +56,9 @@ public static function isFetch()
}

/**
* Sets $_fetch param.
* Sets $fetch param.
*
* @param boolean $fetch @see $_fetch
* @param boolean $fetch
*
* @return void
*/
Expand Down Expand Up @@ -119,6 +120,10 @@ public static function setSuffixFileUrl($suffixFileUrl)
*/
public static function get($url)
{
if (self::$cacheFile === null) {
self::$cacheFile = __DIR__ . '/cache/.tld_set';
}

$host = self::getHost($url);
$extractor = SuffixExtractor::getInstance();

Expand All @@ -145,6 +150,20 @@ public static function get($url)
return new Result(null, $domain, $tld);
}

/**
* Method for manually updating of TLD list's cache
*
* @return bool
*
* @throws Exceptions\IOException
*/
public static function updateCache()
{
$extractor = SuffixExtractor::getInstance();

return $extractor->fetchTldList();
}

/**
* Extract the hostname from a URL.
*
Expand Down Expand Up @@ -175,12 +194,12 @@ private static function getHost($url)
*
* @see http://www.ietf.org/rfc/rfc2732.txt
* */
$closingBracketPosition = strrpos($host, ']');
$bracketPosition = strrpos($host, ']');

if (Helpers::startsWith($host, '[') && $closingBracketPosition !== false) {
if (Helpers::startsWith($host, '[') && $bracketPosition !== false) {
// This is IPv6 literal

return substr($host, 0, $closingBracketPosition + 1);
return substr($host, 0, $bracketPosition + 1);
}

/*
Expand Down

0 comments on commit df542c5

Please sign in to comment.