Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit ace45d2

Browse files
authored
Merge pull request #9 from brammittendorff/feature/added-ftp-to-flysystem-factory
Added FTP to flysystem factory
2 parents 5a62464 + 1287450 commit ace45d2

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ sudo: required
44
env:
55
global:
66
- TEST_S3_LOCATION='//TRAVISACCESSPHPUNIT:wJalrXUtnFEMI%2FSECRET%2FTRAVISPHPUNIT@us-east-1/?endpoint=http%3A%2F%2F127.0.0.1%3A9999&use_path_style_endpoint=1'
7+
- TEST_FTP_LOCATION='ftp://test:test@localhost'
78

89
services:
910
- docker
@@ -40,6 +41,7 @@ install:
4041
-e 'MINIO_ACCESS_KEY=TRAVISACCESSPHPUNIT' \
4142
-e 'MINIO_SECRET_KEY=wJalrXUtnFEMI/K7MDENG/TRAVISKEYPHPUNIT' \
4243
--detach minio/minio server /data"
44+
- "docker run -d -p 21:21 -p 30000-30009:30000-30009 -e 'PUBLICHOST=localhost' jack12816/ftpd_test"
4345

4446
script:
4547
- composer validate

src/Adapter/Ftp.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace MJRider\FlysystemFactory\Adapter;
4+
5+
use League\Flysystem\Filesystem;
6+
use League\Flysystem\Adapter\Ftp as Adapter;
7+
8+
/**
9+
* Static factory class for creating an ftp connection
10+
*/
11+
class Ftp implements AdapterFactoryInterface
12+
{
13+
/**
14+
* Builds the arguments for the FTP adapter
15+
*
16+
* @param [string] $url
17+
* @return void
18+
*/
19+
protected static function buildArgs($url)
20+
{
21+
$args = [
22+
'host' => $url->host,
23+
'username' => urldecode($url->user),
24+
'password' => urldecode($url->pass),
25+
'port' => $url->port,
26+
'root' => $url->path,
27+
];
28+
29+
if (isset($url->query->passive)) {
30+
$args[ 'passive' ] = (bool)$url->query->passive;
31+
}
32+
33+
if (isset($url->query->ssl)) {
34+
$args[ 'ssl' ] = (bool)$url->query->ssl;
35+
}
36+
37+
if (isset($url->query->timeout)) {
38+
$args[ 'timeout' ] = (int)$url->query->timeout;
39+
}
40+
41+
return $args;
42+
}
43+
44+
/**
45+
* Creates the adapter for FTP
46+
*
47+
* @param [string] $url
48+
* @return void
49+
*/
50+
public static function create($url)
51+
{
52+
$args = static::buildArgs($url);
53+
54+
$adapter = new Adapter($args);
55+
56+
return $adapter;
57+
}
58+
}

src/functions.php

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ function create($endpoint)
3030
case 'b2':
3131
$adapter = Adapter\B2::create($url);
3232
break;
33+
case 'ftp':
34+
$adapter = Adapter\Ftp::create($url);
35+
break;
3336
case 'file':
3437
case 'local':
3538
$adapter = Adapter\Local::create($url);

tests/Adapter/FtpTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace MJRider\FlysystemFactory\Adapter;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* @requires PHP 5.4
9+
*/
10+
class FtpTest extends TestCase
11+
{
12+
protected $root = '';
13+
14+
public function setup()
15+
{
16+
$this->root = getenv('TEST_FTP_LOCATION');
17+
if ($this->root === false) {
18+
$this->markTestSkipped('no ftp endpoint available, test skipped');
19+
}
20+
if (defined('HHVM_VERSION')) {
21+
$this->markTestSkipped('HHVM ftp not supported ');
22+
}
23+
}
24+
25+
public function testFtp()
26+
{
27+
$filesystem = \MJRider\FlysystemFactory\create($this->root);
28+
$this->assertInstanceOf('\League\Flysystem\Filesystem', $filesystem);
29+
$this->assertInstanceOf('\League\Flysystem\Adapter\Ftp', $filesystem->getAdapter());
30+
}
31+
}

0 commit comments

Comments
 (0)