withinboredom/building-blocks
最新稳定版本:v0.2.4
Composer 安装命令:
composer require withinboredom/building-blocks
包简介
README 文档
README
This is a library that totally ignores PSRs with a focus on simplicity. It is not a library for building complex applications, but rather when you just need to get stuff done with minimal fuss.
Turning errors into exceptions
The ErrorControl class allows for simply turning errors into exceptions:
<?php new class extends ErrorControl { protected function handleUnhandledException(Throwable $exception): void { $message = [ 'exception_type' => get_class($exception), 'post' => file_get_contents('php://input'), 'url' => $_SERVER['REQUEST_URI'], 'ip' => $_SERVER['REMOTE_ADDR'], 'message' => $exception->getMessage(), 'line' => $exception->getLine(), 'file' => $exception->getFile(), 'trace' => $exception->getTraceAsString(), 'num' => $exception->getCode(), ]; error_log(json_encode($message, JSON_PRETTY_PRINT), 4); if (!headers_sent()) { http_response_code(HttpResponseCode::InternalServerError); die(); } } protected function getIgnoredErrorLevel(): int { return E_DEPRECATED; } };
Cache-Control
The cache is a very important bit of information for a browser, which can save you a huge amount of bandwidth.
$cacheHeaders = new ResponseCacheControl( static fn() => $_SERVER['HTTP_IF_NONE_MATCH'] ?? '', static fn() => new DateTimeImmutable($_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? 'this minute') ); if(($status = $cacheHeaders->recommendStatus($etag, $expiresAt)) !== null) { http_response_code($status->value); die(); } $headers = $cacheHeaders->addTerm(CacheResponseTerms::Immutable)->recommendStatus($etag, $expiresAt); // pass headers to PSR response object
A ridiculously simple router
A really simple router working in O(routes + parameters) time.
$router = new Router($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); $router->addRoute('/', static fn() => new Result(\Withinboredom\ResponseCode\HttpResponseCode::Ok, 'Hello, world!')); $router->addRoute('/hello/:name', static fn(string $name) => new Result(\Withinboredom\ResponseCode\HttpResponseCode::Ok, "Hello, $name!")); // emit the result of the routing and die $router->doRouting()->emit();
统计信息
- 总下载量: 979
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-29