Skip to content

Commit 968b8de

Browse files
committed
Ensure the output stream is always closed when it goes out of scope
1 parent e1ac371 commit 968b8de

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Web change log
55

66
## 3.11.0 / 2023-12-02
77

8+
* Ensured the output stream is always closed when it goes out of scope
9+
(@thekid)
810
* Removed superfluous layer of output buffering in development webserver
911
(@thekid)
1012
* Merged PR #105: Implement `WriteChunks::flush()` to use for explicitely

src/main/php/web/io/Output.class.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,9 @@ public function close() {
5252
$this->finish();
5353
$this->closed= true;
5454
}
55+
56+
/** Ensures `close()` is called */
57+
public function __destruct() {
58+
$this->close();
59+
}
5560
}

src/test/php/web/unittest/io/OutputTest.class.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,19 @@ public function finish() { $this->finished++; }
5151

5252
Assert::equals(1, $out->finished);
5353
}
54+
55+
#[Test]
56+
public function destructor_calls_finish() {
57+
$finished= 0;
58+
$out= new class($finished) extends Output {
59+
private $finished;
60+
public function __construct(&$finished) { $this->finished= &$finished; }
61+
public function begin($status, $message, $headers) { }
62+
public function write($bytes) { }
63+
public function finish() { $this->finished++; }
64+
};
65+
$out= null;
66+
67+
Assert::equals(1, $finished);
68+
}
5469
}

0 commit comments

Comments
 (0)