Skip to content

Commit

Permalink
Add promise adapter
Browse files Browse the repository at this point in the history
Now promise adapters comes out of the box:
* `McGWeb\PromiseFactory\PromiseFactoryInterface` is replaced by `Overblog\PromiseAdapter\PromiseAdapterInterface`,
where the main difference between both interface is `createResolve` renamed to `createFulfilled` and `createRejecte` to `createRejected`
  • Loading branch information
mcg-web committed Feb 4, 2017
1 parent 11c2f80 commit ac9ab0d
Show file tree
Hide file tree
Showing 25 changed files with 903 additions and 138 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
indent_size = 4
trim_trailing_whitespace = false

[*.mk,Makefile]
indent_style = tab

[*.php]
indent_size = 4
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor/
/bin
composer.lock
composer.phar
phpunit.xml
Expand Down
36 changes: 15 additions & 21 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
* file that was distributed with this source code.
*/

require_once __DIR__.'/vendor/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;
use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;

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

// PHP-CS-Fixer 1.x
if (method_exists('Symfony\CS\Fixer\Contrib\HeaderCommentFixer', 'getHeader')) {
HeaderCommentFixer::setHeader($header);
}

$config = ConfigBridge::create();

// PHP-CS-Fixer 2.x
if (method_exists($config, 'setRules')) {
$config->setRules(array_merge($config->getRules(), [
'header_comment' => ['header' => $header]
]));
}

return $config
->setUsingCache(true)
->fixers(array_merge($config->getFixers(), ['header_comment']))
;
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'no_unreachable_default_argument_value' => false,
'heredoc_to_nowdoc' => false,
'header_comment' => ['header' => $header],
])
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude(['vendor'])
)
;
7 changes: 0 additions & 7 deletions .styleci.yml

This file was deleted.

32 changes: 16 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
language: php

php:
- nightly
- hhvm
- 5.5
- 5.6
- 7.0
- 7.1
- nightly
- hhvm
- 5.5
- 5.6
- 7.0
- 7.1

branches:
only:
- master
- /^\d+\.\d+$/
only:
- master
- /^\d+\.\d+$/

allow_failures:
- php: nightly
- php: nightly

cache:
directories:
- $HOME/.composer/cache
directories:
- $HOME/.composer/cache

