From 74919a2706ab937d4da06e2bc0686d8998680496 Mon Sep 17 00:00:00 2001 From: crynobone Date: Wed, 21 May 2014 00:33:45 +0800 Subject: [PATCH 1/3] Add Orchestra\Support\Ftp::createSecureConnection() method. Signed-off-by: crynobone --- src/Support/Ftp.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Support/Ftp.php b/src/Support/Ftp.php index 6bcd58a..809b89f 100644 --- a/src/Support/Ftp.php +++ b/src/Support/Ftp.php @@ -271,16 +271,27 @@ public function connect() protected function createConnection() { if ($this->ssl && @Facade::isCallable('sslConnect')) { - if (! ($this->connection = @Facade::sslConnect($this->host, $this->port, $this->timeout))) { - throw new Ftp\ServerException( - "Failed to connect to [{$this->host}] (SSL Connection)." - ); - } + return $this->createSecureConnection(); } elseif (! ($this->connection = @Facade::connect($this->host, $this->port, $this->timeout))) { throw new Ftp\ServerException("Failed to connect to [{$this->host}]."); } } + /** + * Create a secure (SSL) FTP connection. + * + * @return void + * @throws \Orchestra\Support\Ftp\ServerException + */ + protected function createSecureConnection() + { + if (!($this->connection = @Facade::sslConnect($this->host, $this->port, $this->timeout))) { + throw new Ftp\ServerException( + "Failed to connect to [{$this->host}] (SSL Connection)." + ); + } + } + /** * Close FTP connection. * From 9ed50cdabdfa976da3538a8ef5fa6ebefa70ed33 Mon Sep 17 00:00:00 2001 From: crynobone Date: Wed, 21 May 2014 01:02:23 +0800 Subject: [PATCH 2/3] Cleanup multiple code. Signed-off-by: crynobone --- src/Support/Ftp.php | 112 +++++++++++++++++--------------------------- tests/FtpTest.php | 30 +++++------- 2 files changed, 56 insertions(+), 86 deletions(-) diff --git a/src/Support/Ftp.php b/src/Support/Ftp.php index 809b89f..fc1c450 100644 --- a/src/Support/Ftp.php +++ b/src/Support/Ftp.php @@ -1,37 +1,10 @@ null, + 'port' => 21, + 'user' => null, + 'password' => null, + 'timeout' => 90, + 'passive' => false, + 'ssl' => false, + ); /** * System type of FTP server. @@ -99,20 +66,16 @@ public function __construct($config = array()) */ public function setUp($config = array()) { - $host = array_get($config, 'host'); + $this->connection = array_pull($config, 'connection', $this->connection); - if (preg_match('/^(ftp|sftp):\/\/([a-zA-Z0-9\.\-_]*):?(\d{1,4})$/', $host, $matches)) { + if (preg_match('/^(ftp|sftp):\/\/([a-zA-Z0-9\.\-_]*):?(\d{1,4})$/', array_get($config, 'host'), $matches)) { $config['host'] = $matches[2]; $config['ssl'] = ($matches[1] === 'sftp' ? true : false); isset($matches[3]) && $config['port'] = $matches[3]; } - foreach ($config as $key => $value) { - if (property_exists($this, $key)) { - $this->{$key} = $value; - } - } + $this->config = array_merge($this->config, $config); } /** @@ -242,18 +205,25 @@ public function removeDirectory($directory) */ public function connect() { - if (is_null($this->host)) { + $host = array_get($this->config, 'host'); + $port = array_get($this->config, 'port'); + $user = array_get($this->config, 'user'); + $password = array_get($this->config, 'password'); + $passive = array_get($this->config, 'passive'); + $timeout = array_get($this->config, 'timeout'); + + if (is_null($host)) { return ; } - $this->createConnection(); + $this->createConnection($host, $port, $timeout); - if (! (@Facade::login($this->connection, $this->user, $this->password))) { - throw new Ftp\ServerException("Failed FTP login to [{$this->host}]."); + if (! (@Facade::login($this->connection, $user, $password))) { + throw new ServerException("Failed FTP login to [{$host}]."); } // Set passive mode. - @Facade::pasv($this->connection, (bool) $this->passive); + @Facade::pasv($this->connection, (bool) $passive); // Set system type. $this->systemType = @Facade::systype($this->connection); @@ -264,30 +234,36 @@ public function connect() /** * Create a FTP connection. * + * @param string $host + * @param integer $port + * @param integer $timeout * @return void - * @throws \Orchestra\Support\Ftp\Exception If unable to connect to FTP - * server. + * @throws \Orchestra\Support\Ftp\ServerException If unable to connect to FTP + * server. */ - protected function createConnection() + protected function createConnection($host, $port = 21, $timeout = 90) { - if ($this->ssl && @Facade::isCallable('sslConnect')) { - return $this->createSecureConnection(); - } elseif (! ($this->connection = @Facade::connect($this->host, $this->port, $this->timeout))) { - throw new Ftp\ServerException("Failed to connect to [{$this->host}]."); + if ($this->config['ssl'] && @Facade::isCallable('sslConnect')) { + return $this->createSecureConnection($host, $port, $timeout); + } elseif (! ($this->connection = @Facade::connect($host, $port, $timeout))) { + throw new ServerException("Failed to connect to [{$host}]."); } } /** * Create a secure (SSL) FTP connection. * + * @param string $host + * @param integer $port + * @param integer $timeout * @return void * @throws \Orchestra\Support\Ftp\ServerException */ - protected function createSecureConnection() + protected function createSecureConnection($host, $port = 21, $timeout = 90) { - if (!($this->connection = @Facade::sslConnect($this->host, $this->port, $this->timeout))) { - throw new Ftp\ServerException( - "Failed to connect to [{$this->host}] (SSL Connection)." + if (! ($this->connection = @Facade::sslConnect($host, $port, $timeout))) { + throw new ServerException( + "Failed to connect to [{$host}] (SSL Connection)." ); } } diff --git a/tests/FtpTest.php b/tests/FtpTest.php index 5b66099..717f8f4 100644 --- a/tests/FtpTest.php +++ b/tests/FtpTest.php @@ -51,24 +51,18 @@ public function testInstanceOfFTP() $stub->close(); $this->assertFalse($stub->connected()); - $refl = new \ReflectionObject($this->stub); - $host = $refl->getProperty('host'); - $port = $refl->getProperty('port'); - $ssl = $refl->getProperty('ssl'); - $user = $refl->getProperty('user'); - $password = $refl->getProperty('password'); - - $host->setAccessible(true); - $port->setAccessible(true); - $ssl->setAccessible(true); - $user->setAccessible(true); - $password->setAccessible(true); - - $this->assertEquals('localhost', $host->getValue($this->stub)); - $this->assertTrue($ssl->getValue($this->stub)); - $this->assertEquals('22', $port->getValue($this->stub)); - $this->assertEquals('foo', $user->getValue($this->stub)); - $this->assertEquals('foobar', $password->getValue($this->stub)); + $refl = new \ReflectionObject($this->stub); + $config = $refl->getProperty('config'); + + $config->setAccessible(true); + + $configuration = $config->getValue($this->stub); + + $this->assertEquals('localhost', $configuration['host']); + $this->assertTrue($configuration['ssl']); + $this->assertEquals('22', $configuration['port']); + $this->assertEquals('foo', $configuration['user']); + $this->assertEquals('foobar', $configuration['password']); } /** From aec76d71ee4cd5d34a3b88fe166705b0d9686ebb Mon Sep 17 00:00:00 2001 From: crynobone Date: Wed, 21 May 2014 01:07:53 +0800 Subject: [PATCH 3/3] Another small tweaks. Signed-off-by: crynobone --- src/Support/Ftp.php | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/Support/Ftp.php b/src/Support/Ftp.php index fc1c450..23c1fcb 100644 --- a/src/Support/Ftp.php +++ b/src/Support/Ftp.php @@ -205,25 +205,20 @@ public function removeDirectory($directory) */ public function connect() { - $host = array_get($this->config, 'host'); - $port = array_get($this->config, 'port'); - $user = array_get($this->config, 'user'); - $password = array_get($this->config, 'password'); - $passive = array_get($this->config, 'passive'); - $timeout = array_get($this->config, 'timeout'); - - if (is_null($host)) { + $config = $this->config; + + if (is_null($config['host'])) { return ; } - $this->createConnection($host, $port, $timeout); + $this->createConnection($config['host'], $config['port'], $config['timeout']); - if (! (@Facade::login($this->connection, $user, $password))) { - throw new ServerException("Failed FTP login to [{$host}]."); + if (! (@Facade::login($this->connection, $config['user'], $config['password']))) { + throw new ServerException("Failed FTP login to [{$config['host']}]."); } // Set passive mode. - @Facade::pasv($this->connection, (bool) $passive); + @Facade::pasv($this->connection, (bool) $config['passive']); // Set system type. $this->systemType = @Facade::systype($this->connection);