Skip to content
85 changes: 46 additions & 39 deletions Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Parser
* @var boolean
* @access protected
*/
protected $reload = false;
protected $reloadAlways = false;

/**
* Life time of cached file
Expand Down Expand Up @@ -148,7 +148,7 @@ public function __construct($format = 'object')
public function setCachePath($path = null)
{
if (is_null($path)) {
$this->path = sys_get_temp_dir();
$this->path = __DIR__;
} else {
$this->path = filter_var($path, FILTER_SANITIZE_STRING);
}
Expand Down Expand Up @@ -271,49 +271,61 @@ public function parse($unparsedString, $defaultTld = 'com')
}

/**
* Checks if the domain list exists or cached time is reached
* Loads the list of TLDs.
*
* @throws OpenFileErrorException
* @throws WriteFileErrorException
* @return void
*/
private function load()
{
$filename = $this->path . '/domainparsertld.txt';

if (file_exists($filename)) {
$this->tldList = unserialize(file_get_contents($filename));

// will reload tld list if timestamp of cache file is outdated
if (time() - $this->tldList['timestamp'] > $this->cacheTime) {
$this->reload = true;
}
$filename = $this->path . '/tlds.json';

// will reload tld list if changes to Additional.php have been made
if ($this->tldList['timestamp'] < filemtime(DOMAINPARSERPATH . '/Additional.php')) {
$this->reload = true;
}
if (!file_exists($filename)) {
throw \Novutec\DomainParser\AbstractException::factory('OpenFile', 'Could not open cache file.');
}
$this->tldList = json_decode(file_get_contents($filename), true);
$this->loaded = true;
}

// check connection - if there is no internet connection skip loading
$existFile = file_exists($filename);
/**
* Checks if the list of TLDs needs or should be reloaded.
*
* @return boolean
*/
public function needsReload()
{
if ($this->reloadAlways) {
return true;
}
// will reload tld list if timestamp of cache file is outdated
if (time() - $this->tldList['timestamp'] > $this->cacheTime) {
return true;
}

if (! $existFile || $this->reload === true) {
$this->catchTlds($existFile);
$file = fopen($filename, 'w+');
// will reload tld list if changes to Additional.php have been made
if ($this->tldList['timestamp'] < filemtime(DOMAINPARSERPATH . '/Additional.php')) {
return true;
}
return false;
}

if ($file === false) {
throw \Novutec\DomainParser\AbstractException::factory('OpenFile', 'Could not open cache file.');
}
/**
* Reload the list of TLDs.
*
* @throws WriteFileErrorException
* @return void
*/
public function performReload()
{
$filename = $this->path . '/tlds.json';

if (fwrite($file, serialize($this->tldList)) === false) {
throw \Novutec\DomainParser\AbstractException::factory('WriteFile', 'Could not open cache file for writing.');
}
$this->catchTlds();
$file = fopen($filename, 'w+');

fclose($file);
if (fwrite($file, json_encode($this->tldList, JSON_PRETTY_PRINT)) === false) {
throw \Novutec\DomainParser\AbstractException::factory('WriteFile', 'Could not open cache file for writing.');
}

$this->loaded = true;
fclose($file);
}

/**
Expand All @@ -326,18 +338,13 @@ private function load()
*
* @throws ConnectErrorException
* @see Novutec\Additional.php $additional
* @param boolean $existFile
* @return void
*/
private function catchTlds($existFile)
private function catchTlds()
{
$content = @file_get_contents($this->tldUrl);

if ($content === false) {
if (! $existFile) {
throw \Novutec\DomainParser\AbstractException::factory('Connect', 'Could not catch file from server.');
}

return;
}

Expand Down Expand Up @@ -451,7 +458,7 @@ public function throwExceptions($throwExceptions = false)
*/
public function reload($reload = false)
{
$this->reload = filter_var($reload, FILTER_VALIDATE_BOOLEAN);
$this->reloadAlways = filter_var($reload, FILTER_VALIDATE_BOOLEAN);
}

/**
Expand All @@ -466,4 +473,4 @@ public function cacheTime($cacheTime = 432000)
{
$this->cacheTime = filter_var($cacheTime, FILTER_VALIDATE_INT);
}
}
}
10 changes: 10 additions & 0 deletions reload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

require 'vendor/autoload.php';

use Novutec\DomainParser\Parser;

$parser = new Parser();
$parser->performReload();

?>
Loading