1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace the0rist\imperavi\actions; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Action; |
7
|
|
|
use yii\base\InvalidCallException; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\web\Response; |
10
|
|
|
use the0rist\imperavi\helpers\FileHelper; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class GetAction |
14
|
|
|
* @package the0rist\imperavi\actions |
15
|
|
|
* |
16
|
|
|
* GetAction returns a JSON array of the files found under the specified directory and subdirectories. |
17
|
|
|
* This array can be used in Imperavi Redactor to insert some files that have already been uploaded. |
18
|
|
|
* |
19
|
|
|
* Usage: |
20
|
|
|
* |
21
|
|
|
* ```php |
22
|
|
|
* public function actions() |
23
|
|
|
* { |
24
|
|
|
* return [ |
25
|
|
|
* 'get-image' => [ |
26
|
|
|
* 'class' => GetAction::className(), |
27
|
|
|
* 'url' => 'http://my-site.com/statics/', |
28
|
|
|
* 'path' => '/var/www/my-site.com/web/statics', |
29
|
|
|
* 'type' => GetAction::TYPE_IMAGES, |
30
|
|
|
* ] |
31
|
|
|
* ]; |
32
|
|
|
* } |
33
|
|
|
* ``` |
34
|
|
|
* |
35
|
|
|
* @author Vasile Crudu <[email protected]> |
36
|
|
|
* |
37
|
|
|
* @link https://github.com/the0rist |
38
|
|
|
*/ |
39
|
|
|
class GetAction extends Action |
40
|
|
|
{ |
41
|
|
|
/** Image type */ |
42
|
|
|
const TYPE_IMAGES = 0; |
43
|
|
|
/** File type */ |
44
|
|
|
const TYPE_FILES = 1; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string Files directory |
48
|
|
|
*/ |
49
|
|
|
public $path; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string Files directory URL |
53
|
|
|
*/ |
54
|
|
|
public $url; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* [\the0rist\imperavi\helpers\FileHelper::findFiles()|FileHelper::findFiles()] options argument. |
58
|
|
|
* @var array Options |
59
|
|
|
*/ |
60
|
|
|
public $options = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var int return type (images or files) |
64
|
|
|
*/ |
65
|
|
|
public $type = self::TYPE_IMAGES; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function init() |
71
|
|
|
{ |
72
|
|
|
if ($this->url === null) { |
73
|
|
|
throw new InvalidConfigException('The "url" attribute must be set.'); |
74
|
|
|
} else { |
75
|
|
|
$this->options['url'] = $this->url; |
76
|
|
|
} |
77
|
|
|
if ($this->path === null) { |
78
|
|
|
throw new InvalidConfigException('The "path" attribute must be set.'); |
79
|
|
|
} else { |
80
|
|
|
$this->path = Yii::getAlias($this->path); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
|
|
public function run() |
88
|
|
|
{ |
89
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
90
|
|
|
return FileHelper::findFiles($this->path, $this->options, $this->type); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.