Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Feb 25, 2015
0 parents commit 1e69f49
Show file tree
Hide file tree
Showing 17 changed files with 1,300 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
phpunit.xml
composer.phar
composer.lock
composer-test.lock
vendor/
build/artifacts/
artifacts/
docs/_build
docs/*.pyc
.idea
.DS_STORE
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# CHANGELOG

## 0.1.0 - TBD

Initial release
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2015 Michael Dowling, https://github.com/mtdowling <[email protected]>

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.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all: clean test

test:
vendor/bin/phpunit

coverage:
vendor/bin/phpunit --coverage-html=artifacts/coverage

view-coverage:
open artifacts/coverage/index.html

clean:
rm -rf artifacts/*
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Guzzle Promises

A+ promise implementation that handles promise chaining and resolution
iteratively.

25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "guzzlehttp/promises",
"type": "library",
"description": "Guzzle promises library",
"keywords": ["promise"],
"license": "MIT",
"authors": [
{
"name": "Michael Dowling",
"email": "[email protected]",
"homepage": "https://github.com/mtdowling"
}
],
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Promise\\": "src/"
}
}
}
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true">
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory suffix="Interface.php">src/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
66 changes: 66 additions & 0 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace GuzzleHttp\Promise;

/**
* A promise that has been fulfilled.
*
* Thenning off of this promise will invoke the onFulfilled callback
* immediately and ignore other callbacks.
*/
class FulfilledPromise implements PromiseInterface
{
private $value;

public function __construct($value)
{
if (method_exists($value, 'then')) {
throw new \InvalidArgumentException(
'You cannot create a FulfilledPromise with a promise.');
}

$this->value = $value;
}

public function then(
callable $onFulfilled = null,
callable $onRejected = null
) {
// Return itself if there is no onFulfilled function.
if (!$onFulfilled) {
return $this;
}

try {
// Return a new fulfilled if onFulfilled does not throw.
return Promise::promiseFor($onFulfilled($this->value));
} catch (\Exception $e) {
// Return a rejected promise be onFulfilled failed.
return new RejectedPromise($e);
}
}

public function wait($unwrap = true, $defaultDelivery = null)
{
return $unwrap ? $this->value : null;
}

public function getState()
{
return 'fulfilled';
}

public function resolve($value)
{
// pass
}

public function reject($reason)
{
// pass
}

public function cancel()
{
// pass
}
}
Loading

0 comments on commit 1e69f49

Please sign in to comment.