Skip to content

Commit

Permalink
Adjusted namings, documentation and tests (#3)
Browse files Browse the repository at this point in the history
* Adjusted namings, documentation and tests

* Adjusted dependencies and travis ci

* Adjusted travis ci

* Updated README
  • Loading branch information
nowiko authored Dec 20, 2020
1 parent 3743072 commit 754fda9
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 153 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor/*
composer.lock
phpunit.xml
Tests/cache/*
var/
17 changes: 7 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
language: php
php:
- 5.4
- 5.5
- 5.6

matrix:
include:
- php: 5.3
dist: precise
php:
- 7.0
- 7.1
- 7.2

before_script:
- sudo apt-get update
- sudo apt-get install libssh2-1-dev libssh2-php
- sudo apt-get install libssh2-1-dev php-ssh2
- echo '' | pecl install -f ssh2
- echo "extension=ssh2.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
- php -m | grep ssh2
- "travis_wait 30 sleep 1800 &"
- composer self-update
- composer install

script:
- vendor/bin/phpunit
- ./vendor/bin/phpunit
23 changes: 14 additions & 9 deletions Command/FetchFromCommand.php → Command/FetchCommand.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
<?php

namespace SF2Helpers\SFTPBundle\Command;
namespace NW\SFTPBundle\Command;

use SF2Helpers\SFTPBundle\SFTP\SFTP;
use NW\SFTPBundle\SFTP\SFTP;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class FetchFromCommand extends ContainerAwareCommand
/**
* Class FetchCommand
* @package NW\SFTPBundle\Command
* @author Novikov Viktor
*/
class FetchCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('sf2h:sftp:fetchFrom')
->setName('nw:sftp:fetch')
->setDefinition(array(
new InputArgument('remoteFile', InputArgument::REQUIRED, 'Full path to remote file'),
new InputArgument('localFile', InputArgument::REQUIRED, 'Full path to local file')
))
->setDescription('Copy file from remote SFTP server to local machine')
->setDescription('Copy file from remote SFTP server to the local machine')
->setHelp("
The <info>./app/console sftp:copy</info> command copies file from remote SFTP server to your local machine by specified path:
The <info>./app/console nw:sftp:fetch</info> command copies file from remote SFTP server to your local machine by the specified path:
Command example:
<info>./app/console sftp:copy /path/to/remoteFile.txt /path/to/localFile.txt</info>
<info>./app/console nw:sftp:fetch /path/to/remoteFile.txt /path/to/localFile.txt</info>
");
}

Expand All @@ -35,8 +40,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$sftp = $this->get('sftp');

try {
$output->writeln('Started fetching file from remote SFTP server.');
$sftp->fetchFrom($input->getArgument('remoteFile'), $input->getArgument('localFile'));
$output->writeln('Started fetching a file from a remote SFTP server.');
$sftp->fetch($input->getArgument('remoteFile'), $input->getArgument('localFile'));
$output->writeln('Successful file transfer from the SFTP server.');
} catch (\Exception $e) {
$output->writeln('File transfer failed.');
Expand Down
25 changes: 15 additions & 10 deletions Command/SendToCommand.php → Command/SendCommand.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
<?php

namespace SF2Helpers\SFTPBundle\Command;
namespace NW\SFTPBundle\Command;

use SF2Helpers\SFTPBundle\SFTP\SFTP;
use NW\SFTPBundle\SFTP\SFTP;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SendToCommand extends ContainerAwareCommand
/**
* Class SendCommand
* @package NW\SFTPBundle\Command
* @author Novikov Viktor
*/
class SendCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('sf2h:sftp:sendTo')
->setName('nw:sftp:send')
->setDefinition(array(
new InputArgument('localFile', InputArgument::REQUIRED, 'Full path to local file'),
new InputArgument('remoteFile', InputArgument::REQUIRED, 'Full path to remote file')
))
->setDescription('Send file to remote SFTP server from local machine')
->setDescription('Send a file to remote SFTP server from local machine')
->setHelp("
The <info>./app/console sftp:send</info> command copies file to remote SFTP server from your local machine by specified path:
The <info>./app/console nw:sftp:send</info> command copies file to remote SFTP server from your local machine by the specified path:
Command example:
<info>./app/console sftp:send /path/to/localFile.txt /path/to/remoteFile.txt</info>
<info>./app/console nw:sftp:send /path/to/localFile.txt /path/to/remoteFile.txt</info>
");
}

Expand All @@ -35,9 +40,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$sftp = $this->get('sftp');

try {
$output->writeln('Started sending file to remote SFTP server.');
$sftp->sendTo($input->getArgument('localFile'), $input->getArgument('remoteFile'));
$output->writeln('Successful sending file to the SFTP server.');
$output->writeln('Started sending a file to a remote SFTP server.');
$sftp->send($input->getArgument('localFile'), $input->getArgument('remoteFile'));
$output->writeln('Successful file transfer to the SFTP server.');
} catch (\Exception $e) {
$output->writeln('File transfer failed.');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?php

namespace SF2Helpers\SFTPBundle\DependencyInjection;
namespace NW\SFTPBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class SF2HelpersSFTPExtension extends Extension
/**
* Class NWSFTPExtension
* @package NW\SFTPBundle\DependencyInjection
* @author Novikov Viktor
*/
class NWSFTPExtension extends Extension
{
/**
* {@inheritDoc}
Expand Down
14 changes: 14 additions & 0 deletions NWSFTPBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace NW\SFTPBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Class NWSFTPBundle
* @package NW\SFTPBundle
* @author Novikov Viktor
*/
class NWSFTPBundle extends Bundle
{
}
105 changes: 42 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,55 @@
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/e0b26b60-76f3-40a4-9416-9b6c65fb93a2/mini.png)](https://insight.sensiolabs.com/projects/e0b26b60-76f3-40a4-9416-9b6c65fb93a2) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/NovikovViktor/SFTPBundle/badges/quality-score.png?b=develop)](https://scrutinizer-ci.com/g/NovikovViktor/SFTPBundle/?branch=develop) [![Build Status](https://travis-ci.org/NovikovViktor/SFTPBundle.svg?branch=develop)](https://travis-ci.org/NovikovViktor/SFTPBundle) [![Maintainability](https://api.codeclimate.com/v1/badges/72c8963111d07dacf0c6/maintainability)](https://codeclimate.com/github/NovikovViktor/SFTPBundle/maintainability)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/e0b26b60-76f3-40a4-9416-9b6c65fb93a2/mini.png)](https://insight.sensiolabs.com/projects/e0b26b60-76f3-40a4-9416-9b6c65fb93a2) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/NovikovViktor/SFTPBundle/badges/quality-score.png?b=develop)](https://scrutinizer-ci.com/g/NovikovViktor/SFTPBundle/?branch=develop) [![Build Status](https://travis-ci.org/nowiko/SFTPBundle.svg?branch=master)](https://travis-ci.org/nowiko/SFTPBundle) [![Maintainability](https://api.codeclimate.com/v1/badges/72c8963111d07dacf0c6/maintainability)](https://codeclimate.com/github/NovikovViktor/SFTPBundle/maintainability)

Symfony2 Helpers - SFTP bundle
SFTP Bundle
=====================

This bundle provide simple interface for transfer files by [SFTP](https://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol) protocol.
This bundle provides a simple interface for transfer files by SFTP protocol.

Installation
==============

1) Install libssh2-php library:
1) Install the bundle using Composer:
```bash
composer require nw/sftp-bundle
```

2) Enable bundle in `AppKernel.php`

```php
class AppKernel extends Kernel
{
public function registerBundles()
{
return array(
// ... other bundles
new NW\SFTPBundle\NWSFTPBundle()
);
}
}
```

```bash
// Ubuntu
sudo apt-get update
sudo apt-get install libssh2-1-dev libssh2-php

//MacOS
sudo port install libssh2
sudo pecl install channel://pecl.php.net/ssh2-0.12
// when you will be asked about lib prefix put /opt/local , so terminal will look like
libssh2 prefix? [autodetect] : /opt/local

// enable extension in php.ini
extension = ssh2.so

//Restart server
sudo service apache2 restart
```

2) Install bundle using Composer:

