| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* JCH Optimize - Performs several front-end optimizations for fast downloads |
| 5 |
* |
| 6 |
* @package jchoptimize/core |
| 7 |
* @author Samuel Marshall <samuel@jch-optimize.net> |
| 8 |
* @copyright Copyright (c) 2022 Samuel Marshall / JCH Optimize |
| 9 |
* @license GNU/GPLv3, or later. See LICENSE file |
| 10 |
* |
| 11 |
* If LICENSE file missing, see <http://www.gnu.org/licenses/>. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace JchOptimize\Core; |
| 15 |
|
| 16 |
use JchOptimize\Core\Platform\UtilityInterface; |
| 17 |
|
| 18 |
use function defined; |
| 19 |
use function md5; |
| 20 |
use function trim; |
| 21 |
|
| 22 |
defined('_JCH_EXEC') or die('Restricted access'); |
| 23 |
|
| 24 |
class Browser |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @var Browser[] |
| 28 |
*/ |
| 29 |
protected static array $instances = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var object{browser: string, browserVersion: string, os: string} |
| 33 |
*/ |
| 34 |
protected object $oClient; |
| 35 |
|
| 36 |
public function __construct(UtilityInterface $utility, string $userAgent) |
| 37 |
{ |
| 38 |
$this->oClient = $utility->userAgent($userAgent); |
| 39 |
} |
| 40 |
|
| 41 |
public static function getInstance(UtilityInterface $utility, string $userAgent = ''): Browser |
| 42 |
{ |
| 43 |
if ($userAgent == '' && isset($_SERVER['HTTP_USER_AGENT'])) { |
| 44 |
$userAgent = trim($_SERVER['HTTP_USER_AGENT']); |
| 45 |
} |
| 46 |
|
| 47 |
$signature = md5($userAgent); |
| 48 |
|
| 49 |
if (!isset(self::$instances[$signature])) { |
| 50 |
self::$instances[$signature] = new Browser($utility, $userAgent); |
| 51 |
} |
| 52 |
|
| 53 |
return self::$instances[$signature]; |
| 54 |
} |
| 55 |
|
| 56 |
public function getBrowser(): string |
| 57 |
{ |
| 58 |
return $this->oClient->browser ?? ''; |
| 59 |
} |
| 60 |
|
| 61 |
public function getVersion(): string |
| 62 |
{ |
| 63 |
return $this->oClient->browserVersion ?? ''; |
| 64 |
} |
| 65 |
} |
| 66 |
|