1010
1111namespace Prismic ;
1212
13- use Ivory \HttpAdapter \ConfigurationInterface ;
14- use Ivory \HttpAdapter \Configuration ;
15- use Ivory \HttpAdapter \CurlHttpAdapter ;
16- use Ivory \HttpAdapter \EventDispatcherHttpAdapter ;
17- use Ivory \HttpAdapter \Event \Subscriber \StatusCodeSubscriber ;
18- use Ivory \HttpAdapter \HttpAdapterInterface ;
19- use Ivory \HttpAdapter \MultiHttpAdapterException ;
13+ use GuzzleHttp \Client ;
14+ use GuzzleHttp \Promise ;
2015use Symfony \Component \EventDispatcher \EventDispatcher ;
2116use \Prismic \Cache \CacheInterface ;
2217use \Prismic \Cache \ApcCache ;
@@ -61,23 +56,23 @@ class Api
6156 */
6257 private $ cache ;
6358 /**
64- * @var HttpAdapterInterface
59+ * @var Client
6560 */
66- private $ httpAdapter ;
61+ private $ httpClient ;
6762
6863 /**
6964 * Private constructor, not be used outside of this class.
7065 *
7166 * @param string $data
7267 * @param string|null $accessToken
73- * @param HttpAdapterInterface |null $httpAdapter
68+ * @param HttpClient |null $httpAdapter
7469 * @param CacheInterface|null $cache
7570 */
76- private function __construct ($ data , $ accessToken = null , HttpAdapterInterface $ httpAdapter = null , CacheInterface $ cache = null )
71+ private function __construct ($ data , $ accessToken = null , Client $ httpClient = null , CacheInterface $ cache = null )
7772 {
7873 $ this ->data = $ data ;
7974 $ this ->accessToken = $ accessToken ;
80- $ this ->httpAdapter = is_null ($ httpAdapter ) ? self :: defaultHttpAdapter () : $ httpAdapter ;
75+ $ this ->httpClient = is_null ($ httpClient ) ? new Client () : $ httpClient ;
8176 $ this ->cache = is_null ($ cache ) ? self ::defaultCache () : $ cache ;
8277 }
8378
@@ -229,14 +224,12 @@ public function getExperiments()
229224 */
230225 public function previewSession ($ token , $ linkResolver , $ defaultUrl )
231226 {
232- $ response = $ this ->getHttpAdapter ()->get ($ token );
227+ $ response = $ this ->getHttpClient ()->get ($ token );
233228 $ response = json_decode ($ response ->getBody (true ));
234229 if (isset ($ response ->mainDocument )) {
235- $ documents = $ this ->forms ()->everything
236- ->query (Predicates::at ("document.id " , $ response ->mainDocument ))
237- ->ref ($ token )
238- ->submit ()
239- ->getResults ();
230+ $ documents = $ this
231+ ->query (Predicates::at ("document.id " , $ response ->mainDocument ), ['ref ' => $ token ])
232+ ->getResults ();
240233 if (count ($ documents ) > 0 ) {
241234 if ($ url = $ linkResolver ->resolveDocument ($ documents [0 ])) {
242235 return $ url ;
@@ -287,13 +280,13 @@ public function getCache()
287280 }
288281
289282 /**
290- * Accessing the underlying HTTP adapter object responsible for the CURL requests
283+ * Accessing the underlying Guzzle HTTP client
291284 *
292- * @return HttpAdapterInterface the HTTP adapter object itself
285+ * @return HttpAdapterInterface
293286 */
294- public function getHttpAdapter ()
287+ public function getHttpClient ()
295288 {
296- return $ this ->httpAdapter ;
289+ return $ this ->httpClient ;
297290 }
298291
299292 /**
@@ -303,28 +296,28 @@ public function getHttpAdapter()
303296 *
304297 * @api
305298 *
306- * @param string $action the URL of your repository API's endpoint
307- * @param string $accessToken a permanent access token to use to access your content, for instance if your repository API is set to private
308- * @param HttpAdapterInterface $httpAdapter by default, the HTTP adapter uses CURL with a certain configuration, but you can override it here
309- * @param CacheInterface $cache Cache implementation
310- * @param int $apiCacheTTL max time to keep the API object in cache (in seconds)
299+ * @param string $action the URL of your repository API's endpoint
300+ * @param string $accessToken a permanent access token to use to access your content, for instance if your repository API is set to private
301+ * @param Client $httpClient Custom Guzzle http client
302+ * @param CacheInterface $cache Cache implementation
303+ * @param int $apiCacheTTL max time to keep the API object in cache (in seconds)
311304 *
312305 * @throws \RuntimeException
313306 *
314307 * @return Api the Api object, usable to perform queries
315308 */
316- public static function get ($ action , $ accessToken = null , HttpAdapterInterface $ httpAdapter = null , CacheInterface $ cache = null , $ apiCacheTTL = 5 )
309+ public static function get ($ action , $ accessToken = null , $ httpClient = null , CacheInterface $ cache = null , $ apiCacheTTL = 5 )
317310 {
318311 $ cache = is_null ($ cache ) ? self ::defaultCache () : $ cache ;
319312 $ cacheKey = $ action . (is_null ($ accessToken ) ? "" : ("# " . $ accessToken ));
320313 $ apiData = $ cache ->get ($ cacheKey );
321- $ api = $ apiData ? new Api (unserialize ($ apiData ), $ accessToken , $ httpAdapter , $ cache ) : null ;
314+ $ api = $ apiData ? new Api (unserialize ($ apiData ), $ accessToken , $ httpClient , $ cache ) : null ;
322315 if ($ api ) {
323316 return $ api ;
324317 } else {
325318 $ url = $ action . ($ accessToken ? '?access_token= ' . $ accessToken : '' );
326- $ httpAdapter = is_null ($ httpAdapter ) ? self :: defaultHttpAdapter () : $ httpAdapter ;
327- $ response = $ httpAdapter ->get ($ url );
319+ $ httpClient = is_null ($ httpClient ) ? new Client () : $ httpClient ;
320+ $ response = $ httpClient ->get ($ url );
328321 $ response = json_decode ($ response ->getBody (true ));
329322 $ experiments = isset ($ response ->experiments )
330323 ? Experiments::parse ($ response ->experiments )
@@ -350,7 +343,7 @@ function ($ref) {
350343 $ response ->oauth_token
351344 );
352345
353- $ api = new Api ($ apiData , $ accessToken , $ httpAdapter , $ cache );
346+ $ api = new Api ($ apiData , $ accessToken , $ httpClient , $ cache );
354347 $ cache ->set ($ cacheKey , serialize ($ apiData ), $ apiCacheTTL );
355348
356349 return $ api ;
@@ -373,8 +366,9 @@ public function submit()
373366 $ responses = array ();
374367
375368 // Get what we can from the cache
376- $ all_urls = array ();
377- $ urls = array ();
369+ $ all_urls = [];
370+ $ promises = [];
371+ $ urls = [];
378372 foreach ($ forms as $ i => $ form ) {
379373 $ url = $ form ->url ();
380374 array_push ($ all_urls , $ url );
@@ -384,20 +378,16 @@ public function submit()
384378 } else {
385379 $ responses [$ i ] = null ;
386380 array_push ($ urls , $ url );
381+ $ promises [$ url ] = $ this ->getHttpClient ()->getAsync ($ url );
387382 }
388383 }
389384
390385 // Query the server for the rest
391- if (count ($ urls ) > 0 ) {
392- try {
393- $ raw_responses = $ this ->getHttpAdapter ()->sendRequests ($ urls );
394- } catch (MultiHttpAdapterException $ e ) {
395- $ raw_responses = $ e ->getResponses ();
396- $ exceptions = $ e ->getExceptions ();
397- }
386+ if (count ($ promises ) > 0 ) {
387+ $ raw_responses = Promise \unwrap ($ promises );
398388
399- foreach ($ raw_responses as $ response ) {
400- $ url = $ response -> getParameter ( ' request ' )-> getUri ()-> __toString () ;
389+ foreach ($ urls as $ url ) {
390+ $ response = $ raw_responses [ $ url ] ;
401391 $ cacheControl = $ response ->getHeader ('Cache-Control ' )[0 ];
402392 $ cacheDuration = null ;
403393 if (preg_match ('/^max-age\s*=\s*(\d+)$/ ' , $ cacheControl , $ groups ) == 1 ) {
@@ -510,44 +500,4 @@ public static function defaultCache()
510500 return new NoCache ();
511501 }
512502
513- /**
514- * Get the default HTTP adapter configuration object
515- *
516- * This can be used for example to modify but not completely replace the
517- * default configuration (e.g. to prefix the user agent string), or to use
518- * the default configuration for a non-default HTTP adapter.
519- *
520- * @return \Ivory\HttpAdapter\ConfigurationInterface Configuration object
521- */
522- public static function defaultHttpAdapterConfiguration ()
523- {
524- $ configuration = new Configuration ();
525- $ configuration ->setUserAgent ('Prismic-php-kit/ ' . self ::VERSION . ' PHP/ ' . phpversion ());
526-
527- return $ configuration ;
528- }
529-
530- /**
531- * Get the default HTTP adapter used in the kit; this is entirely
532- * overridable by passing an instance of
533- * Ivory\HttpAdapter\HttpAdapterInterface to Api.get
534- *
535- * @param ConfigurationInterface|null $configuration Configuration object; use default if null
536- * @return HttpAdapterInterface HTTP adapter
537- */
538- public static function defaultHttpAdapter (ConfigurationInterface $ configuration = null )
539- {
540- if ($ configuration === null ) {
541- $ configuration = self ::defaultHttpAdapterConfiguration ();
542- }
543- $ dispatcher = new EventDispatcher ();
544- $ adapter = new EventDispatcherHttpAdapter (new CurlHttpAdapter ($ configuration ), $ dispatcher );
545-
546- // We need to add the subscriber to have errors on 4.x.x and 5.x.x.
547- $ statusCodeSubscriber = new StatusCodeSubscriber ();
548- $ dispatcher ->addSubscriber ($ statusCodeSubscriber );
549-
550- return $ adapter ;
551- }
552-
553503}
0 commit comments