```bash
composer require sf2h/sftp-bundle
```

3) Enable bundle in `AppKernel.php`

```php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ... some other bundles
new SF2Helpers\SFTPBundle\SF2HelpersSFTPBundle()
}
// ... other code
}
```
Usage
=======

1) Connect to server via `sf2h.sftp` service:
```php
$sftp = $this->get('sf2h.sftp');
$sftp->connect($host, $port);
$sftp->login($username, $password);
// or
$sftp->loginWithKey($host, $username, $pubkeyfile, $privkeyfile, $passphrase = null);
```
2) Use sftp service to transfer files over SFTP:
```php
$sftp->fetchFrom('/path/to/remoteFile', '/path/to/localFile');
// or
$sftp->sendTo('/path/to/localFile', '/path/to/remoteFile');
```
1) Connect to the SFTP server:
```php
$sftp = $this->get('nw.sftp');
$sftp->connect($host, $port);
$sftp->login($username, $password);
// or
$sftp->loginWithKey($host, $username, $pubkeyfile, $privkeyfile, $passphrase = null);
```

2) Use SFTP client to transfer files:
```php
$sftp->fetch('/path/to/remoteFile', '/path/to/localFile');
// or
$sftp->send('/path/to/localFile', '/path/to/remoteFile');
```

3) From CLI could be used one of the following commands:
- `php app(bin)/console sf2h:sftp:fetchFrom /path/to/remoteFile /path/to/localFile` - to copy files from remote server to local machine
- `php app(bin)/console sf2h:sftp:sendTo /path/to/localFile /path/to/remoteFile` - to copy files from local server to remote machine
```bash
app/console nw:sftp:fetch /path/to/remoteFile /path/to/localFile # - copy files from a remote server to the local machine
# or
app/console nw:sftp:send /path/to/localFile /path/to/remoteFile # - copy files from a local machine to the remote server
```
4 changes: 2 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="sf2h.sftp.class">SF2Helpers\SFTPBundle\SFTP\SFTP</parameter>
<parameter key="nw.sftp.class">NW\SFTPBundle\SFTP\SFTP</parameter>
</parameters>

