Skip to content

Commit deb0610

Browse files
committed
more configurability for library users
- Make TimeZoneProvider and System overridable - Add system for passing additional options - make timeout configurable
1 parent 2f3ff44 commit deb0610

20 files changed

+299
-107
lines changed

src/AbstractServer.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class AbstractServer implements IServer {
3636
protected $auth;
3737

3838
/**
39-
* @var \Icewind\SMB\System
39+
* @var ISystem
4040
*/
4141
protected $system;
4242

@@ -45,41 +45,41 @@ abstract class AbstractServer implements IServer {
4545
*/
4646
protected $timezoneProvider;
4747

48+
/** @var IOptions */
49+
protected $options;
50+
4851
/**
4952
* @param string $host
5053
* @param IAuth $auth
51-
* @param System $system
54+
* @param ISystem $system
5255
* @param TimeZoneProvider $timeZoneProvider
56+
* @param IOptions $options
5357
*/
54-
public function __construct($host, IAuth $auth, System $system, TimeZoneProvider $timeZoneProvider) {
58+
public function __construct($host, IAuth $auth, ISystem $system, TimeZoneProvider $timeZoneProvider, IOptions $options) {
5559
$this->host = $host;
5660
$this->auth = $auth;
5761
$this->system = $system;
5862
$this->timezoneProvider = $timeZoneProvider;
63+
$this->options = $options;
5964
}
6065

61-
/**
62-
* @return IAuth
63-
*/
6466
public function getAuth() {
6567
return $this->auth;
6668
}
6769

68-
/**
69-
* return string
70-
*/
7170
public function getHost() {
7271
return $this->host;
7372
}
7473

75-
/**
76-
* @return string
77-
*/
7874
public function getTimeZone() {
79-
return $this->timezoneProvider->get();
75+
return $this->timezoneProvider->get($this->host);
8076
}
8177

8278
public function getSystem() {
8379
return $this->system;
8480
}
81+
82+
public function getOptions() {
83+
return $this->options;
84+
}
8585
}

src/IOptions.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
interface IOptions {
25+
/**
26+
* @return int
27+
*/
28+
public function getTimeout();
29+
30+
/**
31+
* @param int $timeout
32+
*/
33+
public function setTimeout($timeout);
34+
}

src/IServer.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ public function getShare($name);
5252
public function getTimeZone();
5353

5454
/**
55-
* @return System
55+
* @return ISystem
5656
*/
5757
public function getSystem();
5858

5959
/**
60-
* @param System $system
60+
* @return IOptions
61+
*/
62+
public function getOptions();
63+
64+
/**
65+
* @param ISystem $system
6166
* @return bool
6267
*/
63-
public static function available(System $system);
68+
public static function available(ISystem $system);
6469
}

src/ISystem.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
/**
25+
* The `ISystem` interface provides a way to access system dependent information
26+
* such as the availability and location of certain binaries.
27+
*/
28+
interface ISystem {
29+
/**
30+
* Get the path to a file descriptor of the current process
31+
*
32+
* @param int $num the file descriptor id
33+
* @return string
34+
*/
35+
public function getFD($num);
36+
37+
/**
38+
* Get the full path to the `smbclient` binary of false if the binary is not available
39+
*
40+
* @return string|bool
41+
*/
42+
public function getSmbclientPath();
43+
44+
/**
45+
* Get the full path to the `net` binary of false if the binary is not available
46+
*
47+
* @return string|bool
48+
*/
49+
public function getNetPath();
50+
51+
/**
52+
* Whether or not the `stdbuf` command is available in the path
53+
*
54+
* @return bool
55+
*/
56+
public function hasStdBuf();
57+
}

src/ITimeZoneProvider.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
interface ITimeZoneProvider {
25+
/**
26+
* Get the timezone of the smb server
27+
*
28+
* @param string $host
29+
* @return string
30+
*/
31+
public function get($host);
32+
}

src/Native/NativeServer.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
use Icewind\SMB\AbstractServer;
1111
use Icewind\SMB\IAuth;
12-
use Icewind\SMB\System;
12+
use Icewind\SMB\IOptions;
13+
use Icewind\SMB\ISystem;
1314
use Icewind\SMB\TimeZoneProvider;
1415

1516
class NativeServer extends AbstractServer {
@@ -18,14 +19,8 @@ class NativeServer extends AbstractServer {
1819
*/
1920
protected $state;
2021

21-
/**
22-
* @param string $host
23-
* @param IAuth $auth
24-
* @param System $system
25-
* @param TimeZoneProvider $timeZoneProvider
26-
*/
27-
public function __construct($host, IAuth $auth, System $system, TimeZoneProvider $timeZoneProvider) {
28-
parent::__construct($host, $auth, $system, $timeZoneProvider);
22+
public function __construct($host, IAuth $auth, ISystem $system, TimeZoneProvider $timeZoneProvider, IOptions $options) {
23+
parent::__construct($host, $auth, $system, $timeZoneProvider, $options);
2924
$this->state = new NativeState();
3025
}
3126

@@ -62,10 +57,10 @@ public function getShare($name) {
6257
/**
6358
* Check if the smbclient php extension is available
6459
*
65-
* @param System $system
60+
* @param ISystem $system
6661
* @return bool
6762
*/
68-
public static function available(System $system) {
63+
public static function available(ISystem $system) {
6964
return function_exists('smbclient_state_new');
7065
}
7166
}

src/Native/NativeShare.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public function notify($path) {
304304
if (!Server::available($this->server->getSystem())) {
305305
throw new DependencyException('smbclient not found in path for notify command');
306306
}
307-
$share = new Share($this->server, $this->getName());
307+
$share = new Share($this->server, $this->getName(), $this->server->getSystem());
308308
return $share->notify($path);
309309
}
310310

src/Options.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
25+
class Options implements IOptions {
26+
/** @var int */
27+
private $timeout = 20;
28+
29+
public function getTimeout() {
30+
return $this->timeout;
31+
}
32+
33+
public function setTimeout($timeout) {
34+
$this->timeout = $timeout;
35+
}
36+
}

src/ServerFactory.php

+36-8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,40 @@ class ServerFactory {
3232
Server::class
3333
];
3434

35-
/** @var System|null */
36-
private $system = null;
35+
/** @var System */
36+
private $system;
37+
38+
/** @var IOptions */
39+
private $options;
40+
41+
/** @var ITimeZoneProvider */
42+
private $timeZoneProvider;
43+
44+
/**
45+
* ServerFactory constructor.
46+
*
47+
* @param IOptions|null $options
48+
* @param ISystem|null $system
49+
* @param ITimeZoneProvider|null $timeZoneProvider
50+
*/
51+
public function __construct(
52+
IOptions $options = null,
53+
ISystem $system = null,
54+
ITimeZoneProvider $timeZoneProvider = null
55+
) {
56+
if (is_null($options)) {
57+
$options = new Options();
58+
}
59+
if (is_null($system)) {
60+
$system = new System();
61+
}
62+
if (is_null($timeZoneProvider)) {
63+
$system = new TimeZoneProvider($system);
64+
}
65+
$this->options = $options;
66+
$this->system = $system;
67+
}
68+
3769

3870
/**
3971
* @param $host
@@ -43,8 +75,8 @@ class ServerFactory {
4375
*/
4476
public function createServer($host, IAuth $credentials) {
4577
foreach (self::BACKENDS as $backend) {
46-
if (call_user_func("$backend::available", $this->getSystem())) {
47-
return new $backend($host, $credentials, $this->getSystem(), new TimeZoneProvider($host, $this->getSystem()));
78+
if (call_user_func("$backend::available", $this->system)) {
79+
return new $backend($host, $credentials, $this->system, $this->timeZoneProvider);
4880
}
4981
}
5082

@@ -55,10 +87,6 @@ public function createServer($host, IAuth $credentials) {
5587
* @return System
5688
*/
5789
private function getSystem() {
58-
if (is_null($this->system)) {
59-
$this->system = new System();
60-
}
61-
6290
return $this->system;
6391
}
6492
}

src/System.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@
99

1010
use Icewind\SMB\Exception\Exception;
1111

12-
class System {
12+
class System implements ISystem {
1313
private $smbclient;
1414

1515
private $net;
1616

1717
private $stdbuf;
1818

19-
public static function getFD($num) {
19+
/**
20+
* Get the path to a file descriptor of the current process
21+
*
22+
* @param int $num the file descriptor id
23+
* @return string
24+
* @throws Exception
25+
*/
26+
public function getFD($num) {
2027
$folders = [
2128
'/proc/self/fd',
2229
'/dev/fd'

0 commit comments

Comments
 (0)