4
4
5
5
use GuzzleHttp \Client as GuzzleClient ;
6
6
use GuzzleHttp \Exception \RequestException ;
7
+ use function GuzzleHttp \Promise \all ;
7
8
use function GuzzleHttp \Promise \coroutine ;
8
- use GuzzleHttp \Promise \FulfilledPromise ;
9
9
use function GuzzleHttp \Promise \promise_for ;
10
10
use GuzzleHttp \Promise \PromiseInterface ;
11
11
use Markup \Contentful \Cache \NullCacheItemPool ;
@@ -103,7 +103,9 @@ public function __construct(array $spaces, array $options = [])
103
103
public function getSpace ($ space = null , array $ options = [])
104
104
{
105
105
if ($ space instanceof SpaceInterface) {
106
- return $ space ;
106
+ return ($ this ->isAsyncCall ($ options ))
107
+ ? promise_for ($ space )
108
+ : $ space ;
107
109
} else {
108
110
$ spaceName = $ space ;
109
111
}
@@ -133,7 +135,9 @@ public function getSpace($space = null, array $options = [])
133
135
public function getEntry ($ id , $ space = null , array $ options = [])
134
136
{
135
137
if ($ this ->envelope ->hasEntry ($ id )) {
136
- return $ this ->envelope ->findEntry ($ id );
138
+ return ($ this ->isAsyncCall ($ options ))
139
+ ? promise_for ($ this ->envelope ->findEntry ($ id ))
140
+ : $ this ->envelope ->findEntry ($ id );
137
141
}
138
142
$ spaceName = ($ space instanceof SpaceInterface) ? $ space ->getName () : $ space ;
139
143
$ spaceData = $ this ->getSpaceDataForName (($ space instanceof SpaceInterface) ? $ space ->getName () : $ space );
@@ -187,7 +191,9 @@ public function getEntries(array $parameters = [], $space = null, array $options
187
191
public function getAsset ($ id , $ space = null , array $ options = [])
188
192
{
189
193
if ($ this ->envelope ->hasAsset ($ id )) {
190
- return $ this ->envelope ->findAsset ($ id );
194
+ return ($ this ->isAsyncCall ($ options ))
195
+ ? promise_for ($ this ->envelope ->findAsset ($ id ))
196
+ : $ this ->envelope ->findAsset ($ id );
191
197
}
192
198
$ spaceName = ($ space instanceof SpaceInterface) ? $ space ->getName () : $ space ;
193
199
$ spaceData = $ this ->getSpaceDataForName ($ spaceName );
@@ -215,7 +221,9 @@ public function getAsset($id, $space = null, array $options = [])
215
221
public function getContentType ($ id , $ space = null , array $ options = [])
216
222
{
217
223
if ($ this ->envelope ->hasContentType ($ id )) {
218
- return $ this ->envelope ->findContentType ($ id );
224
+ return ($ this ->isAsyncCall ($ options ))
225
+ ? promise_for ($ this ->envelope ->findContentType ($ id ))
226
+ : $ this ->envelope ->findContentType ($ id );
219
227
}
220
228
221
229
//fetch them all and pick one out, as it is likely we'll want to access others
@@ -242,6 +250,14 @@ function ($contentTypes) use ($id) {
242
250
public function getContentTypes (array $ parameters = [], $ space = null , array $ options = [])
243
251
{
244
252
$ spaceName = ($ space instanceof SpaceInterface) ? $ space ->getName () : $ space ;
253
+ if (!$ parameters ) {
254
+ $ stashedContentTypes = $ this ->envelope ->getAllContentTypesForSpace ($ spaceName );
255
+ if (null !== $ stashedContentTypes ) {
256
+ return ($ this ->isAsyncCall ($ options ))
257
+ ? promise_for ($ stashedContentTypes )
258
+ : $ stashedContentTypes ;
259
+ }
260
+ }
245
261
$ spaceData = $ this ->getSpaceDataForName (($ space instanceof SpaceInterface) ? $ space ->getName () : $ space );
246
262
$ api = ($ spaceData ['preview_mode ' ]) ? self ::PREVIEW_API : self ::CONTENT_DELIVERY_API ;
247
263
@@ -292,26 +308,40 @@ function () use ($name, $space, $options) {
292
308
}
293
309
294
310
/**
295
- * @param Link $link
311
+ * @param Link $link
296
312
* @param array $options
297
- * @return ResourceInterface| PromiseInterface
313
+ * @return PromiseInterface
298
314
*/
299
315
public function resolveLink ($ link , array $ options = [])
300
316
{
301
317
//check whether the "link" is already actually a resolved resource
302
318
if ($ link instanceof ResourceInterface) {
303
- return $ link ;
319
+ return promise_for ( $ link) ;
304
320
}
305
321
try {
306
322
switch ($ link ->getLinkType ()) {
307
323
case 'Entry ' :
308
- return $ this ->getEntry ($ link ->getId (), $ link ->getSpaceName (), $ options );
324
+ return $ this ->getEntry (
325
+ $ link ->getId (),
326
+ $ link ->getSpaceName (),
327
+ array_merge ($ options , ['async ' => true ])
328
+ );
309
329
case 'Asset ' :
310
- return $ this ->getAsset ($ link ->getId (), $ link ->getSpaceName (), $ options );
330
+ return $ this ->getAsset (
331
+ $ link ->getId (),
332
+ $ link ->getSpaceName (),
333
+ array_merge ($ options , ['async ' => true ])
334
+ );
311
335
case 'ContentType ' :
312
- return $ this ->getContentType ($ link ->getId (), $ link ->getSpaceName (), $ options );
336
+ return $ this ->getContentType (
337
+ $ link ->getId (),
338
+ $ link ->getSpaceName (),
339
+ array_merge ($ options , ['async ' => true ])
340
+ );
313
341
default :
314
- throw new \InvalidArgumentException (sprintf ('Tried to resolve unknown link type "%s". ' , $ link ->getLinkType ()));
342
+ throw new \InvalidArgumentException (
343
+ sprintf ('Tried to resolve unknown link type "%s". ' , $ link ->getLinkType ())
344
+ );
315
345
}
316
346
} catch (ResourceUnavailableException $ e ) {
317
347
throw new LinkUnresolvableException ($ link , null , 0 , $ e );
@@ -387,21 +417,24 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
387
417
* Returns a built response if it passes test, or null if it doesn't.
388
418
*
389
419
* @param string $json
390
- * @return ResourceInterface|ResourceArray|null
420
+ * @return PromiseInterface
391
421
*/
392
422
$ buildResponseFromJson = function ($ json ) use ($ spaceData , $ assetDecorator , $ shouldBuildTypedResources , $ test ) {
393
423
$ json = (is_array ($ json )) ? $ json : json_decode ($ json , true );
394
424
if (null === $ json ) {
395
425
return null ;
396
426
}
397
- $ builtResponse = $ this ->buildResponseFromRaw (
427
+
428
+ return $ this ->buildResponseFromRaw (
398
429
$ json ,
399
430
$ spaceData ['name ' ],
400
431
$ assetDecorator ,
401
432
$ shouldBuildTypedResources
433
+ )->then (
434
+ function ($ builtResponse ) use ($ test ) {
435
+ return (call_user_func ($ test , $ builtResponse )) ? $ builtResponse : null ;
436
+ }
402
437
);
403
-
404
- return (call_user_func ($ test , $ builtResponse )) ? $ builtResponse : null ;
405
438
};
406
439
$ log = function ($ description , $ isCacheHit , $ type ) use ($ timer , $ queryType , $ api ) {
407
440
$ this ->logger ->log (
@@ -425,7 +458,7 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
425
458
LogInterface::TYPE_RESPONSE
426
459
);
427
460
428
- $ builtResponse = $ buildResponseFromJson ($ cacheItemJson );
461
+ $ builtResponse = ( yield $ buildResponseFromJson ($ cacheItemJson) );
429
462
if ($ builtResponse ) {
430
463
yield promise_for ($ builtResponse );
431
464
return ;
@@ -444,7 +477,7 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
444
477
true ,
445
478
LogInterface::TYPE_RESOURCE
446
479
);
447
- $ builtResponse = $ buildResponseFromJson ($ fallbackJson );
480
+ $ builtResponse = ( yield $ buildResponseFromJson ($ fallbackJson) );
448
481
if ($ builtResponse ) {
449
482
yield promise_for ($ builtResponse );
450
483
return ;
@@ -478,7 +511,12 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
478
511
/**
479
512
* @var ResponseInterface $response
480
513
*/
481
- $ response = (yield $ this ->sendRequestAsync ($ request , $ queryParams ));
514
+ $ response = ($ shouldBuildTypedResources )
515
+ ? array_values ((yield all ([
516
+ $ this ->sendRequestWithQueryParams ($ request , $ queryParams ),
517
+ $ this ->ensureContentTypesLoaded ($ spaceName )
518
+ ])))[0 ]
519
+ : (yield $ this ->sendRequestWithQueryParams ($ request , $ queryParams ));
482
520
} catch (RequestException $ e ) {
483
521
/**
484
522
* @var CacheItemInterface $fallbackCacheItem
@@ -496,12 +534,12 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
496
534
$ writeCacheItem ->set ($ fallbackJson );
497
535
$ writeCache ->save ($ writeCacheItem );
498
536
499
- yield promise_for ( $ this ->buildResponseFromRaw (
537
+ yield $ this ->buildResponseFromRaw (
500
538
json_decode ($ fallbackJson , true ),
501
539
$ spaceData ['name ' ],
502
540
$ assetDecorator ,
503
541
$ shouldBuildTypedResources
504
- )) ;
542
+ );
505
543
return ;
506
544
}
507
545
}
@@ -542,7 +580,7 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
542
580
$ responseJson = json_encode (
543
581
(!$ unavailableException ) ? $ this ->responseAsArrayFromJson ($ response ) : null
544
582
);
545
- $ builtResponse = ($ responseJson ) ? $ buildResponseFromJson ($ responseJson ) : null ;
583
+ $ builtResponse = ($ responseJson ) ? ( yield $ buildResponseFromJson ($ responseJson) ) : null ;
546
584
$ isValidResponse = (bool ) $ builtResponse ;
547
585
548
586
//save into cache
@@ -587,7 +625,7 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
587
625
true ,
588
626
LogInterface::TYPE_RESOURCE
589
627
);
590
- $ builtResponse = $ buildResponseFromJson ($ fallbackJson );
628
+ $ builtResponse = ( yield $ buildResponseFromJson ($ fallbackJson) );
591
629
if ($ builtResponse ) {
592
630
yield promise_for ($ builtResponse );
593
631
return ;
@@ -722,10 +760,10 @@ private function setApiVersionHeaderOnRequest($request, $api)
722
760
723
761
/**
724
762
* @param array $data
725
- * @param string $spaceName
726
- * @param AssetDecoratorInterface $assetDecorator
763
+ * @param null $spaceName
764
+ * @param AssetDecoratorInterface|null $assetDecorator
727
765
* @param bool $useTypedResources
728
- * @return ResourceInterface
766
+ * @return PromiseInterface
729
767
*/
730
768
private function buildResponseFromRaw (
731
769
array $ data ,
@@ -891,4 +929,29 @@ private function resolveContentTypeNameFilter(IncompleteParameterInterface $filt
891
929
892
930
return $ contentTypeFilterProvider ->createForContentTypeName ($ filter ->getValue (), $ spaceName );
893
931
}
932
+
933
+ /**
934
+ * @param string $spaceName
935
+ * @return PromiseInterface
936
+ */
937
+ private function ensureContentTypesLoaded ($ spaceName )
938
+ {
939
+ return $ this ->getContentTypes ([], $ spaceName , ['async ' => true , 'untyped ' => true ])
940
+ ->then (
941
+ function ($ types ) use ($ spaceName ) {
942
+ $ this ->envelope ->insertAllContentTypesForSpace ($ types , $ spaceName );
943
+
944
+ return $ types ;
945
+ }
946
+ );
947
+ }
948
+
949
+ /**
950
+ * @param array $options
951
+ * @return bool
952
+ */
953
+ private function isAsyncCall (array $ options )
954
+ {
955
+ return isset ($ options ['async ' ]) && true === $ options ['async ' ];
956
+ }
894
957
}
0 commit comments