ensure we only initialize the state once

This commit is contained in:
Robin Appelman 2018-01-18 15:44:45 +01:00
commit 1f650a5fbf

View file

@ -34,7 +34,6 @@ class NativeShare extends AbstractShare {
parent::__construct(); parent::__construct();
$this->server = $server; $this->server = $server;
$this->name = $name; $this->name = $name;
$this->state = new NativeState();
} }
/** /**
@ -42,12 +41,14 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\AuthenticationException * @throws \Icewind\SMB\Exception\AuthenticationException
* @throws \Icewind\SMB\Exception\InvalidHostException * @throws \Icewind\SMB\Exception\InvalidHostException
*/ */
protected function connect() { protected function getState() {
if ($this->state and $this->state instanceof NativeShare) { if ($this->state and $this->state instanceof NativeState) {
return; return $this->state;
} }
$this->state = new NativeState();
$this->state->init($this->server->getWorkgroup(), $this->server->getUser(), $this->server->getPassword()); $this->state->init($this->server->getWorkgroup(), $this->server->getUser(), $this->server->getPassword());
return $this->state;
} }
/** /**
@ -60,7 +61,6 @@ class NativeShare extends AbstractShare {
} }
private function buildUrl($path) { private function buildUrl($path) {
$this->connect();
$this->verifyPath($path); $this->verifyPath($path);
$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name); $url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
if ($path) { if ($path) {
@ -83,15 +83,15 @@ class NativeShare extends AbstractShare {
public function dir($path) { public function dir($path) {
$files = array(); $files = array();
$dh = $this->state->opendir($this->buildUrl($path)); $dh = $this->getState()->opendir($this->buildUrl($path));
while ($file = $this->state->readdir($dh)) { while ($file = $this->getState()->readdir($dh)) {
$name = $file['name']; $name = $file['name'];
if ($name !== '.' and $name !== '..') { if ($name !== '.' and $name !== '..') {
$files [] = new NativeFileInfo($this, $path . '/' . $name, $name); $files [] = new NativeFileInfo($this, $path . '/' . $name, $name);
} }
} }
$this->state->closedir($dh); $this->getState()->closedir($dh);
return $files; return $files;
} }
@ -104,7 +104,7 @@ class NativeShare extends AbstractShare {
} }
public function getStat($path) { public function getStat($path) {
return $this->state->stat($this->buildUrl($path)); return $this->getState()->stat($this->buildUrl($path));
} }
/** /**
@ -117,7 +117,7 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\AlreadyExistsException * @throws \Icewind\SMB\Exception\AlreadyExistsException
*/ */
public function mkdir($path) { public function mkdir($path) {
return $this->state->mkdir($this->buildUrl($path)); return $this->getState()->mkdir($this->buildUrl($path));
} }
/** /**
@ -130,7 +130,7 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function rmdir($path) { public function rmdir($path) {
return $this->state->rmdir($this->buildUrl($path)); return $this->getState()->rmdir($this->buildUrl($path));
} }
/** /**
@ -143,7 +143,7 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function del($path) { public function del($path) {
return $this->state->unlink($this->buildUrl($path)); return $this->getState()->unlink($this->buildUrl($path));
} }
/** /**
@ -157,7 +157,7 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\AlreadyExistsException * @throws \Icewind\SMB\Exception\AlreadyExistsException
*/ */
public function rename($from, $to) { public function rename($from, $to) {
return $this->state->rename($this->buildUrl($from), $this->buildUrl($to)); return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
} }
/** /**
@ -172,12 +172,12 @@ class NativeShare extends AbstractShare {
*/ */
public function put($source, $target) { public function put($source, $target) {
$sourceHandle = fopen($source, 'rb'); $sourceHandle = fopen($source, 'rb');
$targetHandle = $this->state->create($this->buildUrl($target)); $targetHandle = $this->getState()->create($this->buildUrl($target));
while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) { while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
$this->state->write($targetHandle, $data); $this->getState()->write($targetHandle, $data);
} }
$this->state->close($targetHandle); $this->getState()->close($targetHandle);
return true; return true;
} }
@ -208,16 +208,16 @@ class NativeShare extends AbstractShare {
throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason); throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
} }
$sourceHandle = $this->state->open($this->buildUrl($source), 'r'); $sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
if (!$sourceHandle) { if (!$sourceHandle) {
fclose($targetHandle); fclose($targetHandle);
throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading'); throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
} }
while ($data = $this->state->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) { while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
fwrite($targetHandle, $data); fwrite($targetHandle, $data);
} }
$this->state->close($sourceHandle); $this->getState()->close($sourceHandle);
return true; return true;
} }
@ -232,8 +232,8 @@ class NativeShare extends AbstractShare {
*/ */
public function read($source) { public function read($source) {
$url = $this->buildUrl($source); $url = $this->buildUrl($source);
$handle = $this->state->open($url, 'r'); $handle = $this->getState()->open($url, 'r');
return NativeReadStream::wrap($this->state, $handle, 'r', $url); return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
} }
/** /**
@ -247,8 +247,8 @@ class NativeShare extends AbstractShare {
*/ */
public function write($source) { public function write($source) {
$url = $this->buildUrl($source); $url = $this->buildUrl($source);
$handle = $this->state->create($url); $handle = $this->getState()->create($url);
return NativeWriteStream::wrap($this->state, $handle, 'w', $url); return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
} }
/** /**
@ -259,7 +259,7 @@ class NativeShare extends AbstractShare {
* @return string the attribute value * @return string the attribute value
*/ */
public function getAttribute($path, $attribute) { public function getAttribute($path, $attribute) {
return $this->state->getxattr($this->buildUrl($path), $attribute); return $this->getState()->getxattr($this->buildUrl($path), $attribute);
} }
/** /**
@ -276,7 +276,7 @@ class NativeShare extends AbstractShare {
$value = '0x' . dechex($value); $value = '0x' . dechex($value);
} }
return $this->state->setxattr($this->buildUrl($path), $attribute, $value); return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
} }
/** /**