<services>
<service id="sf2h.sftp" class="%sf2h.sftp.class%" />
<service id="nw.sftp" class="%nw.sftp.class%" public="true"/>
</services>

</container>
9 changes: 0 additions & 9 deletions SF2HelpersSFTPBundle.php

This file was deleted.

7 changes: 6 additions & 1 deletion SFTP/ConnectionInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

namespace SF2Helpers\SFTPBundle\SFTP;
namespace NW\SFTPBundle\SFTP;

/**
* Interface ConnectionInterface
* @package NW\SFTPBundle\SFTP
* @author Novikov Viktor
*/
interface ConnectionInterface
{
/**
Expand Down
13 changes: 9 additions & 4 deletions SFTP/ResourceTransferInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

namespace SF2Helpers\SFTPBundle\SFTP;
namespace NW\SFTPBundle\SFTP;

/**
* Interface ResourceTransferInterface
* @package NW\SFTPBundle\SFTP
* @author Novikov Viktor
*/
interface ResourceTransferInterface
{
/**
Expand All @@ -11,7 +16,7 @@ interface ResourceTransferInterface
* @param $localFile
* @return mixed
*/
public function fetchFrom($remoteFile, $localFile);
public function fetch($remoteFile, $localFile);

/**
* Copy file from local machine to remote
Expand All @@ -20,13 +25,13 @@ public function fetchFrom($remoteFile, $localFile);
* @param $remoteFile
* @return mixed
*/
public function sendTo($localFile, $remoteFile);
public function send($localFile, $remoteFile);

/**
* Return array of files names from remote dir
*
* @param $remoteDir
* @return array
*/
public function getRemoteFilesList($remoteDir);
public function getFilesList($remoteDir);
}
Loading

0 comments on commit 754fda9

Please sign in to comment.