Commit inicial con archivos existentes

This commit is contained in:
2026-01-17 16:14:00 -06:00
parent 48671dc88e
commit 4c48c279de
2539 changed files with 2412708 additions and 0 deletions

145
vendor/react/promise-timer/CHANGELOG.md vendored Executable file
View File

@@ -0,0 +1,145 @@
# Changelog
## 1.11.0 (2024-06-04)
* Feature: Improve PHP 8.4+ support by avoiding implicitly nullable type declarations.
(#70 by @clue)
* Feature: Full PHP 8.3 compatibility.
(#68 by @clue)
## 1.10.0 (2023-07-20)
* Feature: Use Promise v3 template types.
(#67 by @clue and #63 and #64 by @WyriHaximus)
* Minor documentation improvements.
(#59 by @nhedger)
* Improve test suite, avoid unhandled promise rejections and report failed assertions.
(#66 and #62 by @clue and #61 by @WyriHaximus)
## 1.9.0 (2022-06-13)
* Feature: Improve forward compatibility with upcoming Promise v3 API.
(#54 and #55 by @clue)
* Minor documentation improvements for upcoming Promise v3.
(#58 by @clue and #56 by @SimonFrings)
* Improve test suite, fix legacy HHVM build by downgrading Composer.
(#57 by @SimonFrings)
## 1.8.0 (2021-12-06)
* Feature: Add new `sleep()` function and deprecate `resolve()` and `reject()` functions.
(#51 by @clue)
```php
// deprecated
React\Promise\Timer\resolve($time);
React\Promise\Timer\reject($time);
// new
React\Promise\Timer\sleep($time);
```
* Feature: Support PHP 8.1 release.
(#50 by @Thomas-Gelf, #52 by @clue and #48 by @SimonFrings)
* Improve API documentation and add parameter types and return types.
(#49 by @clue and #47 by @SimonFrings)
## 1.7.0 (2021-07-11)
A major new feature release, see [**release announcement**](https://clue.engineering/2021/announcing-reactphp-default-loop).
* Feature: Simplify usage by supporting new [default loop](https://reactphp.org/event-loop/#loop).
(#46 by @clue)
```php
// old (still supported)
$promise = timeout($promise, $time, $loop);
$promise = resolve($time, $loop);
$promise = reject($time, $loop);
// new (using default loop)
$promise = timeout($promise, $time);
$promise = resolve($time);
$promise = reject($time);
```
* Improve test suite, use GitHub actions for continuous integration (CI),
update PHPUnit config, run tests on PHP 8 and add full core team to the license.
(#43 by @WyriHaximus, #44 and #45 by @SimonFrings)
## 1.6.0 (2020-07-10)
* Feature: Forward compatibility with react/promise v3.
(#37 by @WyriHaximus)
* Improve test suite and add `.gitattributes` to exclude dev files from exports.
Run tests on PHPUnit 9 and PHP 7.4 and clean up test suite.
(#38 by @WyriHaximus, #39 by @reedy, #41 by @clue and #42 by @SimonFrings)
## 1.5.1 (2019-03-27)
* Fix: Typo in readme
(#35 by @aak74)
* Improvement: Only include functions file when functions aren't defined
(#36 by @Niko9911)
## 1.5.0 (2018-06-13)
* Feature: Improve memory consumption by cleaning up garbage references to pending promise without canceller.
(#34 by @clue)
## 1.4.0 (2018-06-11)
* Feature: Improve memory consumption by cleaning up garbage references.
(#33 by @clue)
## 1.3.0 (2018-04-24)
* Feature: Improve memory consumption by cleaning up unneeded references.
(#32 by @clue)
## 1.2.1 (2017-12-22)
* README improvements
(#28 by @jsor)
* Improve test suite by adding forward compatiblity with PHPUnit 6 and
fix test suite forward compatibility with upcoming EventLoop releases
(#30 and #31 by @clue)
## 1.2.0 (2017-08-08)
* Feature: Only start timers if input Promise is still pending and
return a settled output promise if the input is already settled.
(#25 by @clue)
* Feature: Cap minimum timer interval at 1µs across all versions
(#23 by @clue)
* Feature: Forward compatibility with EventLoop v1.0 and v0.5
(#27 by @clue)
* Improve test suite by adding PHPUnit to require-dev and
lock Travis distro so new defaults will not break the build
(#24 and #26 by @clue)
## 1.1.1 (2016-12-27)
* Improve test suite to use PSR-4 autoloader and proper namespaces.
(#21 by @clue)
## 1.1.0 (2016-02-29)
* Feature: Support promise cancellation for all timer primitives
(#18 by @clue)
## 1.0.0 (2015-09-29)
* First tagged release

21
vendor/react/promise-timer/LICENSE vendored Executable file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 Christian Lück, Cees-Jan Kiewiet, Jan Sorgalla, Chris Boden
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

318
vendor/react/promise-timer/README.md vendored Executable file
View File

@@ -0,0 +1,318 @@
# PromiseTimer
[![CI status](https://github.com/reactphp/promise-timer/actions/workflows/ci.yml/badge.svg)](https://github.com/reactphp/promise-timer/actions)
[![installs on Packagist](https://img.shields.io/packagist/dt/react/promise-timer?color=blue&label=installs%20on%20Packagist)](https://packagist.org/packages/react/promise-timer)
A trivial implementation of timeouts for `Promise`s, built on top of [ReactPHP](https://reactphp.org/).
**Table of contents**
* [Usage](#usage)
* [timeout()](#timeout)
* [sleep()](#sleep)
* [~~resolve()~~](#resolve)
* [~~reject()~~](#reject)
* [TimeoutException](#timeoutexception)
* [getTimeout()](#gettimeout)
* [Install](#install)
* [Tests](#tests)
* [License](#license)
## Usage
This lightweight library consists only of a few simple functions.
All functions reside under the `React\Promise\Timer` namespace.
The below examples refer to all functions with their fully-qualified names like this:
```php
React\Promise\Timer\timeout();
```
As of PHP 5.6+ you can also import each required function into your code like this:
```php
use function React\Promise\Timer\timeout;
timeout();
```
Alternatively, you can also use an import statement similar to this:
```php
use React\Promise\Timer;
Timer\timeout();
```
### timeout()
The `timeout(PromiseInterface<T> $promise, float $time, ?LoopInterface $loop = null): PromiseInterface<T>` function can be used to
cancel operations that take *too long*.
You need to pass in an input `$promise` that represents a pending operation
and timeout parameters. It returns a new promise with the following
resolution behavior:
- If the input `$promise` resolves before `$time` seconds, resolve the
resulting promise with its fulfillment value.
- If the input `$promise` rejects before `$time` seconds, reject the
resulting promise with its rejection value.
- If the input `$promise` does not settle before `$time` seconds, *cancel*
the operation and reject the resulting promise with a [`TimeoutException`](#timeoutexception).
Internally, the given `$time` value will be used to start a timer that will
*cancel* the pending operation once it triggers. This implies that if you
pass a really small (or negative) value, it will still start a timer and will
thus trigger at the earliest possible time in the future.
If the input `$promise` is already settled, then the resulting promise will
resolve or reject immediately without starting a timer at all.
This function takes an optional `LoopInterface|null $loop` parameter that can be used to
pass the event loop instance to use. You can use a `null` value here in order to
use the [default loop](https://github.com/reactphp/event-loop#loop). This value
SHOULD NOT be given unless you're sure you want to explicitly use a given event
loop instance.
A common use case for handling only resolved values looks like this:
```php
$promise = accessSomeRemoteResource();
React\Promise\Timer\timeout($promise, 10.0)->then(function ($value) {
// the operation finished within 10.0 seconds
});
```
A more complete example could look like this:
```php
$promise = accessSomeRemoteResource();
React\Promise\Timer\timeout($promise, 10.0)->then(
function ($value) {
// the operation finished within 10.0 seconds
},
function ($error) {
if ($error instanceof React\Promise\Timer\TimeoutException) {
// the operation has failed due to a timeout
} else {
// the input operation has failed due to some other error
}
}
);
```
Or if you're using [react/promise v3](https://github.com/reactphp/promise):
```php
React\Promise\Timer\timeout($promise, 10.0)->then(function ($value) {
// the operation finished within 10.0 seconds
})->catch(function (React\Promise\Timer\TimeoutException $error) {
// the operation has failed due to a timeout
})->catch(function (Throwable $error) {
// the input operation has failed due to some other error
});
```
As discussed above, the [`timeout()`](#timeout) function will take care of
the underlying operation if it takes *too long*. In this case, you can be
sure the resulting promise will always be rejected with a
[`TimeoutException`](#timeoutexception). On top of this, the function will
try to *cancel* the underlying operation. Responsibility for this
cancellation logic is left up to the underlying operation.
- A common use case involves cleaning up any resources like open network
sockets or file handles or terminating external processes or timers.
- If the given input `$promise` does not support cancellation, then this is a
NO-OP. This means that while the resulting promise will still be rejected,
the underlying input `$promise` may still be pending and can hence continue
consuming resources
On top of this, the returned promise is implemented in such a way that it can
be cancelled when it is still pending. Cancelling a pending promise will
cancel the underlying operation. As discussed above, responsibility for this
cancellation logic is left up to the underlying operation.
```php
$promise = accessSomeRemoteResource();
$timeout = React\Promise\Timer\timeout($promise, 10.0);
$timeout->cancel();
```
For more details on the promise cancellation, please refer to the
[Promise documentation](https://github.com/reactphp/promise#cancellablepromiseinterface).
If you want to wait for multiple promises to resolve, you can use the normal
promise primitives like this:
```php
$promises = array(
accessSomeRemoteResource(),
accessSomeRemoteResource(),
accessSomeRemoteResource()
);
$promise = React\Promise\all($promises);
React\Promise\Timer\timeout($promise, 10)->then(function ($values) {
// *all* promises resolved
});
```
The applies to all promise collection primitives alike, i.e. `all()`,
`race()`, `any()`, `some()` etc.
For more details on the promise primitives, please refer to the
[Promise documentation](https://github.com/reactphp/promise#functions).
### sleep()
The `sleep(float $time, ?LoopInterface $loop = null): PromiseInterface<void>` function can be used to
create a new promise that resolves in `$time` seconds.
```php
React\Promise\Timer\sleep(1.5)->then(function () {
echo 'Thanks for waiting!' . PHP_EOL;
});
```
Internally, the given `$time` value will be used to start a timer that will
resolve the promise once it triggers. This implies that if you pass a really
small (or negative) value, it will still start a timer and will thus trigger
at the earliest possible time in the future.
This function takes an optional `LoopInterface|null $loop` parameter that can be used to
pass the event loop instance to use. You can use a `null` value here in order to
use the [default loop](https://github.com/reactphp/event-loop#loop). This value
SHOULD NOT be given unless you're sure you want to explicitly use a given event
loop instance.
The returned promise is implemented in such a way that it can be cancelled
when it is still pending. Cancelling a pending promise will reject its value
with a `RuntimeException` and clean up any pending timers.
```php
$timer = React\Promise\Timer\sleep(2.0);
$timer->cancel();
```
### ~~resolve()~~
> Deprecated since v1.8.0, see [`sleep()`](#sleep) instead.
The `resolve(float $time, ?LoopInterface $loop = null): PromiseInterface<float>` function can be used to
create a new promise that resolves in `$time` seconds with the `$time` as the fulfillment value.
```php
React\Promise\Timer\resolve(1.5)->then(function ($time) {
echo 'Thanks for waiting ' . $time . ' seconds' . PHP_EOL;
});
```
Internally, the given `$time` value will be used to start a timer that will
resolve the promise once it triggers. This implies that if you pass a really
small (or negative) value, it will still start a timer and will thus trigger
at the earliest possible time in the future.
This function takes an optional `LoopInterface|null $loop` parameter that can be used to
pass the event loop instance to use. You can use a `null` value here in order to
use the [default loop](https://github.com/reactphp/event-loop#loop). This value
SHOULD NOT be given unless you're sure you want to explicitly use a given event
loop instance.
The returned promise is implemented in such a way that it can be cancelled
when it is still pending. Cancelling a pending promise will reject its value
with a `RuntimeException` and clean up any pending timers.
```php
$timer = React\Promise\Timer\resolve(2.0);
$timer->cancel();
```
### ~~reject()~~
> Deprecated since v1.8.0, see [`sleep()`](#sleep) instead.
The `reject(float $time, ?LoopInterface $loop = null): PromiseInterface<never>` function can be used to
create a new promise which rejects in `$time` seconds with a `TimeoutException`.
```php
React\Promise\Timer\reject(2.0)->then(null, function (React\Promise\Timer\TimeoutException $e) {
echo 'Rejected after ' . $e->getTimeout() . ' seconds ' . PHP_EOL;
});
```
Internally, the given `$time` value will be used to start a timer that will
reject the promise once it triggers. This implies that if you pass a really
small (or negative) value, it will still start a timer and will thus trigger
at the earliest possible time in the future.
This function takes an optional `LoopInterface|null $loop` parameter that can be used to
pass the event loop instance to use. You can use a `null` value here in order to
use the [default loop](https://github.com/reactphp/event-loop#loop). This value
SHOULD NOT be given unless you're sure you want to explicitly use a given event
loop instance.
The returned promise is implemented in such a way that it can be cancelled
when it is still pending. Cancelling a pending promise will reject its value
with a `RuntimeException` and clean up any pending timers.
```php
$timer = React\Promise\Timer\reject(2.0);
$timer->cancel();
```
### TimeoutException
The `TimeoutException` extends PHP's built-in `RuntimeException`.
#### getTimeout()
The `getTimeout(): float` method can be used to
get the timeout value in seconds.
## Install
The recommended way to install this library is [through Composer](https://getcomposer.org/).
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
This project follows [SemVer](https://semver.org/).
This will install the latest supported version:
```bash
composer require react/promise-timer:^1.11
```
See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
This project aims to run on any platform and thus does not require any PHP
extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
HHVM.
It's *highly recommended to use the latest supported PHP version* for this project.
## Tests
To run the test suite, you first need to clone this repo and then install all
dependencies [through Composer](https://getcomposer.org/):
```bash
composer install
```
To run the test suite, go to the project root and run:
```bash
vendor/bin/phpunit
```
## License
MIT, see [LICENSE file](LICENSE).

50
vendor/react/promise-timer/composer.json vendored Executable file
View File

@@ -0,0 +1,50 @@
{
"name": "react/promise-timer",
"description": "A trivial implementation of timeouts for Promises, built on top of ReactPHP.",
"keywords": ["Promise", "timeout", "timer", "event-loop", "ReactPHP", "async"],
"homepage": "https://github.com/reactphp/promise-timer",
"license": "MIT",
"authors": [
{
"name": "Christian Lück",
"homepage": "https://clue.engineering/",
"email": "christian@clue.engineering"
},
{
"name": "Cees-Jan Kiewiet",
"homepage": "https://wyrihaximus.net/",
"email": "reactphp@ceesjankiewiet.nl"
},
{
"name": "Jan Sorgalla",
"homepage": "https://sorgalla.com/",
"email": "jsorgalla@gmail.com"
},
{
"name": "Chris Boden",
"homepage": "https://cboden.dev/",
"email": "cboden@gmail.com"
}
],
"require": {
"php": ">=5.3",
"react/event-loop": "^1.2",
"react/promise": "^3.2 || ^2.7.0 || ^1.2.1"
},
"require-dev": {
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36"
},
"autoload": {
"psr-4": {
"React\\Promise\\Timer\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"autoload-dev": {
"psr-4": {
"React\\Tests\\Promise\\Timer\\": "tests/"
}
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace React\Promise\Timer;
use RuntimeException;
class TimeoutException extends RuntimeException
{
/** @var float */
private $timeout;
/**
* @param float $timeout
* @param string|null $message
* @param int|null $code
* @param null|\Exception|\Throwable $previous
*/
public function __construct($timeout, $message = '', $code = 0, $previous = null)
{
// Preserve compatibility with our former nullable signature, but avoid invalid arguments for the parent constructor:
parent::__construct((string) $message, (int) $code, $previous);
$this->timeout = (float) $timeout;
}
/**
* Get the timeout value in seconds.
*
* @return float
*/
public function getTimeout()
{
return $this->timeout;
}
}

339
vendor/react/promise-timer/src/functions.php vendored Executable file
View File

@@ -0,0 +1,339 @@
<?php
namespace React\Promise\Timer;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
/**
* Cancel operations that take *too long*.
*
* You need to pass in an input `$promise` that represents a pending operation
* and timeout parameters. It returns a new promise with the following
* resolution behavior:
*
* - If the input `$promise` resolves before `$time` seconds, resolve the
* resulting promise with its fulfillment value.
*
* - If the input `$promise` rejects before `$time` seconds, reject the
* resulting promise with its rejection value.
*
* - If the input `$promise` does not settle before `$time` seconds, *cancel*
* the operation and reject the resulting promise with a [`TimeoutException`](#timeoutexception).
*
* Internally, the given `$time` value will be used to start a timer that will
* *cancel* the pending operation once it triggers. This implies that if you
* pass a really small (or negative) value, it will still start a timer and will
* thus trigger at the earliest possible time in the future.
*
* If the input `$promise` is already settled, then the resulting promise will
* resolve or reject immediately without starting a timer at all.
*
* This function takes an optional `LoopInterface|null $loop` parameter that can be used to
* pass the event loop instance to use. You can use a `null` value here in order to
* use the [default loop](https://github.com/reactphp/event-loop#loop). This value
* SHOULD NOT be given unless you're sure you want to explicitly use a given event
* loop instance.
*
* A common use case for handling only resolved values looks like this:
*
* ```php
* $promise = accessSomeRemoteResource();
* React\Promise\Timer\timeout($promise, 10.0)->then(function ($value) {
* // the operation finished within 10.0 seconds
* });
* ```
*
* A more complete example could look like this:
*
* ```php
* $promise = accessSomeRemoteResource();
* React\Promise\Timer\timeout($promise, 10.0)->then(
* function ($value) {
* // the operation finished within 10.0 seconds
* },
* function ($error) {
* if ($error instanceof React\Promise\Timer\TimeoutException) {
* // the operation has failed due to a timeout
* } else {
* // the input operation has failed due to some other error
* }
* }
* );
* ```
*
* Or if you're using [react/promise v3](https://github.com/reactphp/promise):
*
* ```php
* React\Promise\Timer\timeout($promise, 10.0)->then(function ($value) {
* // the operation finished within 10.0 seconds
* })->catch(function (React\Promise\Timer\TimeoutException $error) {
* // the operation has failed due to a timeout
* })->catch(function (Throwable $error) {
* // the input operation has failed due to some other error
* });
* ```
*
* As discussed above, the [`timeout()`](#timeout) function will take care of
* the underlying operation if it takes *too long*. In this case, you can be
* sure the resulting promise will always be rejected with a
* [`TimeoutException`](#timeoutexception). On top of this, the function will
* try to *cancel* the underlying operation. Responsibility for this
* cancellation logic is left up to the underlying operation.
*
* - A common use case involves cleaning up any resources like open network
* sockets or file handles or terminating external processes or timers.
*
* - If the given input `$promise` does not support cancellation, then this is a
* NO-OP. This means that while the resulting promise will still be rejected,
* the underlying input `$promise` may still be pending and can hence continue
* consuming resources
*
* On top of this, the returned promise is implemented in such a way that it can
* be cancelled when it is still pending. Cancelling a pending promise will
* cancel the underlying operation. As discussed above, responsibility for this
* cancellation logic is left up to the underlying operation.
*
* ```php
* $promise = accessSomeRemoteResource();
* $timeout = React\Promise\Timer\timeout($promise, 10.0);
*
* $timeout->cancel();
* ```
*
* For more details on the promise cancellation, please refer to the
* [Promise documentation](https://github.com/reactphp/promise#cancellablepromiseinterface).
*
* If you want to wait for multiple promises to resolve, you can use the normal
* promise primitives like this:
*
* ```php
* $promises = array(
* accessSomeRemoteResource(),
* accessSomeRemoteResource(),
* accessSomeRemoteResource()
* );
*
* $promise = React\Promise\all($promises);
*
* React\Promise\Timer\timeout($promise, 10)->then(function ($values) {
* // *all* promises resolved
* });
* ```
*
* The applies to all promise collection primitives alike, i.e. `all()`,
* `race()`, `any()`, `some()` etc.
*
* For more details on the promise primitives, please refer to the
* [Promise documentation](https://github.com/reactphp/promise#functions).
*
* @template T
* @param PromiseInterface<T> $promise
* @param float $time
* @param ?LoopInterface $loop
* @return PromiseInterface<T>
*/
function timeout(PromiseInterface $promise, $time, $loop = null)
{
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
}
// cancelling this promise will only try to cancel the input promise,
// thus leaving responsibility to the input promise.
$canceller = null;
if (\method_exists($promise, 'cancel')) {
// pass promise by reference to clean reference after cancellation handler
// has been invoked once in order to avoid garbage references in call stack.
$canceller = function () use (&$promise) {
$promise->cancel();
$promise = null;
};
}
if ($loop === null) {
$loop = Loop::get();
}
return new Promise(function ($resolve, $reject) use ($loop, $time, $promise) {
$timer = null;
$promise = $promise->then(function ($v) use (&$timer, $loop, $resolve) {
if ($timer) {
$loop->cancelTimer($timer);
}
$timer = false;
$resolve($v);
}, function ($v) use (&$timer, $loop, $reject) {
if ($timer) {
$loop->cancelTimer($timer);
}
$timer = false;
$reject($v);
});
// promise already resolved => no need to start timer
if ($timer === false) {
return;
}
// start timeout timer which will cancel the input promise
$timer = $loop->addTimer($time, function () use ($time, &$promise, $reject) {
$reject(new TimeoutException($time, 'Timed out after ' . $time . ' seconds'));
// try to invoke cancellation handler of input promise and then clean
// reference in order to avoid garbage references in call stack.
if (\method_exists($promise, 'cancel')) {
$promise->cancel();
}
$promise = null;
});
}, $canceller);
}
/**
* Create a new promise that resolves in `$time` seconds.
*
* ```php
* React\Promise\Timer\sleep(1.5)->then(function () {
* echo 'Thanks for waiting!' . PHP_EOL;
* });
* ```
*
* Internally, the given `$time` value will be used to start a timer that will
* resolve the promise once it triggers. This implies that if you pass a really
* small (or negative) value, it will still start a timer and will thus trigger
* at the earliest possible time in the future.
*
* This function takes an optional `LoopInterface|null $loop` parameter that can be used to
* pass the event loop instance to use. You can use a `null` value here in order to
* use the [default loop](https://github.com/reactphp/event-loop#loop). This value
* SHOULD NOT be given unless you're sure you want to explicitly use a given event
* loop instance.
*
* The returned promise is implemented in such a way that it can be cancelled
* when it is still pending. Cancelling a pending promise will reject its value
* with a `RuntimeException` and clean up any pending timers.
*
* ```php
* $timer = React\Promise\Timer\sleep(2.0);
*
* $timer->cancel();
* ```
*
* @param float $time
* @param ?LoopInterface $loop
* @return PromiseInterface<void>
*/
function sleep($time, $loop = null)
{
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface');
}
if ($loop === null) {
$loop = Loop::get();
}
$timer = null;
return new Promise(function ($resolve) use ($loop, $time, &$timer) {
// resolve the promise when the timer fires in $time seconds
$timer = $loop->addTimer($time, function () use ($resolve) {
$resolve(null);
});
}, function () use (&$timer, $loop) {
// cancelling this promise will cancel the timer, clean the reference
// in order to avoid garbage references in call stack and then reject.
$loop->cancelTimer($timer);
$timer = null;
throw new \RuntimeException('Timer cancelled');
});
}
/**
* [Deprecated] Create a new promise that resolves in `$time` seconds with the `$time` as the fulfillment value.
*
* ```php
* React\Promise\Timer\resolve(1.5)->then(function ($time) {
* echo 'Thanks for waiting ' . $time . ' seconds' . PHP_EOL;
* });
* ```
*
* Internally, the given `$time` value will be used to start a timer that will
* resolve the promise once it triggers. This implies that if you pass a really
* small (or negative) value, it will still start a timer and will thus trigger
* at the earliest possible time in the future.
*
* This function takes an optional `LoopInterface|null $loop` parameter that can be used to
* pass the event loop instance to use. You can use a `null` value here in order to
* use the [default loop](https://github.com/reactphp/event-loop#loop). This value
* SHOULD NOT be given unless you're sure you want to explicitly use a given event
* loop instance.
*
* The returned promise is implemented in such a way that it can be cancelled
* when it is still pending. Cancelling a pending promise will reject its value
* with a `RuntimeException` and clean up any pending timers.
*
* ```php
* $timer = React\Promise\Timer\resolve(2.0);
*
* $timer->cancel();
* ```
*
* @param float $time
* @param ?LoopInterface $loop
* @return PromiseInterface<float>
* @deprecated 1.8.0 See `sleep()` instead
* @see sleep()
*/
function resolve($time, $loop = null)
{
return sleep($time, $loop)->then(function() use ($time) {
return $time;
});
}
/**
* [Deprecated] Create a new promise which rejects in `$time` seconds with a `TimeoutException`.
*
* ```php
* React\Promise\Timer\reject(2.0)->then(null, function (React\Promise\Timer\TimeoutException $e) {
* echo 'Rejected after ' . $e->getTimeout() . ' seconds ' . PHP_EOL;
* });
* ```
*
* Internally, the given `$time` value will be used to start a timer that will
* reject the promise once it triggers. This implies that if you pass a really
* small (or negative) value, it will still start a timer and will thus trigger
* at the earliest possible time in the future.
*
* This function takes an optional `LoopInterface|null $loop` parameter that can be used to
* pass the event loop instance to use. You can use a `null` value here in order to
* use the [default loop](https://github.com/reactphp/event-loop#loop). This value
* SHOULD NOT be given unless you're sure you want to explicitly use a given event
* loop instance.
*
* The returned promise is implemented in such a way that it can be cancelled
* when it is still pending. Cancelling a pending promise will reject its value
* with a `RuntimeException` and clean up any pending timers.
*
* ```php
* $timer = React\Promise\Timer\reject(2.0);
*
* $timer->cancel();
* ```
*
* @param float $time
* @param ?LoopInterface $loop
* @return PromiseInterface<never>
* @deprecated 1.8.0 See `sleep()` instead
* @see sleep()
*/
function reject($time, $loop = null)
{
return sleep($time, $loop)->then(function () use ($time) {
throw new TimeoutException($time, 'Timer expired after ' . $time . ' seconds');
});
}

View File

@@ -0,0 +1,7 @@
<?php
namespace React\Promise\Timer;
if (!function_exists('React\\Promise\\Timer\\timeout')) {
require __DIR__ . '/functions.php';
}