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