[ticket/9871] Allow setting the host/file to load for the version class

PHPBB3-9871
This commit is contained in:
Nathan Guse 2014-03-10 21:26:46 -05:00
parent 1f7c891da2
commit 00d86a4af1
2 changed files with 45 additions and 3 deletions

View file

@ -318,6 +318,7 @@ services:
version_helper: version_helper:
class: phpbb\version_helper class: phpbb\version_helper
scope: prototype
arguments: arguments:
- @cache - @cache
- @config - @config

View file

@ -14,6 +14,21 @@ namespace phpbb;
*/ */
class version_helper class version_helper
{ {
/**
* @var string Host
*/
protected $host = 'version.phpbb.com';
/**
* @var string Path to file
*/
protected $path = '/phpbb';
/**
* @var string File name
*/
protected $file = 'versions.json';
/** @var \phpbb\cache\service */ /** @var \phpbb\cache\service */
protected $cache; protected $cache;
@ -23,6 +38,13 @@ class version_helper
/** @var \phpbb\user */ /** @var \phpbb\user */
protected $user; protected $user;
/**
* Constructor
*
* @param \phpbb\cache\service $cache
* @param \phpbb\config\config $config
* @param \phpbb\user $user
*/
public function __construct(\phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\user $user) public function __construct(\phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\user $user)
{ {
$this->cache = $cache; $this->cache = $cache;
@ -30,6 +52,23 @@ class version_helper
$this->user = $user; $this->user = $user;
} }
/**
* Set location to the file
*
* @param string $host Host (e.g. version.phpbb.com)
* @param string $path Path to file (e.g. /phpbb)
* @param string $file File name (Default: versions.json)
* @return version_helper
*/
public function set_file_location($host, $path, $file = 'versions.json')
{
$this->host = $host;
$this->path = $path;
$this->file = $file;
return $this;
}
/** /**
* Wrapper for version_compare() that allows using uppercase A and B * Wrapper for version_compare() that allows using uppercase A and B
* for alpha and beta releases. * for alpha and beta releases.
@ -142,12 +181,14 @@ class version_helper
*/ */
public function get_versions($force_update = false) public function get_versions($force_update = false)
{ {
$info = $this->cache->get('versioncheck'); $cache_file = 'versioncheck_' . $this->host . $this->path . $this->file;
$info = $this->cache->get($cache_file);
if ($info === false || $force_update) if ($info === false || $force_update)
{ {
$errstr = $errno = ''; $errstr = $errno = '';
$info = get_remote_file('version.phpbb.com', '/phpbb', 'versions.json', $errstr, $errno); $info = get_remote_file($this->host, $this->path, $this->file, $errstr, $errno);
if (!empty($errstr)) if (!empty($errstr))
{ {
@ -172,7 +213,7 @@ class version_helper
} }
} }
$this->cache->put('versioncheck', $info, 86400); // 24 hours $this->cache->put($cache_file, $info, 86400); // 24 hours
} }
return $info; return $info;