refactoring

This commit is contained in:
Milos Stojanovic
2019-06-14 12:46:45 +02:00
parent 667389ab1a
commit f6185c92ab
14 changed files with 1019 additions and 34 deletions

View File

@@ -96,7 +96,7 @@ class ZipArchiver implements Service, ArchiverInterface
foreach ($contents as $item) {
$stream = $archive->readStream($item['path']);
if ($item['type'] == 'dir') {
$storage->createDir($destination, $item['path'], $stream);
$storage->createDir($destination, $item['path']);
}
if ($item['type'] == 'file') {
$storage->store($destination.'/'.$item['dirname'], $item['basename'], $stream);

View File

@@ -29,6 +29,8 @@ class Database implements Service, AuthInterface
protected $session;
protected $conn;
public function __construct(Session $session)
{
$this->session = $session;
@@ -174,11 +176,11 @@ class Database implements Service, AuthInterface
{
$new = new User();
$new->setRole($user->role);
$new->setHomedir($user->homedir);
$new->setPermissions($user->permissions, true);
$new->setUsername($user->username);
$new->setName($user->name);
$new->setRole(isset($user->role) ? $user->role : 'guest');
$new->setHomedir(isset($user->homedir) ? $user->homedir : '/');
$new->setPermissions(isset($user->permissions) ? $user->permissions : '', true);
$new->setUsername(isset($user->username) ? $user->username : '');
$new->setName(isset($user->name) ? $user->name : 'Guest');
return $new;
}

View File

@@ -189,7 +189,7 @@ class JsonFile implements Service, AuthInterface
protected function getUsers(): array
{
$users = json_decode(file_get_contents($this->file), true);
$users = json_decode((string) file_get_contents($this->file), true);
return is_array($users) ? $users : [];
}

View File

@@ -32,7 +32,7 @@ class MonoLogger implements Service, LoggerInterface
$handler->registerFatalHandler();
}
public function log(string $message, string $level = Logger::INFO)
public function log(string $message, int $level = Logger::INFO)
{
$this->logger->log($level, $message);
}

View File

@@ -12,5 +12,5 @@ namespace Filegator\Services\Logger;
interface LoggerInterface
{
public function log(string $message, string $level);
public function log(string $message, int $level);
}

View File

@@ -33,6 +33,8 @@ class Router implements Service
protected $container;
protected $user;
public function __construct(Request $request, AuthInterface $auth, Container $container)
{
$this->request = $request;

View File

@@ -40,22 +40,22 @@ class SessionStorage implements Service, SessionStorageInterface
public function save()
{
$this->getSession()->save();
return $this->getSession() !== null ? $this->getSession()->save() : false;
}
public function set(string $key, $data)
{
return $this->getSession()->set($key, $data);
return $this->getSession() !== null ? $this->getSession()->set($key, $data) : false;
}
public function get(string $key, $default = null)
{
return $this->getSession() ? $this->getSession()->get($key, $default) : $default;
return $this->getSession() !== null ? $this->getSession()->get($key, $default) : $default;
}
public function invalidate()
{
if (! $this->getSession()->isStarted()) {
if ($this->getSession() !== null || ! $this->getSession()->isStarted()) {
$this->getSession()->start();
}

View File

@@ -54,7 +54,7 @@ class Tmpfs implements Service, TmpfsInterface
{
$filename = $this->sanitizeFilename($filename);
return file_get_contents($this->getPath().$filename);
return (string) file_get_contents($this->getPath().$filename);
}
public function readStream(string $filename): array
@@ -79,14 +79,16 @@ class Tmpfs implements Service, TmpfsInterface
public function findAll($pattern): array
{
$files = [];
foreach (glob($this->getPath().$pattern) as $filename) {
if (is_file($filename)) {
$files[] = [
'name' => basename($filename),
'size' => filesize($filename),
'time' => filemtime($filename),
];
$matches = glob($this->getPath().$pattern);
if (! empty($matches)) {
foreach ($matches as $filename) {
if (is_file($filename)) {
$files[] = [
'name' => basename($filename),
'size' => filesize($filename),
'time' => filemtime($filename),
];
}
}
}
@@ -117,7 +119,7 @@ class Tmpfs implements Service, TmpfsInterface
private function sanitizeFilename($filename)
{
$filename = preg_replace(
$filename = (string) preg_replace(
'~
[<>:"/\\|?*]| # file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
[\x00-\x1F]| # control characters http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
@@ -125,12 +127,19 @@ class Tmpfs implements Service, TmpfsInterface
[;\\\{}^\~`] # other non-safe
~x',
'-',
$filename
(string) $filename
);
// maximize filename length to 255 bytes http://serverfault.com/a/9548/44086
$ext = pathinfo($filename, PATHINFO_EXTENSION);
return mb_strcut(pathinfo($filename, PATHINFO_FILENAME), 0, 255 - ($ext ? strlen($ext) + 1 : 0), mb_detect_encoding($filename)).($ext ? '.'.$ext : '');
return mb_strcut(
pathinfo($filename, PATHINFO_FILENAME),
0,
255 - ($ext ? strlen($ext) + 1 : 0),
(string) mb_detect_encoding($filename)
).(
$ext ? '.'.$ext : ''
);
}
}