Skip to content

Commit

Permalink
more configurability for library users
Browse files Browse the repository at this point in the history
- Make TimeZoneProvider and System overridable
- Add system for passing additional options
- make timeout configurable
  • Loading branch information
icewind1991 committed Jul 13, 2018
1 parent 2f3ff44 commit deb0610
Show file tree
Hide file tree
Showing 20 changed files with 299 additions and 107 deletions.
26 changes: 13 additions & 13 deletions src/AbstractServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class AbstractServer implements IServer {
protected $auth;

/**
* @var \Icewind\SMB\System
* @var ISystem
*/
protected $system;

Expand All @@ -45,41 +45,41 @@ abstract class AbstractServer implements IServer {
*/
protected $timezoneProvider;

/** @var IOptions */
protected $options;

/**
* @param string $host
* @param IAuth $auth
* @param System $system
* @param ISystem $system
* @param TimeZoneProvider $timeZoneProvider
* @param IOptions $options
*/
public function __construct($host, IAuth $auth, System $system, TimeZoneProvider $timeZoneProvider) {
public function __construct($host, IAuth $auth, ISystem $system, TimeZoneProvider $timeZoneProvider, IOptions $options) {
$this->host = $host;
$this->auth = $auth;
$this->system = $system;
$this->timezoneProvider = $timeZoneProvider;
$this->options = $options;
}

/**
* @return IAuth
*/
public function getAuth() {
return $this->auth;
}

/**
* return string
*/
public function getHost() {
return $this->host;
}

/**
* @return string
*/
public function getTimeZone() {
return $this->timezoneProvider->get();
return $this->timezoneProvider->get($this->host);
}

public function getSystem() {
return $this->system;
}

public function getOptions() {
return $this->options;
}
}
34 changes: 34 additions & 0 deletions src/IOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace Icewind\SMB;

interface IOptions {
/**
* @return int
*/
public function getTimeout();

/**
* @param int $timeout
*/
public function setTimeout($timeout);
}
11 changes: 8 additions & 3 deletions src/IServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ public function getShare($name);
public function getTimeZone();

/**
* @return System
* @return ISystem
*/
public function getSystem();

/**
* @param System $system
* @return IOptions
*/
public function getOptions();

/**
* @param ISystem $system
* @return bool
*/
public static function available(System $system);
public static function available(ISystem $system);
}
57 changes: 57 additions & 0 deletions src/ISystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace Icewind\SMB;

/**
* The `ISystem` interface provides a way to access system dependent information
* such as the availability and location of certain binaries.
*/
interface ISystem {
/**
* Get the path to a file descriptor of the current process
*
* @param int $num the file descriptor id
* @return string
*/
public function getFD($num);

/**
* Get the full path to the `smbclient` binary of false if the binary is not available
*
* @return string|bool
*/
public function getSmbclientPath();

/**
* Get the full path to the `net` binary of false if the binary is not available
*
* @return string|bool
*/
public function getNetPath();

/**
* Whether or not the `stdbuf` command is available in the path
*
* @return bool
*/
public function hasStdBuf();
}
32 changes: 32 additions & 0 deletions src/ITimeZoneProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace Icewind\SMB;

interface ITimeZoneProvider {
/**
* Get the timezone of the smb server
*
* @param string $host
* @return string
*/
public function get($host);
}
17 changes: 6 additions & 11 deletions src/Native/NativeServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

use Icewind\SMB\AbstractServer;
use Icewind\SMB\IAuth;
use Icewind\SMB\System;
use Icewind\SMB\IOptions;
use Icewind\SMB\ISystem;
use Icewind\SMB\TimeZoneProvider;

class NativeServer extends AbstractServer {
Expand All @@ -18,14 +19,8 @@ class NativeServer extends AbstractServer {
*/
protected $state;

/**
* @param string $host
* @param IAuth $auth
* @param System $system
* @param TimeZoneProvider $timeZoneProvider
*/
public function __construct($host, IAuth $auth, System $system, TimeZoneProvider $timeZoneProvider) {
parent::__construct($host, $auth, $system, $timeZoneProvider);
public function __construct($host, IAuth $auth, ISystem $system, TimeZoneProvider $timeZoneProvider, IOptions $options) {
parent::__construct($host, $auth, $system, $timeZoneProvider, $options);
$this->state = new NativeState();
}

Expand Down Expand Up @@ -62,10 +57,10 @@ public function getShare($name) {
/**
* Check if the smbclient php extension is available
*
* @param System $system
* @param ISystem $system
* @return bool
*/
public static function available(System $system) {
public static function available(ISystem $system) {
return function_exists('smbclient_state_new');
}
}
2 changes: 1 addition & 1 deletion src/Native/NativeShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function notify($path) {
if (!Server::available($this->server->getSystem())) {
throw new DependencyException('smbclient not found in path for notify command');
}
$share = new Share($this->server, $this->getName());
$share = new Share($this->server, $this->getName(), $this->server->getSystem());
return $share->notify($path);
}

Expand Down
36 changes: 36 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace Icewind\SMB;


class Options implements IOptions {
/** @var int */
private $timeout = 20;

public function getTimeout() {
return $this->timeout;
}

public function setTimeout($timeout) {
$this->timeout = $timeout;
}
}
44 changes: 36 additions & 8 deletions src/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,40 @@ class ServerFactory {
Server::class
];

/** @var System|null */
private $system = null;
/** @var System */
private $system;

/** @var IOptions */
private $options;

/** @var ITimeZoneProvider */
private $timeZoneProvider;

/**
* ServerFactory constructor.
*
* @param IOptions|null $options
* @param ISystem|null $system
* @param ITimeZoneProvider|null $timeZoneProvider
*/
public function __construct(
IOptions $options = null,
ISystem $system = null,
ITimeZoneProvider $timeZoneProvider = null
) {
if (is_null($options)) {
$options = new Options();
}
if (is_null($system)) {
$system = new System();
}
if (is_null($timeZoneProvider)) {
$system = new TimeZoneProvider($system);
}
$this->options = $options;
$this->system = $system;
}


/**
* @param $host
Expand All @@ -43,8 +75,8 @@ class ServerFactory {
*/
public function createServer($host, IAuth $credentials) {
foreach (self::BACKENDS as $backend) {
if (call_user_func("$backend::available", $this->getSystem())) {
return new $backend($host, $credentials, $this->getSystem(), new TimeZoneProvider($host, $this->getSystem()));
if (call_user_func("$backend::available", $this->system)) {
return new $backend($host, $credentials, $this->system, $this->timeZoneProvider);
}
}

Expand All @@ -55,10 +87,6 @@ public function createServer($host, IAuth $credentials) {
* @return System
*/
private function getSystem() {
if (is_null($this->system)) {
$this->system = new System();
}

return $this->system;
}
}
11 changes: 9 additions & 2 deletions src/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@

use Icewind\SMB\Exception\Exception;

class System {
class System implements ISystem {
private $smbclient;

private $net;

private $stdbuf;

public static function getFD($num) {
/**
* Get the path to a file descriptor of the current process
*
* @param int $num the file descriptor id
* @return string
* @throws Exception
*/
public function getFD($num) {
$folders = [
'/proc/self/fd',
'/dev/fd'
Expand Down
Loading

0 comments on commit deb0610

Please sign in to comment.