PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/manager/application/libraries/wurfl/WURFL/CustomDeviceRepository.php

https://bitbucket.org/jerwinse/iagh-cms
PHP | 252 lines | 121 code | 20 blank | 111 comment | 16 complexity | 87ee520c796adb5ef81c6b8cec07542a MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2012 ScientiaMobile, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * Refer to the COPYING.txt file distributed with this package.
  11. *
  12. * @category WURFL
  13. * @package WURFL
  14. * @copyright ScientiaMobile, Inc.
  15. * @license GNU Affero General Public License
  16. * @version $id$
  17. *
  18. */
  19. /**
  20. * WURFL Device Repository
  21. * @package WURFL
  22. */
  23. class WURFL_CustomDeviceRepository implements WURFL_DeviceRepository {
  24. const WURFL_USER_AGENTS_CLASSIFIED = "WURFL_USER_AGENTS_CLASSIFIED";
  25. /**
  26. * The persistence provider for this device repository
  27. * @var WURFL_Storage_Base
  28. */
  29. private $persistenceStorage;
  30. /**
  31. * @var array
  32. */
  33. private $deviceClassificationNames;
  34. /**
  35. * Map of groupID => array(capabilitiesNames)
  36. * @var array
  37. */
  38. private $_groupIDCapabilitiesMap = array();
  39. /**
  40. * @var array
  41. */
  42. private $_capabilitiesName = array();
  43. /**
  44. * @var array
  45. */
  46. private $_deviceCache = array();
  47. /**
  48. * Creates a new Device Repository from the given $persistenceStorage and $deviceClassificationNames
  49. * @param WURFL_Storage_Base $persistenceStorage
  50. * @param array $deviceClassificationNames
  51. * @throws InvalidArgumentException
  52. */
  53. public function __construct($persistenceStorage, $deviceClassificationNames) {
  54. if (is_null($persistenceStorage)) {
  55. throw new InvalidArgumentException("Persistence Provider cannot be null");
  56. }
  57. $this->persistenceStorage = $persistenceStorage;
  58. $this->deviceClassificationNames = $deviceClassificationNames;
  59. $this->init();
  60. }
  61. /**
  62. * Initializes this device repository by loading the base generic device capabilities names and group ID map
  63. */
  64. private function init() {
  65. $genericDevice = $this->getDevice(WURFL_Constants::GENERIC);
  66. if (!is_null($genericDevice)) {
  67. $this->_capabilitiesName = array_keys($genericDevice->getCapabilities());
  68. $this->_groupIDCapabilitiesMap = $genericDevice->getGroupIdCapabilitiesNameMap();
  69. }
  70. }
  71. public function getWURFLInfo() {
  72. $wurflInfo = $this->persistenceStorage->load(WURFL_Xml_Info::PERSISTENCE_KEY);
  73. if ($wurflInfo != null) {
  74. return $wurflInfo;
  75. }
  76. return WURFL_Xml_Info::noInfo();
  77. }
  78. public function getVersion() {
  79. return $this->getWURFLInfo()->version;
  80. }
  81. public function getLastUpdated() {
  82. return $this->getWURFLInfo()->lastUpdated;
  83. }
  84. /**
  85. * Returns a device for the given device ID
  86. *
  87. * @param string $deviceId
  88. * @return WURFL_CustomDevice
  89. * @throws WURFL_Exception if $deviceID is not defined in wurfl devices repository
  90. */
  91. public function getDevice($deviceId) {
  92. //echo "Trying to find Device ID $deviceId\n";
  93. if (!isset($this->_deviceCache[$deviceId])) {
  94. $device = $this->persistenceStorage->load($deviceId);
  95. if (is_null($device)) {
  96. throw new Exception("There is no device with ID [$deviceId] in the loaded WURFL Data");
  97. }
  98. $this->_deviceCache[$deviceId] = $device;
  99. }
  100. return $this->_deviceCache[$deviceId];
  101. }
  102. /**
  103. * Returns all devices in the repository
  104. * @return array
  105. */
  106. public function getAllDevices() {
  107. $devices = array();
  108. $devicesId = $this->getAllDevicesID();
  109. foreach($devicesId as $deviceId) {
  110. $devices[] = $this->getDevice($deviceId);
  111. }
  112. return $devices;
  113. }
  114. /**
  115. * Returns an array of all the device ids
  116. * @return array
  117. */
  118. public function getAllDevicesID() {
  119. $devicesId = array();
  120. foreach($this->deviceClassificationNames as $className) {
  121. $currentMap = $this->persistenceStorage->load($className);
  122. if (!is_array($currentMap)) {
  123. $currentMap = array ();
  124. }
  125. $devicesId = array_merge($devicesId, array_values($currentMap));
  126. }
  127. return $devicesId;
  128. }
  129. /**
  130. * Returns the value for the given $deviceId and $capabilityName
  131. *
  132. * @param string $deviceId
  133. * @param string $capabilityName
  134. * @throws WURFL_WURFLException device ID or capability was not found
  135. * @return string value
  136. */
  137. public function getCapabilityForDevice($deviceId, $capabilityName) {
  138. if (! $this->isCapabilityDefined($capabilityName)) {
  139. throw new WURFL_WURFLException("capability name: " . $capabilityName . " not found");
  140. }
  141. $capabilityValue = null;
  142. // TODO: Prevent infinite recursion
  143. while (strcmp($deviceId, "root")) {
  144. $device = $this->persistenceStorage->load($deviceId);
  145. if (!$device) {
  146. throw new WURFL_WURFLException("the device with $deviceId is not found.");
  147. }
  148. if (isset($device->capabilities[$capabilityName])) {
  149. $capabilityValue = $device->capabilities[$capabilityName];
  150. break;
  151. }
  152. $deviceId = $device->fallBack;
  153. }
  154. return $capabilityValue;
  155. }
  156. /**
  157. * Checks if the capability name specified by $capability is defined in the repository
  158. *
  159. * @param string $capability
  160. * @return bool
  161. */
  162. private function isCapabilityDefined($capability) {
  163. return in_array($capability, $this->_capabilitiesName);
  164. }
  165. /**
  166. * Returns an associative array of capabilityName => capabilityValue for the given $deviceID
  167. *
  168. * @param string $deviceID
  169. * @return array associative array of capabilityName, capabilityValue
  170. */
  171. public function getAllCapabilitiesForDevice($deviceID) {
  172. $devices = array_reverse($this->getDeviceHierarchy($deviceID));
  173. $capabilities = array();
  174. foreach ($devices as $device) {
  175. if (is_array($device->capabilities)) {
  176. $capabilities = array_merge($capabilities, $device->capabilities);
  177. }
  178. }
  179. return $capabilities;
  180. }
  181. /**
  182. * Returns an array containing all devices from the root
  183. * device to the device of the given $deviceId
  184. *
  185. * @param string $deviceId
  186. * @return array All WURFL_Device objects in the fallback tree
  187. */
  188. public function getDeviceHierarchy($deviceId) {
  189. $devices = array();
  190. while (strcmp($deviceId, "root")) {
  191. $device = $this->getDevice($deviceId);
  192. $devices[] = $device;
  193. $deviceId = $device->fallBack;
  194. }
  195. return $devices;
  196. }
  197. /**
  198. * Returns an array Of group IDs defined in wurfl
  199. *
  200. * @return array
  201. */
  202. public function getListOfGroups() {
  203. return array_keys($this->_groupIDCapabilitiesMap);
  204. }
  205. /**
  206. * Returns an array of all capability names defined in
  207. * the given group ID
  208. *
  209. * @param string $groupID
  210. * @throws WURFL_WURFLException The given $groupID does not exist
  211. * @return array of capability names
  212. */
  213. public function getCapabilitiesNameForGroup($groupID) {
  214. if (!array_key_exists($groupID, $this->_groupIDCapabilitiesMap)) {
  215. throw new WURFL_WURFLException("The Group ID " . $groupID . " supplied does not exist");
  216. }
  217. return $this->_groupIDCapabilitiesMap [$groupID];
  218. }
  219. /**
  220. * Returns the group id that contains the given $capability
  221. *
  222. * @param string $capability
  223. * @throws InvalidArgumentException an invalid $capability was specified
  224. * @return string
  225. */
  226. public function getGroupIDForCapability($capability) {
  227. if (!isset($capability) || !array_key_exists($capability, $this->_groupIDCapabilitiesMap)) {
  228. throw new InvalidArgumentException("an invalid capability was specified.");
  229. }
  230. return $this->_groupIDCapabilitiesMap[$capability];
  231. }
  232. }