Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
ability to use external yii2-httpclient object
Browse files Browse the repository at this point in the history
  • Loading branch information
himiklab committed Jul 3, 2018
1 parent 96776b1 commit 84c5d9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 9 additions & 0 deletions EasyThumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace himiklab\thumbnail;

use yii\base\BaseObject;
use yii\httpclient\Client as HttpClient;

/**
* EasyThumbnailImage global configuration component.
Expand All @@ -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();
}
}
}
8 changes: 5 additions & 3 deletions EasyThumbnailImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use yii\base\InvalidConfigException;
use yii\helpers\FileHelper;
use yii\helpers\Html;
use yii\httpclient\Client;
use yii\imagine\Image;

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit 84c5d9d

Please sign in to comment.