2014-09-22 22:47:48 +02:00
|
|
|
<?php
|
|
|
|
|
2014-09-23 04:38:32 +02:00
|
|
|
namespace Amp;
|
2014-09-22 22:47:48 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-19 05:57:34 +02:00
|
|
|
* A PrivatePromisor creates read-only Promise instances that can only be
|
|
|
|
* resolved by holders of the PrivatePromisor instance. This creates an
|
|
|
|
* additional layer of API protection beyond the PublicPromisor.
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-05-19 05:57:34 +02:00
|
|
|
trait PrivatePromisor {
|
2015-03-19 16:39:38 +01:00
|
|
|
private $resolver;
|
2015-04-30 19:41:14 +02:00
|
|
|
private $updater;
|
|
|
|
private $promise;
|
2014-09-22 22:47:48 +02:00
|
|
|
|
2015-05-20 00:49:08 +02:00
|
|
|
public function __construct() {
|
|
|
|
$placeholder = new PrivatePlaceholder;
|
2015-04-30 19:41:14 +02:00
|
|
|
$resolver = function(\Exception $error = null, $result = null) {
|
2015-05-19 17:16:04 +02:00
|
|
|
// bound to private PrivatePlaceholder::resolve()
|
|
|
|
$this->resolve($error, $result);
|
2014-09-22 22:47:48 +02:00
|
|
|
};
|
2015-04-30 19:41:14 +02:00
|
|
|
$updater = function($progress) {
|
2015-05-19 17:16:04 +02:00
|
|
|
// bound to private PrivatePlaceholder::update()
|
2015-06-01 01:33:55 +02:00
|
|
|
\call_user_func_array([$this, "update"], func_get_args());
|
2015-04-30 19:41:14 +02:00
|
|
|
};
|
2015-05-19 06:29:03 +02:00
|
|
|
$this->resolver = $resolver->bindTo($placeholder, $placeholder);
|
|
|
|
$this->updater = $updater->bindTo($placeholder, $placeholder);
|
|
|
|
$this->promise = $placeholder;
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-19 05:57:34 +02:00
|
|
|
* Promise future fulfillment of the returned placeholder value
|
2015-04-30 19:41:14 +02:00
|
|
|
*
|
2015-03-19 16:39:57 +01:00
|
|
|
* @return \Amp\Promise
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function promise() {
|
2014-09-22 22:47:48 +02:00
|
|
|
return $this->promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-30 19:41:14 +02:00
|
|
|
* Update watchers of progress resolving the promised value
|
2014-09-22 22:47:48 +02:00
|
|
|
*
|
2015-06-01 01:33:55 +02:00
|
|
|
* @param mixed $progress1, $progress2, ... $progressN
|
2014-09-22 22:47:48 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function update($progress) {
|
2015-06-01 01:33:55 +02:00
|
|
|
\call_user_func_array($this->updater, func_get_args());
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-19 05:57:34 +02:00
|
|
|
* Resolve the associated promise placeholder as a success
|
2014-09-22 22:47:48 +02:00
|
|
|
*
|
|
|
|
* @param mixed $result
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function succeed($result = null) {
|
2015-04-30 19:41:14 +02:00
|
|
|
call_user_func($this->resolver, $error = null, $result);
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-19 05:57:34 +02:00
|
|
|
* Resolve the associated promise placeholder as a failure
|
2014-09-22 22:47:48 +02:00
|
|
|
*
|
|
|
|
* @param \Exception $error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function fail(\Exception $error) {
|
2015-04-30 19:41:14 +02:00
|
|
|
call_user_func($this->resolver, $error, $result = null);
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
2015-05-20 00:49:08 +02:00
|
|
|
}
|