1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WebHemi. |
4
|
|
|
* |
5
|
|
|
* PHP version 7.1 |
6
|
|
|
* |
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* @link http://www.gixx-web.com |
11
|
|
|
*/ |
12
|
|
|
declare(strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace WebHemi\Ftp; |
15
|
|
|
|
16
|
|
|
use RuntimeException; |
17
|
|
|
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class AbstractServiceAdapter |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractServiceAdapter implements ServiceInterface |
23
|
|
|
{ |
24
|
|
|
/** @var string */ |
25
|
|
|
protected $localPath = __DIR__; |
26
|
|
|
/** @var array */ |
27
|
|
|
protected $options = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ServiceAdapter constructor. |
31
|
|
|
* |
32
|
|
|
* @param ConfigurationInterface $configuration |
33
|
|
|
*/ |
34
|
|
|
public function __construct(ConfigurationInterface $configuration) |
35
|
|
|
{ |
36
|
|
|
$this->setOptions($configuration->getData('ftp')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Connect and login to remote host. |
41
|
|
|
* |
42
|
|
|
* @return ServiceInterface |
43
|
|
|
*/ |
44
|
|
|
abstract public function connect() : ServiceInterface; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Disconnect from remote host. |
48
|
|
|
* |
49
|
|
|
* @return ServiceInterface |
50
|
|
|
*/ |
51
|
|
|
abstract public function disconnect() : ServiceInterface; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets an option data. |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @return ServiceInterface |
59
|
|
|
*/ |
60
|
|
|
public function setOption(string $key, $value) : ServiceInterface |
61
|
|
|
{ |
62
|
|
|
$this->options[$key] = $value; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets a group of options. |
68
|
|
|
* |
69
|
|
|
* @param array $options |
70
|
|
|
* @return ServiceInterface |
71
|
|
|
*/ |
72
|
|
|
public function setOptions(array $options) : ServiceInterface |
73
|
|
|
{ |
74
|
|
|
$this->options = array_merge($this->options, $options); |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Gets a specific option data. |
80
|
|
|
* |
81
|
|
|
* @param string $key |
82
|
|
|
* @param mixed $default |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function getOption(string $key, $default = null) |
86
|
|
|
{ |
87
|
|
|
return $this->options[$key] ?? $default; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Toggles connection security level. |
92
|
|
|
* |
93
|
|
|
* @param bool $state |
94
|
|
|
* @return ServiceInterface |
95
|
|
|
*/ |
96
|
|
|
abstract public function setSecureConnection(bool $state) : ServiceInterface; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Toggles connection passive mode. |
100
|
|
|
* |
101
|
|
|
* @param bool $state |
102
|
|
|
* @return ServiceInterface |
103
|
|
|
*/ |
104
|
|
|
abstract public function setPassiveMode(bool $state) : ServiceInterface; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sets remote path. |
108
|
|
|
* |
109
|
|
|
* @param string $path |
110
|
|
|
* @return ServiceInterface |
111
|
|
|
*/ |
112
|
|
|
abstract public function setRemotePath(string $path) : ServiceInterface; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Gets remote path. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
abstract public function getRemotePath() : string; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sets local path. |
123
|
|
|
* |
124
|
|
|
* @param string $path |
125
|
|
|
* @return ServiceInterface |
126
|
|
|
*/ |
127
|
|
|
public function setLocalPath(string $path) : ServiceInterface |
128
|
|
|
{ |
129
|
|
|
// if it's not an absolute path, we take it relative to the current folder |
130
|
|
|
if (strpos($path, '/') !== 0) { |
131
|
|
|
$path = __DIR__.'/'.$path; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (!realpath($path) || !is_dir($path)) { |
135
|
|
|
throw new RuntimeException(sprintf('No such directory: %s', $path), 1003); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!is_readable($path)) { |
139
|
|
|
throw new RuntimeException(sprintf('Cannot read directory: %s; Permission denied.', $path), 1004); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (!is_writable($path)) { |
143
|
|
|
throw new RuntimeException( |
144
|
|
|
sprintf('Cannot write data into directory: %s; Permission denied.', $path), |
145
|
|
|
1005 |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->localPath = $path; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Gets local path. |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getLocalPath() : string |
160
|
|
|
{ |
161
|
|
|
return $this->localPath; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Lists remote path. |
166
|
|
|
* |
167
|
|
|
* @param null|string $path |
168
|
|
|
* @param bool|null $changeToDirectory |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
abstract public function getRemoteFileList(? string $path, ? bool $changeToDirectory) : array; |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Uploads file to remote host. |
175
|
|
|
* |
176
|
|
|
* @see self::setRemotePath |
177
|
|
|
* @see self::setLocalPath |
178
|
|
|
* |
179
|
|
|
* @param string $sourceFileName |
180
|
|
|
* @param string $destinationFileName |
181
|
|
|
* @param int $fileMode |
182
|
|
|
* @return mixed |
183
|
|
|
*/ |
184
|
|
|
abstract public function upload( |
185
|
|
|
string $sourceFileName, |
186
|
|
|
string $destinationFileName, |
187
|
|
|
int $fileMode = self::FILE_MODE_BINARY |
188
|
|
|
) : ServiceInterface; |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Downloads file from remote host. |
192
|
|
|
* |
193
|
|
|
* @see self::setRemotePath |
194
|
|
|
* @see self::setLocalPath |
195
|
|
|
* |
196
|
|
|
* @param string $remoteFileName |
197
|
|
|
* @param string $localFileName |
198
|
|
|
* @param int $fileMode |
199
|
|
|
* @return mixed |
200
|
|
|
*/ |
201
|
|
|
abstract public function download( |
202
|
|
|
string $remoteFileName, |
203
|
|
|
string&$localFileName, |
204
|
|
|
int $fileMode = self::FILE_MODE_BINARY |
205
|
|
|
) : ServiceInterface; |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Moves file on remote host. |
209
|
|
|
* |
210
|
|
|
* @param string $currentPath |
211
|
|
|
* @param string $newPath |
212
|
|
|
* @return ServiceInterface |
213
|
|
|
*/ |
214
|
|
|
abstract public function moveRemoteFile(string $currentPath, string $newPath) : ServiceInterface; |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Deletes file on remote host. |
218
|
|
|
* |
219
|
|
|
* @param string $path |
220
|
|
|
* @return ServiceInterface |
221
|
|
|
*/ |
222
|
|
|
abstract public function deleteRemoteFile(string $path) : ServiceInterface; |
223
|
|
|
} |
224
|
|
|
|