Skip to content
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

Flysystem v3.0 #401

Open
wants to merge 7 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
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
"role": "Developer"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.3",
"php": "^8.0",
"league/climate": "^3.0",
"league/flysystem": "^1.0",
"league/flysystem-sftp": "^1.0"
"league/flysystem": "^3.0",
"league/flysystem-ftp": "^3.0",
"league/flysystem-sftp-v3": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
Expand Down
950 changes: 396 additions & 554 deletions composer.lock

Large diffs are not rendered by default.

Binary file modified dist/phploy.phar
Binary file not shown.
38 changes: 33 additions & 5 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Banago\PHPloy;

use League\Flysystem\Adapter\Ftp as FtpAdapter;
use League\Flysystem\Ftp\FtpAdapter as FtpAdapter;
use League\Flysystem\Ftp\FtpConnectionOptions as FtpConnectionOptions;
use League\Flysystem\Filesystem;
use League\Flysystem\Sftp\SftpAdapter as SftpAdapter;
use League\Flysystem\PhpseclibV3\ConnectionProvider;
use League\Flysystem\PhpseclibV3\SftpAdapter as SftpAdapter;
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;

/**
* Class Connection.
Expand All @@ -15,6 +18,10 @@ class Connection
* @var Filesystem
*/
public $server;
/**
* @var ConnectionProvider
*/
private $provider;

/**
* Connection constructor.
Expand Down Expand Up @@ -84,9 +91,13 @@ protected function connectToFtp($server)
? (bool) $server['passive']
: true;
$options['ssl'] = ($server['ssl'] ?: false);
$options['port'] = ($server['port'] ?: 21);
$options['port'] = (intval($server['port'] ?: 21));


return new Filesystem(new FtpAdapter($options));
$ftp_options = FtpConnectionOptions::fromArray($options);


return new Filesystem(new FtpAdapter($ftp_options));
} catch (\Exception $e) {
echo "\r\nOh Snap: {$e->getMessage()}\r\n";
}
Expand Down Expand Up @@ -116,9 +127,26 @@ protected function connectToSftp($server)
$options['privateKey'] = $server['privkey'];
$options['port'] = ($server['port'] ?: 22);

return new Filesystem(new SftpAdapter($options));
$this->provider = new SftpConnectionProvider(
$options['host'],
$options['username'],
empty($options['privateKey']) ? $options['password'] : null, // password
!empty($options['privateKey']) ? $options['privateKey'] : null, // key
!empty($options['privateKey']) ? $options['password'] : null, // passphrase
$options['port']
);

return new Filesystem(new SftpAdapter($this->provider, $options['root']));
} catch (\Exception $e) {
echo "\r\nOh Snap: {$e->getMessage()}\r\n";
}
}

/**
* @return ConnectionProvider
*/
public function getConnectionProvider()
{
return $this->provider;
}
}
46 changes: 28 additions & 18 deletions src/PHPloy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

namespace Banago\PHPloy;

use League\Flysystem\UnableToCheckExistence;
use League\Flysystem\FilesystemException;
use League\Flysystem\PhpseclibV3\ConnectionProvider;

define('QUOTE', "'");
define('DQUOTE', '"');

Expand Down Expand Up @@ -171,6 +175,10 @@ class PHPloy
* @var \League\Flysystem\Filesystem;
*/
protected $connection = null;
/**
* @var ConnectionProvider
*/
protected $connectionProvider = null;

