Skip to content
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

using static keyword so this class can be extended successfully #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
54 changes: 27 additions & 27 deletions src/phpbrowscap/Browscap.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public function getBrowser($user_agent = null, $return_array = false)
// match with numeric replacements
array_shift($matches);

$match_string = self::COMPRESSION_PATTERN_START . implode(self::COMPRESSION_PATTERN_DELIMITER, $matches);
$match_string = static::COMPRESSION_PATTERN_START . implode(static::COMPRESSION_PATTERN_DELIMITER, $matches);

if (!isset($pattern_data[$match_string])) {
// partial match - numbers are not present, but everything else is ok
Expand All @@ -345,7 +345,7 @@ public function getBrowser($user_agent = null, $return_array = false)

$browser = array(
$user_agent, // Original useragent
trim(strtolower($pattern), self::REGEX_DELIMITER),
trim(strtolower($pattern), static::REGEX_DELIMITER),
$this->_pregUnQuote($pattern, $simple_match ? false : $matches)
);

Expand Down Expand Up @@ -492,7 +492,7 @@ public function updateCache()
$cache_path = $this->cacheDir . $this->cacheFilename;

// Choose the right url
if ($this->_getUpdateMethod() == self::UPDATE_LOCAL) {
if ($this->_getUpdateMethod() == static::UPDATE_LOCAL) {
$url = $this->localFile;
} else {
$url = $this->remoteIniUrl;
Expand All @@ -506,8 +506,8 @@ public function updateCache()
$browsers = parse_ini_file($ini_path, true);
}

$this->_source_version = $browsers[self::BROWSCAP_VERSION_KEY]['Version'];
unset($browsers[self::BROWSCAP_VERSION_KEY]);
$this->_source_version = $browsers[static::BROWSCAP_VERSION_KEY]['Version'];
unset($browsers[static::BROWSCAP_VERSION_KEY]);

unset($browsers['DefaultProperties']['RenderingEngine_Description']);

Expand Down Expand Up @@ -649,7 +649,7 @@ protected function deduplicateCompressionPattern($matches, &$pattern)

foreach ($matches as $i => $some_match)
{
$prepared_matches[self::COMPRESSION_PATTERN_START . implode(self::COMPRESSION_PATTERN_DELIMITER, array_diff_assoc($some_match, $identical))] = $i;
$prepared_matches[static::COMPRESSION_PATTERN_START . implode(static::COMPRESSION_PATTERN_DELIMITER, array_diff_assoc($some_match, $identical))] = $i;
}

$pattern_parts = explode('(\d)', $pattern);
Expand All @@ -674,15 +674,15 @@ protected function deduplicateCompressionPattern($matches, &$pattern)
*/
protected function _pregQuote($user_agent)
{
$pattern = preg_quote($user_agent, self::REGEX_DELIMITER);
$pattern = preg_quote($user_agent, static::REGEX_DELIMITER);

// the \\x replacement is a fix for "Der gro\xdfe BilderSauger 2.00u" user agent match

return self::REGEX_DELIMITER
return static::REGEX_DELIMITER
. '^'
. str_replace(array('\*', '\?', '\\x'), array('.*', '.', '\\\\x'), $pattern)
. '$'
. self::REGEX_DELIMITER;
. static::REGEX_DELIMITER;
}

/**
Expand All @@ -697,8 +697,8 @@ protected function _pregUnQuote($pattern, $matches)
{
// list of escaped characters: http://www.php.net/manual/en/function.preg-quote.php
// to properly unescape '?' which was changed to '.', I replace '\.' (real dot) with '\?', then change '.' to '?' and then '\?' to '.'.
$search = array('\\' . self::REGEX_DELIMITER, '\\.', '\\\\', '\\+', '\\[', '\\^', '\\]', '\\$', '\\(', '\\)', '\\{', '\\}', '\\=', '\\!', '\\<', '\\>', '\\|', '\\:', '\\-', '.*', '.', '\\?');
$replace = array(self::REGEX_DELIMITER, '\\?', '\\', '+', '[', '^', ']', '$', '(', ')', '{', '}', '=', '!', '<', '>', '|', ':', '-', '*', '?', '.');
$search = array('\\' . static::REGEX_DELIMITER, '\\.', '\\\\', '\\+', '\\[', '\\^', '\\]', '\\$', '\\(', '\\)', '\\{', '\\}', '\\=', '\\!', '\\<', '\\>', '\\|', '\\:', '\\-', '.*', '.', '\\?');
$replace = array(static::REGEX_DELIMITER, '\\?', '\\', '+', '[', '^', ']', '$', '(', ')', '{', '}', '=', '!', '<', '>', '|', ':', '-', '*', '?', '.');

$result = substr(str_replace($search, $replace, $pattern), 2, -2);

Expand All @@ -725,7 +725,7 @@ protected function _loadCache($cache_file)
{
require $cache_file;

if (!isset($cache_version) || $cache_version != self::CACHE_FILE_VERSION)
if (!isset($cache_version) || $cache_version != static::CACHE_FILE_VERSION)
{
return false;
}
Expand Down Expand Up @@ -758,7 +758,7 @@ protected function _buildCache()
return sprintf(
$cacheTpl,
"'" . $this->_source_version . "'",
"'" . self::CACHE_FILE_VERSION . "'",
"'" . static::CACHE_FILE_VERSION . "'",
$propertiesArray,
$browsersArray,
$userAgentsArray,
Expand Down Expand Up @@ -797,7 +797,7 @@ protected function _getRemoteIniFile($url, $path)
if (file_exists($path) && filesize($path)) {
$local_tmstp = filemtime($path);

if ($this->_getUpdateMethod() == self::UPDATE_LOCAL) {
if ($this->_getUpdateMethod() == static::UPDATE_LOCAL) {
$remote_tmstp = $this->_getLocalMTime();
} else {
$remote_tmstp = $this->_getRemoteMTime();
Expand All @@ -817,11 +817,11 @@ protected function _getRemoteIniFile($url, $path)

$browscap = explode("\n", $browscap);

$pattern = self::REGEX_DELIMITER
$pattern = static::REGEX_DELIMITER
. '('
. self::VALUES_TO_QUOTE
. static::VALUES_TO_QUOTE
. ')="?([^"]*)"?$'
. self::REGEX_DELIMITER;
. static::REGEX_DELIMITER;


// Ok, lets read the file
Expand Down Expand Up @@ -920,13 +920,13 @@ protected function _getUpdateMethod()
// Caches the result
if ($this->updateMethod === null) {
if ($this->localFile !== null) {
$this->updateMethod = self::UPDATE_LOCAL;
$this->updateMethod = static::UPDATE_LOCAL;
} elseif (ini_get('allow_url_fopen') && function_exists('file_get_contents')) {
$this->updateMethod = self::UPDATE_FOPEN;
$this->updateMethod = static::UPDATE_FOPEN;
} elseif (function_exists('fsockopen')) {
$this->updateMethod = self::UPDATE_FSOCKOPEN;
$this->updateMethod = static::UPDATE_FSOCKOPEN;
} elseif (extension_loaded('curl')) {
$this->updateMethod = self::UPDATE_CURL;
$this->updateMethod = static::UPDATE_CURL;
} else {
$this->updateMethod = false;
}
Expand All @@ -947,23 +947,23 @@ protected function _getRemoteData($url)
ini_set('user_agent', $this->_getUserAgent());

switch ($this->_getUpdateMethod()) {
case self::UPDATE_LOCAL:
case static::UPDATE_LOCAL:
$file = file_get_contents($url);

if ($file !== false) {
return $file;
} else {
throw new Exception('Cannot open the local file');
}
case self::UPDATE_FOPEN:
case static::UPDATE_FOPEN:
// include proxy settings in the file_get_contents() call
$context = $this->_getStreamContext();
$file = file_get_contents($url, false, $context);

if ($file !== false) {
return $file;
} // else try with the next possibility (break omitted)
case self::UPDATE_FSOCKOPEN:
case static::UPDATE_FSOCKOPEN:
$remote_url = parse_url($url);
$remote_handler = fsockopen($remote_url['host'], 80, $c, $e, $this->timeout);

Expand All @@ -975,7 +975,7 @@ protected function _getRemoteData($url)
}

$out = sprintf(
self::REQUEST_HEADERS,
static::REQUEST_HEADERS,
$remote_url['path'],
$remote_url['host'],
$this->_getUserAgent()
Expand All @@ -1001,7 +1001,7 @@ protected function _getRemoteData($url)
return $file;
}
} // else try with the next possibility
case self::UPDATE_CURL:
case static::UPDATE_CURL:
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Expand Down Expand Up @@ -1030,7 +1030,7 @@ protected function _getRemoteData($url)
*/
protected function _getUserAgent()
{
$ua = str_replace('%v', self::VERSION, $this->userAgent);
$ua = str_replace('%v', static::VERSION, $this->userAgent);
$ua = str_replace('%m', $this->_getUpdateMethod(), $ua);

return $ua;
Expand Down