From 53521ba3d9ab5a64b1cbc32a469ddc99cd525199 Mon Sep 17 00:00:00 2001 From: Paul Hennell Date: Sat, 10 Apr 2021 17:17:37 +0100 Subject: [PATCH] Update to use cmfcmf/openweathermap-php-api ^3 * Http client and request factory discovery logic added * Readme updated to show installation additions --- README.md | 6 ++++++ composer.json | 7 +++++-- src/LaravelOWM.php | 23 +++++++++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b1098a3..bb3c99e 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ `composer require coliving/laravel-owm` +You will also need a PSR-17 compatible HTTP factory implementation and a PSR-18 compatible HTTP client implementation. Laravel already requires guzzle as a http client, but you may need to add a http-factory package. Choose any [from this list](https://packagist.org/providers/psr/http-factory-implementation) or use the suggested package below: + +``` +composer require "http-interop/http-factory-guzzle:^1.0" +``` + #### 2. Add this line to your conf/app.php file For Laravel == 5.0.* diff --git a/composer.json b/composer.json index 053adee..b6bd52e 100644 --- a/composer.json +++ b/composer.json @@ -4,9 +4,12 @@ "keywords": ["weather","clima","open weather map","laravel"], "require": { "php": ">=7.2.0", - "cmfcmf/openweathermap-php-api": "^2.1" + "cmfcmf/openweathermap-php-api": "^3.0", + "php-http/discovery": "^1.13" + }, + "require-dev": { + "php-http/mock-client": "1.4.x-dev" }, - "license": "MIT", "authors": [ diff --git a/src/LaravelOWM.php b/src/LaravelOWM.php index 8d04cec..fa5c3f4 100644 --- a/src/LaravelOWM.php +++ b/src/LaravelOWM.php @@ -2,6 +2,10 @@ namespace Gmopx\LaravelOWM; use Cmfcmf\OpenWeatherMap; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; class LaravelOWM { @@ -15,6 +19,18 @@ class LaravelOWM */ protected $api_key; + /** + * Psr18 http client + * @var ClientInterface + */ + protected $httpClient; + + /** + * Psr17 http Factory + * @var RequestFactoryInterface + */ + protected $httpRequestFactory; + public function __construct() { $this->config = config('laravel-owm'); @@ -28,6 +44,9 @@ public function __construct() } $this->api_key = $this->config['api_key']; + + $this->httpClient = Psr18ClientDiscovery::find(); + $this->httpRequestFactory = Psr17FactoryDiscovery::findRequestFactory(); } /** @@ -55,11 +74,11 @@ public function getCurrentWeather($query, $lang = 'en', $units = 'metric', $cach $units = $units ?: 'metric'; if ($cache) { - $owm = new OpenWeatherMap($this->api_key, null, new Cache(), $time); + $owm = new OpenWeatherMap($this->api_key, $this->httpClient, $this->httpRequestFactory, new Cache(), $time); return $owm->getWeather($query, $units, $lang); } - $owm = new OpenWeatherMap($this->api_key); + $owm = new OpenWeatherMap($this->api_key, $this->httpClient, $this->httpRequestFactory); return $owm->getWeather($query, $units, $lang); }