This commit is contained in:
2025-12-01 06:19:25 +08:00
parent 90ee572092
commit 79ce7142d9
2 changed files with 443 additions and 397 deletions
-2
View File
@@ -18,7 +18,5 @@
"psr-4": {
"Kiri\\Router\\": "./src"
}
},
"require-dev": {
}
}
+48
View File
@@ -8,6 +8,33 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
/**
* @param string $path
* @param array $data
*
* @return ResponseInterface
*/
function View(string $path, array $data = []): ResponseInterface
{
$response = \response();
$response->withAddedHeader('Content-Type', 'text/html; charset=utf-8');
try {
ob_start();
$__path = $path;
$__data = $data;
extract($__data, EXTR_SKIP);
require APP_PATH . 'resources/view/' . $path . '.php';
$content = ltrim(ob_get_clean());
} catch (\Exception $e) {
$content = throwable($e);
} finally {
return $response->html($content);
}
}
/**
*
*/
@@ -57,6 +84,7 @@ class Response implements ResponseInterface
/**
* @param ContentType $contentType
*
* @return Response
*/
public function withContentType(ContentType $contentType): ResponseInterface
@@ -68,6 +96,7 @@ class Response implements ResponseInterface
/**
* @param array $content
* @param int $statusCode
*
* @return ResponseInterface
*/
public function json(array $content, int $statusCode = 200): ResponseInterface
@@ -79,6 +108,7 @@ class Response implements ResponseInterface
* @param string $url
* @param array $params
* @param int $statusCode
*
* @return ResponseInterface
*/
public function redirectTo(string $url, array $params = [], int $statusCode = 302): ResponseInterface
@@ -89,6 +119,7 @@ class Response implements ResponseInterface
/**
* @param array $content
* @param int $statusCode
*
* @return ResponseInterface
*/
public function xml(array $content, int $statusCode = 200): ResponseInterface
@@ -100,6 +131,7 @@ class Response implements ResponseInterface
/**
* @param string $content
* @param int $statusCode
*
* @return ResponseInterface
*/
public function html(string $content = '', int $statusCode = 200): ResponseInterface
@@ -111,6 +143,7 @@ class Response implements ResponseInterface
/**
* @param string $content
* @param int $statusCode
*
* @return ResponseInterface
*/
public function sendfile(string $content, int $statusCode = 200): ResponseInterface
@@ -123,6 +156,7 @@ class Response implements ResponseInterface
* @param mixed $data
* @param int $statusCode
* @param ContentType $type
*
* @return Response
*/
public function write(mixed $data, int $statusCode = 200, ContentType $type = ContentType::HTML): ResponseInterface
@@ -134,6 +168,7 @@ class Response implements ResponseInterface
/**
* @param string $method
* @param mixed ...$params
*
* @return mixed
*/
private function __call__(string $method, ...$params): mixed
@@ -144,6 +179,7 @@ class Response implements ResponseInterface
/**
* @param array $cookies
*
* @return ResponseInterface
*/
public function withCookieParams(array $cookies): ResponseInterface
@@ -185,6 +221,7 @@ class Response implements ResponseInterface
* new protocol version.
*
* @param string $version HTTP protocol version
*
* @return static
*/
public function withProtocolVersion(string $version): ResponseInterface
@@ -228,6 +265,7 @@ class Response implements ResponseInterface
* Checks if a header exists by the given case-insensitive name.
*
* @param string $name Case-insensitive header field name.
*
* @return bool Returns true if any header names match the given header
* name using a case-insensitive string comparison. Returns false if
* no matching header name is found in the message.
@@ -248,6 +286,7 @@ class Response implements ResponseInterface
* empty array.
*
* @param string $name Case-insensitive header field name.
*
* @return string[] An array of string values as provided for the given
* header. If the header does not appear in the message, this method MUST
* return an empty array.
@@ -273,6 +312,7 @@ class Response implements ResponseInterface
* an empty string.
*
* @param string $name Case-insensitive header field name.
*
* @return string A string of values as provided for the given header
* concatenated together using a comma. If the header does not appear in
* the message, this method MUST return an empty string.
@@ -295,6 +335,7 @@ class Response implements ResponseInterface
*
* @param string $name Case-insensitive header field name.
* @param string|string[] $value Header value(s).
*
* @return static
* @throws
*/
@@ -307,6 +348,7 @@ class Response implements ResponseInterface
/**
* @param array $headers
*
* @return ResponseInterface
*/
public function withHeaders(array $headers): ResponseInterface
@@ -327,6 +369,7 @@ class Response implements ResponseInterface
*
* @param string $name Case-insensitive header field name to add.
* @param string|string[] $value Header value(s).
*
* @return static
* @throws
*/
@@ -346,6 +389,7 @@ class Response implements ResponseInterface
* the named header.
*
* @param string $name Case-insensitive header field name to remove.
*
* @return static
*/
public function withoutHeader(string $name): ResponseInterface
@@ -375,6 +419,7 @@ class Response implements ResponseInterface
* new body stream.
*
* @param StreamInterface $body Body.
*
* @return static
* @throws
*/
@@ -403,6 +448,7 @@ class Response implements ResponseInterface
* @param string $content
* @param int $statusCode
* @param ContentType $contentType
*
* @return ResponseInterface
*/
public function raw(string $content, int $statusCode = 200, ContentType $contentType = ContentType::JSON): ResponseInterface
@@ -423,10 +469,12 @@ class Response implements ResponseInterface
*
* @link http://tools.ietf.org/html/rfc7231#section-6
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
*
* @param int $code The 3-digit integer result code to set.
* @param string $reasonPhrase The reason phrase to use with the
* provided status code; if none is provided, implementations MAY
* use the defaults as suggested in the HTTP specification.
*
* @return static
* @throws
*/