From 84c5d9d860e1bd4483419135b4135192027600fc Mon Sep 17 00:00:00 2001 From: himiklab Date: Tue, 3 Jul 2018 16:58:20 +0300 Subject: [PATCH] ability to use external yii2-httpclient object --- EasyThumbnail.php | 9 +++++++++ EasyThumbnailImage.php | 8 +++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/EasyThumbnail.php b/EasyThumbnail.php index 75f14ea..cf47cef 100644 --- a/EasyThumbnail.php +++ b/EasyThumbnail.php @@ -8,6 +8,7 @@ namespace himiklab\thumbnail; use yii\base\BaseObject; +use yii\httpclient\Client as HttpClient; /** * EasyThumbnailImage global configuration component. @@ -23,9 +24,17 @@ class EasyThumbnail extends BaseObject /** @var integer $cacheExpire seconds */ public $cacheExpire = 0; + /** @var HttpClient */ + public $httpClient; + public function init() { EasyThumbnailImage::$cacheAlias = $this->cacheAlias; EasyThumbnailImage::$cacheExpire = $this->cacheExpire; + + EasyThumbnailImage::$httpClient = $this->httpClient; + if (EasyThumbnailImage::$httpClient === null || !(EasyThumbnailImage::$httpClient instanceof HttpClient)) { + EasyThumbnailImage::$httpClient = new HttpClient(); + } } } diff --git a/EasyThumbnailImage.php b/EasyThumbnailImage.php index e846aae..2148d6a 100644 --- a/EasyThumbnailImage.php +++ b/EasyThumbnailImage.php @@ -13,7 +13,6 @@ use yii\base\InvalidConfigException; use yii\helpers\FileHelper; use yii\helpers\Html; -use yii\httpclient\Client; use yii\imagine\Image; /** @@ -38,6 +37,9 @@ class EasyThumbnailImage /** @var int $cacheExpire */ public static $cacheExpire = 0; + /** @var yii\httpclient\Client */ + public static $httpClient; + /** * Creates and caches the image thumbnail and returns ImageInterface. * @@ -247,7 +249,7 @@ protected static function errorHandler($error, $filename) */ protected static function fileFromUrlDate($url) { - $response = (new Client()) + $response = self::$httpClient ->head($url) ->send(); if (!$response->isOk) { @@ -264,7 +266,7 @@ protected static function fileFromUrlDate($url) */ protected static function fileFromUrlContent($url) { - $response = (new Client()) + $response = self::$httpClient ->createRequest() ->setMethod('GET') ->setUrl($url)