source: externals/php/vendor/guzzlehttp/promises/src/FulfilledPromise.php

Last change on this file was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[01a620f]1<?php
2namespace GuzzleHttp\Promise;
3
4/**
5 * A promise that has been fulfilled.
6 *
7 * Thenning off of this promise will invoke the onFulfilled callback
8 * immediately and ignore other callbacks.
9 */
10class FulfilledPromise implements PromiseInterface
11{
12 private $value;
13
14 public function __construct($value)
15 {
16 if (method_exists($value, 'then')) {
17 throw new \InvalidArgumentException(
18 'You cannot create a FulfilledPromise with a promise.');
19 }
20
21 $this->value = $value;
22 }
23
24 public function then(
25 callable $onFulfilled = null,
26 callable $onRejected = null
27 ) {
28 // Return itself if there is no onFulfilled function.
29 if (!$onFulfilled) {
30 return $this;
31 }
32
33 $queue = queue();
34 $p = new Promise([$queue, 'run']);
35 $value = $this->value;
36 $queue->add(static function () use ($p, $value, $onFulfilled) {
37 if ($p->getState() === self::PENDING) {
38 try {
39 $p->resolve($onFulfilled($value));
40 } catch (\Exception $e) {
41 $p->reject($e);
42 }
43 }
44 });
45
46 return $p;
47 }
48
49 public function otherwise(callable $onRejected)
50 {
51 return $this->then(null, $onRejected);
52 }
53
54 public function wait($unwrap = true, $defaultDelivery = null)
55 {
56 return $unwrap ? $this->value : null;
57 }
58
59 public function getState()
60 {
61 return self::FULFILLED;
62 }
63
64 public function resolve($value)
65 {
66 if ($value !== $this->value) {
67 throw new \LogicException("Cannot resolve a fulfilled promise");
68 }
69 }
70
71 public function reject($reason)
72 {
73 throw new \LogicException("Cannot reject a fulfilled promise");
74 }
75
76 public function cancel()
77 {
78 // pass
79 }
80}
Note: See TracBrowser for help on using the repository browser.