Skip to content

Commit 9d8f8e5

Browse files
committed
Added SftpFilesystem
1 parent 9b7c4c0 commit 9d8f8e5

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

src/SftpFilesystem.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* @link https://github.com/creocoder/yii2-flysystem
4+
* @copyright Copyright (c) 2015 Alexander Kochetov
5+
* @license http://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
namespace creocoder\flysystem;
9+
10+
use League\Flysystem\Sftp\SftpAdapter;
11+
use Yii;
12+
use yii\base\InvalidConfigException;
13+
14+
/**
15+
* SftpFilesystem
16+
*
17+
* @author Alexander Kochetov <[email protected]>
18+
*/
19+
class SftpFilesystem extends Filesystem
20+
{
21+
/**
22+
* @var string
23+
*/
24+
public $host;
25+
/**
26+
* @var string
27+
*/
28+
public $port;
29+
/**
30+
* @var string
31+
*/
32+
public $username;
33+
/**
34+
* @var string
35+
*/
36+
public $password;
37+
/**
38+
* @var integer
39+
*/
40+
public $timeout;
41+
/**
42+
* @var string
43+
*/
44+
public $root;
45+
/**
46+
* @var string
47+
*/
48+
public $privateKey;
49+
/**
50+
* @var integer
51+
*/
52+
public $permPrivate;
53+
/**
54+
* @var integer
55+
*/
56+
public $permPublic;
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function init()
62+
{
63+
if ($this->host === null) {
64+
throw new InvalidConfigException('The "host" property must be set.');
65+
}
66+
67+
if ($this->username === null) {
68+
throw new InvalidConfigException('The "username" property must be set.');
69+
}
70+
71+
if ($this->password === null || $this->privateKey === null) {
72+
throw new InvalidConfigException('Either "password" or "privateKey" property must be set.');
73+
}
74+
75+
if ($this->root !== null) {
76+
$this->root = Yii::getAlias($this->root);
77+
}
78+
79+
parent::init();
80+
}
81+
82+
/**
83+
* @return SftpAdapter
84+
*/
85+
protected function prepareAdapter()
86+
{
87+
$config = [];
88+
89+
foreach ([
90+
'host',
91+
'port',
92+
'username',
93+
'password',
94+
'timeout',
95+
'root',
96+
'privateKey',
97+
'permPrivate',
98+
'permPublic',
99+
] as $name) {
100+
if ($this->$name !== null) {
101+
$config[$name] = $this->$name;
102+
}
103+
}
104+
105+
return new SftpAdapter($config);
106+
}
107+
}

0 commit comments

Comments
 (0)