1616// ------------------------------------------------------------------------
1717
1818use O2System \Spl \Exceptions \RuntimeException ;
19+ use O2System \Spl \Traits \Collectors \ConfigCollectorTrait ;
20+ use O2System \Spl \Traits \Collectors \ErrorCollectorTrait ;
1921
2022/**
2123 * Class Ftp
2426 */
2527class Ftp
2628{
29+ use ConfigCollectorTrait;
30+ use ErrorCollectorTrait;
31+
2732 /**
2833 * Passive mode flag
2934 *
@@ -62,8 +67,16 @@ class Ftp
6267 /**
6368 * Ftp::__construct
6469 */
65- public function __construct ()
70+ public function __construct (array $ config = [] )
6671 {
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+
6780 language ()
6881 ->addFilePath (str_replace ('Handlers ' , '' , __DIR__ ) . DIRECTORY_SEPARATOR )
6982 ->loadFile ('ftp ' );
@@ -76,32 +89,28 @@ public function __construct()
7689 *
7790 * Connect to FTP server.
7891 *
79- * @param array $config Ftp configuration.
80- *
8192 * @return bool Returns TRUE on success or FALSE on failure.
8293 * @throws RuntimeException
8394 */
84- public function connect (array $ config = [] )
95+ public function connect ()
8596 {
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 ' ]))) {
9398 if ($ this ->debugMode === true ) {
9499 throw new RuntimeException ('FTP_E_UNABLE_TO_CONNECT ' );
95100 }
96101
102+ $ this ->addError (1 , 'FTP_E_UNABLE_TO_CONNECT ' );
103+
97104 return false ;
98105 }
99106
100- if (false !== (@ftp_login ($ this ->handle , $ config [ 'username ' ], $ config [ 'password ' ]))) {
107+ if (false !== (@ftp_login ($ this ->handle , $ this -> config [ 'username ' ], $ this -> config [ 'password ' ]))) {
101108 if ($ this ->debugMode === true ) {
102109 throw new RuntimeException ('FTP_E_UNABLE_TO_LOGIN ' );
103110 }
104111
112+ $ this ->addError (2 , 'FTP_E_UNABLE_TO_LOGIN ' );
113+
105114 return false ;
106115 }
107116
@@ -110,8 +119,6 @@ public function connect(array $config = [])
110119 ftp_pasv ($ this ->handle , true );
111120 }
112121
113- $ this ->config = $ config ;
114-
115122 return true ;
116123 }
117124
@@ -151,6 +158,8 @@ public function download($remoteFilePath, $localFilePath, $mode = 'auto')
151158 throw new RuntimeException ('FTP_E_UNABLE_TO_DOWNLOAD ' );
152159 }
153160
161+ $ this ->addError (3 , 'FTP_E_UNABLE_TO_DOWNLOAD ' );
162+
154163 return false ;
155164 }
156165
@@ -174,6 +183,8 @@ protected function isConnected()
174183 throw new RuntimeException ('FTP_E_NO_CONNECTION ' );
175184 }
176185
186+ $ this ->addError (4 , 'FTP_E_NO_CONNECTION ' );
187+
177188 return false ;
178189 }
179190
@@ -246,6 +257,8 @@ public function rename($oldFilename, $newFilename)
246257 throw new RuntimeException ('FTP_UNABLE_TO_RENAME ' );
247258 }
248259
260+ $ this ->addError (5 , 'FTP_UNABLE_TO_RENAME ' );
261+
249262 return false ;
250263 }
251264
@@ -278,6 +291,8 @@ public function move($oldRemoteFilePath, $newRemoteFilePath)
278291 throw new RuntimeException ('FTP_UNABLE_TO_MOVE ' );
279292 }
280293
294+ $ this ->addError (6 , 'FTP_UNABLE_TO_MOVE ' );
295+
281296 return false ;
282297 }
283298
@@ -309,6 +324,8 @@ public function deleteFile($filePath)
309324 throw new RuntimeException ('FTP_E_UNABLE_TO_DELETE ' );
310325 }
311326
327+ $ this ->addError (7 , 'FTP_E_UNABLE_TO_DELETE ' );
328+
312329 return false ;
313330 }
314331
@@ -353,6 +370,8 @@ public function deleteDir($remotePath)
353370 throw new RuntimeException ('FTP_E_UNABLE_TO_DELETE_DIRECTORY ' );
354371 }
355372
373+ $ this ->addError (8 , 'FTP_E_UNABLE_TO_DELETE_DIRECTORY ' );
374+
356375 return false ;
357376 }
358377
@@ -461,6 +480,8 @@ public function changeDir($remotePath, $suppressDebug = false)
461480 throw new RuntimeException ('FTP_E_UNABLE_TO_CHANGE_DIRECTORY ' );
462481 }
463482
483+ $ this ->addError (9 , 'FTP_E_UNABLE_TO_CHANGE_DIRECTORY ' );
484+
464485 return false ;
465486 }
466487
@@ -493,6 +514,8 @@ public function makeDir($remotePath, $permissions = null)
493514 throw new RuntimeException ('FTP_E_UNABLE_TO_MAKE_DIRECTORY ' );
494515 }
495516
517+ $ this ->addError (10 , 'FTP_E_UNABLE_TO_MAKE_DIRECTORY ' );
518+
496519 return false ;
497520 }
498521
@@ -528,6 +551,8 @@ public function setChmod($remotePath, $mode)
528551 throw new RuntimeException ('FTP_E_UNABLE_TO_CHMOD ' );
529552 }
530553
554+ $ this ->addError (11 , 'FTP_E_UNABLE_TO_CHMOD ' );
555+
531556 return false ;
532557 }
533558
@@ -572,6 +597,8 @@ public function upload($localFilePath, $remoteFilePath, $mode = 'auto', $permiss
572597 throw new RuntimeException ('FTP_E_UNABLE_TO_UPLOAD ' );
573598 }
574599
600+ $ this ->addError (12 , 'FTP_E_UNABLE_TO_UPLOAD ' );
601+
575602 return false ;
576603 }
577604
0 commit comments