PageRenderTime 82ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/WindowsAzure/Common/Internal/StorageServiceSettings.php

https://bitbucket.org/skudatech/azure-sdk-for-php
PHP | 485 lines | 241 code | 50 blank | 194 comment | 7 complexity | 12b6c3d915837724eb070b169831b3e2 MD5 | raw file
  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\Common\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\Common\Internal;
  24. use WindowsAzure\Common\Internal\ConnectionStringParser;
  25. use WindowsAzure\Common\Internal\Resources;
  26. /**
  27. * Represents the settings used to sign and access a request against the storage
  28. * service. For more information about storage service connection strings check this
  29. * page: http://msdn.microsoft.com/en-us/library/ee758697
  30. *
  31. * @category Microsoft
  32. * @package WindowsAzure\Common\Internal
  33. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  34. * @copyright 2012 Microsoft Corporation
  35. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  36. * @version Release: @package_version@
  37. * @link https://github.com/windowsazure/azure-sdk-for-php
  38. */
  39. class StorageServiceSettings extends ServiceSettings
  40. {
  41. /**
  42. * The storage service name.
  43. *
  44. * @var string
  45. */
  46. private $_name;
  47. /**
  48. * A base64 representation.
  49. *
  50. * @var string
  51. */
  52. private $_key;
  53. /**
  54. * The endpoint for the blob service.
  55. *
  56. * @var string
  57. */
  58. private $_blobEndpointUri;
  59. /**
  60. * The endpoint for the queue service.
  61. *
  62. * @var string
  63. */
  64. private $_queueEndpointUri;
  65. /**
  66. * The endpoint for the table service.
  67. *
  68. * @var string
  69. */
  70. private $_tableEndpointUri;
  71. /**
  72. * @var StorageServiceSettings
  73. */
  74. private static $_devStoreAccount;
  75. /**
  76. * Validator for the UseDevelopmentStorage setting. Must be "true".
  77. *
  78. * @var array
  79. */
  80. private static $_useDevelopmentStorageSetting;
  81. /**
  82. * Validator for the DevelopmentStorageProxyUri setting. Must be a valid Uri.
  83. *
  84. * @var array
  85. */
  86. private static $_developmentStorageProxyUriSetting;
  87. /**
  88. * Validator for the DefaultEndpointsProtocol setting. Must be either "http"
  89. * or "https".
  90. *
  91. * @var array
  92. */
  93. private static $_defaultEndpointsProtocolSetting;
  94. /**
  95. * Validator for the AccountName setting. No restrictions.
  96. *
  97. * @var array
  98. */
  99. private static $_accountNameSetting;
  100. /**
  101. * Validator for the AccountKey setting. Must be a valid base64 string.
  102. *
  103. * @var array
  104. */
  105. private static $_accountKeySetting;
  106. /**
  107. * Validator for the BlobEndpoint setting. Must be a valid Uri.
  108. *
  109. * @var array
  110. */
  111. private static $_blobEndpointSetting;
  112. /**
  113. * Validator for the QueueEndpoint setting. Must be a valid Uri.
  114. *
  115. * @var array
  116. */
  117. private static $_queueEndpointSetting;
  118. /**
  119. * Validator for the TableEndpoint setting. Must be a valid Uri.
  120. *
  121. * @var array
  122. */
  123. private static $_tableEndpointSetting;
  124. /**
  125. * @var boolean
  126. */
  127. protected static $isInitialized = false;
  128. /**
  129. * Holds the expected setting keys.
  130. *
  131. * @var array
  132. */
  133. protected static $validSettingKeys = array();
  134. /**
  135. * Initializes static members of the class.
  136. *
  137. * @return none
  138. */
  139. protected static function init()
  140. {
  141. self::$_useDevelopmentStorageSetting = self::setting(
  142. Resources::USE_DEVELOPMENT_STORAGE_NAME,
  143. 'true'
  144. );
  145. self::$_developmentStorageProxyUriSetting = self::settingWithFunc(
  146. Resources::DEVELOPMENT_STORAGE_PROXY_URI_NAME,
  147. Validate::getIsValidUri()
  148. );
  149. self::$_defaultEndpointsProtocolSetting = self::setting(
  150. Resources::DEFAULT_ENDPOINTS_PROTOCOL_NAME,
  151. 'http', 'https'
  152. );
  153. self::$_accountNameSetting = self::setting(Resources::ACCOUNT_NAME_NAME);
  154. self::$_accountKeySetting = self::settingWithFunc(
  155. Resources::ACCOUNT_KEY_NAME,
  156. // base64_decode will return false if the $key is not in base64 format.
  157. function ($key) {
  158. $isValidBase64String = base64_decode($key, true);
  159. if ($isValidBase64String) {
  160. return true;
  161. } else {
  162. throw new \RuntimeException(
  163. sprintf(Resources::INVALID_ACCOUNT_KEY_FORMAT, $key)
  164. );
  165. }
  166. }
  167. );
  168. self::$_blobEndpointSetting = self::settingWithFunc(
  169. Resources::BLOB_ENDPOINT_NAME,
  170. Validate::getIsValidUri()
  171. );
  172. self::$_queueEndpointSetting = self::settingWithFunc(
  173. Resources::QUEUE_ENDPOINT_NAME,
  174. Validate::getIsValidUri()
  175. );
  176. self::$_tableEndpointSetting = self::settingWithFunc(
  177. Resources::TABLE_ENDPOINT_NAME,
  178. Validate::getIsValidUri()
  179. );
  180. self::$validSettingKeys[] = Resources::USE_DEVELOPMENT_STORAGE_NAME;
  181. self::$validSettingKeys[] = Resources::DEVELOPMENT_STORAGE_PROXY_URI_NAME;
  182. self::$validSettingKeys[] = Resources::DEFAULT_ENDPOINTS_PROTOCOL_NAME;
  183. self::$validSettingKeys[] = Resources::ACCOUNT_NAME_NAME;
  184. self::$validSettingKeys[] = Resources::ACCOUNT_KEY_NAME;
  185. self::$validSettingKeys[] = Resources::BLOB_ENDPOINT_NAME;
  186. self::$validSettingKeys[] = Resources::QUEUE_ENDPOINT_NAME;
  187. self::$validSettingKeys[] = Resources::TABLE_ENDPOINT_NAME;
  188. }
  189. /**
  190. * Creates new storage service settings instance.
  191. *
  192. * @param string $name The storage service name.
  193. * @param string $key The storage service key.
  194. * @param string $blobEndpointUri The sotrage service blob endpoint.
  195. * @param string $queueEndpointUri The sotrage service queue endpoint.
  196. * @param string $tableEndpointUri The sotrage service table endpoint.
  197. */
  198. public function __construct(
  199. $name,
  200. $key,
  201. $blobEndpointUri,
  202. $queueEndpointUri,
  203. $tableEndpointUri
  204. ) {
  205. $this->_name = $name;
  206. $this->_key = $key;
  207. $this->_blobEndpointUri = $blobEndpointUri;
  208. $this->_queueEndpointUri = $queueEndpointUri;
  209. $this->_tableEndpointUri = $tableEndpointUri;
  210. }
  211. /**
  212. * Returns a StorageServiceSettings with development storage credentials using
  213. * the specified proxy Uri.
  214. *
  215. * @param string $proxyUri The proxy endpoint to use.
  216. *
  217. * @return StorageServiceSettings
  218. */
  219. private static function _getDevelopmentStorageAccount($proxyUri)
  220. {
  221. if (is_null($proxyUri)) {
  222. return self::developmentStorageAccount();
  223. }
  224. $scheme = parse_url($proxyUri, PHP_URL_SCHEME);
  225. $host = parse_url($proxyUri, PHP_URL_HOST);
  226. $prefix = $scheme . "://" . $host;
  227. return new StorageServiceSettings(
  228. Resources::DEV_STORE_NAME,
  229. Resources::DEV_STORE_KEY,
  230. $prefix . ':10000/devstoreaccount1/',
  231. $prefix . ':10001/devstoreaccount1/',
  232. $prefix . ':10002/devstoreaccount1/'
  233. );
  234. }
  235. /**
  236. * Gets a StorageServiceSettings object that references the development storage
  237. * account.
  238. *
  239. * @return StorageServiceSettings
  240. */
  241. public static function developmentStorageAccount()
  242. {
  243. if (is_null(self::$_devStoreAccount)) {
  244. self::$_devStoreAccount = self::_getDevelopmentStorageAccount(
  245. Resources::DEV_STORE_URI
  246. );
  247. }
  248. return self::$_devStoreAccount;
  249. }
  250. /**
  251. * Gets the default service endpoint using the specified protocol and account
  252. * name.
  253. *
  254. * @param array $settings The service settings.
  255. * @param string $dns The service DNS.
  256. *
  257. * @return string
  258. */
  259. private static function _getDefaultServiceEndpoint($settings, $dns)
  260. {
  261. $scheme = Utilities::tryGetValueInsensitive(
  262. Resources::DEFAULT_ENDPOINTS_PROTOCOL_NAME,
  263. $settings
  264. );
  265. $accountName = Utilities::tryGetValueInsensitive(
  266. Resources::ACCOUNT_NAME_NAME,
  267. $settings
  268. );
  269. return sprintf(Resources::SERVICE_URI_FORMAT, $scheme, $accountName, $dns);
  270. }
  271. /**
  272. * Creates StorageServiceSettings object given endpoints uri.
  273. *
  274. * @param array $settings The service settings.
  275. * @param string $blobEndpointUri The blob endpoint uri.
  276. * @param string $queueEndpointUri The queue endpoint uri.
  277. * @param string $tableEndpointUri The table endpoint uri.
  278. *
  279. * @return \WindowsAzure\Common\Internal\StorageServiceSettings
  280. */
  281. private static function _createStorageServiceSettings(
  282. $settings,
  283. $blobEndpointUri = null,
  284. $queueEndpointUri = null,
  285. $tableEndpointUri = null
  286. ) {
  287. $blobEndpointUri = Utilities::tryGetValueInsensitive(
  288. Resources::BLOB_ENDPOINT_NAME,
  289. $settings,
  290. $blobEndpointUri
  291. );
  292. $queueEndpointUri = Utilities::tryGetValueInsensitive(
  293. Resources::QUEUE_ENDPOINT_NAME,
  294. $settings,
  295. $queueEndpointUri
  296. );
  297. $tableEndpointUri = Utilities::tryGetValueInsensitive(
  298. Resources::TABLE_ENDPOINT_NAME,
  299. $settings,
  300. $tableEndpointUri
  301. );
  302. $accountName = Utilities::tryGetValueInsensitive(
  303. Resources::ACCOUNT_NAME_NAME,
  304. $settings
  305. );
  306. $accountKey = Utilities::tryGetValueInsensitive(
  307. Resources::ACCOUNT_KEY_NAME,
  308. $settings
  309. );
  310. return new StorageServiceSettings(
  311. $accountName,
  312. $accountKey,
  313. $blobEndpointUri,
  314. $queueEndpointUri,
  315. $tableEndpointUri
  316. );
  317. }
  318. /**
  319. * Creates a StorageServiceSettings object from the given connection string.
  320. *
  321. * @param string $connectionString The storage settings connection string.
  322. *
  323. * @return StorageServiceSettings
  324. */
  325. public static function createFromConnectionString($connectionString)
  326. {
  327. $tokenizedSettings = self::parseAndValidateKeys($connectionString);
  328. // Devstore case
  329. $matchedSpecs = self::matchedSpecification(
  330. $tokenizedSettings,
  331. self::allRequired(self::$_useDevelopmentStorageSetting),
  332. self::optional(self::$_developmentStorageProxyUriSetting)
  333. );
  334. if ($matchedSpecs) {
  335. $proxyUri = Utilities::tryGetValueInsensitive(
  336. Resources::DEVELOPMENT_STORAGE_PROXY_URI_NAME,
  337. $tokenizedSettings
  338. );
  339. return self::_getDevelopmentStorageAccount($proxyUri);
  340. }
  341. // Automatic case
  342. $matchedSpecs = self::matchedSpecification(
  343. $tokenizedSettings,
  344. self::allRequired(
  345. self::$_defaultEndpointsProtocolSetting,
  346. self::$_accountNameSetting,
  347. self::$_accountKeySetting
  348. ),
  349. self::optional(
  350. self::$_blobEndpointSetting,
  351. self::$_queueEndpointSetting,
  352. self::$_tableEndpointSetting
  353. )
  354. );
  355. if ($matchedSpecs) {
  356. return self::_createStorageServiceSettings(
  357. $tokenizedSettings,
  358. self::_getDefaultServiceEndpoint(
  359. $tokenizedSettings,
  360. Resources::BLOB_BASE_DNS_NAME
  361. ),
  362. self::_getDefaultServiceEndpoint(
  363. $tokenizedSettings,
  364. Resources::QUEUE_BASE_DNS_NAME
  365. ),
  366. self::_getDefaultServiceEndpoint(
  367. $tokenizedSettings,
  368. Resources::TABLE_BASE_DNS_NAME
  369. )
  370. );
  371. }
  372. // Explicit case
  373. $matchedSpecs = self::matchedSpecification(
  374. $tokenizedSettings,
  375. self::atLeastOne(
  376. self::$_blobEndpointSetting,
  377. self::$_queueEndpointSetting,
  378. self::$_tableEndpointSetting
  379. ),
  380. self::allRequired(
  381. self::$_accountNameSetting,
  382. self::$_accountKeySetting
  383. )
  384. );
  385. if ($matchedSpecs) {
  386. return self::_createStorageServiceSettings($tokenizedSettings);
  387. }
  388. self::noMatch($connectionString);
  389. }
  390. /**
  391. * Gets storage service name.
  392. *
  393. * @return string
  394. */
  395. public function getName()
  396. {
  397. return $this->_name;
  398. }
  399. /**
  400. * Gets storage service key.
  401. *
  402. * @return string
  403. */
  404. public function getKey()
  405. {
  406. return $this->_key;
  407. }
  408. /**
  409. * Gets storage service blob endpoint uri.
  410. *
  411. * @return string
  412. */
  413. public function getBlobEndpointUri()
  414. {
  415. return $this->_blobEndpointUri;
  416. }
  417. /**
  418. * Gets storage service queue endpoint uri.
  419. *
  420. * @return string
  421. */
  422. public function getQueueEndpointUri()
  423. {
  424. return $this->_queueEndpointUri;
  425. }
  426. /**
  427. * Gets storage service table endpoint uri.
  428. *
  429. * @return string
  430. */
  431. public function getTableEndpointUri()
  432. {
  433. return $this->_tableEndpointUri;
  434. }
  435. }