1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link https://github.com/rkit/filemanager-yii2 |
5
|
|
|
* @copyright Copyright (c) 2015 Igor Romanov |
6
|
|
|
* @license [MIT](http://opensource.org/licenses/MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace rkit\filemanager\storages; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use yii\helpers\FileHelper; |
13
|
|
|
use rkit\filemanager\Storage; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The local storage for files |
17
|
|
|
* |
18
|
|
|
* @author Igor Romanov <[email protected]> |
19
|
|
|
* @since 2.0 |
20
|
|
|
*/ |
21
|
|
|
class LocalStorage extends Storage |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Upload directory |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
34 |
|
private function uploadDir() |
29
|
|
|
{ |
30
|
34 |
|
if ($this->getFile()->isProtected()) { |
31
|
12 |
|
return Yii::getAlias(Yii::$app->fileManager->uploadDirProtected); |
32
|
|
|
} |
33
|
22 |
|
return Yii::getAlias(Yii::$app->fileManager->uploadDirUnprotected); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Path to the temporary directory of the file |
38
|
|
|
* |
39
|
|
|
* @param bool $realPath The real path of the directory |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
29 |
|
private function dirTmp($realPath = false) |
43
|
|
|
{ |
44
|
29 |
|
$file = $this->getFile(); |
45
|
|
|
|
46
|
29 |
|
$path = $realPath ? $this->uploadDir() : ''; |
47
|
29 |
|
$path .= '/' . Yii::$app->fileManager->publicPath . '/tmp'; |
48
|
29 |
|
$path .= '/' . $file->getDateOfFile(); |
49
|
29 |
|
$path .= '/' . $file->owner_type . '/' . $file->id; |
50
|
|
|
|
51
|
29 |
|
return $path; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Path to the directory of the file |
56
|
|
|
* |
57
|
|
|
* @param bool $realPath The real path of the directory |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
34 |
|
private function dir($realPath = false) |
61
|
|
|
{ |
62
|
34 |
|
$file = $this->getFile(); |
63
|
|
|
|
64
|
34 |
|
if ($file->tmp) { |
65
|
29 |
|
return $this->dirTmp($realPath); |
66
|
|
|
} |
67
|
24 |
|
$path = $realPath ? $this->uploadDir() : ''; |
68
|
24 |
|
$path .= '/' . Yii::$app->fileManager->publicPath; |
69
|
24 |
|
$path .= '/' . $file->getDateOfFile(); |
70
|
24 |
|
$path .= '/' . $file->owner_type . '/' . $file->owner_id . '/' . $file->id; |
71
|
|
|
|
72
|
24 |
|
return $path; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Path to the temporary file |
77
|
|
|
* |
78
|
|
|
* @param bool $realPath The real path of the file |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
19 |
|
private function pathTmp($realPath = false) |
82
|
|
|
{ |
83
|
19 |
|
return $this->dirTmp($realPath) . '/'. $this->getFile()->name; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Path to the file |
88
|
|
|
* |
89
|
|
|
* @param bool $realPath The real path of the file |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
34 |
|
public function path($realPath = false) |
93
|
|
|
{ |
94
|
34 |
|
return $this->dir($realPath) . '/'. $this->getFile()->name; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Save the file to the storage |
99
|
|
|
* If the file is temporary, then in the temporary directory |
100
|
|
|
* |
101
|
|
|
* @param string $path The path of the file |
102
|
|
|
* @return \rkit\filemanager\models\File|bool |
103
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
104
|
|
|
*/ |
105
|
35 |
|
public function save($path) |
106
|
|
|
{ |
107
|
35 |
|
if (file_exists($path)) { |
108
|
34 |
|
if (FileHelper::createDirectory($this->dir(true))) { |
109
|
34 |
|
$isConsole = Yii::$app instanceof \yii\console\Application; |
110
|
34 |
|
if (!is_uploaded_file($path) || $isConsole) { |
111
|
34 |
|
$saved = rename($path, $this->path(true)); |
112
|
34 |
|
} else { |
113
|
|
|
$saved = move_uploaded_file($path, $this->path(true)); // @codeCoverageIgnore |
114
|
|
|
} |
115
|
|
|
|
116
|
34 |
|
if ($saved) { |
117
|
34 |
|
return $this->getFile(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} // @codeCoverageIgnore |
120
|
|
|
} // @codeCoverageIgnore |
121
|
|
|
|
122
|
1 |
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Save the temporary file to the storage |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
19 |
|
public function saveTemporaryFileToStorage() |
131
|
|
|
{ |
132
|
19 |
|
if (file_exists($this->pathTmp(true))) { |
133
|
19 |
|
FileHelper::copyDirectory($this->dirTmp(true), $this->dir(true)); |
134
|
19 |
|
FileHelper::removeDirectory($this->dirTmp(true)); |
135
|
19 |
|
return true; |
136
|
|
|
} // @codeCoverageIgnore |
137
|
|
|
|
138
|
2 |
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
8 |
|
public function delete() |
142
|
|
|
{ |
143
|
8 |
|
FileHelper::removeDirectory($this->dir(true)); |
144
|
8 |
|
} |
145
|
|
|
} |
146
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.