PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/codes-php/phpjakarta/WindowsAzure/ServiceRuntime/Internal/XmlRoleEnvironmentDataDeserializer.php

http://bukuphpjs.codeplex.com
PHP | 309 lines | 177 code | 49 blank | 83 comment | 14 complexity | 94c049654bd84f15a6edbdb6fe4ac12c MD5 | raw file
Possible License(s): Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\ServiceRuntime\Internal
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\ServiceRuntime\Internal;
  24. use WindowsAzure\Common\Internal\Utilities;
  25. /**
  26. * The XML role environment data deserializer.
  27. *
  28. * @category Microsoft
  29. * @package WindowsAzure\ServiceRuntime\Internal
  30. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  31. * @copyright 2012 Microsoft Corporation
  32. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  33. * @version Release: @package_version@
  34. * @link https://github.com/windowsazure/azure-sdk-for-php
  35. */
  36. class XmlRoleEnvironmentDataDeserializer
  37. {
  38. /**
  39. * Deserializes the role environment data.
  40. *
  41. * @param IInputChannel $inputChannel The input Channel.
  42. *
  43. * @return RoleEnvironmentData
  44. */
  45. public function deserialize($inputChannel)
  46. {
  47. $document = stream_get_contents($inputChannel);
  48. $environmentInfo = Utilities::unserialize($document);
  49. $configurationSettings = $this->_translateConfigurationSettings(
  50. $environmentInfo
  51. );
  52. $localResources = $this->_translateLocalResources($environmentInfo);
  53. $currentInstance = $this
  54. ->_translateCurrentInstance($environmentInfo);
  55. $roles = $this->_translateRoles(
  56. $environmentInfo,
  57. $currentInstance,
  58. $environmentInfo['CurrentInstance']['@attributes']['roleName']
  59. );
  60. return new RoleEnvironmentData(
  61. $environmentInfo['Deployment']['@attributes']['id'],
  62. $configurationSettings,
  63. $localResources,
  64. $currentInstance,
  65. $roles,
  66. ($environmentInfo['Deployment']['@attributes']['emulated'] == 'true')
  67. );
  68. }
  69. /**
  70. * Translates the configuration settings.
  71. *
  72. * @param string $environmentInfo The role environment info.
  73. *
  74. * @return array
  75. */
  76. private function _translateConfigurationSettings($environmentInfo)
  77. {
  78. $configurationSettings = array();
  79. $settingsInfo = Utilities::tryGetKeysChainValue(
  80. $environmentInfo,
  81. 'CurrentInstance',
  82. 'ConfigurationSettings',
  83. 'ConfigurationSetting'
  84. );
  85. if (!is_null($settingsInfo)) {
  86. if (array_key_exists('@attributes', $settingsInfo)) {
  87. $settingsInfo = array(0 => $settingsInfo);
  88. }
  89. foreach ($settingsInfo as $settingInfo) {
  90. $configurationSettings
  91. [$settingInfo['@attributes']['name']] = $settingInfo
  92. ['@attributes']['value'];
  93. }
  94. }
  95. return $configurationSettings;
  96. }
  97. /**
  98. * Translates the local resources.
  99. *
  100. * @param string $environmentInfo The role environment info.
  101. *
  102. * @return array
  103. */
  104. private function _translateLocalResources($environmentInfo)
  105. {
  106. $localResourcesMap = array();
  107. $localResourcesInfo = Utilities::tryGetKeysChainValue(
  108. $environmentInfo,
  109. 'CurrentInstance',
  110. 'LocalResources',
  111. 'LocalResource'
  112. );
  113. if (!is_null($localResourcesInfo)) {
  114. if (array_key_exists('@attributes', $localResourcesInfo)) {
  115. $localResourcesInfo = array(0 => $localResourcesInfo);
  116. }
  117. foreach ($localResourcesInfo as $localResourceInfo) {
  118. $localResource = new LocalResource(
  119. $localResourceInfo['@attributes']['sizeInMB'],
  120. $localResourceInfo['@attributes']['name'],
  121. $localResourceInfo['@attributes']['path']
  122. );
  123. $localResourcesMap[$localResource->getName()] = $localResource;
  124. }
  125. }
  126. return $localResourcesMap;
  127. }
  128. /**
  129. * Translates the roles.
  130. *
  131. * @param string $environmentInfo The role environment info.
  132. * @param RoleInstance $currentInstance The current instance info.
  133. * @param string $currentRole The current role.
  134. *
  135. * @return array
  136. */
  137. private function _translateRoles($environmentInfo, $currentInstance,
  138. $currentRole
  139. ) {
  140. $rolesMap = array();
  141. $rolesInfo = Utilities::tryGetKeysChainValue(
  142. $environmentInfo,
  143. 'Roles',
  144. 'Role'
  145. );
  146. if (!is_null($rolesInfo)) {
  147. if (array_key_exists('@attributes', $rolesInfo)) {
  148. $rolesInfo = array(0 => $rolesInfo);
  149. }
  150. foreach ($rolesInfo as $roleInfo) {
  151. $roleInstances = $this->_translateRoleInstances($roleInfo);
  152. if ($roleInfo['@attributes']['name'] == $currentRole) {
  153. $roleInstances[$currentInstance->getId()] = $currentInstance;
  154. }
  155. $role = new Role($roleInfo['@attributes']['name'], $roleInstances);
  156. foreach ($roleInstances as $instance) {
  157. $instance->setRole($role);
  158. }
  159. $rolesMap[$roleInfo['@attributes']['name']] = $role;
  160. }
  161. }
  162. if (!array_key_exists($currentRole, $rolesMap)) {
  163. $roleInstances = array();
  164. $roleInstances[$currentInstance->getId()] = $currentInstance;
  165. $singleRole = new Role($currentRole, $roleInstances);
  166. $currentInstance->setRole($singleRole);
  167. $rolesMap[$currentRole] = $singleRole;
  168. }
  169. return $rolesMap;
  170. }
  171. /**
  172. * Translates the role instances.
  173. *
  174. * @param string $instancesInfo The instance info.
  175. *
  176. * @return array
  177. */
  178. private function _translateRoleInstances($instancesInfo)
  179. {
  180. $roleInstanceMap = array();
  181. $instances = Utilities::tryGetKeysChainValue(
  182. $instancesInfo,
  183. 'Instances',
  184. 'Instance'
  185. );
  186. if (!is_null($instances)) {
  187. if (array_key_exists('@attributes', $instances)) {
  188. $instances = array(0 => $instances);
  189. }
  190. foreach ($instances as $instanceInfo) {
  191. $endpoints = $this->_translateRoleInstanceEndpoints(
  192. $instanceInfo['Endpoints']['Endpoint']
  193. );
  194. $roleInstance = new RoleInstance(
  195. $instanceInfo['@attributes']['id'],
  196. $instanceInfo['@attributes']['faultDomain'],
  197. $instanceInfo['@attributes']['updateDomain'],
  198. $endpoints
  199. );
  200. $roleInstanceMap
  201. [$instanceInfo['@attributes']['id']] = $roleInstance;
  202. }
  203. }
  204. return $roleInstanceMap;
  205. }
  206. /**
  207. * Translates the role instance endpoints.
  208. *
  209. * @param string $endpointsInfo The endpoints info.
  210. *
  211. * @return array
  212. */
  213. private function _translateRoleInstanceEndpoints($endpointsInfo)
  214. {
  215. $endpointsMap = array();
  216. $endpoints = $endpointsInfo;
  217. if (array_key_exists('@attributes', $endpoints)) {
  218. $endpoints = array(0 => $endpointsInfo);
  219. }
  220. foreach ($endpoints as $endpoint) {
  221. $roleInstanceEndpoint = new RoleInstanceEndpoint(
  222. $endpoint['@attributes']['protocol'],
  223. $endpoint['@attributes']['address'],
  224. intval($endpoint['@attributes']['port'], 10)
  225. );
  226. $endpointsMap[$endpoint['@attributes']['name']] = $roleInstanceEndpoint;
  227. }
  228. return $endpointsMap;
  229. }
  230. /**
  231. * Translates the current instance info.
  232. *
  233. * @param string $environmentInfo The environment info.
  234. *
  235. * @return RoleInstance
  236. */
  237. private function _translateCurrentInstance($environmentInfo)
  238. {
  239. $endpoints = array();
  240. $endpointsInfo = Utilities::tryGetKeysChainValue(
  241. $environmentInfo,
  242. 'CurrentInstance',
  243. 'Endpoints',
  244. 'Endpoint'
  245. );
  246. if (!is_null($endpointsInfo)) {
  247. $endpoints = $this->_translateRoleInstanceEndpoints($endpointsInfo);
  248. }
  249. $currentInstance = new RoleInstance(
  250. $environmentInfo['CurrentInstance']['@attributes']['id'],
  251. $environmentInfo['CurrentInstance']['@attributes']['faultDomain'],
  252. $environmentInfo['CurrentInstance']['@attributes']['updateDomain'],
  253. $endpoints
  254. );
  255. foreach ($currentInstance->getInstanceEndpoints() as $endpoint) {
  256. $endpoint->setRoleInstance($currentInstance);
  257. }
  258. return $currentInstance;
  259. }
  260. }