1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\behaviors; |
4
|
|
|
|
5
|
|
|
use app\modules\image\components\CompileSrcInterface; |
6
|
|
|
use app\modules\image\models\ErrorImage; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\base\Behavior; |
9
|
|
|
use yii\helpers\ArrayHelper; |
10
|
|
|
use yii\web\HttpException; |
11
|
|
|
|
12
|
|
|
class ImageExist extends Behavior |
13
|
|
|
{ |
14
|
|
|
public $srcAttrName = 'filename'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return mixed |
18
|
|
|
* @throws HttpException |
19
|
|
|
*/ |
20
|
|
|
public function getFile() |
21
|
|
|
{ |
22
|
|
|
$src = $this->owner->{$this->srcAttrName}; |
23
|
|
|
if (Yii::$app->fs->has($src) === false) { |
24
|
|
|
$src = Yii::$app->getModule('image')->noImageSrc; |
25
|
|
|
$errorImage = ErrorImage::findOne( |
26
|
|
|
['img_id' => $this->owner->id, 'class_name' => $this->owner->className()] |
|
|
|
|
27
|
|
|
); |
28
|
|
|
if ($errorImage === null) { |
29
|
|
|
$errorImage = new ErrorImage; |
30
|
|
|
$errorImage->setAttributes(['img_id' => $this->owner->id, 'class_name' => $this->owner->className()]); |
|
|
|
|
31
|
|
|
$errorImage->save(); |
32
|
|
|
} |
33
|
|
|
} else { |
34
|
|
|
$fs = Yii::$app->fs; |
35
|
|
|
$components = ArrayHelper::index(Yii::$app->getModule('image')->components, 'necessary.class'); |
36
|
|
|
$adapterName = ArrayHelper::getValue($components, $fs::className() . '.necessary.srcAdapter', null); |
37
|
|
|
if ($adapterName === null) { |
38
|
|
|
throw new HttpException(Yii::t('app', 'Set src compiler adapter')); |
39
|
|
|
} |
40
|
|
|
if (class_exists($adapterName) === false) { |
41
|
|
|
throw new HttpException(Yii::t('app', "Class $adapterName not found")); |
42
|
|
|
} |
43
|
|
|
$adapter = new $adapterName; |
44
|
|
|
if ($adapter instanceof CompileSrcInterface) { |
45
|
|
|
$src = $adapter->CompileSrc($src); |
46
|
|
|
} else { |
47
|
|
|
throw new HttpException(Yii::t('app', "Class $adapterName should implement CompileSrcInterface")); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
return $src; |
51
|
|
|
} |
52
|
|
|
} |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.