1 | <?php |
||
8 | class Sftp extends FtpAbstract implements Logger\LoggerInterface |
||
9 | { |
||
10 | use Logger\LoggerTrait; |
||
11 | |||
12 | const METHODS = 'methods'; |
||
13 | const CALLBACKS = 'callbacks'; |
||
14 | const USE_INCLUDE_PATH = 'use_include_path'; |
||
15 | const GET_CONTENT_CONTEXT = 'get_content_context'; |
||
16 | const PUT_CONTENT_CONTEXT = 'put_content_context'; |
||
17 | const PUT_CONTENT_FLAGS = 'put_content_flags'; |
||
18 | const OFFSET = 'offset'; |
||
19 | const MAXLEN = 'maxlen'; |
||
20 | const CREATE_MODE = 'create_mode'; |
||
21 | |||
22 | private $sftpSession; |
||
23 | |||
24 | /** |
||
25 | * @return resource |
||
26 | * @throws FtpException |
||
27 | */ |
||
28 | 1 | protected function getSftpSession() |
|
29 | { |
||
30 | 1 | if (!$this->sftpSession) { |
|
31 | 1 | $this->sftpSession = $this->ssh2Sftp($this->getSession()); |
|
32 | } |
||
33 | 1 | return $this->sftpSession; |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * @return array|null |
||
38 | */ |
||
39 | 2 | protected function getMethods() |
|
43 | |||
44 | /** |
||
45 | * @return array|null |
||
46 | */ |
||
47 | 2 | protected function getCallbacks() |
|
51 | |||
52 | /** |
||
53 | * Optional param for file_get_content |
||
54 | * @return bool |
||
55 | */ |
||
56 | 1 | protected function getUseIncludePath() |
|
60 | |||
61 | /** |
||
62 | * Optional param for file_get_content |
||
63 | * @return resource|null |
||
64 | */ |
||
65 | 1 | protected function getGetContentContext() |
|
69 | |||
70 | /** |
||
71 | * Optional param for file_get_content |
||
72 | * @return int |
||
73 | */ |
||
74 | 1 | protected function getOffset() |
|
78 | |||
79 | /** |
||
80 | * Optional param for file_get_content |
||
81 | * @return int|null |
||
82 | */ |
||
83 | 1 | protected function getMaxLen() |
|
87 | |||
88 | /** |
||
89 | * Optional param for file_put_content |
||
90 | * @return int |
||
91 | */ |
||
92 | 1 | protected function getPutContentFlags() |
|
96 | |||
97 | /** |
||
98 | * Optional param for file_put_content |
||
99 | * @return resource|null |
||
100 | */ |
||
101 | 1 | protected function getPutContentContext() |
|
105 | |||
106 | /** |
||
107 | * Optional param for file_get_content |
||
108 | * @return int |
||
109 | */ |
||
110 | 1 | protected function getCreateMode() |
|
114 | |||
115 | /** |
||
116 | * @param string $host |
||
117 | * @param int $port |
||
118 | * @return $this |
||
119 | * @throws FtpException |
||
120 | */ |
||
121 | 1 | public function connect($host, $port = 22) |
|
127 | |||
128 | /** |
||
129 | * @param string $host |
||
130 | * @param int $port |
||
131 | * @param array|null $methods |
||
132 | * @param array|null $callbacks |
||
133 | * @return resource |
||
134 | * @codeCoverageIgnore |
||
135 | */ |
||
136 | protected function ssh2Connect($host, $port = 22, array $methods = null, array $callbacks = null) |
||
140 | |||
141 | /** |
||
142 | * @param string $user |
||
143 | * @param string $pass |
||
144 | * @return bool |
||
145 | * @throws FtpException |
||
146 | */ |
||
147 | 2 | public function login($user, $pass) |
|
156 | |||
157 | /** |
||
158 | * @param resource $session |
||
159 | * @param string $username |
||
160 | * @param string $password |
||
161 | * @return bool |
||
162 | * @codeCoverageIgnore |
||
163 | */ |
||
164 | protected function ssh2AuthPassword($session, $username, $password) |
||
168 | |||
169 | /** |
||
170 | * @param string $localFile |
||
171 | * @param string $remoteFile |
||
172 | * @return int |
||
173 | */ |
||
174 | 2 | public function get($localFile, $remoteFile) |
|
175 | { |
||
176 | 2 | if ($this->getMaxLen() === null) { |
|
177 | 1 | $fileContent = $this->fileGetContents( |
|
178 | 1 | 'ssh2.sftp://' . $this->getSftpSession() . '/' . $remoteFile, |
|
179 | 1 | $this->getUseIncludePath(), |
|
180 | 1 | $this->getGetContentContext(), |
|
181 | 1 | $this->getOffset() |
|
182 | ); |
||
183 | } else { |
||
184 | 1 | $fileContent = $this->fileGetContents( |
|
185 | 1 | 'ssh2.sftp://' . $this->getSftpSession() . '/' . $remoteFile, |
|
186 | 1 | $this->getUseIncludePath(), |
|
187 | 1 | $this->getGetContentContext(), |
|
188 | 1 | $this->getOffset(), |
|
189 | 1 | $this->getMaxLen() |
|
190 | ); |
||
191 | } |
||
192 | 2 | return $this->filePutContents( |
|
|
|||
193 | 2 | $localFile, |
|
194 | 2 | $fileContent, |
|
195 | 2 | $this->getPutContentFlags(), |
|
196 | 2 | $this->getPutContentContext() |
|
197 | ); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Put file on ftp server |
||
202 | * @param string $remoteFile |
||
203 | * @param string $localFile |
||
204 | * @return bool |
||
205 | */ |
||
206 | 2 | public function put($remoteFile, $localFile) |
|
207 | { |
||
208 | 2 | if ($this->getMaxLen() === null) { |
|
209 | 1 | $fileContent = $this->fileGetContents( |
|
210 | 1 | $localFile, |
|
211 | 1 | $this->getUseIncludePath(), |
|
212 | 1 | $this->getGetContentContext(), |
|
213 | 1 | $this->getOffset() |
|
214 | ); |
||
215 | } else { |
||
216 | 1 | $fileContent = $this->fileGetContents( |
|
217 | 1 | $localFile, |
|
218 | 1 | $this->getUseIncludePath(), |
|
219 | 1 | $this->getGetContentContext(), |
|
220 | 1 | $this->getOffset(), |
|
221 | 1 | $this->getMaxLen() |
|
222 | ); |
||
223 | } |
||
224 | 2 | return $this->filePutContents( |
|
225 | 2 | 'ssh2.sftp://' . intval($this->getSftpSession()) . '/' . $remoteFile, |
|
226 | 2 | $fileContent, |
|
227 | 2 | $this->getPutContentFlags(), |
|
228 | 2 | $this->getPutContentContext() |
|
229 | ); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param resource $session |
||
234 | * @param string $localFile |
||
235 | * @param string $remoteFile |
||
236 | * @param int $createMode |
||
237 | * @return bool |
||
238 | * @codeCoverageIgnore |
||
239 | */ |
||
240 | protected function ssh2ScpSend($session, $localFile, $remoteFile, $createMode) |
||
244 | |||
245 | /** |
||
246 | * @return string |
||
247 | * @codeCoverageIgnore |
||
248 | */ |
||
249 | protected function fileGetContents() |
||
253 | |||
254 | /** |
||
255 | * @param string $fileName |
||
256 | * @param mixed $data |
||
257 | * @param int $flags |
||
258 | * @param resource $context |
||
259 | * @return int |
||
260 | * @codeCoverageIgnore |
||
261 | */ |
||
262 | protected function filePutContents($fileName, $data, $flags = 0, $context = null) |
||
266 | |||
267 | /** |
||
268 | * @param string $file |
||
269 | * @return bool |
||
270 | */ |
||
271 | 1 | public function delete($file) |
|
275 | |||
276 | /** |
||
277 | * @param resource $sftpSession |
||
278 | * @param string $path |
||
279 | * @return bool |
||
280 | * @codeCoverageIgnore |
||
281 | */ |
||
282 | protected function ssh2SftpUnlink($sftpSession, $path) |
||
286 | |||
287 | /** |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | 1 | public function close() |
|
295 | |||
296 | /** |
||
297 | * @param resource $session |
||
298 | * @return resource |
||
299 | * @codeCoverageIgnore |
||
300 | */ |
||
301 | protected function ssh2Sftp($session) |
||
305 | } |
||
306 |
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.