Skip to content

Commit

Permalink
Added more filesystems related examples (#1273)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Dec 14, 2024
1 parent 685391c commit b4fe644
Show file tree
Hide file tree
Showing 19 changed files with 106 additions and 16 deletions.
1 change: 0 additions & 1 deletion examples/topics/cloud_storage/priority.txt

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions examples/topics/filesystem/azure/priority.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
18 changes: 18 additions & 0 deletions examples/topics/filesystem/local/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use function Flow\Filesystem\DSL\{fstab, path, protocol};

require __DIR__ . '/../../../autoload.php';

$filesystem = fstab()->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();
46 changes: 46 additions & 0 deletions examples/topics/filesystem/local/description.md
Original file line number Diff line number Diff line change
@@ -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`

6 changes: 6 additions & 0 deletions examples/topics/filesystem/local/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Files List

File: code.php
File: description.md
File: output.txt
File: priority.txt
1 change: 1 addition & 0 deletions examples/topics/filesystem/local/priority.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions examples/topics/filesystem/priority.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
5 changes: 5 additions & 0 deletions examples/topics/filesystem/s3/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

// Coming soon!
1 change: 1 addition & 0 deletions examples/topics/filesystem/s3/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
S3 or any S3 like filesystem is coming soon!
1 change: 1 addition & 0 deletions examples/topics/filesystem/s3/priority.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
17 changes: 17 additions & 0 deletions examples/topics/filesystem/stdout/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use function Flow\Filesystem\DSL\{fstab, path, protocol};

require __DIR__ . '/../../../autoload.php';

$outputStream = fstab()->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();
7 changes: 7 additions & 0 deletions examples/topics/filesystem/stdout/description.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions examples/topics/filesystem/stdout/priority.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
2 changes: 1 addition & 1 deletion examples/topics/join/priority.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4
7
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 0 additions & 4 deletions src/lib/filesystem/src/Flow/Filesystem/Stream/VoidStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,4 @@ public function size() : int
{
return 0;
}

public function write(string $data) : void
{
}
}

0 comments on commit b4fe644

Please sign in to comment.