Skip to content

Added method close. Code inspection #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 54 additions & 28 deletions FtpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@

namespace yii2mod\ftp;

use Countable;
use Exception;
use RuntimeException;

/**
* The FTP and SSL-FTP client for PHP.
*
* @method bool alloc() alloc(int $filesize, string &$result = null) Allocates space for a file to be uploaded
* @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
Expand All @@ -45,7 +48,7 @@
*
* @see `nicolab/php-ftp-client`
*/
class FtpClient implements \Countable
class FtpClient implements Countable
{
/**
* The connection with the server
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion FtpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
2 changes: 1 addition & 1 deletion FtpWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

/**
Expand Down