Skip to content

Commit b9b09c5

Browse files
committed
Add promise adapter
Now promise adapters comes out of the box: * `McGWeb\PromiseFactory\PromiseFactoryInterface` is replaced by `Overblog\PromiseAdapter\PromiseAdapterInterface`, where the main difference between both interfaces are methods `createResolve` renamed to `createFulfilled` and `createReject` to `createRejected`
1 parent 11c2f80 commit b9b09c5

25 files changed

+898
-141
lines changed

.editorconfig

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 2
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
indent_size = 4
13+
trim_trailing_whitespace = false
14+
15+
[*.mk,Makefile]
16+
indent_style = tab
17+
18+
[*.php]
19+
indent_size = 4

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor/
2+
/bin
23
composer.lock
34
composer.phar
45
phpunit.xml

.php_cs

+15-21
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
require_once __DIR__.'/vendor/autoload.php';
13-
14-
use SLLH\StyleCIBridge\ConfigBridge;
1512
use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;
1613

1714
$header = <<<EOF
@@ -23,21 +20,18 @@ For the full copyright and license information, please view the LICENSE
2320
file that was distributed with this source code.
2421
EOF;
2522

26-
// PHP-CS-Fixer 1.x
27-
if (method_exists('Symfony\CS\Fixer\Contrib\HeaderCommentFixer', 'getHeader')) {
28-
HeaderCommentFixer::setHeader($header);
29-
}
30-
31-
$config = ConfigBridge::create();
32-
33-
// PHP-CS-Fixer 2.x
34-
if (method_exists($config, 'setRules')) {
35-
$config->setRules(array_merge($config->getRules(), [
36-
'header_comment' => ['header' => $header]
37-
]));
38-
}
39-
40-
return $config
41-
->setUsingCache(true)
42-
->fixers(array_merge($config->getFixers(), ['header_comment']))
43-
;
23+
return PhpCsFixer\Config::create()
24+
->setRules([
25+
'@PSR2' => true,
26+
'array_syntax' => ['syntax' => 'short'],
27+
'no_unreachable_default_argument_value' => false,
28+
'heredoc_to_nowdoc' => false,
29+
'header_comment' => ['header' => $header],
30+
])
31+
->setRiskyAllowed(true)
32+
->setFinder(
33+
PhpCsFixer\Finder::create()
34+
->in(__DIR__)
35+
->exclude(['vendor'])
36+
)
37+
;

.styleci.yml

-7
This file was deleted.

.travis.yml

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
language: php
22

33
php:
4-
- nightly
5-
- hhvm
6-
- 5.5
7-
- 5.6
8-
- 7.0
9-
- 7.1
4+
- hhvm
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- 7.1
109

1110
branches:
12-
only:
13-
- master
14-
- /^\d+\.\d+$/
15-
16-
allow_failures:
17-
- php: nightly
11+
only:
12+
- master
13+
- /^\d+\.\d+$/
1814

1915
cache:
20-
directories:
21-
- $HOME/.composer/cache
16+
directories:
17+
- $HOME/.composer/cache
2218

