aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StudipResponse.php
diff options
context:
space:
mode:
authorPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
committerPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
commit4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch)
tree5c07151ae61276d334e88f6309c30d439a85c12e /lib/classes/StudipResponse.php
parentda0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff)
parent97a188592c679890a25c37ab78463add76a52ff7 (diff)
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'lib/classes/StudipResponse.php')
-rw-r--r--lib/classes/StudipResponse.php55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/classes/StudipResponse.php b/lib/classes/StudipResponse.php
new file mode 100644
index 0000000..a9f1a4c
--- /dev/null
+++ b/lib/classes/StudipResponse.php
@@ -0,0 +1,55 @@
+<?php
+class StudipResponse extends Trails\Response
+{
+ /**
+ * Outputs this response to the client using "echo" and "header".
+ *
+ * This extension allows the body to be a callable and handles generators
+ * by outputting the chunks yielded by the generator.
+ */
+ public function output()
+ {
+ if (isset($this->status)) {
+ $this->send_header(
+ "{$_SERVER['SERVER_PROTOCOL']} {$this->status} {$this->reason}",
+ true,
+ $this->status
+ );
+ }
+
+ // Send headers
+ foreach ($this->headers as $k => $v) {
+ $this->send_header("{$k}: {$v}");
+ }
+
+ // Determine output
+ if (is_callable($this->body)) {
+ $output = call_user_func($this->body);
+ } else {
+ $output = $this->body;
+ }
+
+ if ($output instanceof Generator) {
+ // Clear output buffer
+ while (ob_get_level()) {
+ ob_end_clean();
+ }
+
+ // Ensure generator will run to the end
+ $abort = ignore_user_abort(true);
+
+ // Output chunks yielded by generator
+ foreach ($output as $chunk) {
+ if (!connection_aborted()) {
+ echo $chunk;
+ flush();
+ }
+ }
+
+ // Reset user abort to previous state
+ ignore_user_abort($abort);
+ } else {
+ echo $output;
+ }
+ }
+}