16
16
// ------------------------------------------------------------------------
17
17
18
18
use O2System \Spl \Exceptions \RuntimeException ;
19
+ use O2System \Spl \Traits \Collectors \ConfigCollectorTrait ;
20
+ use O2System \Spl \Traits \Collectors \ErrorCollectorTrait ;
19
21
20
22
/**
21
23
* Class Ftp
24
26
*/
25
27
class Ftp
26
28
{
29
+ use ConfigCollectorTrait;
30
+ use ErrorCollectorTrait;
31
+
27
32
/**
28
33
* Passive mode flag
29
34
*
@@ -62,8 +67,16 @@ class Ftp
62
67
/**
63
68
* Ftp::__construct
64
69
*/
65
- public function __construct ()
70
+ public function __construct (array $ config = [] )
66
71
{
72
+ $ this ->setConfig ($ config );
73
+
74
+ // Prep the port
75
+ $ this ->config [ 'port ' ] = empty ($ this ->config [ 'port ' ]) ? 21 : (int )$ this ->config [ 'port ' ];
76
+
77
+ // Prep the hostname
78
+ $ this ->config [ 'hostname ' ] = preg_replace ('|.+?://| ' , '' , $ this ->config [ 'hostname ' ]);
79
+
67
80
language ()
68
81
->addFilePath (str_replace ('Handlers ' , '' , __DIR__ ) . DIRECTORY_SEPARATOR )
69
82
->loadFile ('ftp ' );
@@ -76,32 +89,28 @@ public function __construct()
76
89
*
77
90
* Connect to FTP server.
78
91
*
79
- * @param array $config Ftp configuration.
80
- *
81
92
* @return bool Returns TRUE on success or FALSE on failure.
82
93
* @throws RuntimeException
83
94
*/
84
- public function connect (array $ config = [] )
95
+ public function connect ()
85
96
{
86
- // Prep the port
87
- $ config [ 'port ' ] = empty ($ config [ 'port ' ]) ? 21 : (int )$ config [ 'port ' ];
88
-
89
- // Prep the hostname
90
- $ config [ 'hostname ' ] = preg_replace ('|.+?://| ' , '' , $ config [ 'hostname ' ]);
91
-
92
- if (false === ($ this ->handle = @ftp_connect ($ config [ 'hostname ' ], $ config [ 'port ' ]))) {
97
+ if (false === ($ this ->handle = @ftp_connect ($ this ->config [ 'hostname ' ], $ this ->config [ 'port ' ]))) {
93
98
if ($ this ->debugMode === true ) {
94
99
throw new RuntimeException ('FTP_E_UNABLE_TO_CONNECT ' );
95
100
}
96
101
102
+ $ this ->addError (1 , 'FTP_E_UNABLE_TO_CONNECT ' );
103
+
97
104
return false ;
98
105
}
99
106
100
- if (false !== (@ftp_login ($ this ->handle , $ config [ 'username ' ], $ config [ 'password ' ]))) {
107
+ if (false !== (@ftp_login ($ this ->handle , $ this -> config [ 'username ' ], $ this -> config [ 'password ' ]))) {
101
108
if ($ this ->debugMode === true ) {
102
109
throw new RuntimeException ('FTP_E_UNABLE_TO_LOGIN ' );
103
110
}
104
111
112
+ $ this ->addError (2 , 'FTP_E_UNABLE_TO_LOGIN ' );
113
+
105
114
return false ;
106
115
}
107
116
@@ -110,8 +119,6 @@ public function connect(array $config = [])
110
119
ftp_pasv ($ this ->handle , true );
111
120
}
112
121
113
- $ this ->config = $ config ;
114
-
115
122
return true ;
116
123
}
117
124
@@ -151,6 +158,8 @@ public function download($remoteFilePath, $localFilePath, $mode = 'auto')
151
158
throw new RuntimeException ('FTP_E_UNABLE_TO_DOWNLOAD ' );
152
159
}
153
160
161
+ $ this ->addError (3 , 'FTP_E_UNABLE_TO_DOWNLOAD ' );
162
+
154
163
return false ;
155
164
}
156
165
@@ -174,6 +183,8 @@ protected function isConnected()
174
183
throw new RuntimeException ('FTP_E_NO_CONNECTION ' );
175
184
}
176
185
186
+ $ this ->addError (4 , 'FTP_E_NO_CONNECTION ' );
187
+
177
188
return false ;
178
189
}
179
190
@@ -246,6 +257,8 @@ public function rename($oldFilename, $newFilename)
246
257
throw new RuntimeException ('FTP_UNABLE_TO_RENAME ' );
247
258
}
248
259
260
+ $ this ->addError (5 , 'FTP_UNABLE_TO_RENAME ' );
261
+
249
262
return false ;
250
263
}
251
264
@@ -278,6 +291,8 @@ public function move($oldRemoteFilePath, $newRemoteFilePath)
278
291
throw new RuntimeException ('FTP_UNABLE_TO_MOVE ' );
279
292
}
280
293
294
+ $ this ->addError (6 , 'FTP_UNABLE_TO_MOVE ' );
295
+
281
296
return false ;
282
297
}
283
298
@@ -309,6 +324,8 @@ public function deleteFile($filePath)
309
324
throw new RuntimeException ('FTP_E_UNABLE_TO_DELETE ' );
310
325
}
311
326
327
+ $ this ->addError (7 , 'FTP_E_UNABLE_TO_DELETE ' );
328
+
312
329
return false ;
313
330
}
314
331
@@ -353,6 +370,8 @@ public function deleteDir($remotePath)
353
370
throw new RuntimeException ('FTP_E_UNABLE_TO_DELETE_DIRECTORY ' );
354
371
}
355
372
373
+ $ this ->addError (8 , 'FTP_E_UNABLE_TO_DELETE_DIRECTORY ' );
374
+
356
375
return false ;
357
376
}
358
377
@@ -461,6 +480,8 @@ public function changeDir($remotePath, $suppressDebug = false)
461
480
throw new RuntimeException ('FTP_E_UNABLE_TO_CHANGE_DIRECTORY ' );
462
481
}
463
482
483
+ $ this ->addError (9 , 'FTP_E_UNABLE_TO_CHANGE_DIRECTORY ' );
484
+
464
485
return false ;
465
486
}
466
487
@@ -493,6 +514,8 @@ public function makeDir($remotePath, $permissions = null)
493
514
throw new RuntimeException ('FTP_E_UNABLE_TO_MAKE_DIRECTORY ' );
494
515
}
495
516
517
+ $ this ->addError (10 , 'FTP_E_UNABLE_TO_MAKE_DIRECTORY ' );
518
+
496
519
return false ;
497
520
}
498
521
@@ -528,6 +551,8 @@ public function setChmod($remotePath, $mode)
528
551
throw new RuntimeException ('FTP_E_UNABLE_TO_CHMOD ' );
529
552
}
530
553
554
+ $ this ->addError (11 , 'FTP_E_UNABLE_TO_CHMOD ' );
555
+
531
556
return false ;
532
557
}
533
558
@@ -572,6 +597,8 @@ public function upload($localFilePath, $remoteFilePath, $mode = 'auto', $permiss
572
597
throw new RuntimeException ('FTP_E_UNABLE_TO_UPLOAD ' );
573
598
}
574
599
600
+ $ this ->addError (12 , 'FTP_E_UNABLE_TO_UPLOAD ' );
601
+
575
602
return false ;
576
603
}
577
604
0 commit comments