From 4a7cb9f3ffecc24b65d1fa03d56b812ea0ffa8f9 Mon Sep 17 00:00:00 2001 From: Kristopher Wilson Date: Fri, 9 Sep 2016 11:58:36 -0400 Subject: [PATCH 1/5] Overcomplicated PSR7 Http client --- composer.json | 11 +- composer.lock | 817 ++++++++++++++++++++++++++++- peridot.php | 3 + specs/.gitignore | 0 specs/Http/guzzle-adapter.spec.php | 35 ++ src/.gitignore | 0 src/Http/ClientInterface.php | 26 + src/Http/GuzzleAdapter.php | 48 ++ 8 files changed, 935 insertions(+), 5 deletions(-) delete mode 100644 specs/.gitignore create mode 100644 specs/Http/guzzle-adapter.spec.php delete mode 100644 src/.gitignore create mode 100644 src/Http/ClientInterface.php create mode 100644 src/Http/GuzzleAdapter.php diff --git a/composer.json b/composer.json index cbe5183..052fba9 100644 --- a/composer.json +++ b/composer.json @@ -5,9 +5,18 @@ "Vnn\\WpApiClient\\": "src/" } }, + "require": { + "php": ">= 5.6", + "psr/http-message": "^1.0" + }, "require-dev": { "peridot-php/peridot": "^1.18", "peridot-php/leo": "^1.5", - "squizlabs/php_codesniffer": "^2.7" + "squizlabs/php_codesniffer": "^2.7", + "guzzlehttp/guzzle": "^6.2", + "peridot-php/peridot-prophecy-plugin": "^1.1" + }, + "suggest": { + "guzzlehttp/guzzle": "^6.2" } } diff --git a/composer.lock b/composer.lock index bd57883..13f0cc3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,10 +4,115 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "5dba94a391899dd736ce0767f606653e", - "content-hash": "5bac286736f830ed763c50cf915611c7", - "packages": [], + "hash": "386ffeda4b3c19deeeadf03e7a335a5c", + "content-hash": "05d766f207578cb0a80013d83085219f", + "packages": [ + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06 14:39:51" + } + ], "packages-dev": [ + { + "name": "doctrine/instantiator", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", + "shasum": "" + }, + "require": { + "php": ">=5.3,<8.0-DEV" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2015-06-14 21:17:01" + }, { "name": "evenement/evenement", "version": "v2.0.0", @@ -54,6 +159,177 @@ ], "time": "2012-11-02 14:49:47" }, + { + "name": "guzzlehttp/guzzle", + "version": "6.2.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "3f808fba627f2c5b69e2501217bf31af349c1427" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/3f808fba627f2c5b69e2501217bf31af349c1427", + "reference": "3f808fba627f2c5b69e2501217bf31af349c1427", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.3.1", + "php": ">=5.5" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.0", + "psr/log": "^1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.2-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2016-07-15 17:22:37" + }, + { + "name": "guzzlehttp/promises", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "c10d860e2a9595f8883527fa0021c7da9e65f579" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/c10d860e2a9595f8883527fa0021c7da9e65f579", + "reference": "c10d860e2a9595f8883527fa0021c7da9e65f579", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-05-18 16:56:05" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "5c6447c9df362e8f8093bda8f5d8873fe5c7f65b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/5c6447c9df362e8f8093bda8f5d8873fe5c7f65b", + "reference": "5c6447c9df362e8f8093bda8f5d8873fe5c7f65b", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "PSR-7 message implementation", + "keywords": [ + "http", + "message", + "stream", + "uri" + ], + "time": "2016-06-24 23:00:38" + }, { "name": "peridot-php/leo", "version": "1.5.0", @@ -163,6 +439,43 @@ ], "time": "2016-04-22 17:32:25" }, + { + "name": "peridot-php/peridot-prophecy-plugin", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/peridot-php/peridot-prophecy-plugin.git", + "reference": "425c097aa93b8949d26f6820ca9905ec3d988c2d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/peridot-php/peridot-prophecy-plugin/zipball/425c097aa93b8949d26f6820ca9905ec3d988c2d", + "reference": "425c097aa93b8949d26f6820ca9905ec3d988c2d", + "shasum": "" + }, + "require": { + "peridot-php/peridot": "~1.0", + "phpspec/prophecy": "~1.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Peridot\\Plugin\\Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Brian Scaturro", + "email": "scaturrob@gmail.com" + } + ], + "description": "Inject prophecy mocks into your Peridot tests", + "time": "2014-11-25 17:54:09" + }, { "name": "peridot-php/peridot-scope", "version": "1.3.0", @@ -202,6 +515,214 @@ "description": "Scopes for function binding and mixins", "time": "2016-02-22 13:27:54" }, + { + "name": "phpdocumentor/reflection-common", + "version": "1.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/144c307535e82c8fdcaacbcfc1d6d8eeb896687c", + "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c", + "shasum": "" + }, + "require": { + "php": ">=5.5" + }, + "require-dev": { + "phpunit/phpunit": "^4.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "time": "2015-12-27 11:43:31" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "3.1.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "9270140b940ff02e58ec577c237274e92cd40cdd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/9270140b940ff02e58ec577c237274e92cd40cdd", + "reference": "9270140b940ff02e58ec577c237274e92cd40cdd", + "shasum": "" + }, + "require": { + "php": ">=5.5", + "phpdocumentor/reflection-common": "^1.0@dev", + "phpdocumentor/type-resolver": "^0.2.0", + "webmozart/assert": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^4.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "time": "2016-06-10 09:48:41" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "0.2", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/b39c7a5b194f9ed7bd0dd345c751007a41862443", + "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443", + "shasum": "" + }, + "require": { + "php": ">=5.5", + "phpdocumentor/reflection-common": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^5.2||^4.8.24" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "time": "2016-06-10 07:14:17" + }, + { + "name": "phpspec/prophecy", + "version": "v1.6.1", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "58a8137754bc24b25740d4281399a4a3596058e0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/58a8137754bc24b25740d4281399a4a3596058e0", + "reference": "58a8137754bc24b25740d4281399a4a3596058e0", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": "^5.3|^7.0", + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2", + "sebastian/comparator": "^1.1", + "sebastian/recursion-context": "^1.0" + }, + "require-dev": { + "phpspec/phpspec": "^2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.6.x-dev" + } + }, + "autoload": { + "psr-0": { + "Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "https://github.com/phpspec/prophecy", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2016-06-07 08:13:47" + }, { "name": "phpunit/php-timer", "version": "1.0.8", @@ -246,6 +767,242 @@ ], "time": "2016-05-12 18:03:57" }, + { + "name": "sebastian/comparator", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "937efb279bd37a375bcadf584dec0726f84dbf22" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/937efb279bd37a375bcadf584dec0726f84dbf22", + "reference": "937efb279bd37a375bcadf584dec0726f84dbf22", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/diff": "~1.2", + "sebastian/exporter": "~1.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "http://www.github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2015-07-26 15:48:44" + }, + { + "name": "sebastian/diff", + "version": "1.4.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "13edfd8706462032c2f52b4b862974dd46b71c9e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/13edfd8706462032c2f52b4b862974dd46b71c9e", + "reference": "13edfd8706462032c2f52b4b862974dd46b71c9e", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.8" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "https://github.com/sebastianbergmann/diff", + "keywords": [ + "diff" + ], + "time": "2015-12-08 07:14:41" + }, + { + "name": "sebastian/exporter", + "version": "1.2.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/42c4c2eec485ee3e159ec9884f95b431287edde4", + "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/recursion-context": "~1.0" + }, + "require-dev": { + "ext-mbstring": "*", + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2016-06-17 09:04:28" + }, + { + "name": "sebastian/recursion-context", + "version": "1.0.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "913401df809e99e4f47b27cdd781f4a258d58791" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/913401df809e99e4f47b27cdd781f4a258d58791", + "reference": "913401df809e99e4f47b27cdd781f4a258d58791", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2015-11-11 19:50:13" + }, { "name": "squizlabs/php_codesniffer", "version": "2.7.0", @@ -442,6 +1199,56 @@ "shim" ], "time": "2016-05-18 14:26:46" + }, + { + "name": "webmozart/assert", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/webmozart/assert.git", + "reference": "bb2d123231c095735130cc8f6d31385a44c7b308" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozart/assert/zipball/bb2d123231c095735130cc8f6d31385a44c7b308", + "reference": "bb2d123231c095735130cc8f6d31385a44c7b308", + "shasum": "" + }, + "require": { + "php": "^5.3.3|^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.6", + "sebastian/version": "^1.0.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "time": "2016-08-09 15:02:57" } ], "aliases": [], @@ -449,6 +1256,8 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "php": ">= 5.6" + }, "platform-dev": [] } diff --git a/peridot.php b/peridot.php index 69aaa14..f42fb37 100644 --- a/peridot.php +++ b/peridot.php @@ -1,10 +1,13 @@ on('peridot.start', function (\Peridot\Console\Environment $environment) { $environment->getDefinition()->getArgument('path')->setDefault('specs'); }); + + new ProphecyPlugin($eventEmitter); }; diff --git a/specs/.gitignore b/specs/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/specs/Http/guzzle-adapter.spec.php b/specs/Http/guzzle-adapter.spec.php new file mode 100644 index 0000000..bd521b5 --- /dev/null +++ b/specs/Http/guzzle-adapter.spec.php @@ -0,0 +1,35 @@ +makeUri('http://lol.com'); + + expect($uri)->to->be->instanceof(UriInterface::class); + expect($uri->getScheme())->to->equal('http'); + expect($uri->getHost())->to->equal('lol.com'); + }); + }); + + describe('send()', function () { + it('should pass the request off to Guzzle and return the response', function () { + $client = $this->getProphet()->prophesize(Client::class); + $adapter = new GuzzleAdapter($client->reveal()); + + $request = new Psr7\Request('GET', 'foo.com'); + $expectedResponse = new Psr7\Response(); + + $client->send($request)->willReturn($expectedResponse)->shouldBeCalled(); + + $response = $adapter->send($request); + + expect($response)->to->equal($expectedResponse); + }); + }); +}); diff --git a/src/.gitignore b/src/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/src/Http/ClientInterface.php b/src/Http/ClientInterface.php new file mode 100644 index 0000000..06ace07 --- /dev/null +++ b/src/Http/ClientInterface.php @@ -0,0 +1,26 @@ +guzzle = $client ?: new Client(); + } + + /** + * {@inheritdoc} + */ + public function makeUri($uri) + { + return new Uri($uri); + } + + /** + * {@inheritdoc} + */ + public function send(RequestInterface $request) + { + return $this->guzzle->send($request); + } +} From 31e32c5886cfde63be3838e9c474c7283838c644 Mon Sep 17 00:00:00 2001 From: Kristopher Wilson Date: Fri, 9 Sep 2016 12:07:36 -0400 Subject: [PATCH 2/5] Wordpress API basic authorization --- specs/Auth/wp-basic-auth.spec.php | 20 ++++++++++++++ src/Auth/AuthInterface.php | 18 +++++++++++++ src/Auth/WpBasicAuth.php | 44 +++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 specs/Auth/wp-basic-auth.spec.php create mode 100644 src/Auth/AuthInterface.php create mode 100644 src/Auth/WpBasicAuth.php diff --git a/specs/Auth/wp-basic-auth.spec.php b/specs/Auth/wp-basic-auth.spec.php new file mode 100644 index 0000000..b360c03 --- /dev/null +++ b/specs/Auth/wp-basic-auth.spec.php @@ -0,0 +1,20 @@ +addCredentials($request); + + expect($newRequest)->to->be->instanceof(Request::class); + expect($newRequest->getHeader('Authorization'))->to->equal([ + 'Basic ' . base64_encode('jim:hunter2') + ]); + }); + }); +}); diff --git a/src/Auth/AuthInterface.php b/src/Auth/AuthInterface.php new file mode 100644 index 0000000..6266464 --- /dev/null +++ b/src/Auth/AuthInterface.php @@ -0,0 +1,18 @@ +username = $username; + $this->password = $password; + } + + /** + * {@inheritdoc} + */ + public function addCredentials(RequestInterface $request) + { + return $request->withHeader( + 'Authorization', + 'Basic ' . base64_encode($this->username . ':' . $this->password) + ); + } +} From 80e32f50eacd66605c2a713930b19cb97eb21938 Mon Sep 17 00:00:00 2001 From: Kristopher Wilson Date: Fri, 9 Sep 2016 14:03:30 -0400 Subject: [PATCH 3/5] Wordpress API client implementation --- composer.json | 2 +- composer.lock | 6 +- specs/Endpoint/abstract-wp-endpoint.spec.php | 48 +++++++++ specs/wp-client.spec.php | 1 + src/Endpoint/AbstractWpEndpoint.php | 71 +++++++++++++ src/Endpoint/Categories.php | 18 ++++ src/Endpoint/Comments.php | 18 ++++ src/Endpoint/Media.php | 18 ++++ src/Endpoint/PostStatuses.php | 18 ++++ src/Endpoint/PostTypes.php | 18 ++++ src/Endpoint/Posts.php | 18 ++++ src/Endpoint/Tags.php | 18 ++++ src/Endpoint/Users.php | 18 ++++ src/WpClient.php | 100 +++++++++++++++++++ 14 files changed, 368 insertions(+), 4 deletions(-) create mode 100644 specs/Endpoint/abstract-wp-endpoint.spec.php create mode 100644 specs/wp-client.spec.php create mode 100644 src/Endpoint/AbstractWpEndpoint.php create mode 100644 src/Endpoint/Categories.php create mode 100644 src/Endpoint/Comments.php create mode 100644 src/Endpoint/Media.php create mode 100644 src/Endpoint/PostStatuses.php create mode 100644 src/Endpoint/PostTypes.php create mode 100644 src/Endpoint/Posts.php create mode 100644 src/Endpoint/Tags.php create mode 100644 src/Endpoint/Users.php create mode 100644 src/WpClient.php diff --git a/composer.json b/composer.json index 052fba9..f35dc19 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ } }, "require": { - "php": ">= 5.6", + "php": ">= 7.0, <= 8.0", "psr/http-message": "^1.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 13f0cc3..2d33d55 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "386ffeda4b3c19deeeadf03e7a335a5c", - "content-hash": "05d766f207578cb0a80013d83085219f", + "hash": "dbedbe75432865bb6906b0da32a7c330", + "content-hash": "7d153173ee1771e7a620a1eba4f42a03", "packages": [ { "name": "psr/http-message", @@ -1257,7 +1257,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">= 5.6" + "php": ">= 7.0, <= 8.0" }, "platform-dev": [] } diff --git a/specs/Endpoint/abstract-wp-endpoint.spec.php b/specs/Endpoint/abstract-wp-endpoint.spec.php new file mode 100644 index 0000000..8766fa4 --- /dev/null +++ b/specs/Endpoint/abstract-wp-endpoint.spec.php @@ -0,0 +1,48 @@ +getProphet()->prophesize(WpClient::class); + + $request = new Request('GET', '/foo/55'); + $response = new \GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], '{"foo": "bar"}'); + + $client->send($request)->willReturn($response)->shouldBeCalled(); + + $endpoint = new class($client->reveal()) extends AbstractWpEndpoint { + public function getEndpoint() { + return '/foo'; + } + }; + + $data = $endpoint->get(55); + expect($data)->to->equal(['foo' => 'bar']); + }); + }); + + describe('save()', function () { + it('should make a POST request to the endpoint URL', function () { + $client = $this->getProphet()->prophesize(WpClient::class); + $response = new \GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], '{"foo": "bar"}'); + $client->send(\Prophecy\Argument::type(Request::class))->willReturn($response)->shouldBeCalled(); + + $endpoint = new class($client->reveal()) extends AbstractWpEndpoint { + public function getEndpoint() { + return '/foo'; + } + }; + + $data = $endpoint->save(['foo' => 'bar']); + expect($data)->to->equal(['foo' => 'bar']); + }); + }); + + afterEach(function () { + $this->getProphet()->checkPredictions(); + }); +}); diff --git a/specs/wp-client.spec.php b/specs/wp-client.spec.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/specs/wp-client.spec.php @@ -0,0 +1 @@ +client = $client; + } + + abstract protected function getEndpoint(); + + /** + * @param int $id + * @return array + */ + public function get($id) + { + $request = new Request('GET', $this->getEndpoint() . '/' . $id); + $response = $this->client->send($request); + + if ($response->hasHeader('Content-Type') + && substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') { + return json_decode($response->getBody()->getContents(), true); + } + + throw new RuntimeException('Unexpected response'); + } + + /** + * @param array $data + * @return array + */ + public function save(array $data) + { + $url = $this->getEndpoint(); + + if (isset($data['id'])) { + $url .= '/' . $data['id']; + unset($data['id']); + } + + $request = new Request('POST', $url, ['Content-Type' => 'application/json'], json_encode($data)); + $response = $this->client->send($request); + + if ($response->hasHeader('Content-Type') + && substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') { + return json_decode($response->getBody()->getContents(), true); + } + + throw new RuntimeException('Unexpected response'); + } +} diff --git a/src/Endpoint/Categories.php b/src/Endpoint/Categories.php new file mode 100644 index 0000000..6077059 --- /dev/null +++ b/src/Endpoint/Categories.php @@ -0,0 +1,18 @@ +httpClient = $httpClient; + $this->wordpressUrl = $wordpressUrl; + } + + /** + * @param AuthInterface $auth + */ + public function setCredentials(AuthInterface $auth) + { + $this->credentials = $auth; + } + + /** + * @param $endpoint + * @param array $args + * @return Endpoint\AbstractWpEndpoint + */ + public function __call($endpoint, array $args) + { + if (!isset($this->endPoints[$endpoint])) { + $class = 'Vnn\WpApiClient\Endpoint\\' . ucfirst($endpoint); + if (class_exists($class)) { + $this->endPoints[$endpoint] = new $class($this); + } else { + throw new RuntimeException('Endpoint "' . $endpoint . '" does not exist"'); + } + } + + return $this->endPoints[$endpoint]; + } + + /** + * @param RequestInterface $request + * @return ResponseInterface + */ + public function send(RequestInterface $request) + { + if ($this->credentials) { + $request = $this->credentials->addCredentials($request); + } + + $request = $request->withUri( + $this->httpClient->makeUri($this->wordpressUrl . '/wp-json/wp/v2' . $request->getUri()) + ); + + return $this->httpClient->send($request); + } +} From d92ba92da50d672357ae71f2e3a0b9eef5e065f6 Mon Sep 17 00:00:00 2001 From: Kristopher Wilson Date: Fri, 9 Sep 2016 14:06:19 -0400 Subject: [PATCH 4/5] projects love badges --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b2f561a..da59250 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ > A Wordpress REST API client for PHP +[![Travis](https://img.shields.io/travis/varsitynewsnetwork/wordpress-rest-api-client.svg?maxAge=2592000?style=flat-square)](https://travis-ci.org/varsitynewsnetwork/wordpress-rest-api-client) + For when you need to make [Wordpress REST API calls](http://v2.wp-api.org/) from some other PHP project, for some reason. From cc55ab25dc8bbb838dc148dca3a8c3428b202c58 Mon Sep 17 00:00:00 2001 From: Kristopher Wilson Date: Fri, 9 Sep 2016 14:07:44 -0400 Subject: [PATCH 5/5] Updating README example --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index da59250..11c8e29 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,16 @@ composer require vnn/wordpress-rest-api-client Example: ```php -$client = new WpClient($url); -$client->setCredentials(new WpBasicCredentials('username', 'password')); +use Vnn\WpApiClient\Auth\WpBasicAuth; +use Vnn\WpApiClient\Http\GuzzleAdapter; +use Vnn\WpApiClient\WpClient; -$user = $client->users()->get(15); +require 'vendor/autoload.php'; -echo $user['username']; +$client = new WpClient(new GuzzleAdapter(new GuzzleHttp\Client()), 'http://yourwordpress.com'); +$client->setCredentials(new WpBasicAuth('user', 'securepassword')); + +$user = $client->users()->get(2); + +print_r($user); ```