From 77f2a6cd93e2ec6111b53565e9f1173613cddc78 Mon Sep 17 00:00:00 2001 From: Daniel Nilsson Date: Sat, 30 Jul 2016 22:01:41 +0200 Subject: [PATCH 1/7] introduces manager --- README.md | 43 ++++++++++ composer.json | 25 +++--- config/laravel-httplug.php | 84 +++++++++++++----- src/Facade/Httplug.php | 18 ++++ src/Httplug.php | 22 ----- src/HttplugFacade.php | 19 ----- src/HttplugManager.php | 123 +++++++++++++++++++++++++++ src/HttplugServiceProvider.php | 93 ++++++++++++++------ tests/AbstractTestCase.php | 20 +++++ tests/Facade/HttplugTest.php | 42 +++++++++ tests/HttplugServiceProviderTest.php | 19 +++++ 11 files changed, 409 insertions(+), 99 deletions(-) create mode 100644 src/Facade/Httplug.php delete mode 100644 src/Httplug.php delete mode 100644 src/HttplugFacade.php create mode 100644 src/HttplugManager.php create mode 100644 tests/AbstractTestCase.php create mode 100644 tests/Facade/HttplugTest.php create mode 100644 tests/HttplugServiceProviderTest.php diff --git a/README.md b/README.md index 86885ca..78cbb52 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,52 @@ Via Composer $ composer require php-http/laravel-httplug ``` +Add service provider and alias +```php + [ + ..., + ..., + + Http\Httplug\HttplugServiceProvider::class, + +], + +'aliases' => [ + ..., + ..., + + 'Httplug' => Http\Httplug\Facade\Httplug::class, + +], + + +``` ## Usage +```php +make('httplug'); + $request = new Request('GET', 'http://httpbin.org'); + + /** + * @var \Psr\Http\Message\ResponseInterface + */ + $response = $httplug->driver()->sendRequest($request); + + // with Facade: + // Default driver + Httplug::sendRequest($request); + // another driver + Httplug::driver('curl')->sendRequest($request) + +``` ## Testing diff --git a/composer.json b/composer.json index 6b8874a..7073fb2 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "php-http/laravel-httplug", "description": "Laravel package to integrate the Httplug generic HTTP client into Laravel", - "keywords": ["http", "discovery", "adapter", "message", "factory", "bundle", "httplug", "php-http", "psr-7", "laravel"], + "keywords": ["http", "discovery", "adapter", "message", "factory", "httplug", "php-http", "psr-7", "laravel"], "homepage": "http://php-http.org", "license": "MIT", "authors": [ @@ -13,31 +13,34 @@ "require": { "php": ">=5.4", "illuminate/support": "~5", - "php-http/client-common": "^1.1", - "php-http/message-factory": "^1.0", + "php-http/message": "^1.3", + "php-http/client-common": "^1.2", "php-http/cache-plugin": "^1.0", "php-http/logger-plugin": "^1.0" }, "require-dev": { "phpunit/phpunit": "^5.1", + "graham-campbell/testbench": "^3.1", "friendsofphp/php-cs-fixer": "1.9.*", "sllh/php-cs-fixer-styleci-bridge": "^1.5", "php-http/curl-client": "^1.0", "php-http/socket-client": "^1.0", - "php-http/guzzle6-adapter": "^1.1", - "php-http/react-adapter": "^0.1", - "php-http/buzz-adapter": "^0.1", - "php-http/message": "^1.0" + "php-http/guzzle6-adapter": "^1.1.1", + "php-http/react-adapter": "^0.2.1", + "php-http/buzz-adapter": "^0.3" }, - "prefer-stable": true, - "minimum-stability": "dev", "autoload": { "psr-4": { "Http\\Httplug\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Http\\Httplug\\": "tests/" + } + }, "scripts": { - "test": "", - "test-ci": "" + "test": "vendor/bin/phpunit", + "test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml" } } diff --git a/config/laravel-httplug.php b/config/laravel-httplug.php index 2e4135c..ff6487a 100644 --- a/config/laravel-httplug.php +++ b/config/laravel-httplug.php @@ -1,31 +1,71 @@ [ - 'client' => 'httplug.client.default', - 'message_factory' => 'httplug.message_factory.default', - 'uri_factory' => 'httplug.uri_factory.default', - 'stream_factory' => 'httplug.stream_factory.default', - ], - - 'classes' => [ - # uses discovery if not specified - 'client' => '', - 'message_factory' => '', - 'uri_factory' => '', - 'stream_factory' => '', - ], - - 'clients' => [ - 'acme' => [ + + 'default' => 'guzzle6', + + + 'adapters' => [ + + /** + * @link https://github.com/php-http/guzzle6-adapter + */ + 'guzzle6' => [ 'factory' => 'httplug.factory.guzzle6', - 'plugins' => ['httplug.plugin.authentication.my_wsse', 'httplug.plugin.cache', 'httplug.plugin.retry'], + 'plugins' => [ + 'httplug.plugin.authentication.my_wsse', + 'httplug.plugin.cache', + 'httplug.plugin.retry', + + ], + 'config' => [ - 'verify' => false, + 'verify' => false, 'timeout' => 2, - # more options to the guzzle 6 constructor ], ], + + /** + * @link https://github.com/php-http/guzzle5-adapter + */ + 'guzzle5' => [ + + ], + + /** + * @link https://github.com/php-http/curl-client + */ + 'curl' => [ + + ], + + /** + * @link https://github.com/php-http/socket-client + */ + 'socket' => [ + + ], + + /** + * @link https://github.com/php-http/buzz-adapter + */ + 'buzz' => [ + 'resolver' => [ + 'timeout' => 5, + 'verify_peer' => true, + 'verify_host' => 2, + 'proxy' => null, + ], + ], + + /** + * @link https://github.com/php-http/react-adapter + */ + 'react' => [ + + ], + ], + ]; diff --git a/src/Facade/Httplug.php b/src/Facade/Httplug.php new file mode 100644 index 0000000..a393342 --- /dev/null +++ b/src/Facade/Httplug.php @@ -0,0 +1,18 @@ + - */ - -namespace Http\LaravelHttplug; - -use Http\Client\HttpClient; - -class Httplug -{ - /** @var HttpClient */ - private $client; - - public function __construct() - { - $this->client = ''; - } -} diff --git a/src/HttplugFacade.php b/src/HttplugFacade.php deleted file mode 100644 index bbdefe4..0000000 --- a/src/HttplugFacade.php +++ /dev/null @@ -1,19 +0,0 @@ - - */ - -namespace Http\LaravelHttplug; - -use Illuminate\Support\Facades\Facade; - -class HttplugFacade extends Facade -{ - protected static function getFacadeAccessor() - { - return 'laravel-httplug'; - } -} diff --git a/src/HttplugManager.php b/src/HttplugManager.php new file mode 100644 index 0000000..8f69b89 --- /dev/null +++ b/src/HttplugManager.php @@ -0,0 +1,123 @@ +app = $app; + } + + /** + * Get default driver + * + * @return string + */ + public function getDefaultDriver() + { + return $this->app['config']['httplug.default']; + } + + /** + * @param string $name + * @return array + */ + protected function getConfig($name) + { + return $this->app['config']["httplug.adapters.{$name}"]; + } + + /** + * @return \Http\Adapter\Guzzle6\Client + */ + public function createGuzzle6Driver() + { + return GuzzleSixAdapter::createWithConfig($this->getConfig('guzzle6')); + } + + /** + * @return \Http\Adapter\Guzzle5\Client + */ + public function createGuzzle5Driver() + { + return new GuzzleFiveAdapter( + new GuzzleFiveClient($this->getConfig('guzzle5')), + $this->app->make(MessageFactory::class) + ); + } + + /** + * @return \Http\Client\Curl\Client + */ + public function createCurlDriver() + { + return new CurlAdapter( + $this->app->make(MessageFactory::class), + $this->app->make(StreamFactory::class), + $this->getConfig('curl') + ); + } + + /** + * @return \Http\Client\Socket\Client + */ + public function createSocketDriver() + { + return new SocketAdapter( + $this->app->make(MessageFactory::class), + $this->getConfig('socket') + ); + } + + /** + * @todo add custom configuration + * + * @return \Http\Adapter\Buzz\Client + */ + public function createBuzzDriver() + { + return new BuzzAdapter( + null, + $this->app->make(ResponseFactory::class) + ); + } + + /** + * @todo add custom configuration + * + * @return \Http\Adapter\React\Client + */ + public function createReactDriver() + { + return new ReactAdapter( + $this->app->make(ResponseFactory::class) + ); + } +} diff --git a/src/HttplugServiceProvider.php b/src/HttplugServiceProvider.php index 4c5b839..1fd6494 100644 --- a/src/HttplugServiceProvider.php +++ b/src/HttplugServiceProvider.php @@ -1,51 +1,94 @@ - */ - -namespace Http\LaravelHttplug; +namespace Http\Httplug; use Illuminate\Support\ServiceProvider; +use Http\Discovery\UriFactoryDiscovery; +use Http\Discovery\StreamFactoryDiscovery; +use Http\Discovery\MessageFactoryDiscovery; class HttplugServiceProvider extends ServiceProvider { + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = true; + /** * Bootstrap the application services. */ public function boot() { - $this->publishes([ - __DIR__.'/../config/laravel-httplug.php' => $this->app->configPath().'/'.'laravel-httplug.php', - ], 'config'); + $source = __DIR__.'/../config/laravel-httplug.php'; + $this->publishes([$source => config_path('httplug.php')]); + $this->mergeConfigFrom($source, 'httplug'); } /** * Register the application services. + * + * @return void */ public function register() { - $this->mergeConfigFrom(__DIR__.'/../config/laravel-httplug.php', 'laravel-httplug'); - $this->app->bind(Httplug::class, function () { - return new Httplug(); + $this->registerHttplugFactories(); + $this->registerHttplug(); + } + + /** + * Register php-http interfaces to container + * + * @return void + */ + protected function registerHttplugFactories() + { + $this->app->bind('httplug.message_factory.default', function ($app) { + return MessageFactoryDiscovery::find(); + }); + $this->app->alias('httplug.message_factory.default', 'Http\Message\MessageFactory'); + $this->app->alias('httplug.message_factory.default', 'Http\Message\ResponseFactory'); + + $this->app->bind('httplug.uri_factory.default', function ($app) { + return UriFactoryDiscovery::find(); + }); + $this->app->alias('httplug.uri_factory.default', 'Http\Message\UriFactory'); + + $this->app->bind('httplug.stream_factory.default', function ($app) { + return StreamFactoryDiscovery::find(); }); + $this->app->alias('httplug.stream_factory.default', 'Http\Message\StreamFactory'); + } - $config = config('laravel-httplug'); + /** + * Register httplug to container + * + * @return void + */ + protected function registerHttplug() + { + $this->app->singleton('httplug', function ($app) { + return new HttplugManager($app); + }); + $this->app->alias('httplug', HttplugManager::class); - foreach ($config['classes'] as $service => $class) { - if (!empty($class)) { - $this->app->register(sprintf('httplug.%s.default', $service), function() use($class) { - return new $class(); - }); - } - } + $this->app->singleton('httplug.default', function ($app) { + return $app['httplug']->driver(); + }); + } - foreach ($config['main_alias'] as $type => $id) { - $this->app->alias(sprintf('httplug.%s', $type), $id); - } + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return [ + 'httplug', + 'httplug.default', - $this->app->alias(Httplug::class, 'laravel-httplug'); + ]; } } diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php new file mode 100644 index 0000000..2c4dd44 --- /dev/null +++ b/tests/AbstractTestCase.php @@ -0,0 +1,20 @@ +assertIsInjectable(HttplugManager::class); + } +} From 1366d64cc0d220cfcb368f50cdf1e95a3b5a487b Mon Sep 17 00:00:00 2001 From: Daniel Nilsson Date: Sun, 31 Jul 2016 15:55:16 +0200 Subject: [PATCH 2/7] Updated README --- README.md | 42 +++++++++++++++++++++------------- src/HttplugServiceProvider.php | 2 +- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 78cbb52..1b6457b 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Via Composer $ composer require php-http/laravel-httplug ``` -Add service provider and alias +Add serviceprovider and alias ```php make('httplug'); - $request = new Request('GET', 'http://httpbin.org'); - - /** - * @var \Psr\Http\Message\ResponseInterface - */ - $response = $httplug->driver()->sendRequest($request); - - // with Facade: - // Default driver - Httplug::sendRequest($request); - // another driver - Httplug::driver('curl')->sendRequest($request) +use GuzzleHttp\Psr7\Request; + +$httplug = app()->make('httplug'); +$request = new Request('GET', 'http://httpbin.org'); + +/** + * Send request with default driver + * @var \Psr\Http\Message\ResponseInterface + */ +$response = $httplug->sendRequest($request); +/** + * Send request with another driver + */ +$response = $httplug->driver('curl')->sendRequest($request); + +// with Facade: +// Default driver +Httplug::sendRequest($request); +// another driver +Httplug::driver('curl')->sendRequest($request) ``` diff --git a/src/HttplugServiceProvider.php b/src/HttplugServiceProvider.php index 1fd6494..491c8d4 100644 --- a/src/HttplugServiceProvider.php +++ b/src/HttplugServiceProvider.php @@ -22,7 +22,7 @@ class HttplugServiceProvider extends ServiceProvider public function boot() { $source = __DIR__.'/../config/laravel-httplug.php'; - $this->publishes([$source => config_path('httplug.php')]); + $this->publishes([$source => config_path('httplug.php')], 'config'); $this->mergeConfigFrom($source, 'httplug'); } From 16f6b64300cc5d0060c8bda7282b84fa3fe8081f Mon Sep 17 00:00:00 2001 From: Daniel Nilsson Date: Sun, 31 Jul 2016 17:47:41 +0200 Subject: [PATCH 3/7] bumped versions --- .travis.yml | 1 - composer.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7c466c1..c0ebd49 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ cache: - $HOME/.composer/cache php: - - 5.4 - 5.5 - 5.6 - 7.0 diff --git a/composer.json b/composer.json index 7073fb2..dbc77bb 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ } ], "require": { - "php": ">=5.4", + "php": "^5.5 || ^7.0", "illuminate/support": "~5", "php-http/message": "^1.3", "php-http/client-common": "^1.2", From 5cdaf1e8249814c718906544638f686c8797f9e4 Mon Sep 17 00:00:00 2001 From: Daniel Nilsson Date: Tue, 2 Aug 2016 08:04:15 +0200 Subject: [PATCH 4/7] added container injection tests --- src/HttplugServiceProvider.php | 21 ++++++++++++---- tests/HttplugServiceProviderTest.php | 36 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/HttplugServiceProvider.php b/src/HttplugServiceProvider.php index 491c8d4..312ab21 100644 --- a/src/HttplugServiceProvider.php +++ b/src/HttplugServiceProvider.php @@ -2,6 +2,10 @@ namespace Http\Httplug; +use Http\Message\UriFactory; +use Http\Message\StreamFactory; +use Http\Message\MessageFactory; +use Http\Message\ResponseFactory; use Illuminate\Support\ServiceProvider; use Http\Discovery\UriFactoryDiscovery; use Http\Discovery\StreamFactoryDiscovery; @@ -15,7 +19,7 @@ class HttplugServiceProvider extends ServiceProvider * @var bool */ protected $defer = true; - + /** * Bootstrap the application services. */ @@ -47,18 +51,18 @@ protected function registerHttplugFactories() $this->app->bind('httplug.message_factory.default', function ($app) { return MessageFactoryDiscovery::find(); }); - $this->app->alias('httplug.message_factory.default', 'Http\Message\MessageFactory'); - $this->app->alias('httplug.message_factory.default', 'Http\Message\ResponseFactory'); + $this->app->alias('httplug.message_factory.default', MessageFactory::class); + $this->app->alias('httplug.message_factory.default', ResponseFactory::class); $this->app->bind('httplug.uri_factory.default', function ($app) { return UriFactoryDiscovery::find(); }); - $this->app->alias('httplug.uri_factory.default', 'Http\Message\UriFactory'); + $this->app->alias('httplug.uri_factory.default', UriFactory::class); $this->app->bind('httplug.stream_factory.default', function ($app) { return StreamFactoryDiscovery::find(); }); - $this->app->alias('httplug.stream_factory.default', 'Http\Message\StreamFactory'); + $this->app->alias('httplug.stream_factory.default', StreamFactory::class); } /** @@ -88,6 +92,13 @@ public function provides() return [ 'httplug', 'httplug.default', + 'httplug.message_factory.default', + 'httplug.uri_factory.default', + 'httplug.stream_factory.default', + MessageFactory::class, + ResponseFactory::class, + StreamFactory::class, + UriFactory::class, ]; } diff --git a/tests/HttplugServiceProviderTest.php b/tests/HttplugServiceProviderTest.php index 19ed6b6..785dcd0 100644 --- a/tests/HttplugServiceProviderTest.php +++ b/tests/HttplugServiceProviderTest.php @@ -2,6 +2,10 @@ namespace Http\Httplug; +use Http\Message\MessageFactory; +use Http\Message\ResponseFactory; +use Http\Message\UriFactory; +use Http\Message\StreamFactory; use Http\Client\Curl\Client as CurlAdapter; use GrahamCampbell\TestBenchCore\ServiceProviderTrait; @@ -16,4 +20,36 @@ public function canInjectManager() { $this->assertIsInjectable(HttplugManager::class); } + + /** + * @test + */ + public function canInjectMessageFactory() + { + $this->assertIsInjectable(MessageFactory::class); + } + + /** + * @test + */ + public function canInjectResponseFactory() + { + $this->assertIsInjectable(ResponseFactory::class); + } + + /** + * @test + */ + public function canInjectUriFactory() + { + $this->assertIsInjectable(UriFactory::class); + } + + /** + * @test + */ + public function canInjectStreamFactory() + { + $this->assertIsInjectable(StreamFactory::class); + } } From 086b2c90971c2395aa614baa7eee3b47b5e4989d Mon Sep 17 00:00:00 2001 From: Daniel Nilsson Date: Tue, 2 Aug 2016 08:04:36 +0200 Subject: [PATCH 5/7] updated example --- README.md | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1b6457b..391a72d 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Add serviceprovider and alias 'providers' => [ ..., ..., - + Http\Httplug\HttplugServiceProvider::class, ], @@ -31,7 +31,7 @@ Add serviceprovider and alias 'aliases' => [ ..., ..., - + 'Httplug' => Http\Httplug\Facade\Httplug::class, ], @@ -51,6 +51,12 @@ php artisan vendor:publish --provider="Http\Httplug\HttplugServiceProvider" use GuzzleHttp\Psr7\Request; +/** +* Using the factory + */ +$factory = app()->make('httplug.message_factory.default'); +$request = $factory->createRequest('GET', 'http://httpbin.org'); + $httplug = app()->make('httplug'); $request = new Request('GET', 'http://httpbin.org'); @@ -64,11 +70,15 @@ $response = $httplug->sendRequest($request); */ $response = $httplug->driver('curl')->sendRequest($request); -// with Facade: -// Default driver -Httplug::sendRequest($request); -// another driver -Httplug::driver('curl')->sendRequest($request) +/** + * Send request with default driver using facade + * @var \Psr\Http\Message\ResponseInterface + */ +$response = Httplug::sendRequest($request); +/** + * Send request with another driver using facade + */ +$response = Httplug::driver('curl')->sendRequest($request) ``` From 1f7c4072d8cd55436c10b7b7ea2517d1d4d3fbe1 Mon Sep 17 00:00:00 2001 From: Daniel Nilsson Date: Tue, 2 Aug 2016 08:04:47 +0200 Subject: [PATCH 6/7] added phpunit.xml --- phpunit.xml.dist | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 phpunit.xml.dist diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..f211df4 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,28 @@ + + + + + ./tests/ + + + + + ./src + + + From 50ce797d4e5aa383f2a901c97475a95a5b0a6206 Mon Sep 17 00:00:00 2001 From: Daniel Nilsson Date: Tue, 2 Aug 2016 08:43:11 +0200 Subject: [PATCH 7/7] remove support for php 5.4 --- .travis.yml | 5 ----- composer.json | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index c0ebd49..88de70b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,11 +20,6 @@ branches: except: - /^analysis-.*$/ -matrix: - fast_finish: true - include: - - php: 5.4 - env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" before_install: - travis_retry composer self-update diff --git a/composer.json b/composer.json index dbc77bb..a619fc9 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "php-http/logger-plugin": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^5.1", + "phpunit/phpunit": "^4.5 || ^5.4", "graham-campbell/testbench": "^3.1", "friendsofphp/php-cs-fixer": "1.9.*", "sllh/php-cs-fixer-styleci-bridge": "^1.5",