From b380b0a352050efabee6820482bd1ef397c0fbc6 Mon Sep 17 00:00:00 2001 From: Charlotte Dunois Date: Mon, 22 Oct 2018 18:14:58 +0200 Subject: [PATCH] Merge and remove unnecessary classes --- src/AbstractSyncAdapter.php | 236 -------------------------- src/ArgsExceptionTrait.php | 18 -- src/ChildProcess/Adapter.php | 191 ++++++++++++++++++++- src/ChildProcess/Process.php | 234 +++++++++++++++++++++++++- src/Eio/RuntimeException.php | 13 +- src/Eio/UnexpectedValueException.php | 13 +- src/NotSupportedException.php | 10 -- src/WoolTrait.php | 240 --------------------------- tests/ArgsExceptionTraitTest.php | 20 --- 9 files changed, 442 insertions(+), 533 deletions(-) delete mode 100644 src/AbstractSyncAdapter.php delete mode 100644 src/ArgsExceptionTrait.php delete mode 100644 src/NotSupportedException.php delete mode 100644 src/WoolTrait.php delete mode 100644 tests/ArgsExceptionTraitTest.php diff --git a/src/AbstractSyncAdapter.php b/src/AbstractSyncAdapter.php deleted file mode 100644 index e5e9df02..00000000 --- a/src/AbstractSyncAdapter.php +++ /dev/null @@ -1,236 +0,0 @@ - SCANDIR_SORT_NONE, - ]; - - /** - * @param string $path - * @return PromiseInterface - */ - public function rmdir($path) - { - return $this->invoker->invokeCall('rmdir', [ - 'path' => $path, - ]); - } - - /** - * @param string $path - * @return PromiseInterface - */ - public function unlink($path) - { - return $this->invoker->invokeCall('unlink', [ - 'path' => $path, - ]); - } - - /** - * @param string $path - * @param int $uid - * @param int $gid - * @return PromiseInterface - */ - public function chown($path, $uid, $gid) - { - return $this->invoker->invokeCall('chown', [ - 'path' => $path, - 'uid' => $uid, - 'gid' => $gid, - ]); - } - - /** - * @param string $filename - * @return PromiseInterface - */ - public function stat($filename) - { - return $this->invoker->invokeCall('stat', [ - 'path' => $filename, - ])->then(function ($stat) { - $stat['atime'] = new DateTime('@' . $stat['atime']); - $stat['mtime'] = new DateTime('@' . $stat['mtime']); - $stat['ctime'] = new DateTime('@' . $stat['ctime']); - return \React\Promise\resolve($stat); - }); - } - - /** - * @param string $path - * @return PromiseInterface - */ - public function ls($path) - { - $stream = new ObjectStream(); - - $this->invoker->invokeCall('readdir', [ - 'path' => $path, - 'flags' => $this->options['lsFlags'], - ])->then(function ($result) use ($path, $stream) { - $this->processLsContents($path, $result, $stream); - }); - - return $stream; - } - - protected function processLsContents($basePath, $result, ObjectStream $stream) - { - $promises = []; - - foreach ($result as $entry) { - $path = $basePath . DIRECTORY_SEPARATOR . $entry['name']; - $node = [ - 'path' => $path, - 'type' => $entry['type'], - ]; - $promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) { - $stream->write($node); - }); - } - - \React\Promise\all($promises)->then(function () use ($stream) { - $stream->close(); - }); - } - - /** - * @param string $path - * @param $mode - * @return PromiseInterface - */ - public function touch($path, $mode = self::CREATION_MODE) - { - return $this->invoker->invokeCall('touch', [ - 'path' => $path, - 'mode' => decoct($this->permissionFlagResolver->resolve($mode)), - ]); - } - - /** - * @param string $path - * @param string $flags - * @param $mode - * @return PromiseInterface - */ - public function open($path, $flags, $mode = self::CREATION_MODE) - { - return \React\Promise\reject(new LogicException('Not implemented')); - } - - /** - * @param $fileDescriptor - * @param int $length - * @param int $offset - * @return PromiseInterface - */ - public function read($fileDescriptor, $length, $offset) - { - return \React\Promise\reject(new LogicException('Not implemented')); - } - - /** - * @param $fileDescriptor - * @param string $data - * @param int $length - * @param int $offset - * @return PromiseInterface - */ - public function write($fileDescriptor, $data, $length, $offset) - { - return \React\Promise\reject(new LogicException('Not implemented')); - } - - /** - * @param resource $fd - * @return PromiseInterface - */ - public function close($fd) - { - return \React\Promise\reject(new LogicException('Not implemented')); - } - - /** - * @param string $fromPath - * @param string $toPath - * @return PromiseInterface - */ - public function rename($fromPath, $toPath) - { - return $this->invoker->invokeCall('rename', [ - 'from' => $fromPath, - 'to' => $toPath, - ]); - } - - /** - * @param string $path - * @return PromiseInterface - */ - public function readlink($path) - { - return $this->invoker->invokeCall('readlink', [ - 'path' => $path, - ])->then(function ($result) { - return \React\Promise\resolve($result['path']); - }); - } - - /** - * @param string $fromPath - * @param string $toPath - * @return PromiseInterface - */ - public function symlink($fromPath, $toPath) - { - return $this->invoker->invokeCall('symlink', [ - 'from' => $fromPath, - 'to' => $toPath, - ])->then(function ($result) { - return \React\Promise\resolve($result['result']); - }); - } - - /** - * @inheritDoc - */ - public function detectType($path) - { - return \React\Filesystem\detectType($this->typeDetectors, [ - 'path' => $path, - ]); - } -} diff --git a/src/ArgsExceptionTrait.php b/src/ArgsExceptionTrait.php deleted file mode 100644 index 8ad7e92c..00000000 --- a/src/ArgsExceptionTrait.php +++ /dev/null @@ -1,18 +0,0 @@ -args = $args; - } - - public function getArgs() - { - return $this->args; - } -} diff --git a/src/ChildProcess/Adapter.php b/src/ChildProcess/Adapter.php index 331eb036..bd7dd1ec 100644 --- a/src/ChildProcess/Adapter.php +++ b/src/ChildProcess/Adapter.php @@ -2,16 +2,19 @@ namespace React\Filesystem\ChildProcess; +use DateTime; use Exception; use React\EventLoop\LoopInterface; -use React\Filesystem\AbstractSyncAdapter; +use React\Filesystem\ObjectStream; use React\Filesystem\AdapterInterface; use React\Filesystem\CallInvokerInterface; use React\Filesystem\FilesystemInterface; use React\Filesystem\MappedTypeDetector; use React\Filesystem\ModeTypeDetector; use React\Filesystem\OpenFileLimiter; +use React\Filesystem\TypeDetectorInterface; use React\Filesystem\PermissionFlagResolver; +use React\Filesystem\Node\NodeInterface; use React\Filesystem\Stream\StreamFactory; use React\Promise\PromiseInterface; use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory; @@ -20,7 +23,7 @@ use WyriHaximus\React\ChildProcess\Pool\Options; use WyriHaximus\React\ChildProcess\Pool\PoolInterface; -class Adapter extends AbstractSyncAdapter implements AdapterInterface +class Adapter implements AdapterInterface { const DEFAULT_POOL = 'WyriHaximus\React\ChildProcess\Pool\Factory\Flexible'; const POOL_INTERFACE = 'WyriHaximus\React\ChildProcess\Pool\PoolFactoryInterface'; @@ -45,6 +48,33 @@ class Adapter extends AbstractSyncAdapter implements AdapterInterface * @var OpenFileLimiter */ protected $openFileLimiter; + + /** + * @var array + */ + protected $fileDescriptors = []; + + /** + * @var TypeDetectorInterface[] + */ + protected $typeDetectors = []; + + /** + * @var PermissionFlagResolver + */ + protected $permissionFlagResolver; + + /** + * @var CallInvokerInterface + */ + protected $invoker; + + /** + * @var array + */ + protected $options = [ + 'lsFlags' => SCANDIR_SORT_NONE, + ]; /** * Adapter constructor. @@ -87,7 +117,7 @@ protected function setUpPool($options) } /** - * @return boolean + * @return bool */ public static function isSupported() { @@ -233,4 +263,159 @@ public function close($fd) return $fileDescriptor->softTerminate(); }); } + + /** + * @param string $path + * @return PromiseInterface + */ + public function rmdir($path) + { + return $this->invoker->invokeCall('rmdir', [ + 'path' => $path, + ]); + } + + /** + * @param string $path + * @return PromiseInterface + */ + public function unlink($path) + { + return $this->invoker->invokeCall('unlink', [ + 'path' => $path, + ]); + } + + /** + * @param string $path + * @param int $uid + * @param int $gid + * @return PromiseInterface + */ + public function chown($path, $uid, $gid) + { + return $this->invoker->invokeCall('chown', [ + 'path' => $path, + 'uid' => $uid, + 'gid' => $gid, + ]); + } + + /** + * @param string $filename + * @return PromiseInterface + */ + public function stat($filename) + { + return $this->invoker->invokeCall('stat', [ + 'path' => $filename, + ])->then(function ($stat) { + $stat['atime'] = new DateTime('@' . $stat['atime']); + $stat['mtime'] = new DateTime('@' . $stat['mtime']); + $stat['ctime'] = new DateTime('@' . $stat['ctime']); + return \React\Promise\resolve($stat); + }); + } + + /** + * @param string $path + * @return PromiseInterface + */ + public function ls($path) + { + $stream = new ObjectStream(); + + $this->invoker->invokeCall('readdir', [ + 'path' => $path, + 'flags' => $this->options['lsFlags'], + ])->then(function ($result) use ($path, $stream) { + $this->processLsContents($path, $result, $stream); + }); + + return $stream; + } + + protected function processLsContents($basePath, $result, ObjectStream $stream) + { + $promises = []; + + foreach ($result as $entry) { + $path = $basePath . DIRECTORY_SEPARATOR . $entry['name']; + $node = [ + 'path' => $path, + 'type' => $entry['type'], + ]; + $promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) { + $stream->write($node); + }); + } + + \React\Promise\all($promises)->then(function () use ($stream) { + $stream->close(); + }); + } + + /** + * @param string $path + * @param $mode + * @return PromiseInterface + */ + public function touch($path, $mode = self::CREATION_MODE) + { + return $this->invoker->invokeCall('touch', [ + 'path' => $path, + 'mode' => decoct($this->permissionFlagResolver->resolve($mode)), + ]); + } + + /** + * @param string $fromPath + * @param string $toPath + * @return PromiseInterface + */ + public function rename($fromPath, $toPath) + { + return $this->invoker->invokeCall('rename', [ + 'from' => $fromPath, + 'to' => $toPath, + ]); + } + + /** + * @param string $path + * @return PromiseInterface + */ + public function readlink($path) + { + return $this->invoker->invokeCall('readlink', [ + 'path' => $path, + ])->then(function ($result) { + return \React\Promise\resolve($result['path']); + }); + } + + /** + * @param string $fromPath + * @param string $toPath + * @return PromiseInterface + */ + public function symlink($fromPath, $toPath) + { + return $this->invoker->invokeCall('symlink', [ + 'from' => $fromPath, + 'to' => $toPath, + ])->then(function ($result) { + return \React\Promise\resolve($result['result']); + }); + } + + /** + * @inheritDoc + */ + public function detectType($path) + { + return \React\Filesystem\detectType($this->typeDetectors, [ + 'path' => $path, + ]); + } } diff --git a/src/ChildProcess/Process.php b/src/ChildProcess/Process.php index e0137b87..c3302626 100644 --- a/src/ChildProcess/Process.php +++ b/src/ChildProcess/Process.php @@ -3,14 +3,14 @@ namespace React\Filesystem\ChildProcess; use React\EventLoop\LoopInterface; -use React\Filesystem\WoolTrait; +use React\Promise\PromiseInterface; use WyriHaximus\React\ChildProcess\Messenger\ChildInterface; use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; use WyriHaximus\React\ChildProcess\Messenger\Messenger; class Process implements ChildInterface { - use WoolTrait; + protected $fd; /** * @inheritDoc @@ -53,4 +53,234 @@ protected function wrapper($function) return $this->$function($payload->getPayload()); }; } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function mkdir(array $payload) + { + if ( + @mkdir( + $payload['path'], + octdec($payload['mode']) + ) + ) { + return \React\Promise\resolve([]); + } + + return \React\Promise\reject([ + 'error' => error_get_last(), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function rmdir(array $payload) + { + if (@rmdir($payload['path'])) { + return \React\Promise\resolve([]); + } + + return \React\Promise\reject([ + 'error' => error_get_last(), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function unlink(array $payload) + { + if (unlink($payload['path'])) { + return \React\Promise\resolve([]); + } + + return \React\Promise\reject([ + 'error' => error_get_last(), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function chmod(array $payload) + { + if (chmod($payload['path'], octdec($payload['mode']))) { + return \React\Promise\resolve([]); + } + + return \React\Promise\reject([ + 'error' => error_get_last(), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function chown(array $payload) + { + if (chown($payload['path'], $payload['uid']) && + chgrp($payload['path'], $payload['gid']) + ) { + return \React\Promise\resolve([]); + } + + return \React\Promise\reject([ + 'error' => error_get_last(), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function stat(array $payload) + { + if (!file_exists($payload['path'])) { + return \React\Promise\reject([ + 'error' => ['message' => 'Path doesn\'t exist'], + ]); + } + + $stat = lstat($payload['path']); + return \React\Promise\resolve([ + 'dev' => $stat['dev'], + 'ino' => $stat['ino'], + 'mode' => $stat['mode'], + 'nlink' => $stat['nlink'], + 'uid' => $stat['uid'], + 'size' => $stat['size'], + 'gid' => $stat['gid'], + 'rdev' => $stat['rdev'], + 'blksize' => $stat['blksize'], + 'blocks' => $stat['blocks'], + 'atime' => $stat['atime'], + 'mtime' => $stat['mtime'], + 'ctime' => $stat['ctime'], + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function readdir(array $payload) + { + $list = []; + foreach (scandir($payload['path'], $payload['flags']) as $node) { + $path = $payload['path'] . DIRECTORY_SEPARATOR . $node; + if ($node == '.' || $node == '..' || (!is_dir($path) && !is_file($path))) { + continue; + } + + $list[] = [ + 'type' => is_dir($path) ? 'dir' : 'file', + 'name' => $node, + ]; + } + return \React\Promise\resolve($list); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function open(array $payload) + { + $this->fd = @fopen($payload['path'], $payload['flags']); + return \React\Promise\resolve([ + 'result' => (string)$this->fd, + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function touch(array $payload) + { + return \React\Promise\resolve([ + touch($payload['path']) && chmod($payload['path'], octdec($payload['mode'])), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function read(array $payload) + { + fseek($this->fd, $payload['offset']); + return \React\Promise\resolve([ + 'chunk' => base64_encode(fread($this->fd, $payload['length'])), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function write(array $payload) + { + fseek($this->fd, $payload['offset']); + return \React\Promise\resolve([ + 'written' => fwrite($this->fd, base64_decode($payload['chunk']), $payload['length']), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function close(array $payload) + { + $closed = fclose($this->fd); + $this->fd = null; + return \React\Promise\resolve([ + $closed, + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function rename(array $payload) + { + if (rename($payload['from'], $payload['to'])) { + return \React\Promise\resolve([]); + } + + return \React\Promise\reject([ + 'error' => error_get_last(), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function readlink(array $payload) + { + return \React\Promise\resolve([ + 'path' => readlink($payload['path']), + ]); + } + + /** + * @param array $payload + * @return PromiseInterface + */ + public function symlink(array $payload) + { + return \React\Promise\resolve([ + 'result' => symlink($payload['from'], $payload['to']), + ]); + } } diff --git a/src/Eio/RuntimeException.php b/src/Eio/RuntimeException.php index eca53e28..7ff5a4cc 100644 --- a/src/Eio/RuntimeException.php +++ b/src/Eio/RuntimeException.php @@ -2,10 +2,19 @@ namespace React\Filesystem\Eio; -use React\Filesystem\ArgsExceptionTrait; use RuntimeException as SplRuntimeException; class RuntimeException extends SplRuntimeException { - use ArgsExceptionTrait; + protected $args = []; + + public function setArgs(array $args = []) + { + $this->args = $args; + } + + public function getArgs() + { + return $this->args; + } } diff --git a/src/Eio/UnexpectedValueException.php b/src/Eio/UnexpectedValueException.php index 637f2d0a..cfd56d0e 100644 --- a/src/Eio/UnexpectedValueException.php +++ b/src/Eio/UnexpectedValueException.php @@ -2,10 +2,19 @@ namespace React\Filesystem\Eio; -use React\Filesystem\ArgsExceptionTrait; use UnexpectedValueException as SplUnexpectedValueException; class UnexpectedValueException extends SplUnexpectedValueException { - use ArgsExceptionTrait; + protected $args = []; + + public function setArgs(array $args = []) + { + $this->args = $args; + } + + public function getArgs() + { + return $this->args; + } } diff --git a/src/NotSupportedException.php b/src/NotSupportedException.php deleted file mode 100644 index 0c5f51a2..00000000 --- a/src/NotSupportedException.php +++ /dev/null @@ -1,10 +0,0 @@ - error_get_last(), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function rmdir(array $payload) - { - if (@rmdir($payload['path'])) { - return \React\Promise\resolve([]); - } - - return \React\Promise\reject([ - 'error' => error_get_last(), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function unlink(array $payload) - { - if (unlink($payload['path'])) { - return \React\Promise\resolve([]); - } - - return \React\Promise\reject([ - 'error' => error_get_last(), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function chmod(array $payload) - { - if (chmod($payload['path'], octdec($payload['mode']))) { - return \React\Promise\resolve([]); - } - - return \React\Promise\reject([ - 'error' => error_get_last(), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function chown(array $payload) - { - if (chown($payload['path'], $payload['uid']) && - chgrp($payload['path'], $payload['gid']) - ) { - return \React\Promise\resolve([]); - } - - return \React\Promise\reject([ - 'error' => error_get_last(), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function stat(array $payload) - { - if (!file_exists($payload['path'])) { - return \React\Promise\reject([ - 'error' => ['message' => 'Path doesn\'t exist'], - ]); - } - - $stat = lstat($payload['path']); - return \React\Promise\resolve([ - 'dev' => $stat['dev'], - 'ino' => $stat['ino'], - 'mode' => $stat['mode'], - 'nlink' => $stat['nlink'], - 'uid' => $stat['uid'], - 'size' => $stat['size'], - 'gid' => $stat['gid'], - 'rdev' => $stat['rdev'], - 'blksize' => $stat['blksize'], - 'blocks' => $stat['blocks'], - 'atime' => $stat['atime'], - 'mtime' => $stat['mtime'], - 'ctime' => $stat['ctime'], - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function readdir(array $payload) - { - $list = []; - foreach (scandir($payload['path'], $payload['flags']) as $node) { - $path = $payload['path'] . DIRECTORY_SEPARATOR . $node; - if ($node == '.' || $node == '..' || (!is_dir($path) && !is_file($path))) { - continue; - } - - $list[] = [ - 'type' => is_dir($path) ? 'dir' : 'file', - 'name' => $node, - ]; - } - return \React\Promise\resolve($list); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function open(array $payload) - { - $this->fd = @fopen($payload['path'], $payload['flags']); - return \React\Promise\resolve([ - 'result' => (string)$this->fd, - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function touch(array $payload) - { - return \React\Promise\resolve([ - touch($payload['path']) && chmod($payload['path'], octdec($payload['mode'])), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function read(array $payload) - { - fseek($this->fd, $payload['offset']); - return \React\Promise\resolve([ - 'chunk' => base64_encode(fread($this->fd, $payload['length'])), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function write(array $payload) - { - fseek($this->fd, $payload['offset']); - return \React\Promise\resolve([ - 'written' => fwrite($this->fd, base64_decode($payload['chunk']), $payload['length']), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function close(array $payload) - { - $closed = fclose($this->fd); - $this->fd = null; - return \React\Promise\resolve([ - $closed, - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function rename(array $payload) - { - if (rename($payload['from'], $payload['to'])) { - return \React\Promise\resolve([]); - } - - return \React\Promise\reject([ - 'error' => error_get_last(), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function readlink(array $payload) - { - return \React\Promise\resolve([ - 'path' => readlink($payload['path']), - ]); - } - - /** - * @param array $payload - * @return PromiseInterface - */ - public function symlink(array $payload) - { - return \React\Promise\resolve([ - 'result' => symlink($payload['from'], $payload['to']), - ]); - } -} diff --git a/tests/ArgsExceptionTraitTest.php b/tests/ArgsExceptionTraitTest.php deleted file mode 100644 index f3ad5f1c..00000000 --- a/tests/ArgsExceptionTraitTest.php +++ /dev/null @@ -1,20 +0,0 @@ - 'bar', - 'wibbly wobbly' => time(), - ]; - - $mock = $this->getMockForTrait('React\Filesystem\ArgsExceptionTrait'); - - $this->assertSame([], $mock->getArgs()); - $mock->setArgs($args); - $this->assertSame($args, $mock->getArgs()); - } -} \ No newline at end of file