/**
* @var string
Expand Down Expand Up @@ -826,6 +834,7 @@ public function connect($server)
{
$connection = new Connection($server);
$this->connection = $connection->server;
$this->connectionProvider = $connection->getConnectionProvider();
}

/**
Expand Down Expand Up @@ -871,7 +880,7 @@ public function deploy()

$this->connect($server);

$this->connection->createDir($path);
$this->connection->createDirectory($path);
$this->cli->green('Deployment directory created. Ready to deploy.');

$this->connection = null;
Expand Down Expand Up @@ -1022,7 +1031,7 @@ public function compare($localRevision)

if ($this->fresh) {
$this->cli->out('Manual fresh upload...');
} elseif ($this->connection->has($this->dotRevision)) {
} elseif ($this->connection->fileExists($this->dotRevision)) {
$remoteRevision = $this->connection->read($this->dotRevision);
$this->debug('Remote revision: <bold>'.$remoteRevision);
} else {
Expand Down Expand Up @@ -1168,8 +1177,8 @@ public function push($files, $localRevision = null)
for ($i = 0, $count = count($dir); $i < $count; ++$i) {
$path .= $dir[$i].'/';
if (!isset($pathsThatExist[$path])) {
if (!$this->connection->has($path)) {
$this->connection->createDir($path);
if (!$this->connection->directoryExists($path)) {
$this->connection->createDirectory($path);
$this->cli->out(" + Created directory '$path'.");
$pathsThatExist[$path] = true;
} else {
Expand All @@ -1191,15 +1200,19 @@ public function push($files, $localRevision = null)
// If base is set, remove it from filename
$remoteFile = $fileBaseless;

$uploaded = $this->connection->put($remoteFile, $data);

if (!$uploaded) {
try {
$this->connection->write($remoteFile, $data);
} catch (FilesystemException | UnableToCheckExistence $e) {
$this->cli->error(" ! Failed to upload {$fileBaseless}.");

if (!$this->connection) {
$this->cli->info(' * Connection lost, trying to reconnect...');
$this->connect($this->currentServerInfo);
$uploaded = $this->connection->put($remoteFile, $data);
try {
$this->connection->write($remoteFile, $data);
} catch (FilesystemException | UnableToCheckExistence $e) {
$this->cli->error(" ! Failed to upload {$fileBaseless}.");
}
}
}

Expand All @@ -1218,7 +1231,7 @@ public function push($files, $localRevision = null)
}
$numberOfFilesToDelete = count($filesToDelete);
$fileNo = str_pad(++$fileNo, strlen($numberOfFilesToDelete), ' ', STR_PAD_LEFT);
if ($this->connection->has($file)) {
if ($this->connection->fileExists($file)) {
$this->connection->delete($file);
$this->cli->out("<red> × $fileNo of $numberOfFilesToDelete <white>{$file}");
} else {
Expand All @@ -1235,8 +1248,8 @@ public function push($files, $localRevision = null)
}
$numberOfdirsToDelete = count($dirsToDelete);
$dirNo = str_pad(++$dirNo, strlen($numberOfdirsToDelete), ' ', STR_PAD_LEFT);
if ($this->connection->has($dir)) {
$this->connection->deleteDir($dir);
if ($this->connection->directoryExists($dir)) {
$this->connection->deleteDirectory($dir);
$this->cli->out("<red> × $dirNo of $numberOfdirsToDelete <white>{$dir}");
} else {
$this->cli->out("<red> ! $dirNo of $numberOfdirsToDelete <white>{$dir} not found");
Expand Down Expand Up @@ -1284,7 +1297,7 @@ public function setRevision($localRevision = null)
$this->cli->info("Setting remote revision to: $localRevision");
}

$this->connection->put($this->dotRevision, $localRevision);
$this->connection->write($this->dotRevision, $localRevision);
}

/**
Expand Down Expand Up @@ -1429,7 +1442,7 @@ public function purge($purgeDirs)

if (count($innerDirs) > 0) {
foreach ($innerDirs as $innerDir) {
$this->connection->deleteDir($innerDir);
$this->connection->deleteDirectory($innerDir);
$this->cli->out("<red> × {$innerDir} directory");
}
}
Expand Down Expand Up @@ -1477,7 +1490,7 @@ public function copy($copyDirs)
foreach ($contents as $item) {
if ($item['type'] === 'file') {
$newPath = $toDir.'/'.pathinfo($item['path'], PATHINFO_BASENAME);
if ($this->connection->has($newPath)) {
if ($this->connection->fileExists($newPath)) {
$this->connection->delete($newPath);
}
$this->connection->copy($item['path'], $newPath);
Expand Down Expand Up @@ -1551,10 +1564,7 @@ public function postDeployRemote(array $commands)
*/
public function executeOnRemoteServer(array $commands)
{
/*
* @var \phpseclib\Net\SFTP
*/
$connection = $this->connection->getAdapter()->getConnection();
$connection = $this->connectionProvider->provideConnection();

if ($this->servers[$this->currentServerName]['scheme'] != 'sftp') {
$this->cli->yellow()->out("\r\nConnection scheme is not 'sftp' ignoring [pre/post]-deploy-remote");
Expand Down
41 changes: 41 additions & 0 deletions tests/CommitFileTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require_once 'PHPLoyTestHelper.php';
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;

class CommitFileTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -36,6 +37,28 @@ public function testSyncDeletedFileShouldSucceed($testHelper)
$this->thenRepositoryIsSynchronizedSuccessfully();
}

/**
* @dataProvider provider
*/
public function testSyncAddedDirectoryShouldSucceed($testHelper)
{
$this->testHelper = new PHPLoyTestHelper($testHelper);
$this->testHelper->givenRepositoryWithConfiguration();
$this->whenFileInSubDirIsAdded();
$this->thenRepositoryIsSynchronizedSuccessfully();
}

/**
* @dataProvider provider
*/
public function testSyncDeletedDirectoryShouldSucceed($testHelper)
{
$this->testHelper = new PHPLoyTestHelper($testHelper);
$this->givenSynchronizedRepositoryWithFileInSubDir();
$this->whenFileInSubDirIsDeleted();
$this->thenRepositoryIsSynchronizedSuccessfully();
}

/**
* @dataProvider provider
*/
Expand All @@ -53,6 +76,12 @@ protected function givenSynchronizedRepositoryWithSingleFile()
$this->whenFileIsAdded();
$this->thenRepositoryIsSynchronizedSuccessfully();
}
protected function givenSynchronizedRepositoryWithFileInSubDir()
{
$this->testHelper->givenRepositoryWithConfiguration();
$this->whenFileInSubDirIsAdded();
$this->thenRepositoryIsSynchronizedSuccessfully();
}

