diff --git a/examples/topics/cloud_storage/priority.txt b/examples/topics/cloud_storage/priority.txt deleted file mode 100644 index c7930257d..000000000 --- a/examples/topics/cloud_storage/priority.txt +++ /dev/null @@ -1 +0,0 @@ -7 \ No newline at end of file diff --git a/examples/topics/cloud_storage/.gitignore b/examples/topics/filesystem/.gitignore similarity index 100% rename from examples/topics/cloud_storage/.gitignore rename to examples/topics/filesystem/.gitignore diff --git a/examples/topics/cloud_storage/azure/.env.dist b/examples/topics/filesystem/azure/.env.dist similarity index 100% rename from examples/topics/cloud_storage/azure/.env.dist rename to examples/topics/filesystem/azure/.env.dist diff --git a/examples/topics/cloud_storage/azure/code.php b/examples/topics/filesystem/azure/code.php similarity index 100% rename from examples/topics/cloud_storage/azure/code.php rename to examples/topics/filesystem/azure/code.php diff --git a/examples/topics/filesystem/azure/priority.txt b/examples/topics/filesystem/azure/priority.txt new file mode 100644 index 000000000..d8263ee98 --- /dev/null +++ b/examples/topics/filesystem/azure/priority.txt @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/examples/topics/filesystem/local/code.php b/examples/topics/filesystem/local/code.php new file mode 100644 index 000000000..ae856c730 --- /dev/null +++ b/examples/topics/filesystem/local/code.php @@ -0,0 +1,18 @@ +for(protocol('file')); +$outputStream = $filesystem->writeTo(path(__DIR__ . '/output.txt')); + +$outputStream->append("Files List\n\n"); + +foreach ($filesystem->list(path(__DIR__ . '/*')) as $file) { + $outputStream->append(($file->isFile() ? 'File' : 'Directory') . ': ' . $file->path->basename() . "\n"); +} + +$outputStream->close(); diff --git a/examples/topics/filesystem/local/description.md b/examples/topics/filesystem/local/description.md new file mode 100644 index 000000000..31a359b92 --- /dev/null +++ b/examples/topics/filesystem/local/description.md @@ -0,0 +1,46 @@ +Flow is based on a dedicated file system library that provides a clear +separation between reading/writing streams. + +Filesystem component is available as a standalone package; + +``` +composer require flow-php/filesystem +``` + +It was inspired by a linux FStab, and similarly to it to start +using a filesystem it needs to be first mounted. +By default `fstab()` function registers two default filesystems: + +- local native filesystem +- stdout write-only filesystem + +All filesystems supports listing files and opening streams +into which you can read or write data. + +## Listing Files +--- + +To list files use `Filesystem::list(Path $path)` method +which also supports `glob` pattern. + +List method returns and generator of `FileStatus` objects. + +Alternatively to check if a single file exists, use `Filesystem::status(Path $path) : ?FileStatus` + +## Reading Data +--- + +Reading data from a filesystem is done by opening a stream +and deciding if you want to read it at once or in chunks. + +Additionally, you can read a part of a file from a specific offset. + +## Writing Data +--- + +Writing data to a filesystem is done by opening a stream throught +one of two methods: + +- `appendTo(Path $path) : DestinationStream` +- `writeTo(Path $path) : DestinationStream` + diff --git a/examples/topics/filesystem/local/output.txt b/examples/topics/filesystem/local/output.txt new file mode 100644 index 000000000..079915bdd --- /dev/null +++ b/examples/topics/filesystem/local/output.txt @@ -0,0 +1,6 @@ +Files List + +File: code.php +File: description.md +File: output.txt +File: priority.txt diff --git a/examples/topics/filesystem/local/priority.txt b/examples/topics/filesystem/local/priority.txt new file mode 100644 index 000000000..56a6051ca --- /dev/null +++ b/examples/topics/filesystem/local/priority.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/examples/topics/filesystem/priority.txt b/examples/topics/filesystem/priority.txt new file mode 100644 index 000000000..bf0d87ab1 --- /dev/null +++ b/examples/topics/filesystem/priority.txt @@ -0,0 +1 @@ +4 \ No newline at end of file diff --git a/examples/topics/filesystem/s3/code.php b/examples/topics/filesystem/s3/code.php new file mode 100644 index 000000000..ba011f8ef --- /dev/null +++ b/examples/topics/filesystem/s3/code.php @@ -0,0 +1,5 @@ +for(protocol('stdout'))->writeTo(path('stdout://')); + +$outputStream->append("Files List\n\n"); + +foreach (fstab()->for(protocol('file'))->list(path(__DIR__ . '/*')) as $file) { + $outputStream->append(($file->isFile() ? 'File' : 'Directory') . ': ' . $file->path->basename() . "\n"); +} + +$outputStream->close(); diff --git a/examples/topics/filesystem/stdout/description.md b/examples/topics/filesystem/stdout/description.md new file mode 100644 index 000000000..23a8e9248 --- /dev/null +++ b/examples/topics/filesystem/stdout/description.md @@ -0,0 +1,7 @@ +Stdout is a special type of filesystem allowing to +write straight to stdout of the process. + +`Stdout is a write-only filesystem. It is not possible to read from it.` + +Its main purpose is to allow to allow web servers to stream +data to the client without buffering it in memory. \ No newline at end of file diff --git a/examples/topics/filesystem/stdout/priority.txt b/examples/topics/filesystem/stdout/priority.txt new file mode 100644 index 000000000..7813681f5 --- /dev/null +++ b/examples/topics/filesystem/stdout/priority.txt @@ -0,0 +1 @@ +5 \ No newline at end of file diff --git a/examples/topics/join/priority.txt b/examples/topics/join/priority.txt index bf0d87ab1..c7930257d 100644 --- a/examples/topics/join/priority.txt +++ b/examples/topics/join/priority.txt @@ -1 +1 @@ -4 \ No newline at end of file +7 \ No newline at end of file diff --git a/src/lib/filesystem/src/Flow/Filesystem/Stream/NativeLocalDestinationStream.php b/src/lib/filesystem/src/Flow/Filesystem/Stream/NativeLocalDestinationStream.php index 05bb5dce1..64c3460ac 100644 --- a/src/lib/filesystem/src/Flow/Filesystem/Stream/NativeLocalDestinationStream.php +++ b/src/lib/filesystem/src/Flow/Filesystem/Stream/NativeLocalDestinationStream.php @@ -112,14 +112,4 @@ public function path() : Path { return $this->path; } - - public function write(string $data) : void - { - if (!$this->isOpen()) { - throw new RuntimeException('Cannot write to closed stream'); - } - - \fseek($this->handle, 0); - \fwrite($this->handle, $data); - } } diff --git a/src/lib/filesystem/src/Flow/Filesystem/Stream/VoidStream.php b/src/lib/filesystem/src/Flow/Filesystem/Stream/VoidStream.php index 1f68cca11..63ffb814f 100644 --- a/src/lib/filesystem/src/Flow/Filesystem/Stream/VoidStream.php +++ b/src/lib/filesystem/src/Flow/Filesystem/Stream/VoidStream.php @@ -67,8 +67,4 @@ public function size() : int { return 0; } - - public function write(string $data) : void - { - } }