Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
127 changes: 0 additions & 127 deletions Entity/Profile.php

This file was deleted.

17 changes: 16 additions & 1 deletion Exception/ConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,22 @@
/**
* SSH2 connection exception
*/
class ConnectionException extends Exception
class ConnectionException extends \Exception
{
/**
* Constructor
*
* @param string $message
* @param Profile $profile
*/
public function __construct($message, Profile $profile = null)
{
if ($profile instanceof Profile) {
$message = '[' . (string)$profile . '] ' . $message;
} else {
$message = '[No profile specified] ' . $message;
}

parent::__construct($message);
}
}
28 changes: 0 additions & 28 deletions Exception/Exception.php

This file was deleted.

23 changes: 23 additions & 0 deletions Factory/ConnectionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace steevanb\SSH2Bundle\Factory;

use steevanb\SSH2Bundle\Model\Profile;
use steevanb\SSH2Bundle\Model\Connection;

/**
* Service for SSH2 connections
*/
class ConnectionFactory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern Factory is a good one, but when there is just 2 lines of code, not sure it's usefull to create a service just for it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did the same, I just move the Service\Connection.php class to Factory\ConnectionFactory.php

{
/**
* @param array $args
* @param bool $autoConnect
* @return Connection
*/
public function connect(array $args, $autoConnect = true)
{
$profile = new Profile($args);
return new Connection($profile, $autoConnect);
}
}
43 changes: 35 additions & 8 deletions Entity/Connection.php → Model/Connection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace steevanb\SSH2Bundle\Entity;
namespace steevanb\SSH2Bundle\Model;

use steevanb\SSH2Bundle\Exception\ConnectionException;

Expand Down Expand Up @@ -55,7 +55,10 @@ public function connect()
if ($this->connection === false) {
$this->state = self::STATE_INVALID_ADDRESS;
} else {
$auth = @ssh2_auth_password($this->connection, $profile->getLogin(), $profile->getPassword());
$auth = @ssh2_auth_pubkey_file($this->connection, $profile->getLogin(), $profile->getRsaPubKey(), $profile->getRsaPemKey(), $profile->getPassphrase());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do it only when a RsaPubKey is specified, instead of try to connect with an empty Rsa, then connect without Rsa ?

if ($auth === false) {
$auth = @ssh2_auth_password($this->connection, $profile->getLogin(), $profile->getPassword());
}
if ($auth === false) {
$this->state = self::STATE_INVALID_LOGIN;
} else {
Expand Down Expand Up @@ -119,20 +122,40 @@ public function getProfile()
return $this->profile;
}

public function getConnection()
{
return $this->connection;
}

/**
* Execute a command
*
* @param string $command
* @return string
* @param $command
* @return bool|string
* @throws ConnectionException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't add this PHPDoc, always wrong :)

*
*/
public function exec($command)
{
$this->assertConnected();

$stream = ssh2_exec($this->connection, $command);
stream_set_blocking($stream, true);
$streamOut = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
return stream_get_contents($streamOut);
if( !($stream = ssh2_exec($this->connection, $command)) ) {
throw new ConnectionException(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok to throw an exception if command fail, but not a ConnectionException. Maybe use deleted Exception, or create CommandException

sprintf('%s command failed through ssh', $command),
$this->profile
);
}
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($errorStream, TRUE);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true in lowercase

stream_set_blocking($stream, TRUE);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true in lowercase

$output = stream_get_contents($stream);
$error_output = stream_get_contents($errorStream);
fclose($stream);
fclose($errorStream);
if (!empty($error_output)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i prefer empty() === false

throw new ConnectionException($error_output, $this->profile);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok to throw an exception if command fail, but not a ConnectionException. Maybe use deleted Exception, or create CommandException

}
return $output;
}

/**
Expand All @@ -141,6 +164,7 @@ public function exec($command)
* @param string $command
* @param string $separator
* @return array
* @throws ConnectionException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't add this PHPDoc

*/
public function execExplode($command, $separator = ' ')
{
Expand All @@ -155,6 +179,7 @@ public function execExplode($command, $separator = ' ')
* @param string $separator Separator
* @param mixed $default Default value if $index is not found
* @return string
* @throws ConnectionException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't add this PHPDoc

*/
public function execExplodeIndex($command, $index, $separator = ' ', $default = null)
{
Expand All @@ -167,6 +192,7 @@ public function execExplodeIndex($command, $index, $separator = ' ', $default =
*
* @param type $command
* @return array
* @throws ConnectionException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't add this PHPDoc

*/
public function execLines($command)
{
Expand All @@ -182,6 +208,7 @@ public function execLines($command)
* @param int $index Index of the line to return
* @param string $default Default value
* @return string
* @throws ConnectionException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't add this PHPDoc

*/
public function execLine($command, $index = 0, $default = null)
{
Expand Down
Loading