From be457e676e8f5f55c2b392760e9f585742dad407 Mon Sep 17 00:00:00 2001 From: core23 Date: Sun, 8 Dec 2019 12:58:14 +0100 Subject: [PATCH] Replace HTTPlug with PSR http client --- README.md | 46 ++----------------- composer.json | 13 +++--- src/DependencyInjection/Configuration.php | 4 +- src/Resources/config/services.xml | 4 +- .../DependencyInjection/ConfigurationTest.php | 4 +- .../Core23LastFmExtensionTest.php | 8 +++- 6 files changed, 23 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 1e0d0fd..65a32b7 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ Open a command console, enter your project directory and execute the following c ``` composer require core23/lastfm-bundle -composer require php-http/guzzle6-adapter # if you want to use Guzzle +# To define a default http client and message factory +composer require symfony/http-client nyholm/psr7 ``` ### Enable the Bundle @@ -33,7 +34,6 @@ Then, enable the bundle by adding it to the list of registered bundles in `confi return [ // ... - Http\HttplugBundle\HttplugBundle::class => ['all' => true], Core23\LastFmBundle\Core23LastFmBundle::class => ['all' => true], ]; ``` @@ -49,50 +49,14 @@ core23_lastfm: api: app_id: "%lastfm_api.id%" shared_secret: "%lastfm_api.secret%" -``` - -## Usage - -Define a [HTTPlug] client in your configuration. -```yaml -# config/packages/httplug.yaml - -httplug: - classes: - client: Http\Adapter\Guzzle6\Client - message_factory: Http\Message\MessageFactory\GuzzleMessageFactory - uri_factory: Http\Message\UriFactory\GuzzleUriFactory - stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory - clients: - default: - # ... - plugins: - - httplug.plugin.redirect # plugin is needed for the webcrawler -``` - -### API cache - -It is recommended to use a cache to reduce the API usage. - -```yaml -# config/packages/httplug.yaml - -httplug: - plugins: - cache: - cache_pool: 'acme.httplug_cache' - config: - default_ttl: 7200 # Two hours - clients: - default: - plugins: - - httplug.plugin.cache + http: + client: 'httplug.client' + message_factory: 'nyholm.psr7.psr17_factory' ``` ## License This bundle is under the [MIT license](LICENSE.md). -[HTTPlug]: http://docs.php-http.org/en/latest/index.html [Last.fm API]: http://www.last.fm/api diff --git a/composer.json b/composer.json index 5fd3f51..19d8159 100644 --- a/composer.json +++ b/composer.json @@ -21,8 +21,8 @@ "require": { "php": "^7.2", "core23/lastfm-api": "^2.0 || dev-master", - "php-http/client-implementation": "*", - "php-http/httplug-bundle": "^1.15", + "psr/http-client": "^1.0", + "psr/http-message": "^1.0", "symfony/config": "^3.4 || ^4.2", "symfony/dependency-injection": "^3.4 || ^4.2", "symfony/event-dispatcher-contracts": "^1.1", @@ -36,14 +36,13 @@ }, "require-dev": { "bamarni/composer-bin-plugin": "^1.2", - "php-http/message-factory": "^1.0", - "php-http/mock-client": "^1.3", - "sllh/composer-lint": "^1.0" + "nyholm/psr7": "^1.0", + "sllh/composer-lint": "^1.0", + "symfony/http-client": "^4.4 || ^5.0" }, "suggest": { "cache/adapter-bundle": "Symfony cache adapter", - "php-http/buzz-adapter": "Buzz HTTP client implementation", - "php-http/guzzle6-adapter": "Guzzle HTTP client implementation" + "symfony/http-client": "Symfony HTTP client implementation" }, "config": { "sort-packages": true diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index af0ee89..30ded94 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -59,8 +59,8 @@ private function addHttpClientSection(ArrayNodeDefinition $node): void ->arrayNode('http') ->addDefaultsIfNotSet() ->children() - ->scalarNode('client')->defaultValue('httplug.client.default')->end() - ->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end() + ->scalarNode('client')->defaultNull()->end() + ->scalarNode('message_factory')->defaultNull()->end() ->end() ->end() ->end() diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 0918dea..4ca123b 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -4,12 +4,12 @@ - + %core23_lastfm.api.endpoint% - + %core23_lastfm.api.app_id% diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index e191980..bb7e758 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -36,8 +36,8 @@ public function testOptions(): void 'auth_url' => 'http://www.last.fm/api/auth/', ], 'http' => [ - 'client' => 'httplug.client.default', - 'message_factory' => 'httplug.message_factory.default', + 'client' => null, + 'message_factory' => null, ], ]; diff --git a/tests/DependencyInjection/Core23LastFmExtensionTest.php b/tests/DependencyInjection/Core23LastFmExtensionTest.php index 85028a3..60f6909 100644 --- a/tests/DependencyInjection/Core23LastFmExtensionTest.php +++ b/tests/DependencyInjection/Core23LastFmExtensionTest.php @@ -23,6 +23,10 @@ public function testLoadDefault(): void 'app_id' => 'foo_id', 'shared_secret' => 'bar_secret', ], + 'http' => [ + 'client' => 'acme.client', + 'message_factory' => 'acme.message_factory', + ], ]); $this->assertContainerBuilderHasParameter('core23_lastfm.api.app_id', 'foo_id'); @@ -30,8 +34,8 @@ public function testLoadDefault(): void $this->assertContainerBuilderHasParameter('core23_lastfm.api.endpoint', 'http://ws.audioscrobbler.com/2.0/'); $this->assertContainerBuilderHasParameter('core23_lastfm.api.auth_url', 'http://www.last.fm/api/auth/'); - $this->assertContainerBuilderHasAlias('core23_lastfm.http.client', 'httplug.client.default'); - $this->assertContainerBuilderHasAlias('core23_lastfm.http.message_factory', 'httplug.message_factory.default'); + $this->assertContainerBuilderHasAlias('core23_lastfm.http.client', 'acme.client'); + $this->assertContainerBuilderHasAlias('core23_lastfm.http.message_factory', 'acme.message_factory'); } protected function getContainerExtensions(): array