Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 1... Línea 1...
1
<?php
1
<?php
Línea -... Línea 2...
-
 
2
 
-
 
3
declare(strict_types=1);
2
 
4
 
Línea 3... Línea 5...
3
namespace GuzzleHttp\Promise;
5
namespace GuzzleHttp\Promise;
4
 
6
 
5
/**
7
/**
6
 * A promise that has been rejected.
8
 * A promise that has been rejected.
7
 *
9
 *
-
 
10
 * Thenning off of this promise will invoke the onRejected callback
-
 
11
 * immediately and ignore other callbacks.
8
 * Thenning off of this promise will invoke the onRejected callback
12
 *
9
 * immediately and ignore other callbacks.
13
 * @final
10
 */
14
 */
11
class RejectedPromise implements PromiseInterface
15
class RejectedPromise implements PromiseInterface
Línea -... Línea 16...
-
 
16
{
-
 
17
    private $reason;
-
 
18
 
12
{
19
    /**
13
    private $reason;
20
     * @param mixed $reason
14
 
21
     */
15
    public function __construct($reason)
22
    public function __construct($reason)
16
    {
23
    {
Línea 22... Línea 29...
22
 
29
 
23
        $this->reason = $reason;
30
        $this->reason = $reason;
Línea 24... Línea 31...
24
    }
31
    }
25
 
32
 
26
    public function then(
33
    public function then(
27
        callable $onFulfilled = null,
34
        ?callable $onFulfilled = null,
28
        callable $onRejected = null
35
        ?callable $onRejected = null
29
    ) {
36
    ): PromiseInterface {
30
        // If there's no onRejected callback then just return self.
37
        // If there's no onRejected callback then just return self.
31
        if (!$onRejected) {
38
        if (!$onRejected) {
Línea 32... Línea 39...
32
            return $this;
39
            return $this;
33
        }
40
        }
34
 
41
 
35
        $queue = Utils::queue();
42
        $queue = Utils::queue();
36
        $reason = $this->reason;
43
        $reason = $this->reason;
37
        $p = new Promise([$queue, 'run']);
44
        $p = new Promise([$queue, 'run']);
38
        $queue->add(static function () use ($p, $reason, $onRejected) {
45
        $queue->add(static function () use ($p, $reason, $onRejected): void {
39
            if (Is::pending($p)) {
46
            if (Is::pending($p)) {
40
                try {
47
                try {
41
                    // Return a resolved promise if onRejected does not throw.
48
                    // Return a resolved promise if onRejected does not throw.
42
                    $p->resolve($onRejected($reason));
49
                    $p->resolve($onRejected($reason));
43
                } catch (\Throwable $e) {
-
 
44
                    // onRejected threw, so return a rejected promise.
-
 
45
                    $p->reject($e);
-
 
46
                } catch (\Exception $e) {
50
                } catch (\Throwable $e) {
47
                    // onRejected threw, so return a rejected promise.
51
                    // onRejected threw, so return a rejected promise.
48
                    $p->reject($e);
52
                    $p->reject($e);
Línea 49... Línea 53...
49
                }
53
                }
50
            }
54
            }
Línea 51... Línea 55...
51
        });
55
        });
52
 
56
 
53
        return $p;
57
        return $p;
54
    }
58
    }
Línea 55... Línea 59...
55
 
59
 
56
    public function otherwise(callable $onRejected)
60
    public function otherwise(callable $onRejected): PromiseInterface
57
    {
61
    {
58
        return $this->then(null, $onRejected);
62
        return $this->then(null, $onRejected);
59
    }
63
    }
Línea 60... Línea 64...
60
 
64
 
61
    public function wait($unwrap = true, $defaultDelivery = null)
65
    public function wait(bool $unwrap = true)
Línea 62... Línea 66...
62
    {
66
    {
63
        if ($unwrap) {
67
        if ($unwrap) {
64
            throw Create::exceptionFor($this->reason);
68
            throw Create::exceptionFor($this->reason);
65
        }
69
        }
Línea 66... Línea 70...
66
 
70
 
67
        return null;
71
        return null;
68
    }
72
    }
69
 
73
 
Línea 70... Línea 74...
70
    public function getState()
74
    public function getState(): string
71
    {
75
    {
72
        return self::REJECTED;
76
        return self::REJECTED;
73
    }
77
    }
74
 
78
 
75
    public function resolve($value)
79
    public function resolve($value): void
Línea 76... Línea 80...
76
    {
80
    {
77
        throw new \LogicException("Cannot resolve a rejected promise");
81
        throw new \LogicException('Cannot resolve a rejected promise');
78
    }
82
    }
79
 
83
 
80
    public function reject($reason)
84
    public function reject($reason): void