before_install:
- if [[ "$TRAVIS_PHP_VERSION" != "5.6" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini || true; fi
- composer selfupdate
- composer require "guzzlehttp/promises"
- if [[ "$TRAVIS_PHP_VERSION" != "5.6" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini || true; fi
- composer selfupdate

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

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

after_success:
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then composer require "satooshi/php-coveralls:^1.0" && travis_retry php vendor/bin/coveralls -v; fi
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then composer require "satooshi/php-coveralls:^1.0" && travis_retry php vendor/bin/coveralls -v; fi
- if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then bin/php-cs-fixer fix --diff --dry-run -v; fi;
File renamed without changes.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Create loaders by providing a batch loading instance.
use Overblog\DataLoader\DataLoader;

$myBatchGetUsers = function ($keys) { /* ... */ };
$promiseFactory = new MyPromiseFactory();
$promiseAdapter = new MyPromiseAdapter();

$userLoader = new DataLoader($myBatchGetUsers, $promiseFactory);
$userLoader = new DataLoader($myBatchGetUsers, $promiseAdapter);
```

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

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

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

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

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

This is equivalent to the more verbose:

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

## Using with Webonyx/GraphQL [WIP]
## Using with Webonyx/GraphQL

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

## Credits
Expand Down
62 changes: 41 additions & 21 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
{
"name": "overblog/dataloader-php",
"type": "library",
"license": "MIT",
"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.",
"keywords": ["dataLoader", "caching", "batching"],
"autoload": {
"psr-4": {
"Overblog\\DataLoader\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Overblog\\DataLoader\\Tests\\": "tests/"
}
},
"require": {
"php": "^5.5|^7.0",
"mcg-web/promise-factory": "^0.2"
},
"require-dev": {
"phpunit/phpunit": "^4.1|^5.1"
"name": "overblog/dataloader-php",
"type": "library",
"license": "MIT",
"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.",
"keywords": ["dataLoader", "caching", "batching"],
"config" : {
"bin-dir": "bin",
"sort-packages": true
},
"autoload": {
"psr-4": {
"Overblog\\DataLoader\\": "src/",
"Overblog\\PromiseAdapter\\": "lib/promise-adapter/src/"
}
},
"autoload-dev": {
"psr-4": {
"Overblog\\DataLoader\\Test\\": "tests/",
"Overblog\\PromiseAdapter\\Test\\": "lib/promise-adapter/tests/"
}
},
"replace": {
"overblog/promise-adapter": "self.version"
},
"require": {
"php": "^5.5|^7.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.0",
"guzzlehttp/promises": "^1.3.0",
"phpunit/phpunit": "^4.1|^5.1",
"react/promise": "^2.5.0"
},
"suggest": {
"guzzlehttp/promises": "To use with Guzzle promise",
"react/promise": "To use with ReactPhp promise"
},
"extra": {
"branch-alias": {
"dev-master": "0.3-dev"
}
}
}
5 changes: 5 additions & 0 deletions lib/promise-adapter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor/
composer.lock
composer.phar
phpunit.xml
/bin
32 changes: 32 additions & 0 deletions lib/promise-adapter/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
language: php

php:
- nightly
- hhvm
- 5.5
- 5.6
- 7.0
- 7.1

branches:
only:
- master
- /^\d+\.\d+$/

allow_failures:
- php: nightly

cache:
directories:
- $HOME/.composer/cache

before_install:
- if [[ "$TRAVIS_PHP_VERSION" != "5.6" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini || true; fi
- composer selfupdate

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

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

after_success:
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then composer require "satooshi/php-coveralls:^1.0" && travis_retry php vendor/bin/coveralls -v; fi
21 changes: 21 additions & 0 deletions lib/promise-adapter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Overblog

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.
26 changes: 26 additions & 0 deletions lib/promise-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# PromiseAdapter

This library tries to create a simple promise adapter standard while waiting for a psr.
It Comes out of the box with adapter for [ReactPhp/Promise](https://github.com/reactphp/promise) and
[Guzzle/Promises](https://github.com/guzzle/promises).

[![Build Status](https://travis-ci.org/overblog/promise-adapter.svg?branch=master)](https://travis-ci.org/overblog/promise-adapter)
[![Coverage Status](https://coveralls.io/repos/github/overblog/promise-adapter/badge.svg?branch=master)](https://coveralls.io/github/overblog/promise-adapter?branch=master)
[![Latest Stable Version](https://poser.pugx.org/overblog/promise-adapter/version)](https://packagist.org/packages/overblog/promise-adapter)
[![License](https://poser.pugx.org/overblog/promise-adapter/license)](https://packagist.org/packages/overblog/promise-adapter)

## Installation

First, install PromiseAdapter using composer.

```sh
composer require "overblog/promise-adapter"
```

# Usage

see [here](./docs/usage.md)

## License

Overblog/PromiseAdapter is released under the [MIT](https://github.com/overblog/promise-adapter/blob/master/LICENSE) license.
38 changes: 38 additions & 0 deletions lib/promise-adapter/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "overblog/promise-adapter",
"description": "This library tries to create a simple promise adapter standard while waiting for a psr.",
"type": "library",
"keywords": [
"promise",
"adapter",
"guzzle",
"react"
],
"config" : {
"bin-dir": "bin",
"sort-packages": true
},
"autoload": {
"psr-4": {
"Overblog\\PromiseAdapter\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Overblog\\PromiseAdapter\\Test\\": "tests/"
}
},
"require": {
"php": "^5.5|^7.0"
},
"require-dev": {
"guzzlehttp/promises": "^1.3.0",
"phpunit/phpunit": "^4.1|^5.1",
"react/promise": "^2.5.0"
},
"suggest": {
"guzzlehttp/promises": "To use with Guzzle promise",
"react/promise": "To use with ReactPhp promise"
},
"license": "MIT"
}
23 changes: 23 additions & 0 deletions lib/promise-adapter/docs/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Promise adapter usage

## Optional requirements

Optional to use Guzzle:

```sh
composer require "guzzlehttp/promises"
```

Optional to use ReactPhp:

```sh
composer require "react/promise"
```

## Supported Adapter

*Guzzle*: `Overblog\PromiseAdapter\Adapter\GuzzleHttpPromiseAdapter`

*ReactPhp*: `Overblog\PromiseAdapter\Adapter\ReactPromiseAdapter`

To use a custom Promise lib you can implement `Overblog\PromiseAdapter\PromiseAdapterInterface`
Loading

0 comments on commit ac9ab0d

Please sign in to comment.