DeviceAtlas Device API
 All Classes Namespaces Functions Variables
Public Member Functions | List of all members
Mobi_Mtld_DA_ApiCached Class Reference

Public Member Functions

 __construct ($pathToJson, Mobi_Mtld_DA_CacheProvider_CacheProviderInterface $cacheProvider=null, $useTreeOptimizer=true, $treeOptimizerCacheDir=null, $includeChangeableUserAgentProperties=true)
 getProperties ($userAgent, $cookie=null)
 getProperty ($userAgent, $property, $cookie=null)
 setUseSysTempDir ($useSysTempDir)
 clearTreeOptimizerCache ()
 populateTreeOptimizerCache ($force=false)

Detailed Description

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:

1) tree optimizer - speeds up loading the json file
2) 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

Author
dotMobi

Constructor & Destructor Documentation

Mobi_Mtld_DA_ApiCached::__construct (   $pathToJson,
Mobi_Mtld_DA_CacheProvider_CacheProviderInterface  $cacheProvider = null,
  $useTreeOptimizer = true,
  $treeOptimizerCacheDir = null,
  $includeChangeableUserAgentProperties = true 
)

Creates new instance of Mobi_Mtld_DA_ApiCached object

Parameters
string$pathToJsonThe location of the file to read in.
Mobi_Mtld_DA_CacheProvider_CacheProviderInterface$cacheProvider
boolean$useTreeOptimizer
string$treeOptimizerCacheDirCache directory for the tree optimizer; uses sys_get_temp_dir() by default
boolean$includeChangeableUserAgentPropertiesAlso detect changeable user-agent properties

Member Function Documentation

Mobi_Mtld_DA_ApiCached::clearTreeOptimizerCache ( )

Deletes all TreeOptimizer cache files

Mobi_Mtld_DA_ApiCached::getProperties (   $userAgent,
  $cookie = null 
)

Returns an array of known properties (as strings) for the user agent

Parameters
string$userAgentString from the device's User-Agent header
string | null$cookieThe contents of the cookie containing the client side properties
Returns
array properties Property name => Property value
Exceptions
Mobi_Mtld_Da_Exception_JsonException
Mobi_Mtld_DA_ApiCached::getProperty (   $userAgent,
  $property,
  $cookie = null 
)

Returns a value for the named property for this user agent

Parameters
string$userAgentString from the device's User-Agent header
string$propertyThe name of the property to return
string | null$cookieThe contents of the cookie containing the client side properties
Returns
string property
Exceptions
Mobi_Mtld_Da_Exception_UnknownPropertyException
Mobi_Mtld_Da_Exception_InvalidPropertyException
Mobi_Mtld_Da_Exception_JsonException
Mobi_Mtld_DA_ApiCached::populateTreeOptimizerCache (   $force = false)

Automatically populate full TreeOptimizer cache

Mobi_Mtld_DA_ApiCached::setUseSysTempDir (   $useSysTempDir)

DEPRECATED, not functional

Parameters
boolean$useSysTempDir

The documentation for this class was generated from the following file: