Split off connection code to a seperate class

This commit is contained in:
Robin Appelman 2013-03-03 22:20:01 +01:00
commit 37fbf43f7d
3 changed files with 81 additions and 44 deletions

71
src/connection.php Normal file
View file

@ -0,0 +1,71 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB;
class Connection {
/**
* @var resource[] $pipes
*
* $pipes[0] holds STDIN for smbclient
* $pipes[1] holds STDOUT for smbclient
*/
private $pipes;
/**
* @var resource $process
*/
private $process;
public function __construct($command) {
$descriptorSpec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
);
putenv('LC_ALL=' . Server::LOCALE);
setlocale(LC_ALL, Server::LOCALE);
$this->process = proc_open($command, $descriptorSpec, $this->pipes, null, array(
'CLI_FORCE_INTERACTIVE' => 'y' // Needed or the prompt isn't displayed!!
));
if (!is_resource($this->process)) {
throw new ConnectionError();
}
}
/**
* send input to smbclient
*
* @param string $input
*/
public function write($input) {
fwrite($this->pipes[0], $input);
fwrite($this->pipes[0], PHP_EOL); //make sure we have a recognizable delimiter
fflush($this->pipes[0]);
}
/**
* get all unprocessed output from smbclient
*
* @return array
*/
public function read() {
fgets($this->pipes[1]); //first line is prompt
$output = array();
$line = fgets($this->pipes[1]);
while (substr($line, 0, 4) !== 'smb:') { //next prompt functions as delimiter
$output[] .= $line;
$line = fgets($this->pipes[1]);
}
return $output;
}
public function __destruct() {
proc_close($this->process);
}
}

View file

@ -1,6 +1,6 @@
<?php <?php
/** /**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com> * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or * This file is licensed under the Affero General Public License version 3 or
* later. * later.
* See the COPYING-README file. * See the COPYING-README file.
@ -14,24 +14,16 @@ class Share {
*/ */
private $server; private $server;
/**
* @var resource $process
*/
private $process;
/**
* @var resource[] $pipes
*
* $pipes[0] holds STDIN for smbclient
* $pipes[1] holds STDOUT for smbclient
*/
private $pipes;
/** /**
* @var string $name * @var string $name
*/ */
private $name; private $name;
/**
* @var Connection $connection
*/
private $connection;
/** /**
* @param Server $server * @param Server $server
* @param string $share * @param string $share
@ -40,26 +32,9 @@ class Share {
$this->server = $server; $this->server = $server;
$this->name = $name; $this->name = $name;
$descriptorSpec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
// 2 => array("file", "/tmp/smberror", "a")
);
putenv('LC_ALL=' . Server::LOCALE);
setlocale(LC_ALL, Server::LOCALE);
$command = Server::CLIENT . ' -N -U ' . $this->server->getAuthString() . $command = Server::CLIENT . ' -N -U ' . $this->server->getAuthString() .
' //' . $this->server->getHost() . '/' . $this->name; ' //' . $this->server->getHost() . '/' . $this->name;
$this->process = proc_open($command, $descriptorSpec, $this->pipes, null, array( $this->connection = new Connection($command);
'CLI_FORCE_INTERACTIVE' => 'y' // Needed or the prompt isn't displayed!!
));
if (!is_resource($this->process)) {
throw new ConnectionError();
}
}
public function __destruct() {
proc_close($this->process);
} }
/** /**
@ -141,9 +116,7 @@ class Share {
* @param string $input * @param string $input
*/ */
public function write($input) { public function write($input) {
fwrite($this->pipes[0], $input); $this->connection->write($input);
fwrite($this->pipes[0], PHP_EOL); //make sure we have a recognizable delimiter
fflush($this->pipes[0]);
} }
/** /**
@ -152,14 +125,7 @@ class Share {
* @return array * @return array
*/ */
public function read() { public function read() {
fgets($this->pipes[1]); //first line is prompt return $this->connection->read();
$output = array();
$line = fgets($this->pipes[1]);
while (substr($line, 0, 4) !== 'smb:') { //next prompt functions as delimiter
$output[] .= $line;
$line = fgets($this->pipes[1]);
}
return $output;
} }
/** /**

View file

@ -24,7 +24,7 @@ class Test extends PHPUnit_Framework_TestCase {
} }
public function tearDown() { public function tearDown() {
// $this->share->rmdir($this->root); $this->share->rmdir($this->root);
} }
public function testDirectory() { public function testDirectory() {