| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\DeviceDetector; |
|
|
| use DeviceDetector\ClientHints; |
| use DeviceDetector\DeviceDetector; |
| use Piwik\Container\StaticContainer; |
|
|
| class DeviceDetectorFactory |
| { |
| protected static $deviceDetectorInstances = []; |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function makeInstance($userAgent, array $clientHints = []) |
| { |
| $cacheKey = self::getNormalizedUserAgent($userAgent, $clientHints); |
|
|
| if (array_key_exists($cacheKey, self::$deviceDetectorInstances)) { |
| return self::$deviceDetectorInstances[$cacheKey]; |
| } |
|
|
| $deviceDetector = $this->getDeviceDetectionInfo($userAgent, $clientHints); |
|
|
| self::$deviceDetectorInstances[$cacheKey] = $deviceDetector; |
|
|
| return $deviceDetector; |
| } |
|
|
| public static function getNormalizedUserAgent($userAgent, array $clientHints = []) |
| { |
| $normalizedClientHints = ''; |
| if (is_array($clientHints) && count($clientHints)) { |
| $hints = ClientHints::factory($clientHints); |
| $brands = $hints->getBrandList(); |
| ksort($brands); |
|
|
| |
| |
| $normalizedClientHints = md5(json_encode($brands) . $hints->getOperatingSystem() . $hints->getOperatingSystemVersion() . $hints->getModel()); |
| } |
|
|
| return mb_substr($normalizedClientHints . trim($userAgent), 0, 500); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected function getDeviceDetectionInfo($userAgent, array $clientHints = []) |
| { |
| $deviceDetector = new DeviceDetector($userAgent, ClientHints::factory($clientHints)); |
| $deviceDetector->discardBotInformation(); |
| $deviceDetector->setCache(StaticContainer::get('DeviceDetector\Cache\Cache')); |
| $deviceDetector->parse(); |
| return $deviceDetector; |
| } |
|
|
| public static function clearInstancesCache() |
| { |
| self::$deviceDetectorInstances = []; |
| } |
| } |
|
|