├── .gitignore
├── test
├── phpt
│ ├── error_handler_002.phpt
│ ├── error_handler_005.phpt
│ ├── error_handler_006.phpt
│ ├── error_handler_001.phpt
│ ├── error_handler_003.phpt
│ └── error_handler_004.phpt
└── ErrorHandlerTest.php
├── .travis.yml
├── phpunit.xml.dist
├── src
├── Promise.php
└── Promise
│ └── ErrorHandler.php
├── composer.json
├── LICENSE
├── META.md
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | /composer.lock
2 | /vendor/
--------------------------------------------------------------------------------
/test/phpt/error_handler_002.phpt:
--------------------------------------------------------------------------------
1 | --TEST--
2 | ErrorHandler::notify() does not fatal with a handler
3 | --FILE--
4 |
12 | --EXPECT--
13 | 1
14 |
--------------------------------------------------------------------------------
/test/ErrorHandlerTest.php:
--------------------------------------------------------------------------------
1 | assertEquals($f, $expectedF);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - 5.4
5 | - 5.5
6 | - 5.6
7 | - 7.0
8 | - nightly
9 | - hhvm
10 |
11 | matrix:
12 | allow_failures:
13 | - php: nightly
14 | fast_finish: true
15 |
16 | cache:
17 | directories:
18 | - $HOME/.composer/cache
19 |
20 | install:
21 | - composer install
22 | - composer show -t
23 |
24 | script:
25 | - php vendor/bin/parallel-lint --exclude vendor .
26 | - php vendor/bin/phpunit --coverage-text
27 |
--------------------------------------------------------------------------------
/test/phpt/error_handler_005.phpt:
--------------------------------------------------------------------------------
1 | --TEST--
2 | ErrorHandler::set() replaces the current handler
3 | --FILE--
4 |
14 | --EXPECT--
15 | 3
16 |
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ./test
5 |
6 |
7 | ./test/phpt
8 |
9 |
10 |
11 |
12 | ./src
13 |
14 |
15 |
--------------------------------------------------------------------------------
/test/phpt/error_handler_006.phpt:
--------------------------------------------------------------------------------
1 | --TEST--
2 | ErrorHandler::notify() fatals if handler throws
3 | --FILE--
4 |
12 | --EXPECTF--
13 | Fatal error: An exception has been thrown from the promise error handler registered to AsyncInterop\Promise\ErrorHandler::set().
14 |
15 | %s in %s:%d
16 | Stack trace:
17 | #0 {main} in %s on line %d
18 |
--------------------------------------------------------------------------------
/src/Promise.php:
--------------------------------------------------------------------------------
1 |
11 | --EXPECTF--
12 | Fatal error: An exception has been thrown from an AsyncInterop\Promise::when() handler, but no handler has been registered via AsyncInterop\Promise\ErrorHandler::set(). A handler has to be registered to prevent exceptions from going unnoticed. Do NOT install an empty handler that just does nothing. If the handler is called, there is ALWAYS something wrong.
13 |
14 | %s in %s:%d
15 | Stack trace:
16 | #0 {main} in %s on line %d
17 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "async-interop/promise",
3 | "description": "A promise interface for interoperability in async operations.",
4 | "keywords": ["promise", "future", "awaitable", "async", "interop"],
5 | "license": "MIT",
6 | "require": {
7 | "php": ">=5.4.0"
8 | },
9 | "require-dev": {
10 | "phpunit/phpunit": "^4.8.25|^5.3.3",
11 | "jakub-onderka/php-parallel-lint": "^0.9.2",
12 | "jakub-onderka/php-console-highlighter": "^0.3.2"
13 | },
14 | "autoload": {
15 | "psr-4": {
16 | "AsyncInterop\\": "src"
17 | }
18 | },
19 | "autoload-dev": {
20 | "psr-4": {
21 | "AsyncInterop\\Promise\\Test\\": "test"
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/test/phpt/error_handler_003.phpt:
--------------------------------------------------------------------------------
1 | --TEST--
2 | ErrorHandler::notify() fatals after handlers have been removed with ErrorHandler::set(null)
3 | --FILE--
4 |
13 | --EXPECTF--
14 | Fatal error: An exception has been thrown from an AsyncInterop\Promise::when() handler, but no handler has been registered via AsyncInterop\Promise\ErrorHandler::set(). A handler has to be registered to prevent exceptions from going unnoticed. Do NOT install an empty handler that just does nothing. If the handler is called, there is ALWAYS something wrong.
15 |
16 | %s in %s:%d
17 | Stack trace:
18 | #0 {main} in %s on line %d
19 |
--------------------------------------------------------------------------------
/test/phpt/error_handler_004.phpt:
--------------------------------------------------------------------------------
1 | --TEST--
2 | ErrorHandler::notify() converts non-exception to exception
3 | --FILE--
4 |
11 | --EXPECTF--
12 | Fatal error: An exception has been thrown from an AsyncInterop\Promise::when() handler, but no handler has been registered via AsyncInterop\Promise\ErrorHandler::set(). A handler has to be registered to prevent exceptions from going unnoticed. Do NOT install an empty handler that just does nothing. If the handler is called, there is ALWAYS something wrong.
13 |
14 | %SException%SPromise implementation called AsyncInterop\Promise\ErrorHandler::notify() with an invalid argument of type 'integer'%S in %s:%d
15 | Stack trace:
16 | #0 %s(%d): AsyncInterop\Promise\ErrorHandler::notify(%S)
17 | #1 {main} in %s on line %d
18 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 PHP Asynchronous Interoperability Group
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/META.md:
--------------------------------------------------------------------------------
1 | # Promise
2 |
3 | This document describes a few design considerations, for the main specification, refer to [the README document](README.md).
4 |
5 | ## Recommended usage of Promises
6 |
7 | We are explicitly _not_ providing a chainable method, thus we are also not recommending to chain them, but rather to use coroutines in form of generators inside application code.
8 |
9 | Coroutines solve the problem of the so-called callback hell very nicely and also allow proper usage of `try` / `catch`.
10 |
11 | ## Choice of `when()`
12 |
13 | The specification proposes `when()` in order to only expose the most primitive common denominator needed for interoperability, which is a simple callable to be executed after the resolution.
14 |
15 | If implementations wish to adhere to e.g. [Promises/A+ from Javascript](https://promisesaplus.com) (which had been implemented in many PHP Promise libraries at the time of writing this specification) or implement any other methods, they still may do so; `when()` however is the fundamental interoperable primitive every `Promise` is guaranteed to have.
16 |
17 | Additionally, coroutines do not use the returned `Promise` of a `then()` callback, but returning a `Promise` is required by Promises/A+. Thus there's a lot of useless object creations when using Promises the recommended way, which isn't cheap and adds up. This conflicts with the goal of being as lightweight as possible.
18 |
19 | ## Naming
20 |
21 | `Promise` is a recognized and accepted name for future value placeholders, it's not strictly tied to the `Thenable` API.
22 |
23 | ## Creation of Promises
24 |
25 | `Promise` creation and managing is out of scope of this specification, as managing never shall cross library boundaries and thus does not need to be interoperable at all. Each library shall resolve the `Promise` it created itself.
26 |
27 | ## Resolution of Promises
28 |
29 | The specification defines that the value of a `Promise` must never be another `Promise`. Implementations may still accept to be resolved by a promise and then defer the resolution until the passed promise resolves.
30 |
--------------------------------------------------------------------------------
/src/Promise/ErrorHandler.php:
--------------------------------------------------------------------------------
1 |
MAY transition to either the `succeeded` or `failed` state. |
33 | |`succeeded`| - MUST NOT transition to any other state.
- MUST have a value which MUST NOT change.*
|
34 | |`failed` | - MUST NOT transition to any other state.
- MUST have a reason which MUST NOT change.*
|
35 |
36 | * _Must not change_ refers to the _reference_ being immutable in case of an object, _not the object itself_ being immutable.
37 |
38 | A `Promise` is resolved once it either succeeded or failed.
39 |
40 | ## Consumption
41 |
42 | A `Promise` MUST implement `AsyncInterop\Promise` and thus provide a `when()` method to access its value or reason.
43 |
44 | ```php
45 |