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

View file

@ -38,8 +38,6 @@ class Share implements IShare {
*/
protected $parser;
private $serverTimezone;
/**
* @param Server $server
* @param string $name
@ -47,7 +45,7 @@ class Share implements IShare {
public function __construct($server, $name) {
$this->server = $server;
$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
*/
protected function escapePath($path) {
if ($path === '/') {
$path = '';
}
$path = str_replace('/', '\\', $path);
$path = str_replace('"', '^"', $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;
}
}