2319
before_install:
24-
- if [[ "$TRAVIS_PHP_VERSION" != "5.6" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini || true; fi
25-
- composer selfupdate
26-
- composer require "guzzlehttp/promises"
20+
- if [[ "$TRAVIS_PHP_VERSION" != "5.6" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini || true; fi
21+
- composer selfupdate
2722

2823
install: composer update --prefer-dist --no-interaction
2924

30-
script: if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then phpunit -d xdebug.max_nesting_level=1000 --debug --coverage-clover build/logs/clover.xml; else phpunit --debug; fi
25+
script:
26+
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then bin/phpunit --debug --coverage-clover build/logs/clover.xml; else bin/phpunit --debug; fi
27+
- if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then composer require "friendsofphp/php-cs-fixer:^2.0" && bin/php-cs-fixer fix --diff --dry-run -v; fi;
3128

3229
after_success:
33-
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then composer require "satooshi/php-coveralls:^1.0" && travis_retry php vendor/bin/coveralls -v; fi
30+
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then composer require "satooshi/php-coveralls:^1.0" && travis_retry php vendor/bin/coveralls -v; fi

LICENSE.txt LICENSE

File renamed without changes.

README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ Create loaders by providing a batch loading instance.
3131
use Overblog\DataLoader\DataLoader;
3232

3333
$myBatchGetUsers = function ($keys) { /* ... */ };
34-
$promiseFactory = new MyPromiseFactory();
34+
$promiseAdapter = new MyPromiseAdapter();
3535

36-
$userLoader = new DataLoader($myBatchGetUsers, $promiseFactory);
36+
$userLoader = new DataLoader($myBatchGetUsers, $promiseAdapter);
3737
```
3838

3939
A batch loading callable / callback accepts an Array of keys, and returns a Promise which
@@ -123,12 +123,12 @@ Each `DataLoaderPHP` instance contains a unique memoized cache. Use caution when
123123
used in long-lived applications or those which serve many users with different
124124
access permissions and consider creating a new instance per web request.
125125

126-
##### `new DataLoader(callable $batchLoadFn, PromiseFactoryInterface $promiseFactory [, Option $options])`
126+
##### `new DataLoader(callable $batchLoadFn, PromiseAdapterInterface $promiseAdapter [, Option $options])`
127127

128128
Create a new `DataLoaderPHP` given a batch loading instance and options.
129129

130130
- *$batchLoadFn*: A callable / callback which accepts an Array of keys, and returns a Promise which resolves to an Array of values.
131-
- *$promiseFactory*: Any object that implements `McGWeb\PromiseFactory\PromiseFactoryInterface`. (see [McGWeb/Promise-Factory](https://github.com/mcg-web/promise-factory))
131+
- *$promiseAdapter*: Any object that implements `Overblog\PromiseAdapter\PromiseAdapterInterface`. (see [Overblog/Promise-Adapter](./lib/promise-adapter/docs/usage.md))
132132
- *$options*: An optional object of options:
133133

134134
- *batch*: Default `true`. Set to `false` to disable batching, instead
@@ -163,7 +163,7 @@ list($a, $b) = DataLoader::await($myLoader->loadMany(['a', 'b']));
163163

164164
This is equivalent to the more verbose:
165165

166-
```js
166+
```php
167167
list($a, $b) = DataLoader::await(\React\Promise\all([
168168
$myLoader->load('a'),
169169
$myLoader->load('b')
@@ -202,10 +202,8 @@ Await method process all waiting promise in all dataLoaderPHP instances.
202202
- *$unwrap*: controls whether or not the value of the promise is returned for a fulfilled promise
203203
or if an exception is thrown if the promise is rejected. Default `true`.
204204

205-
## Using with Webonyx/GraphQL [WIP]
205+
## Using with Webonyx/GraphQL
206206

207-
A [PR](https://github.com/webonyx/graphql-php/pull/67) is open on [Webonyx/GraphQL](https://github.com/webonyx/graphql-php)
208-
to supports DataLoaderPHP and more generally promise.
209207
Here [an example](https://github.com/mcg-web/sandbox-dataloader-graphql-php/blob/master/with-dataloader.php).
210208

211209
## Credits

composer.json

+40-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
{
2-
"name": "overblog/dataloader-php",
3-
"type": "library",
4-
"license": "MIT",
5-
"description": "DataLoaderPhp is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.",
6-
"keywords": ["dataLoader", "caching", "batching"],
7-
"autoload": {
8-
"psr-4": {
9-
"Overblog\\DataLoader\\": "src/"
10-
}
11-
},
12-
"autoload-dev": {
13-
"psr-4": {
14-
"Overblog\\DataLoader\\Tests\\": "tests/"
15-
}
16-
},
17-
"require": {
18-
"php": "^5.5|^7.0",
19-
"mcg-web/promise-factory": "^0.2"
20-
},
21-
"require-dev": {
22-
"phpunit/phpunit": "^4.1|^5.1"
2+
"name": "overblog/dataloader-php",
3+
"type": "library",
4+
"license": "MIT",
5+
"description": "DataLoaderPhp is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.",
6+
"keywords": ["dataLoader", "caching", "batching"],
7+
"config" : {
8+
"bin-dir": "bin",
9+
"sort-packages": true
10+
},
11+
"autoload": {
12+
"psr-4": {
13+
"Overblog\\DataLoader\\": "src/",
14+
"Overblog\\PromiseAdapter\\": "lib/promise-adapter/src/"
2315
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"Overblog\\DataLoader\\Test\\": "tests/",
20+
"Overblog\\PromiseAdapter\\Test\\": "lib/promise-adapter/tests/"
21+
}
22+
},
23+
"replace": {
24+
"overblog/promise-adapter": "self.version"
25+
},
26+
"require": {
27+
"php": "^5.5|^7.0"
28+
},
29+
"require-dev": {
30+
"guzzlehttp/promises": "^1.3.0",
31+
"phpunit/phpunit": "^4.1|^5.1",
32+
"react/promise": "^2.5.0"
33+
},
34+
"suggest": {
35+
"guzzlehttp/promises": "To use with Guzzle promise",
36+
"react/promise": "To use with ReactPhp promise"
37+
},
38+
"extra": {
39+
"branch-alias": {
40+
"dev-master": "0.3-dev"
41+
}
42+
}
2443
}

lib/promise-adapter/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor/
2+
composer.lock
3+
composer.phar
4+
phpunit.xml
5+
/bin

lib/promise-adapter/.travis.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
language: php
2+
3+
php:
4+
- hhvm
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- 7.1
9+
10+
branches:
11+
only:
12+
- master
13+
- /^\d+\.\d+$/
14+
15+
cache:
16+
directories:
17+
- $HOME/.composer/cache
18+
19+
before_install:
20+
- if [[ "$TRAVIS_PHP_VERSION" != "5.6" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini || true; fi
21+
- composer selfupdate
22+
23+
install: composer update --prefer-dist --no-interaction
24+
25+
script: if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then bin/phpunit --debug --coverage-clover build/logs/clover.xml; else bin/phpunit --debug; fi
26+
27+
after_success:
28+
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then composer require "satooshi/php-coveralls:^1.0" && travis_retry php vendor/bin/coveralls -v; fi

lib/promise-adapter/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Overblog
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
13+
all 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
21+
THE SOFTWARE.

lib/promise-adapter/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# PromiseAdapter
2+
3+
This library tries to create a simple promise adapter standard while waiting for a psr.
4+
It Comes out of the box with adapter for [ReactPhp/Promise](https://github.com/reactphp/promise) and
5+
[Guzzle/Promises](https://github.com/guzzle/promises).
6+
7+
[![Build Status](https://travis-ci.org/overblog/promise-adapter.svg?branch=master)](https://travis-ci.org/overblog/promise-adapter)
8+
[![Coverage Status](https://coveralls.io/repos/github/overblog/promise-adapter/badge.svg?branch=master)](https://coveralls.io/github/overblog/promise-adapter?branch=master)
9+
[![Latest Stable Version](https://poser.pugx.org/overblog/promise-adapter/version)](https://packagist.org/packages/overblog/promise-adapter)
10+
[![License](https://poser.pugx.org/overblog/promise-adapter/license)](https://packagist.org/packages/overblog/promise-adapter)
11+
12+
## Installation
13+
14+
First, install PromiseAdapter using composer.
15+
16+
```sh
17+
composer require "overblog/promise-adapter"
18+
```
19+
20+
# Usage
21+
22+
see [here](./docs/usage.md)
23+
24+
## License
25+
26+
Overblog/PromiseAdapter is released under the [MIT](https://github.com/overblog/promise-adapter/blob/master/LICENSE) license.

lib/promise-adapter/composer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "overblog/promise-adapter",
3+
"description": "This library tries to create a simple promise adapter standard while waiting for a psr.",
4+
"type": "library",
5+
"keywords": [
6+
"promise",
7+
"adapter",
8+
"guzzle",
9+
"react"
10+
],
11+
"config" : {
12+
"bin-dir": "bin",
13+
"sort-packages": true
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Overblog\\PromiseAdapter\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Overblog\\PromiseAdapter\\Test\\": "tests/"
23+
}
24+
},
25+
"require": {
26+
"php": "^5.5|^7.0"
27+
},
28+
"require-dev": {
29+
"guzzlehttp/promises": "^1.3.0",
30+
"phpunit/phpunit": "^4.1|^5.1",
31+
"react/promise": "^2.5.0"
32+
},
33+
"suggest": {
34+
"guzzlehttp/promises": "To use with Guzzle promise",
35+
"react/promise": "To use with ReactPhp promise"
36+
},
37+
"license": "MIT"
38+
}

lib/promise-adapter/docs/usage.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Promise adapter usage
2+
3+
## Optional requirements
4+
5+
Optional to use Guzzle:
6+
7+
```sh
8+
composer require "guzzlehttp/promises"
9+
```
10+
11+
Optional to use ReactPhp:
12+
13+
```sh
14+
composer require "react/promise"
15+
```
16+
17+
## Supported Adapter
18+
19+
*Guzzle*: `Overblog\PromiseAdapter\Adapter\GuzzleHttpPromiseAdapter`
20+
21+
*ReactPhp*: `Overblog\PromiseAdapter\Adapter\ReactPromiseAdapter`
22+
23+
To use a custom Promise lib you can implement `Overblog\PromiseAdapter\PromiseAdapterInterface`

0 commit comments

Comments
 (0)