122 lines
3.9 KiB
PHP
122 lines
3.9 KiB
PHP
<?php declare(strict_types=1);
|
|
/** License
|
|
* Copyright 2025-2026 Narta Xaymar Dirks <info@xaymar.com>
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
**/
|
|
|
|
namespace Peninsula\HTTP;
|
|
|
|
class Response {
|
|
// There can't be more than a single response to a request anyway.
|
|
use \Peninsula\Traits\Singleton;
|
|
|
|
private int $status;
|
|
private array $headers;
|
|
|
|
private bool $haveSentHeader;
|
|
private bool $haveBegunStreaming;
|
|
|
|
private function __construct() {
|
|
$this->$status = http_response_code();
|
|
$this->$headers = array();
|
|
$this->$haveSentHeader = false;
|
|
$this->$haveBeginStreaming = false;
|
|
|
|
// Default to shifting any output into the void for now.
|
|
mb_http_output("UTF-8");
|
|
ob_start(self::yellIntoTheVoid, 1024, PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_REMOVABLE);
|
|
}
|
|
|
|
public function __destruct(): void {
|
|
// Ensure we cleanly stop everything.
|
|
$this->stop();
|
|
|
|
// Stop and clean the output buffer.
|
|
ob_end_clean();
|
|
}
|
|
|
|
/** Retrieve the value of a header, if set.
|
|
*
|
|
*/
|
|
public function header(string $header): string|array|null {
|
|
return $headers[$header];
|
|
}
|
|
|
|
/** Replace, Set or Clear the value of a header.
|
|
*
|
|
*/
|
|
public function header(string $header, string|array $value): void {
|
|
$headers[$header] = $value;
|
|
}
|
|
|
|
//----------------------------------------//
|
|
// Transmission Status
|
|
//----------------------------------------//
|
|
|
|
/**
|
|
*
|
|
*/
|
|
protected function sendHeader(): void {
|
|
// Ignore the call if we've already sent a header.
|
|
if ($this->$haveSentHeader) return;
|
|
|
|
http_response_code($this->$status);
|
|
foreach($headers as $key => &$value) {
|
|
if (is_string($value)) {
|
|
header("{$key}: {$value}");
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
// Flush the headers to the output.
|
|
ob_flush();
|
|
|
|
$this->$haveSentHeader = true;
|
|
}
|
|
|
|
public function start(): void {
|
|
// Ignore the call if we've already begin streaming output.
|
|
if ($this->$haveBegunStreaming) return;
|
|
|
|
// Forcefully send headers.
|
|
$this->sendHeaders();
|
|
|
|
// Set the flag for streaming
|
|
$this->$haveBegunStreaming = true;
|
|
|
|
// Actually set the output buffering to on so we can perform streamed output.
|
|
ob_start(\Peninsula)
|
|
|
|
|
|
|
|
}
|
|
|
|
public function stop(): void {
|
|
if (!$this->$haveBegunStreaming) {
|
|
|
|
ob_end_flush();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static function toProperEncoding(string $buffer, int $phase): string {
|
|
$buffer = mb_convert_encoding()
|
|
}
|
|
|
|
public static function yellIntoTheVoid(string $buffer, int $phase): string {
|
|
// Literally just ignore all output while output buffering is on.
|
|
return true;
|
|
}
|
|
}
|