diff --git a/FtpClient.php b/FtpClient.php index 30baaad..b550e0d 100644 --- a/FtpClient.php +++ b/FtpClient.php @@ -12,6 +12,10 @@ namespace yii2mod\ftp; +use Countable; +use Exception; +use RuntimeException; + /** * The FTP and SSL-FTP client for PHP. * @@ -19,7 +23,6 @@ * @method bool cdup() cdup() Changes to the parent directory * @method bool chdir() chdir(string $directory) Changes the current directory on a FTP server * @method int chmod() chmod(int $mode, string $filename) Set permissions on a file via FTP - * @method bool close() close() Closes an FTP connection * @method bool delete() delete(string $path) Deletes a file on the FTP server * @method bool exec() exec(string $command) Requests execution of a command on the FTP server * @method bool fget() fget(resource $handle, string $remote_file, int $mode, int $resumepos = 0) Downloads a file from the FTP server and saves to an open file @@ -45,7 +48,7 @@ * * @see `nicolab/php-ftp-client` */ -class FtpClient implements \Countable +class FtpClient implements Countable { /** * The connection with the server @@ -87,8 +90,24 @@ public function __construct($connection = null) public function __destruct() { if ($this->conn) { - $this->ftp->close(); + $this->close(); + $this->conn = null; + } + } + + /** + * @return bool + */ + public function close() + { + if (!$this->ftp) { + $this->conn = null; + return true; } + $return = $this->ftp->close(); + $this->conn = null; + $this->ftp = null; + return $return; } /** @@ -100,9 +119,10 @@ public function __destruct() * @param $method * @param array $arguments * + * @return mixed + * @throws FtpException * @internal param string $function * - * @return mixed */ public function __call($method, array $arguments) { @@ -173,6 +193,7 @@ public function connect($host, $ssl = false, $port = 21, $timeout = 90) * @param bool $include_hidden * * @return FtpClient + * @throws FtpException */ public function getAll($source_directory, $target_directory, $mode = FTP_BINARY, $include_hidden = false) { @@ -182,9 +203,11 @@ public function getAll($source_directory, $target_directory, $mode = FTP_BINARY, foreach ($d as $file) { $new_source_directory = $source_directory . '/' . $file['name']; $new_target_directory = $target_directory . '/' . $file['name']; - if ($file['type'] == 'directory') { + if ($file['type'] === 'directory') { if (!is_dir($new_target_directory)) { - mkdir($new_target_directory); + if (!mkdir($new_target_directory) && !is_dir($new_target_directory)) { + throw new RuntimeException(sprintf('Directory "%s" was not created', $new_target_directory)); + } } $this->getAll($new_source_directory, $new_target_directory, $mode, $include_hidden); } else { @@ -350,15 +373,16 @@ public function nlist($directory = '.', $recursive = false, $filter = 'sort') /** * Creates a directory * + * @param string $directory The directory + * @param bool $recursive + * + * @return string|false + * @throws FtpException * @see FtpClient::rmdir() * @see FtpClient::remove() * @see FtpClient::put() * @see FtpClient::putAll() * - * @param string $directory The directory - * @param bool $recursive - * - * @return string|false */ public function mkdir($directory, $recursive = false) { @@ -413,13 +437,14 @@ public function rmdir($directory, $recursive = true) /** * Empty directory * + * @param string $directory + * + * @return bool + * @throws FtpException * @see FtpClient::remove() * @see FtpClient::delete() * @see FtpClient::rmdir() * - * @param string $directory - * - * @return bool */ public function cleanDir($directory) { @@ -449,12 +474,9 @@ public function cleanDir($directory) public function remove($path, $recursive = false) { try { - if (@$this->ftp->delete($path) || ($this->isDir($path) and @$this->rmdir($path, $recursive))) { - return true; - } - - return false; - } catch (\Exception $e) { + return @$this->ftp->delete($path) + || ($this->isDir($path) and @$this->rmdir($path, $recursive)); + } catch (Exception $e) { return false; } } @@ -490,25 +512,27 @@ public function isDir($directory) * @param string $directory * * @return bool + * @throws FtpException */ public function isEmpty($directory) { - return $this->count($directory, null, false) === 0 ? true : false; + return $this->count($directory, null, false) === 0; } /** * Scan a directory and returns the details of each item. * - * @see FtpClient::nlist() - * @see FtpClient::rawlist() - * @see FtpClient::parseRawList() - * @see FtpClient::dirSize() - * * @param string $directory * @param bool $recursive * @param bool $includeHidden * * @return array + * @throws FtpException + * @see FtpClient::rawlist() + * @see FtpClient::parseRawList() + * @see FtpClient::dirSize() + * + * @see FtpClient::nlist() */ public function scanDir($directory = '.', $recursive = false, $includeHidden = false) { @@ -523,6 +547,7 @@ public function scanDir($directory = '.', $recursive = false, $includeHidden = f * @param bool $include_hidden false by default * * @return int The size in bytes + * @throws FtpException */ public function dirSize($directory = '.', $recursive = true, $include_hidden = false) { @@ -544,6 +569,7 @@ public function dirSize($directory = '.', $recursive = true, $include_hidden = f * @param bool $include_hidden * * @return int + * @throws FtpException */ public function count($directory = '.', $type = null, $recursive = true, $include_hidden = false) { @@ -677,7 +703,7 @@ public function rawlist($directory = '.', $recursive = false, $includeHidden = f $items = []; if (false == $recursive) { foreach ($list as $path => $item) { - $chunks = preg_split("/\s+/", $item); + $chunks = preg_split('/\s+/', $item); // if not "name" if (empty($chunks[8]) || $chunks[8] == '.' || $chunks[8] == '..') { continue; @@ -702,7 +728,7 @@ public function rawlist($directory = '.', $recursive = false, $includeHidden = f ) { continue; } - $chunks = preg_split("/\s+/", $item); + $chunks = preg_split('/\s+/', $item); // if not "name" if (empty($chunks[8]) || $chunks[8] == '.' || $chunks[8] == '..') { continue; @@ -739,7 +765,7 @@ public function parseRawList(array $rawlist) $items = []; $path = ''; foreach ($rawlist as $key => $child) { - $chunks = preg_split("/\s+/", $child); + $chunks = preg_split('/\s+/', $child); if (isset($chunks[8]) && ($chunks[8] == '.' or $chunks[8] == '..')) { continue; } diff --git a/FtpException.php b/FtpException.php index 5316bff..94ccee9 100644 --- a/FtpException.php +++ b/FtpException.php @@ -12,9 +12,11 @@ namespace yii2mod\ftp; +use Exception; + /** * The FtpException class. Exception thrown if an error on runtime of the FTP client occurs. */ -class FtpException extends \Exception +class FtpException extends Exception { } diff --git a/FtpWrapper.php b/FtpWrapper.php index efdb03d..7eb11b7 100644 --- a/FtpWrapper.php +++ b/FtpWrapper.php @@ -89,7 +89,7 @@ public function __call($function, array $arguments) return call_user_func_array($function, $arguments); } - throw new FtpException("{$function} is not a valid FTP function"); + throw new FtpException("$function is not a valid FTP function"); } /**