Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git

Honours file handle constraints

AuthorDave Jarvis <email>
Date2026-02-21 11:34:36 GMT-0800
Commit5536f3fbafedcd2dd7f410f94f3b8daaabc4ee16
Parent6de003a
git/FileHandlePool.php
string $fallback = ''
): string {
- $computed = $this->withDedicatedHandle( $path, $action );
+ $handle = @fopen( $path, 'rb' );
+ $computed = false;
+
+ if( is_resource( $handle ) ) {
+ $computed = $action( $handle );
+
+ fclose( $handle );
+ }
return is_string( $computed ) ? $computed : $fallback;
}
public function streamGeneratorDedicated(
string $path,
callable $action
): Generator {
- $resultGenerator = $this->withDedicatedHandle( $path, $action );
+ $handle = @fopen( $path, 'rb' );
- if( $resultGenerator instanceof Generator ) {
- yield from $resultGenerator;
+ if( is_resource( $handle ) ) {
+ try {
+ $resultGenerator = $action( $handle );
+
+ if( $resultGenerator instanceof Generator ) {
+ yield from $resultGenerator;
+ }
+ } finally {
+ fclose( $handle );
+ }
}
}
if( array_key_exists( $path, $this->handles ) ) {
$result = $action( $this->handles[$path] );
- }
-
- return $result;
- }
-
- private function withDedicatedHandle(
- string $path,
- callable $action
- ): mixed {
- $handle = @fopen( $path, 'rb' );
- $result = false;
-
- if( is_resource( $handle ) ) {
- $result = $action( $handle );
-
- fclose( $handle );
}
Delta19 lines added, 20 lines removed, 1-line decrease