Skip to content

Commit

Permalink
Reduced indention
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Dec 16, 2022
1 parent d7085d0 commit c6187f8
Showing 1 changed file with 65 additions and 48 deletions.
113 changes: 65 additions & 48 deletions src/Eio/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,67 +30,84 @@ public function stat(): PromiseInterface
public function getContents(int $offset = 0 , ?int $maxlen = null): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve, callable $reject) use ($offset, $maxlen): void {
\eio_open(
$this->path . DIRECTORY_SEPARATOR . $this->name,
\EIO_O_RDONLY,
0,
\EIO_PRI_DEFAULT,
function ($_, $fileDescriptor) use ($resolve, $reject, $offset, $maxlen): void {
try {
\eio_fstat($fileDescriptor, \EIO_PRI_DEFAULT, function ($fileDescriptor, $stat) use ($resolve, $reject, $offset, $maxlen): void {
try {
\eio_read($fileDescriptor, $maxlen ?? (int)$stat['size'], $offset, \EIO_PRI_DEFAULT, function ($fileDescriptor, string $buffer) use ($resolve): void {
\eio_close($fileDescriptor, \EIO_PRI_DEFAULT, function () use ($resolve, $buffer) {
$this->deactivate();
$resolve($buffer);
});
}, $fileDescriptor);
} catch (\Throwable $error) {
$this->deactivate();
$reject($error);
}
return $this->openFile(
$this->path . DIRECTORY_SEPARATOR . $this->name,
\EIO_O_RDONLY,
0,
)->then(
function ($fileDescriptor) use ($offset, $maxlen): PromiseInterface {
return $this->statFileDescriptor($fileDescriptor)->then(function ($stat) use ($fileDescriptor, $offset, $maxlen): PromiseInterface {
return new Promise (function (callable $resolve) use ($fileDescriptor, $offset, $maxlen, $stat): void {
\eio_read($fileDescriptor, $maxlen ?? (int)$stat['size'], $offset, \EIO_PRI_DEFAULT, function ($fileDescriptor, string $buffer) use ($resolve): void {
$resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($buffer): string {
return $buffer;
}));
}, $fileDescriptor);
} catch (\Throwable $error) {
$this->deactivate();
$reject($error);
}
}
);
});
});
});
}
);
}

public function putContents(string $contents, int $flags = 0)
{
$this->activate();
return new Promise(function (callable $resolve, callable $reject) use ($contents, $flags): void {
\eio_open(
return $this->openFile(
$this->path . DIRECTORY_SEPARATOR . $this->name,
(($flags & \FILE_APPEND) == \FILE_APPEND) ? \EIO_O_RDWR | \EIO_O_APPEND : \EIO_O_RDWR | \EIO_O_CREAT,
0644,
0644
)->then(
function ($fileDescriptor) use ($contents, $flags): PromiseInterface {
return new Promise (function (callable $resolve) use ($contents, $fileDescriptor): void {
\eio_write($fileDescriptor, $contents, strlen($contents), 0, \EIO_PRI_DEFAULT, function ($fileDescriptor, int $bytesWritten) use ($resolve): void {
$resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($bytesWritten): int {
return $bytesWritten;
}));
}, $fileDescriptor);
});
}
);
}

private function statFileDescriptor($fileDescriptor): PromiseInterface
{
return new Promise(function (callable $resolve, callable $reject) use ($fileDescriptor) {
\eio_fstat($fileDescriptor, \EIO_PRI_DEFAULT, function ($_, $stat) use ($resolve): void {
$resolve($stat);
}, $fileDescriptor);
});
}

private function openFile(string $path, int $flags, int $mode): PromiseInterface
{
return new Promise(function (callable $resolve, callable $reject) use ($path, $flags, $mode): void {
\eio_open(
$path,
$flags,
$mode,
\EIO_PRI_DEFAULT,
function ($_, $fileDescriptor) use ($resolve, $reject, $contents, $flags): void {
try {
\eio_write($fileDescriptor, $contents, strlen($contents), 0, \EIO_PRI_DEFAULT, function ($fileDescriptor, int $bytesWritten) use ($resolve, $reject): void {
try {
\eio_close($fileDescriptor, \EIO_PRI_DEFAULT, function () use ($resolve, $bytesWritten): void {
$this->deactivate();
$resolve($bytesWritten);
});
} catch (\Throwable $error) {
$this->deactivate();
$reject($error);
}
}, $fileDescriptor);
} catch (\Throwable $error) {
$this->deactivate();
$reject($error);
}
function ($_, $fileDescriptor) use ($resolve): void {
$resolve($fileDescriptor);
}
);
});
}

private function closeOpenFile($fileDescriptor): PromiseInterface
{
return new Promise(function (callable $resolve) use ($fileDescriptor) {
try {
\eio_close($fileDescriptor, \EIO_PRI_DEFAULT, function () use ($resolve): void {
$this->deactivate();
$resolve();
});
} catch (\Throwable $error) {
$this->deactivate();
throw $error;
}
});
}

public function unlink(): PromiseInterface
{
$this->activate();
Expand Down Expand Up @@ -121,4 +138,4 @@ protected function deactivate(): void
{
$this->poll->deactivate();
}
}
}

0 comments on commit c6187f8

Please sign in to comment.