Hoa central
Promise.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Promise;
38 
39 use Hoa\Core;
40 
51 class Promise {
52 
53  const STATE_PENDING = 0;
54  const STATE_FULFILLED = 1;
55  const STATE_REJECTED = 2;
56 
58  const HANDLER_FULFILL = 1;
59  const HANDLER_ONREJECTED = 2;
60  const HANDLER_REJECT = 3;
61 
62  protected $_state = self::STATE_PENDING;
63  protected $_value = null;
64  protected $_deferred = null;
65 
66  public function __construct ( $executor = null ) {
67 
68  if(null !== $executor)
69  $executor(
70  xcallable($this, 'resolve'),
71  xcallable($this, 'reject')
72  );
73 
74  return;
75  }
76 
77  public function resolve ( $value ) {
78 
79  if(self::STATE_PENDING !== $this->_state)
80  throw new Exception(
81  'This promise is not pending, cannot resolve it.', 0);
82 
83  try {
84 
85  if($value instanceof self) {
86 
87  $value->then(
88  xcallable($this, 'resolve'),
89  xcallable($this, 'reject')
90  );
91 
92  return;
93  }
94 
95  $this->setValue($value);
96  $this->_state = self::STATE_FULFILLED;
97 
98  if(null === $this->_deferred)
99  return;
100 
101  $this->handle($this->_deferred);
102  }
103  catch ( \Exception $e ) {
104 
105  $this->reject($e);
106  }
107 
108  return;
109  }
110 
111  public function reject ( $reason ) {
112 
113  if(self::STATE_PENDING !== $this->_state)
114  throw new Exception(
115  'This promise is not pending, cannot reject it.', 1);
116 
117  $this->setValue($reason);
118  $this->_state = self::STATE_REJECTED;
119 
120  if(null === $this->_deferred)
121  return;
122 
123  $this->handle($this->_deferred);
124 
125  return;
126  }
127 
128  protected function handle ( $handler ) {
129 
130  if(self::STATE_PENDING === $this->_state) {
131 
132  $this->_deferred = $handler;
133 
134  return;
135  }
136 
137  $handlerOn = null;
138 
139  if(true === $this->isFulfilled())
140  $handlerOn = $handler[self::HANDLER_ONFULFILLED];
141  else
142  $handlerOn = $handler[self::HANDLER_ONREJECTED];
143 
144  $out = null;
145 
146  if(null === $handlerOn)
147  $out = $this->getValue();
148  else
149  try {
150 
151  $out = $handlerOn($this->getValue());
152  }
153  catch ( \Exception $e ) {
154 
155  $handler[self::HANDLER_REJECT]($e);
156 
157  return;
158  }
159 
160  if(true === $this->isFulfilled())
161  $handler[self::HANDLER_FULFILL]($out);
162  else
163  $handler[self::HANDLER_REJECT]($out);
164 
165  return;
166  }
167 
168  public function then ( $onFulfilled = null, $onRejected = null ) {
169 
170  $self = $this;
171 
172  return new static(function ( $fulfill, $reject ) use ( $self,
173  $onFulfilled,
174  $onRejected ) {
175 
176  $self->handle([
177  self::HANDLER_ONFULFILLED => $onFulfilled,
178  self::HANDLER_FULFILL => $fulfill,
179  self::HANDLER_ONREJECTED => $onRejected,
180  self::HANDLER_REJECT => $reject
181  ]);
182  });
183  }
184 
185  public function __call ( $name, $arguments ) {
186 
187  if('catch' === $name) {
188 
189  if(!isset($arguments[0]))
190  throw new Exception(
191  'The catch method must have one argument.', 2);
192 
193  return $this->then(null, $arguments[0]);
194  }
195 
196  throw new Exception(
197  'Method %s does not exist.', 3, $name);
198  }
199 
200  public function isPending ( ) {
201 
202  return self::STATE_PENDING === $this->_state;
203  }
204 
205  public function isFulfilled ( ) {
206 
207  return self::STATE_FULFILLED === $this->_state;
208  }
209 
210  public function isRejected ( ) {
211 
212  return self::STATE_REJECTED === $this->_state;
213  }
214 
215  protected function setValue ( $value ) {
216 
217  $old = $this->_value;
218  $this->_value = $value;
219 
220  return $old;
221  }
222 
223  public function getValue ( ) {
224 
225  return $this->_value;
226  }
227 }
228 
232 Core\Consistency::flexEntity('Hoa\Promise\Promise');
const HANDLER_ONFULFILLED
Definition: Promise.php:57
const HANDLER_FULFILL
Definition: Promise.php:58
const HANDLER_ONREJECTED
Definition: Promise.php:59
__call($name, $arguments)
Definition: Promise.php:185
const STATE_FULFILLED
Definition: Promise.php:54
then($onFulfilled=null, $onRejected=null)
Definition: Promise.php:168
handle($handler)
Definition: Promise.php:128
const HANDLER_REJECT
Definition: Promise.php:60
__construct($executor=null)
Definition: Promise.php:66
const STATE_REJECTED
Definition: Promise.php:55