Skip to content
Open
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
27 changes: 23 additions & 4 deletions src/main/php/web/Response.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
class Response {
private $output;
private $flushing= [];
private $flushed= false;
private $status= 200;
private $message= 'OK';
Expand Down Expand Up @@ -111,26 +112,39 @@ public function headers() {

/** @param web.io.Output $output */
private function begin($output) {
foreach ($this->flushing as $function) {
$function($this);
}
$output->begin($this->status, $this->message, $this->cookies
? array_merge($this->headers, ['Set-Cookie' => array_map(function($c) { return $c->header(); }, $this->cookies)])
: $this->headers
);
$this->flushed= true;
}

/**
* Passes a function to call before flushing the response
*
* @param function(self): void $function
* @return self
*/
public function flushing(callable $function) {
$this->flushing[]= $function;
return $this;
}

/**
* Flushes response
*
* @param web.io.Output $output
* @return void
* @throws lang.IllegalStateException
*/
public function flush($output= null) {
public function flush() {
if ($this->flushed) {
throw new IllegalStateException('Response already flushed');
}

$this->begin($output ?: $this->output);
$this->begin($this->output);
}

/**
Expand All @@ -157,16 +171,21 @@ public function end() {
*
* @param int $size If omitted, uses chunked transfer encoding
* @return io.streams.OutputStream
* @throws lang.IllegalStateException
*/
public function stream($size= null) {
if ($this->flushed) {
throw new IllegalStateException('Response already flushed');
}

if (null === $size) {
$output= $this->output->stream();
} else {
$this->headers['Content-Length']= [$size];
$output= $this->output;
}

$this->flush($output);
$this->begin($output);
return $output;
}

Expand Down
49 changes: 49 additions & 0 deletions src/test/php/web/unittest/ResponseTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,59 @@ public function flushed() {
Assert::true($res->flushed());
}

#[Test]
public function ended() {
$res= new Response(new TestOutput());
Assert::false($res->flushed());
$res->end();
Assert::true($res->flushed());
}

#[Test, Expect(IllegalStateException::class)]
public function flush_twice() {
$res= new Response(new TestOutput());
$res->flush();
$res->flush();
}

#[Test]
public function flushing_explicitely() {
$executed= 0;
$res= (new Response(new TestOutput()))->flushing(function() use(&$executed) {
$executed++;
});
$res->flush();
Assert::equals(1, $executed);
}

#[Test]
public function flushing_implicitely() {
$executed= 0;
$res= (new Response(new TestOutput()))->flushing(function() use(&$executed) {
$executed++;
});
$res->send('Test', 'text/plain');
Assert::equals(1, $executed);
}

#[Test]
public function flushing_on_end() {
$executed= 0;
$res= (new Response(new TestOutput()))->flushing(function() use(&$executed) {
$executed++;
});
$res->end();
Assert::equals(1, $executed);
}

#[Test]
public function multiple_flushing_functions() {
$executed= ['one' => 0, 'two' => 0];
$res= (new Response(new TestOutput()))
->flushing(function() use(&$executed) { $executed['one']++; })
->flushing(function() use(&$executed) { $executed['two']++; })
;
$res->flush();
Assert::equals(['one' => 1, 'two' => 1], $executed);
}
}