File tree 2 files changed +52
-1
lines changed
src/Symfony/Component/HttpClient
2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \HttpClient \Response ;
13
13
14
+ use function GuzzleHttp \Promise \promise_for ;
14
15
use GuzzleHttp \Promise \PromiseInterface as GuzzlePromiseInterface ;
15
16
use Http \Promise \Promise as HttplugPromiseInterface ;
16
17
use Psr \Http \Message \ResponseInterface as Psr7ResponseInterface ;
@@ -31,7 +32,10 @@ public function __construct(GuzzlePromiseInterface $promise)
31
32
32
33
public function then (callable $ onFulfilled = null , callable $ onRejected = null ): self
33
34
{
34
- return new self ($ this ->promise ->then ($ onFulfilled , $ onRejected ));
35
+ return new self ($ this ->promise ->then (
36
+ $ this ->wrapThenCallback ($ onFulfilled ),
37
+ $ this ->wrapThenCallback ($ onRejected )
38
+ ));
35
39
}
36
40
37
41
public function cancel (): void
@@ -62,4 +66,15 @@ public function wait($unwrap = true)
62
66
63
67
return $ result ;
64
68
}
69
+
70
+ private function wrapThenCallback (?callable $ callback ): ?callable
71
+ {
72
+ if (null === $ callback ) {
73
+ return null ;
74
+ }
75
+
76
+ return static function ($ value ) use ($ callback ) {
77
+ return promise_for ($ callback ($ value ));
78
+ };
79
+ }
65
80
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Symfony \Component \HttpClient \Tests \Response ;
13
+
14
+ use GuzzleHttp \Promise \Promise ;
15
+ use PHPUnit \Framework \TestCase ;
16
+ use Symfony \Component \HttpClient \Response \HttplugPromise ;
17
+
18
+ class HttplugPromiseTest extends TestCase
19
+ {
20
+ public function testComplexNesting ()
21
+ {
22
+ $ mkPromise = function ($ result ): HttplugPromise {
23
+ $ guzzlePromise = new Promise (function () use (&$ guzzlePromise , $ result ) {
24
+ $ guzzlePromise ->resolve ($ result );
25
+ });
26
+
27
+ return new HttplugPromise ($ guzzlePromise );
28
+ };
29
+
30
+ $ promise1 = $ mkPromise ('result ' );
31
+ $ promise2 = $ promise1 ->then ($ mkPromise );
32
+ $ promise3 = $ promise2 ->then (function ($ result ) { return $ result ; });
33
+
34
+ $ this ->assertSame ('result ' , $ promise3 ->wait ());
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments