Skip to content

Commit b4fe644

Browse files
authored
Added more filesystems related examples (#1273)
1 parent 685391c commit b4fe644

File tree

19 files changed

+106
-16
lines changed

19 files changed

+106
-16
lines changed

examples/topics/cloud_storage/priority.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use function Flow\Filesystem\DSL\{fstab, path, protocol};
6+
7+
require __DIR__ . '/../../../autoload.php';
8+
9+
$filesystem = fstab()->for(protocol('file'));
10+
$outputStream = $filesystem->writeTo(path(__DIR__ . '/output.txt'));
11+
12+
$outputStream->append("Files List\n\n");
13+
14+
foreach ($filesystem->list(path(__DIR__ . '/*')) as $file) {
15+
$outputStream->append(($file->isFile() ? 'File' : 'Directory') . ': ' . $file->path->basename() . "\n");
16+
}
17+
18+
$outputStream->close();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Flow is based on a dedicated file system library that provides a clear
2+
separation between reading/writing streams.
3+
4+
Filesystem component is available as a standalone package;
5+
6+
```
7+
composer require flow-php/filesystem
8+
```
9+
10+
It was inspired by a linux FStab, and similarly to it to start
11+
using a filesystem it needs to be first mounted.
12+
By default `fstab()` function registers two default filesystems:
13+
14+
- local native filesystem
15+
- stdout write-only filesystem
16+
17+
All filesystems supports listing files and opening streams
18+
into which you can read or write data.
19+
20+
## Listing Files
21+
---
22+
23+
To list files use `Filesystem::list(Path $path)` method
24+
which also supports `glob` pattern.
25+
26+
List method returns and generator of `FileStatus` objects.
27+
28+
Alternatively to check if a single file exists, use `Filesystem::status(Path $path) : ?FileStatus`
29+
30+
## Reading Data
31+
---
32+
33+
Reading data from a filesystem is done by opening a stream
34+
and deciding if you want to read it at once or in chunks.
35+
36+
Additionally, you can read a part of a file from a specific offset.
37+
38+
## Writing Data
39+
---
40+
41+
Writing data to a filesystem is done by opening a stream throught
42+
one of two methods:
43+
44+
- `appendTo(Path $path) : DestinationStream`
45+
- `writeTo(Path $path) : DestinationStream`
46+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Files List
2+
3+
File: code.php
4+
File: description.md
5+
File: output.txt
6+
File: priority.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
// Coming soon!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
S3 or any S3 like filesystem is coming soon!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use function Flow\Filesystem\DSL\{fstab, path, protocol};
6+
7+
require __DIR__ . '/../../../autoload.php';
8+
9+
$outputStream = fstab()->for(protocol('stdout'))->writeTo(path('stdout://'));
10+
11+
$outputStream->append("Files List\n\n");
12+
13+
foreach (fstab()->for(protocol('file'))->list(path(__DIR__ . '/*')) as $file) {
14+
$outputStream->append(($file->isFile() ? 'File' : 'Directory') . ': ' . $file->path->basename() . "\n");
15+
}
16+
17+
$outputStream->close();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Stdout is a special type of filesystem allowing to
2+
write straight to stdout of the process.
3+
4+
`Stdout is a write-only filesystem. It is not possible to read from it.`
5+
6+
Its main purpose is to allow to allow web servers to stream
7+
data to the client without buffering it in memory.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5

examples/topics/join/priority.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4
1+
7

src/lib/filesystem/src/Flow/Filesystem/Stream/NativeLocalDestinationStream.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,4 @@ public function path() : Path
112112
{
113113
return $this->path;
114114
}
115-
116-
public function write(string $data) : void
117-
{
118-
if (!$this->isOpen()) {
119-
throw new RuntimeException('Cannot write to closed stream');
120-
}
121-
122-
\fseek($this->handle, 0);
123-
\fwrite($this->handle, $data);
124-
}
125115
}

src/lib/filesystem/src/Flow/Filesystem/Stream/VoidStream.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,4 @@ public function size() : int
6767
{
6868
return 0;
6969
}
70-
71-
public function write(string $data) : void
72-
{
73-
}
7470
}

0 commit comments

Comments
 (0)