protected function whenFileIsAdded()
{
Expand All @@ -66,6 +95,18 @@ protected function whenFileIsDeleted()
$this->testHelper->whenRepositoryIsSynchronized();
}

protected function whenFileInSubDirIsAdded()
{
$commit = $this->testHelper->git->writeFile('testdir/testsub.txt', 'Added test directory');
$this->testHelper->whenRepositoryIsSynchronized();
}

protected function whenFileInSubDirIsDeleted()
{
$commit = $this->testHelper->git->removeFile('testdir', 'Removed test directory', true);
$this->testHelper->whenRepositoryIsSynchronized();
}

protected function whenFileIsChanged()
{
$result = $this->testHelper->git->transactional(function (TQ\Vcs\Repository\Transaction $t) {
Expand Down
3 changes: 2 additions & 1 deletion tests/ExecutionErrorTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

require_once 'PHPLoyTestHelper.php';
use TQ\Git\Repository\Repository;
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;

require_once 'PHPLoyTestHelper.php';

class ExecutionErrorTest extends PHPUnit_Framework_TestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/SharedConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;

class SharedConfigurationTest extends PHPUnit_Framework_TestCase
{
/**
Expand Down
4 changes: 2 additions & 2 deletions tests/start_test_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ mkdir -p "$WORKSPACE/ftp_share/share"
docker run \
--name sftp_testserver \
-d \
-v "$WORKSPACE/sftp_share/share":/home/sftpuser \
-v "$WORKSPACE/sftp_share":/home/sftpuser \
-p 2222:22 atmoz/sftp \
"sftpuser:password:$(id -u)"

chmod +x "$BASE_DIR/ftp_setup.sh"
docker run \
--name ftp_testserver \
-d \
-v "$WORKSPACE/ftp_share/share":/home/ftpusers/ftpuser \
-v "$WORKSPACE/ftp_share":/home/ftpusers/ftpuser \
-v "$BASE_DIR/ftp_setup.sh:/root/ftp_setup.sh" \
-p 21:21 -p 30000-30009:30000-30009 \
-e "PUBLICHOST=localhost" \
Expand Down