Hoa central
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Hoa\Promise\Promise Class Reference

Public Member Functions

 __construct ($executor=null)
 
 resolve ($value)
 
 reject ($reason)
 
 then ($onFulfilled=null, $onRejected=null)
 
 __call ($name, $arguments)
 
 isPending ()
 
 isFulfilled ()
 
 isRejected ()
 
 getValue ()
 

Public Attributes

const STATE_PENDING = 0
 
const STATE_FULFILLED = 1
 
const STATE_REJECTED = 2
 
const HANDLER_ONFULFILLED = 0
 
const HANDLER_FULFILL = 1
 
const HANDLER_ONREJECTED = 2
 
const HANDLER_REJECT = 3
 

Protected Member Functions

 handle ($handler)
 
 setValue ($value)
 

Protected Attributes

 $_state = self::STATE_PENDING
 
 $_value = null
 
 $_deferred = null
 

Detailed Description

Class .

Author
Ivan Enderlin ivan..nosp@m.ende.nosp@m.rlin@.nosp@m.hoa-.nosp@m.proje.nosp@m.ct.n.nosp@m.et

Definition at line 51 of file Promise.php.

Constructor & Destructor Documentation

Hoa\Promise\Promise::__construct (   $executor = null)

Definition at line 66 of file Promise.php.

66  {
67 
68  if(null !== $executor)
69  $executor(
70  xcallable($this, 'resolve'),
71  xcallable($this, 'reject')
72  );
73 
74  return;
75  }

Member Function Documentation

Hoa\Promise\Promise::__call (   $name,
  $arguments 
)

Definition at line 185 of file Promise.php.

185  {
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  }
then($onFulfilled=null, $onRejected=null)
Definition: Promise.php:168

Here is the call graph for this function:

Hoa\Promise\Promise::getValue ( )

Definition at line 223 of file Promise.php.

223  {
224 
225  return $this->_value;
226  }

Here is the caller graph for this function:

Hoa\Promise\Promise::handle (   $handler)
protected

Definition at line 128 of file Promise.php.

128  {
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  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Promise\Promise::isFulfilled ( )

Definition at line 205 of file Promise.php.

205  {
206 
207  return self::STATE_FULFILLED === $this->_state;
208  }

Here is the caller graph for this function:

Hoa\Promise\Promise::isPending ( )

Definition at line 200 of file Promise.php.

200  {
201 
202  return self::STATE_PENDING === $this->_state;
203  }
Hoa\Promise\Promise::isRejected ( )

Definition at line 210 of file Promise.php.

210  {
211 
212  return self::STATE_REJECTED === $this->_state;
213  }
Hoa\Promise\Promise::reject (   $reason)

Definition at line 111 of file Promise.php.

111  {
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  }
handle($handler)
Definition: Promise.php:128

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Promise\Promise::resolve (   $value)

Definition at line 77 of file Promise.php.

77  {
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  }
handle($handler)
Definition: Promise.php:128

Here is the call graph for this function:

Hoa\Promise\Promise::setValue (   $value)
protected

Definition at line 215 of file Promise.php.

215  {
216 
217  $old = $this->_value;
218  $this->_value = $value;
219 
220  return $old;
221  }

Here is the caller graph for this function:

Hoa\Promise\Promise::then (   $onFulfilled = null,
  $onRejected = null 
)

Definition at line 168 of file Promise.php.

168  {
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  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Promise\Promise::$_deferred = null
protected

Definition at line 64 of file Promise.php.

Hoa\Promise\Promise::$_state = self::STATE_PENDING
protected

Definition at line 62 of file Promise.php.

Hoa\Promise\Promise::$_value = null
protected

Definition at line 63 of file Promise.php.

const Hoa\Promise\Promise::HANDLER_FULFILL = 1

Definition at line 58 of file Promise.php.

const Hoa\Promise\Promise::HANDLER_ONFULFILLED = 0

Definition at line 57 of file Promise.php.

const Hoa\Promise\Promise::HANDLER_ONREJECTED = 2

Definition at line 59 of file Promise.php.

const Hoa\Promise\Promise::HANDLER_REJECT = 3

Definition at line 60 of file Promise.php.

const Hoa\Promise\Promise::STATE_FULFILLED = 1

Definition at line 54 of file Promise.php.

const Hoa\Promise\Promise::STATE_PENDING = 0

Definition at line 53 of file Promise.php.

const Hoa\Promise\Promise::STATE_REJECTED = 2

Definition at line 55 of file Promise.php.


The documentation for this class was generated from the following file: