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

/src/Common/Internal/StorageServiceSettings.php

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