Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Added

- `Innmind\IO\Files\Temporary::close()`

## 3.1.0 - 2025-04-21

### Added
Expand Down
6 changes: 6 additions & 0 deletions documentation/files/temporary.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ You can then use it in 2 ways:
```

The call to `->chunk()` is stateful as it remembers the position in the file. This means that you can only read forward, and only once.

Once you're down working with the file you can close it like this:

```php
$temporary->close()->unwrap();
```
40 changes: 40 additions & 0 deletions proofs/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Innmind\IO\{
IO,
Files\Read,
Files\Temporary,
Files\Temporary\Pull,
};
use Innmind\Url\Path;
Expand Down Expand Up @@ -350,4 +351,43 @@ static function($assert, $chunks, $size, $encoding) {
);
},
);

yield proof(
'IO::files()->temporary()->close()',
given($strings),
static function($assert, $chunks) {
$tmp = IO::fromAmbientAuthority()
->files()
->temporary(Sequence::of(...$chunks)->map(Str::of(...)))
->match(
static fn($tmp) => $tmp,
static fn() => null,
);

$assert
->object($tmp)
->instance(Temporary::class);

$assert->not()->null(
$tmp->read()->size()->match(
static fn($size) => $size,
static fn() => null,
),
);

$assert
->object($tmp->close()->match(
static fn($sideEffect) => $sideEffect,
static fn() => null,
))
->instance(SideEffect::class);

$assert->null(
$tmp->read()->size()->match(
static fn($size) => $size,
static fn() => null,
),
);
},
);
};
13 changes: 12 additions & 1 deletion src/Files/Temporary.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
Internal,
Internal\Capabilities,
};
use Innmind\Immutable\Attempt;
use Innmind\Immutable\{
Attempt,
SideEffect,
};

final class Temporary
{
Expand Down Expand Up @@ -53,4 +56,12 @@ public function pull(): Attempt
fn() => Pull::of($this->capabilities, $this->stream),
);
}

/**
* @return Attempt<SideEffect>
*/
public function close(): Attempt
{
return $this->stream->close();
}
}