forked from matomo-org/plugin-DeviceDetectorCache
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFactory.php
More file actions
52 lines (42 loc) · 1.52 KB
/
Factory.php
File metadata and controls
52 lines (42 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DynamicDeviceDetectorCache;
use Piwik\Cache;
use Piwik\Common;
use Piwik\DeviceDetector\DeviceDetectorFactory;
use Piwik\Version;
class Factory extends DeviceDetectorFactory
{
private $config;
public function __construct()
{
$this->config = new Configuration();
}
protected function getDeviceDetectionInfo($userAgent, array $clientHints = [])
{
$lazyCache = Cache::getLazyCache();
$userAgent = self::getNormalizedUserAgent($userAgent, $clientHints);
$cacheKey = "ua." . Version::VERSION . '.' . sha1($userAgent);
// check if a compatible device detector is in lazy cache
$serialized = $lazyCache->fetch($cacheKey);
if ($serialized !== false) {
// if we find a detector, deserialize it
$cdd = Common::safe_unserialize($serialized, true);
if (isset($cdd) && $cdd !== FALSE) {
return $cdd;
}
}
// parse usr agent.
$deviceDetector = parent::getDeviceDetectionInfo($userAgent, $clientHints);
# remove parsers & caches from device detector
# and serialize it into cache.
$serialized = serialize(new SerializableDeviceDetector($deviceDetector));
$lazyCache->save($cacheKey, $serialized, $this->config->getCacheTTLInSeconds());
return $deviceDetector;
}
}