Wait with getting the timezone from the server until we need it

This commit is contained in:
Robin Appelman 2015-03-13 13:12:00 +01:00
commit 476980d4ce
5 changed files with 72 additions and 15 deletions

View file

@ -17,15 +17,15 @@ use Icewind\SMB\Exception\NotFoundException;
class Parser { class Parser {
/** /**
* @var string * @var \Icewind\SMB\TimeZoneProvider
*/ */
protected $timeZone; protected $timeZoneProvider;
/** /**
* @param string $timeZone * @param \Icewind\SMB\TimeZoneProvider $timeZoneProvider
*/ */
public function __construct($timeZone) { public function __construct(TimeZoneProvider $timeZoneProvider) {
$this->timeZone = $timeZone; $this->timeZoneProvider = $timeZoneProvider;
} }
public function checkForError($output, $path) { public function checkForError($output, $path) {
@ -120,7 +120,7 @@ class Parser {
list(, $name, $mode, $size, $time) = $matches; list(, $name, $mode, $size, $time) = $matches;
if ($name !== '.' and $name !== '..') { if ($name !== '.' and $name !== '..') {
$mode = $this->parseMode($mode); $mode = $this->parseMode($mode);
$time = strtotime($time . ' ' . $this->timeZone); $time = strtotime($time . ' ' . $this->timeZoneProvider->get());
$content[] = new FileInfo($basePath . '/' . $name, $name, $size, $time, $mode); $content[] = new FileInfo($basePath . '/' . $name, $name, $size, $time, $mode);
} }
} }

View file

@ -38,8 +38,6 @@ class Share implements IShare {
*/ */
protected $parser; protected $parser;
private $serverTimezone;
/** /**
* @param Server $server * @param Server $server
* @param string $name * @param string $name
@ -47,7 +45,7 @@ class Share implements IShare {
public function __construct($server, $name) { public function __construct($server, $name) {
$this->server = $server; $this->server = $server;
$this->name = $name; $this->name = $name;
$this->parser = new Parser($this->server->getTimeZone()); $this->parser = new Parser(new TimeZoneProvider($this->server->getHost()));
} }
/** /**
@ -374,6 +372,9 @@ class Share implements IShare {
* @return string * @return string
*/ */
protected function escapePath($path) { protected function escapePath($path) {
if ($path === '/') {
$path = '';
}
$path = str_replace('/', '\\', $path); $path = str_replace('/', '\\', $path);
$path = str_replace('"', '^"', $path); $path = str_replace('"', '^"', $path);
return '"' . $path . '"'; return '"' . $path . '"';

35
src/TimeZoneProvider.php Normal file
View file

@ -0,0 +1,35 @@
<?php
/**
* Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB;
class TimeZoneProvider {
/**
* @var string
*/
private $host;
/**
* @var string
*/
private $timeZone;
/**
* @param string $host
*/
function __construct($host) {
$this->host = $host;
}
public function get() {
if (!$this->timeZone) {
$command = 'net time zone -S ' . escapeshellarg($this->host);
$this->timeZone = exec($command);
}
return $this->timeZone;
}
}

View file

@ -331,7 +331,7 @@ abstract class AbstractShare extends \PHPUnit_Framework_TestCase {
$this->share->put($this->getTextFile(), $this->root . '/foo.txt'); $this->share->put($this->getTextFile(), $this->root . '/foo.txt');
$dir = $this->share->dir($this->root); $dir = $this->share->dir($this->root);
$mtime = $dir[0]->getMTime(); $mtime = $dir[0]->getMTime();
$this->assertTrue(abs($now - $mtime) <= 1, 'Modified time differs by ' . abs($now - $mtime) . ' seconds'); $this->assertTrue(abs($now - $mtime) <= 2, 'Modified time differs by ' . abs($now - $mtime) . ' seconds');
$this->share->del($this->root . '/foo.txt'); $this->share->del($this->root . '/foo.txt');
} }
@ -531,4 +531,9 @@ abstract class AbstractShare extends \PHPUnit_Framework_TestCase {
$this->assertCount(1, $content); $this->assertCount(1, $content);
$this->assertEquals($name, $content[0]->getName()); $this->assertEquals($name, $content[0]->getName());
} }
public function testStatRoot() {
$info = $this->share->stat('/');
$this->assertInstanceOf('\Icewind\SMB\IFileInfo', $info);
}
} }

View file

@ -24,11 +24,25 @@ class Parser extends \PHPUnit_Framework_TestCase {
); );
} }
/**
* @param string $timeZone
* @return \Icewind\SMB\TimeZoneProvider
*/
private function getTimeZoneProvider($timeZone) {
$mock = $this->getMockBuilder('\Icewind\SMB\TimeZoneProvider')
->disableOriginalConstructor()
->getMock();
$mock->expects($this->any())
->method('get')
->will($this->returnValue($timeZone));
return $mock;
}
/** /**
* @dataProvider modeProvider * @dataProvider modeProvider
*/ */
public function testParseMode($string, $mode) { public function testParseMode($string, $mode) {
$parser = new \Icewind\SMB\Parser('UTC'); $parser = new \Icewind\SMB\Parser($this->getTimeZoneProvider('UTC'));
$this->assertEquals($mode, $parser->parseMode($string), 'Failed parsing ' . $string); $this->assertEquals($mode, $parser->parseMode($string), 'Failed parsing ' . $string);
} }
@ -48,7 +62,8 @@ class Parser extends \PHPUnit_Framework_TestCase {
'mtime' => strtotime('12 Oct 2013 19:05:58 CEST'), 'mtime' => strtotime('12 Oct 2013 19:05:58 CEST'),
'mode' => FileInfo::MODE_NORMAL, 'mode' => FileInfo::MODE_NORMAL,
'size' => 29634 'size' => 29634
)) )
)
); );
} }
@ -56,7 +71,7 @@ class Parser extends \PHPUnit_Framework_TestCase {
* @dataProvider statProvider * @dataProvider statProvider
*/ */
public function testStat($output, $stat) { public function testStat($output, $stat) {
$parser = new \Icewind\SMB\Parser('UTC'); $parser = new \Icewind\SMB\Parser($this->getTimeZoneProvider('UTC'));
$this->assertEquals($stat, $parser->parseStat($output)); $this->assertEquals($stat, $parser->parseStat($output));
} }
@ -71,7 +86,8 @@ class Parser extends \PHPUnit_Framework_TestCase {
' 62536 blocks of size 8388608. 57113 blocks available' ' 62536 blocks of size 8388608. 57113 blocks available'
), ),
array( array(
new FileInfo('/c.pdf', 'c.pdf', 29634, strtotime('12 Oct 2013 19:05:58 CEST'), FileInfo::MODE_NORMAL) new FileInfo('/c.pdf', 'c.pdf', 29634, strtotime('12 Oct 2013 19:05:58 CEST'),
FileInfo::MODE_NORMAL)
) )
) )
); );
@ -81,7 +97,7 @@ class Parser extends \PHPUnit_Framework_TestCase {
* @dataProvider dirProvider * @dataProvider dirProvider
*/ */
public function testDir($output, $dir) { public function testDir($output, $dir) {
$parser = new \Icewind\SMB\Parser('CEST'); $parser = new \Icewind\SMB\Parser($this->getTimeZoneProvider('CEST'));
$this->assertEquals($dir, $parser->parseDir($output, '')); $this->assertEquals($dir, $parser->parseDir($output, ''));
} }
} }