Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit c727d72

Browse files
author
Brian Retterer
committed
Merge branch 'fervo-feature/psr-7' into dev
2 parents 72384b3 + f0800d6 commit c727d72

20 files changed

+547
-980
lines changed

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,20 @@
3737
},
3838
"require": {
3939
"php": ">=5.5",
40-
"guzzle/guzzle": "3.9.*",
4140
"firebase/php-jwt": "2.2.*",
4241
"phpseclib/phpseclib": "0.3.*|~1.0|~2.0",
4342
"cache/taggable-cache": "0.4.*",
4443
"cache/redis-adapter": "0.4.*",
4544
"cache/memcached-adapter": "0.3.*",
4645
"cache/array-adapter": "0.4.*",
47-
"cache/void-adapter": "0.3.*"
46+
"cache/void-adapter": "0.3.*",
47+
"guzzlehttp/psr7": "^1.3",
48+
"psr/http-message": "^1.0",
49+
"php-http/httplug": "^1.0",
50+
"php-http/discovery": "^1.0",
51+
"php-http/curl-client": "^1.0",
52+
"php-http/message": "^1.3",
53+
"php-http/client-common": "^1.2"
4854
},
4955
"require-dev": {
5056
"phpunit/phpunit": "4.*",

src/Client.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@
1818
* limitations under the License.
1919
*/
2020

21+
use Cache\Taggable\TaggablePSR6PoolAdapter;
22+
use Http\Client\HttpClient;
23+
use Http\Message\MessageFactory;
24+
use Http\Message\UriFactory;
2125
use Stormpath\Cache\CacheManager;
22-
use Stormpath\Cache\PSR6CacheManagerInterface;
2326
use Stormpath\Cache\CachePSR6Adapter;
27+
use Stormpath\Cache\PSR6CacheManagerInterface;
2428
use Stormpath\DataStore\DefaultDataStore;
2529
use Stormpath\Exceptions\Cache\InvalidCacheManagerException;
2630
use Stormpath\Exceptions\Cache\InvalidLegacyCacheManagerException;
27-
use Stormpath\Http\Authc\RequestSigner;
28-
use Stormpath\Http\HttpClientRequestExecutor;
31+
use Stormpath\Http\Authc\SAuthc1Authentication;
32+
use Stormpath\Http\Authc\StormpathBasicAuthentication;
2933
use Stormpath\Resource\Resource;
3034
use Stormpath\Stormpath;
3135
use Stormpath\Util\Magic;
32-
use Cache\Taggable\TaggablePSR6PoolAdapter;
36+
use Stormpath\Http\Authc\SAuthc1RequestSigner;
37+
use Stormpath\Http\Authc\BasicRequestSigner;
3338

3439
function toObject($properties)
3540
{
@@ -111,14 +116,12 @@ class Client extends Magic
111116
* @param $baseUrl optional parameter for specifying the base URL when not using the default one
112117
* (https://api.stormpath.com/v1).
113118
*/
114-
public function __construct(ApiKey $apiKey, $cacheManager, $cacheManagerOptions, $baseUrl = null, RequestSigner $requestSigner = null)
119+
public function __construct(ApiKey $apiKey, $cacheManager, $cacheManagerOptions, $baseUrl = null, RequestSigner $requestSigner = null, $authenticationScheme = Stormpath::SAUTHC1_AUTHENTICATION_SCHEME, HttpClient $httpClient = null, MessageFactory $messageFactory = null, UriFactory $uriFactory = null)
115120
{
116121
parent::__construct();
117122
self::$cacheManager = $cacheManager;
118123
self::$cacheManagerOptions = $cacheManagerOptions;
119124

120-
$requestExecutor = new HttpClientRequestExecutor($requestSigner);
121-
122125
if (is_string($cacheManager)) { // Legacy cache manager
123126
$legacyCache = new $cacheManager($cacheManagerOptions);
124127

@@ -137,7 +140,26 @@ public function __construct(ApiKey $apiKey, $cacheManager, $cacheManagerOptions,
137140
}
138141

139142
$this->cachePool = TaggablePSR6PoolAdapter::makeTaggable($cache);
140-
$this->dataStore = new DefaultDataStore($requestExecutor, $apiKey, $this->cachePool, $baseUrl);
143+
144+
if (!$requestSigner) {
145+
if ($authenticationScheme === Stormpath::SAUTHC1_AUTHENTICATION_SCHEME) {
146+
$auth = new SAuthc1Authentication($apiKey);
147+
} elseif ($authenticationScheme === Stormpath::BASIC_AUTHENTICATION_SCHEME) {
148+
$auth = new StormpathBasicAuthentication($apiKey);
149+
} else {
150+
throw new InvalidArgumentException("Unknown authentication scheme \"" . $authenticationScheme . "\"");
151+
}
152+
} else {
153+
if ($requestSigner instanceOf SAuthc1RequestSigner) {
154+
$auth = new SAuthc1Authentication($apiKey);
155+
} elseif ($requestSigner instanceOf BasicRequestSigner) {
156+
$auth = new StormpathBasicAuthentication($apiKey);
157+
} else {
158+
throw new InvalidArgumentException("Unknown RequestSigner \"" . get_class($requestSigner) . "\" passed. Instead of passing a request signer, pass the \$authenticationScheme parameter.");
159+
}
160+
}
161+
162+
$this->dataStore = new DefaultDataStore($apiKey, $auth, $this->cachePool, $httpClient, $messageFactory, $uriFactory, $baseUrl);
141163
}
142164

143165
public static function get($href, $className, $path = null, array $options = array())

src/ClientBuilder.php

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@
2020
use Stormpath\Cache\ArrayCacheManager;
2121
use Stormpath\Cache\MemcachedCacheManager;
2222
use Stormpath\Cache\NullCacheManager;
23-
use Stormpath\Cache\RedisCacheManager;
2423
use Stormpath\Cache\PSR6CacheManagerInterface;
25-
use Stormpath\Cache\Exceptions\InvalidCacheManagerException;
26-
use Stormpath\Http\Authc\SAuthc1RequestSigner;
24+
use Stormpath\Cache\RedisCacheManager;
2725
use Stormpath\Http\DefaultRequest;
2826
use Stormpath\Http\HttpClientRequestExecutor;
2927
use Stormpath\Http\Request;
3028
use Stormpath\Util\Magic;
31-
use Stormpath\Util\Spyc;
32-
use Stormpath\Util\YAMLUtil;
3329

3430
/**
3531
* A <a href="http://en.wikipedia.org/wiki/Builder_pattern">Builder design pattern</a> implementation used to
@@ -57,7 +53,7 @@ class ClientBuilder extends Magic
5753
private $apiKeySecretPropertyName = "apiKey.secret";
5854
private $apiKeyProperties;
5955
private $apiKeyFileLocation;
60-
private $cacheManager = NULL;
56+
private $cacheManager = null;
6157
private $cacheManagerOptions = array();
6258
private $baseURL;
6359
private $authenticationScheme = Stormpath::SAUTHC1_AUTHENTICATION_SCHEME;
@@ -216,7 +212,7 @@ public function setApiKeyProperties($apiKeyProperties)
216212

217213
public function setCacheManager($cacheManager)
218214
{
219-
if ($cacheManager instanceOf PSR6CacheManagerInterface) {
215+
if ($cacheManager instanceof PSR6CacheManagerInterface) {
220216
$this->cacheManager = $cacheManager;
221217
} else {
222218
switch ($cacheManager) {
@@ -240,10 +236,10 @@ public function setCacheManager($cacheManager)
240236
return $this;
241237
}
242238

243-
public function setCacheManagerOptions(Array $cacheManagerOptions = array())
239+
public function setCacheManagerOptions(array $cacheManagerOptions = array())
244240
{
245241
$this->cacheManagerOptions = $this->setCacheOptionsArray($cacheManagerOptions);
246-
if(!$this->cacheManager) {
242+
if (!$this->cacheManager) {
247243
$this->setCacheManager($this->cacheManagerOptions['cachemanager']);
248244
}
249245
return $this;
@@ -278,46 +274,39 @@ public function build()
278274
{
279275
$apiKeyProperties = null;
280276

281-
if ($this->apiKeyProperties)
282-
{
277+
if ($this->apiKeyProperties) {
283278

284279
$apiKeyProperties = parse_ini_string($this->apiKeyProperties);
285280

286-
} else
287-
{
281+
} else {
288282

289283
// need to load the properties file
290284
$apiKeyProperties = $this->getFileExtract();
291285

292-
if (!$apiKeyProperties)
293-
{
286+
if (!$apiKeyProperties) {
294287
throw new \InvalidArgumentException('No API Key file could be found or loaded from a file location. ' .
295288
'Please configure the "apiKeyFileLocation" property or alternatively configure a ' .
296289
"PHP 'ini' compliant string, by setting the 'apiKeyProperties' property.");
297290
}
298291
}
299292

300-
if (!$this->cacheManager)
301-
{
293+
if (!$this->cacheManager) {
302294
$this->setCacheManagerOptions();
303295
}
304296

305-
306297
$apiKeyId = $this->getRequiredPropertyValue($apiKeyProperties, 'apiKeyId', $this->apiKeyIdPropertyName);
307298

308299
$apiKeySecret = $this->getRequiredPropertyValue($apiKeyProperties, 'apiKeySecret', $this->apiKeySecretPropertyName);
309300

310301
$apiKey = new ApiKey($apiKeyId, $apiKeySecret);
311302

312-
$signer = $this->resolveSigner();
313-
$requestSigner = new $signer;
314-
315303
return new Client(
316304
$apiKey,
317305
$this->cacheManager,
318306
$this->cacheManagerOptions,
319307
$this->baseURL,
320-
$requestSigner
308+
null,
309+
$this->authenticationScheme
321310
);
322311

323312
}
@@ -332,8 +321,7 @@ private function getRequiredPropertyValue(array $apiKeyProperties, $masterName,
332321
{
333322
$result = array_key_exists($propertyName, $apiKeyProperties) ? $apiKeyProperties[$propertyName] : false;
334323

335-
if (!$result)
336-
{
324+
if (!$result) {
337325
throw new \InvalidArgumentException("There is no '$propertyName' property in the " .
338326
"configured apiKey file or properties string. You can either specify that property or " .
339327
"configure the '$masterName' PropertyName value on the ClientBuilder to specify a " .
@@ -346,29 +334,25 @@ private function getRequiredPropertyValue(array $apiKeyProperties, $masterName,
346334
private function getFileExtract()
347335
{
348336
// @codeCoverageIgnoreStart
349-
if (stripos($this->apiKeyFileLocation, 'http') === 0)
350-
{
337+
if (stripos($this->apiKeyFileLocation, 'http') === 0) {
351338
$request = new DefaultRequest(Request::METHOD_GET, $this->apiKeyFileLocation);
352339

353340
$executor = new HttpClientRequestExecutor;
354341

355342
try {
356343
$response = $executor->executeRequest($request);
357344

358-
if (!$response->isError())
359-
{
345+
if (!$response->isError()) {
360346
return parse_ini_string($response->getBody());
361347

362348
}
363-
} catch (Exception $e)
364-
{
349+
} catch (Exception $e) {
365350
return false;
366351
}
367352
}
368353
// @codeCoverageIgnoreEnd
369354

370-
if ($this->apiKeyFileLocation)
371-
{
355+
if ($this->apiKeyFileLocation) {
372356
return parse_ini_file($this->apiKeyFileLocation);
373357
}
374358
}
@@ -384,25 +368,25 @@ private function setCacheOptionsArray($overrides)
384368
'regions' => array(
385369
'accounts' => array(
386370
'ttl' => 60,
387-
'tti' => 120
371+
'tti' => 120,
388372
),
389373
'applications' => array(
390374
'ttl' => 60,
391-
'tti' => 120
375+
'tti' => 120,
392376
),
393377
'directories' => array(
394378
'ttl' => 60,
395-
'tti' => 120
379+
'tti' => 120,
396380
),
397381
'groups' => array(
398382
'ttl' => 60,
399-
'tti' => 120
383+
'tti' => 120,
400384
),
401385
'tenants' => array(
402386
'ttl' => 60,
403-
'tti' => 120
387+
'tti' => 120,
404388
),
405-
)
389+
),
406390
);
407391
return array_replace($defaults, $overrides);
408392
}
@@ -411,30 +395,23 @@ private function qualifyCacheManager($cacheManager)
411395
{
412396
$notCoreClass = true;
413397

414-
if(class_exists($cacheManager))
398+
if (class_exists($cacheManager)) {
415399
$notCoreClass = class_implements($cacheManager) == 'Stormpath\Cache\CacheManager';
400+
}
416401

417-
if(class_exists($cacheManager) && $notCoreClass) return $cacheManager;
402+
if (class_exists($cacheManager) && $notCoreClass) {
403+
return $cacheManager;
404+
}
418405

419-
if(strpos($cacheManager, 'CacheManager')) {
406+
if (strpos($cacheManager, 'CacheManager')) {
420407
$cacheManagerPath = "{$cacheManager}";
421408
} else {
422409
$cacheManagerPath = "Stormpath\\Cache\\{$cacheManager}CacheManager";
423410
}
424411

425-
426-
if(class_exists($cacheManagerPath)) return $cacheManagerPath;
427-
428-
}
429-
430-
private function resolveSigner()
431-
{
432-
$signer = "\\Stormpath\\Http\\Authc\\" . $this->authenticationScheme . "RequestSigner";
433-
434-
if(!class_exists($signer))
435-
throw new \InvalidArgumentException('Authentication Scheme is not supported.');
436-
437-
return new $signer;
412+
if (class_exists($cacheManagerPath)) {
413+
return $cacheManagerPath;
414+
}
438415

439416
}
440417
}

0 commit comments

Comments
 (0)