Mobi_Mtld_DA_ApiCached
in package
This class significantly improves core DA PHP Api performance when used on a standard website with a single detection per request.
Current optimizations consists of two main parts:
- tree optimizer - speeds up loading the json file
- cache provider - caches the results
Typical usage:
$ua = $_SERVER['HTTP_USER_AGENT']; $da_cache_provider = new Mobi_Mtld_DA_CacheProvider_FileCacheProvider(); $da_api_cached = new Mobi_Mtld_DA_ApiCached("json/sample.json", $da_cache_provider); $properties = $da_api_cached->getProperties($ua);
Client side properties
Client side properties can be collected and merged into the results by using the DeviceAtlas Javascript detection file. The results from the client side are sent to the server inside a cookie. The contents of this cookie can be passed to the DeviceAtlas getProperty and getProperties methods. The client side properties over-ride any data file properties and also serve as an input into additional logic to determine other properties such as the iPhone models that are otherwise not detectable. The following code shows how this can be done:
$ua = $_SERVER['HTTP_USER_AGENT']; // Get the cookie containing the client side properties $cookie_contents = null; if (isset($_COOKIE['DAPROPS'])){ $cookie_contents = $_COOKIE['DAPROPS']; } $da_cache_provider = new Mobi_Mtld_DA_CacheProvider_FileCacheProvider(); $da_api_cached = new Mobi_Mtld_DA_ApiCached("json/sample.json", $da_cache_provider); $properties = $da_api_cached->getProperties($ua, $cookie_contents);
Note:
It is not recommended to use ApiCached extension for batch processing (i.e. multiple User-Agent detections during a single request). In these situations use standard API interface.
See Api.php for more information
Tags
Table of Contents
- $config : mixed
- $deviceApi : mixed
- $pathToJson : mixed
- __construct() : mixed
- Creates new instance of Mobi_Mtld_DA_ApiCached object
- clearTreeOptimizerCache() : mixed
- Deletes all TreeOptimizer cache files
- getProperties() : array<string|int, mixed>
- Returns an array of known properties (as strings) for the user agent
- getProperty() : string
- Returns a value for the named property for this user agent
- populateTreeOptimizerCache() : mixed
- Automatically populate full TreeOptimizer cache
- setUseSysTempDir() : mixed
- DEPRECATED, not functional
Properties
$config
private
mixed
$config
$deviceApi
private
mixed
$deviceApi
$pathToJson
private
mixed
$pathToJson
Methods
__construct()
Creates new instance of Mobi_Mtld_DA_ApiCached object
public
__construct(string $pathToJson[, Mobi_Mtld_DA_CacheProvider_CacheProviderInterface $cacheProvider = null ][, bool $useTreeOptimizer = true ][, string $treeOptimizerCacheDir = null ][, bool $includeChangeableUserAgentProperties = true ]) : mixed
Parameters
- $pathToJson : string
-
The location of the file to read in.
- $cacheProvider : Mobi_Mtld_DA_CacheProvider_CacheProviderInterface = null
- $useTreeOptimizer : bool = true
- $treeOptimizerCacheDir : string = null
-
Cache directory for the tree optimizer; uses sys_get_temp_dir() by default
- $includeChangeableUserAgentProperties : bool = true
-
Also detect changeable user-agent properties
Return values
mixed —clearTreeOptimizerCache()
Deletes all TreeOptimizer cache files
public
clearTreeOptimizerCache() : mixed
Return values
mixed —getProperties()
Returns an array of known properties (as strings) for the user agent
public
getProperties(string $userAgent[, string|null $cookie = null ]) : array<string|int, mixed>
Parameters
- $userAgent : string
-
String from the device's User-Agent header
- $cookie : string|null = null
-
The contents of the cookie containing the client side properties
Tags
Return values
array<string|int, mixed> —properties Property name => Property value
getProperty()
Returns a value for the named property for this user agent
public
getProperty(string $userAgent, string $property[, string|null $cookie = null ]) : string
Parameters
- $userAgent : string
-
String from the device's User-Agent header
- $property : string
-
The name of the property to return
- $cookie : string|null = null
-
The contents of the cookie containing the client side properties
Tags
Return values
string —property
populateTreeOptimizerCache()
Automatically populate full TreeOptimizer cache
public
populateTreeOptimizerCache([mixed $force = false ]) : mixed
Parameters
- $force : mixed = false
Return values
mixed —setUseSysTempDir()
DEPRECATED, not functional
public
setUseSysTempDir(bool $useSysTempDir) : mixed
Parameters
- $useSysTempDir : bool