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 fulfilled.
8
 * A promise that has been fulfilled.
7
 *
9
 *
-
 
10
 * Thenning off of this promise will invoke the onFulfilled callback
-
 
11
 * immediately and ignore other callbacks.
8
 * Thenning off of this promise will invoke the onFulfilled callback
12
 *
9
 * immediately and ignore other callbacks.
13
 * @final
10
 */
14
 */
11
class FulfilledPromise implements PromiseInterface
15
class FulfilledPromise implements PromiseInterface
Línea -... Línea 16...
-
 
16
{
-
 
17
    private $value;
-
 
18
 
12
{
19
    /**
13
    private $value;
20
     * @param mixed $value
14
 
21
     */
15
    public function __construct($value)
22
    public function __construct($value)
16
    {
23
    {
Línea 22... Línea 29...
22
 
29
 
23
        $this->value = $value;
30
        $this->value = $value;
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
        // Return itself if there is no onFulfilled function.
37
        // Return itself if there is no onFulfilled function.
31
        if (!$onFulfilled) {
38
        if (!$onFulfilled) {
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
        $p = new Promise([$queue, 'run']);
43
        $p = new Promise([$queue, 'run']);
37
        $value = $this->value;
44
        $value = $this->value;
38
        $queue->add(static function () use ($p, $value, $onFulfilled) {
45
        $queue->add(static function () use ($p, $value, $onFulfilled): void {
39
            if (Is::pending($p)) {
46
            if (Is::pending($p)) {
40
                try {
47
                try {
41
                    $p->resolve($onFulfilled($value));
-
 
42
                } catch (\Throwable $e) {
-
 
43
                    $p->reject($e);
48
                    $p->resolve($onFulfilled($value));
44
                } catch (\Exception $e) {
49
                } catch (\Throwable $e) {
45
                    $p->reject($e);
50
                    $p->reject($e);
Línea 46... Línea 51...
46
                }
51
                }
47
            }
52
            }
Línea 48... Línea 53...
48
        });
53
        });
49
 
54
 
50
        return $p;
55
        return $p;
51
    }
56
    }
Línea 52... Línea 57...
52
 
57
 
53
    public function otherwise(callable $onRejected)
58
    public function otherwise(callable $onRejected): PromiseInterface
54
    {
59
    {
55
        return $this->then(null, $onRejected);
60
        return $this->then(null, $onRejected);
Línea 56... Línea 61...
56
    }
61
    }
57
 
62
 
58
    public function wait($unwrap = true, $defaultDelivery = null)
63
    public function wait(bool $unwrap = true)
59
    {
64
    {
Línea 60... Línea 65...
60
        return $unwrap ? $this->value : null;
65
        return $unwrap ? $this->value : null;
61
    }
66
    }
62
 
67
 
63
    public function getState()
68
    public function getState(): string
64
    {
69
    {
65
        return self::FULFILLED;
70
        return self::FULFILLED;
Línea 66... Línea 71...
66
    }
71
    }
67
 
72
 
68
    public function resolve($value)
73
    public function resolve($value): void
69
    {
74
    {
Línea 70... Línea 75...
70
        if ($value !== $this->value) {
75
        if ($value !== $this->value) {
71
            throw new \LogicException("Cannot resolve a fulfilled promise");
76
            throw new \LogicException('Cannot resolve a fulfilled promise');
72
        }
77
        }
73
    }
78
    }
74
 
79