|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace Icewind\SMB; |
|
23
|
|
|
|
|
24
|
|
|
use Icewind\SMB\Exception\DependencyException; |
|
25
|
|
|
use Icewind\SMB\Native\NativeServer; |
|
26
|
|
|
use Icewind\SMB\Wrapped\Server; |
|
27
|
|
|
|
|
28
|
|
|
class ServerFactory { |
|
29
|
|
|
const BACKENDS = [ |
|
30
|
|
|
NativeServer::class, |
|
31
|
|
|
Server::class |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** @var ISystem */ |
|
35
|
|
|
private $system; |
|
36
|
|
|
|
|
37
|
|
|
/** @var IOptions */ |
|
38
|
|
|
private $options; |
|
39
|
|
|
|
|
40
|
|
|
/** @var ITimeZoneProvider */ |
|
41
|
|
|
private $timeZoneProvider; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* ServerFactory constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param IOptions|null $options |
|
47
|
|
|
* @param ISystem|null $system |
|
48
|
|
|
* @param ITimeZoneProvider|null $timeZoneProvider |
|
49
|
|
|
*/ |
|
50
|
6 |
|
public function __construct( |
|
51
|
|
|
IOptions $options = null, |
|
52
|
|
|
ISystem $system = null, |
|
53
|
|
|
ITimeZoneProvider $timeZoneProvider = null |
|
54
|
|
|
) { |
|
55
|
6 |
|
if (is_null($options)) { |
|
56
|
6 |
|
$options = new Options(); |
|
57
|
|
|
} |
|
58
|
6 |
|
if (is_null($system)) { |
|
59
|
2 |
|
$system = new System(); |
|
60
|
|
|
} |
|
61
|
6 |
|
if (is_null($timeZoneProvider)) { |
|
62
|
6 |
|
$timeZoneProvider = new TimeZoneProvider($system); |
|
63
|
|
|
} |
|
64
|
6 |
|
$this->options = $options; |
|
65
|
6 |
|
$this->system = $system; |
|
66
|
6 |
|
$this->timeZoneProvider = $timeZoneProvider; |
|
67
|
6 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $host |
|
72
|
|
|
* @param IAuth $credentials |
|
73
|
|
|
* @return IServer |
|
74
|
|
|
* @throws DependencyException |
|
75
|
|
|
*/ |
|
76
|
6 |
|
public function createServer(string $host, IAuth $credentials): IServer { |
|
77
|
6 |
|
foreach (self::BACKENDS as $backend) { |
|
78
|
6 |
|
if (call_user_func("$backend::available", $this->system)) { |
|
79
|
4 |
|
return new $backend($host, $credentials, $this->system, $this->timeZoneProvider, $this->options); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
throw new DependencyException('No valid backend available, ensure smbclient is in the path or php-smbclient is installed'); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|