reduce code duplication

This commit is contained in:
Robin Appelman 2016-08-26 23:14:17 +02:00
commit 68aa34962f

View file

@ -60,6 +60,7 @@ 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) {
@ -80,7 +81,6 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function dir($path) { public function dir($path) {
$this->connect();
$files = array(); $files = array();
$dh = $this->state->opendir($this->buildUrl($path)); $dh = $this->state->opendir($this->buildUrl($path));
@ -104,7 +104,6 @@ class NativeShare extends AbstractShare {
} }
public function getStat($path) { public function getStat($path) {
$this->connect();
return $this->state->stat($this->buildUrl($path)); return $this->state->stat($this->buildUrl($path));
} }
@ -118,7 +117,6 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\AlreadyExistsException * @throws \Icewind\SMB\Exception\AlreadyExistsException
*/ */
public function mkdir($path) { public function mkdir($path) {
$this->connect();
return $this->state->mkdir($this->buildUrl($path)); return $this->state->mkdir($this->buildUrl($path));
} }
@ -132,7 +130,6 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function rmdir($path) { public function rmdir($path) {
$this->connect();
return $this->state->rmdir($this->buildUrl($path)); return $this->state->rmdir($this->buildUrl($path));
} }
@ -146,7 +143,6 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function del($path) { public function del($path) {
$this->connect();
return $this->state->unlink($this->buildUrl($path)); return $this->state->unlink($this->buildUrl($path));
} }
@ -161,7 +157,6 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\AlreadyExistsException * @throws \Icewind\SMB\Exception\AlreadyExistsException
*/ */
public function rename($from, $to) { public function rename($from, $to) {
$this->connect();
return $this->state->rename($this->buildUrl($from), $this->buildUrl($to)); return $this->state->rename($this->buildUrl($from), $this->buildUrl($to));
} }
@ -176,7 +171,6 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function put($source, $target) { public function put($source, $target) {
$this->connect();
$sourceHandle = fopen($source, 'rb'); $sourceHandle = fopen($source, 'rb');
$targetHandle = $this->state->create($this->buildUrl($target)); $targetHandle = $this->state->create($this->buildUrl($target));
@ -214,7 +208,6 @@ 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);
} }
$this->connect();
$sourceHandle = $this->state->open($this->buildUrl($source), 'r'); $sourceHandle = $this->state->open($this->buildUrl($source), 'r');
if (!$sourceHandle) { if (!$sourceHandle) {
fclose($targetHandle); fclose($targetHandle);
@ -238,7 +231,6 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function read($source) { public function read($source) {
$this->connect();
$url = $this->buildUrl($source); $url = $this->buildUrl($source);
$handle = $this->state->open($url, 'r'); $handle = $this->state->open($url, 'r');
return NativeStream::wrap($this->state, $handle, 'r', $url); return NativeStream::wrap($this->state, $handle, 'r', $url);
@ -254,7 +246,6 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Icewind\SMB\Exception\InvalidTypeException
*/ */
public function write($source) { public function write($source) {
$this->connect();
$url = $this->buildUrl($source); $url = $this->buildUrl($source);
$handle = $this->state->create($url); $handle = $this->state->create($url);
return NativeStream::wrap($this->state, $handle, 'w', $url); return NativeStream::wrap($this->state, $handle, 'w', $url);
@ -268,10 +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) {
$this->connect(); return $this->state->getxattr($this->buildUrl($path), $attribute);
$result = $this->state->getxattr($this->buildUrl($path), $attribute);
return $result;
} }
/** /**
@ -283,7 +271,6 @@ class NativeShare extends AbstractShare {
* @return string the attribute value * @return string the attribute value
*/ */
public function setAttribute($path, $attribute, $value) { public function setAttribute($path, $attribute, $value) {
$this->connect();
if ($attribute === 'system.dos_attr.mode' and is_int($value)) { if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
$value = '0x' . dechex($value); $value = '0x' . dechex($value);