|
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
|
|
|
* This is the local storage for files |
|
17
|
|
|
*/ |
|
18
|
|
|
class LocalStorage extends Storage |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Upload directory |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
35 |
|
private function uploadDir() |
|
26
|
|
|
{ |
|
27
|
35 |
|
if ($this->getFile()->isProtected()) { |
|
28
|
8 |
|
return Yii::getAlias(Yii::$app->fileManager->uploadDirProtected); |
|
29
|
|
|
} else { |
|
30
|
27 |
|
return Yii::getAlias(Yii::$app->fileManager->uploadDirUnprotected); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Path to the temporary directory of the file |
|
36
|
|
|
* |
|
37
|
|
|
* @param bool $realPath |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
29 |
|
private function dirTmp($realPath = false) |
|
41
|
|
|
{ |
|
42
|
29 |
|
$file = $this->getFile(); |
|
43
|
|
|
|
|
44
|
29 |
|
$path = $realPath ? $this->uploadDir() : ''; |
|
45
|
29 |
|
$path .= '/' . Yii::$app->fileManager->publicPath . '/tmp'; |
|
46
|
29 |
|
$path .= '/' . $file->getDateOfFile(); |
|
47
|
29 |
|
$path .= '/' . $file->owner_type . '/' . $file->id; |
|
48
|
|
|
|
|
49
|
29 |
|
return $path; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Path to the directory of the file |
|
54
|
|
|
* |
|
55
|
|
|
* @param bool $realPath |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
35 |
|
private function dir($realPath = false) |
|
59
|
|
|
{ |
|
60
|
35 |
|
$file = $this->getFile(); |
|
61
|
|
|
|
|
62
|
35 |
|
if ($file->tmp) { |
|
63
|
29 |
|
return $this->dirTmp($realPath); |
|
64
|
|
|
} else { |
|
65
|
32 |
|
$path = $realPath ? $this->uploadDir() : ''; |
|
66
|
32 |
|
$path .= '/' . Yii::$app->fileManager->publicPath; |
|
67
|
32 |
|
$path .= '/' . $file->getDateOfFile(); |
|
68
|
32 |
|
$path .= '/' . $file->owner_type . '/' . $file->owner_id . '/' . $file->id; |
|
69
|
|
|
|
|
70
|
32 |
|
return $path; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Path to the temporary file |
|
76
|
|
|
* |
|
77
|
|
|
* @param bool $realPath |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
26 |
|
private function pathTmp($realPath = false) |
|
81
|
|
|
{ |
|
82
|
26 |
|
return $this->dirTmp($realPath) . '/'. $this->getFile()->name; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Path to the file |
|
87
|
|
|
* |
|
88
|
|
|
* @param bool $realPath |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
35 |
|
public function path($realPath = false) |
|
92
|
|
|
{ |
|
93
|
35 |
|
return $this->dir($realPath) . '/'. $this->getFile()->name; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Save the file to the storage |
|
98
|
|
|
* If the file is temporary, then in the temporary directory |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $path |
|
101
|
|
|
* @param bool $isUploadedFile File has been uploaded or manually created |
|
102
|
|
|
* @return \rkit\filemanager\models\File|bool |
|
103
|
|
|
*/ |
|
104
|
36 |
|
public function save($path, $isUploadedFile = true) |
|
105
|
|
|
{ |
|
106
|
36 |
|
if (file_exists($path)) { |
|
107
|
35 |
|
if (FileHelper::createDirectory($this->dir(true))) { |
|
108
|
35 |
|
$isConsole = Yii::$app instanceof \yii\console\Application; |
|
109
|
35 |
|
if (!$isUploadedFile || $isConsole) { |
|
110
|
35 |
|
$saved = rename($path, $this->path(true)); |
|
111
|
35 |
|
} else { |
|
112
|
|
|
$saved = move_uploaded_file($path, $this->path(true)); // @codeCoverageIgnore |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
35 |
|
if ($saved) { |
|
116
|
35 |
|
return $this->getFile(); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
} // @codeCoverageIgnore |
|
119
|
|
|
} // @codeCoverageIgnore |
|
120
|
|
|
|
|
121
|
1 |
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Save the temporary file to the storage |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
26 |
|
public function saveTemporaryFileToStorage() |
|
130
|
|
|
{ |
|
131
|
26 |
|
if (file_exists($this->pathTmp(true))) { |
|
132
|
26 |
|
FileHelper::copyDirectory($this->dirTmp(true), $this->dir(true)); |
|
133
|
26 |
|
FileHelper::removeDirectory($this->dirTmp(true)); |
|
134
|
26 |
|
return true; |
|
135
|
|
|
} // @codeCoverageIgnore |
|
136
|
|
|
|
|
137
|
2 |
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
11 |
|
public function delete() |
|
141
|
|
|
{ |
|
142
|
11 |
|
FileHelper::removeDirectory($this->dir(true)); |
|
143
|
11 |
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.