mirror of
https://github.com/rekryt/iplist.git
synced 2025-10-13 08:59:34 +03:00
Initial commit
This commit is contained in:
60
src/App/Controller/AbstractController.php
Normal file
60
src/App/Controller/AbstractController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
use Amp\Http\HttpStatus;
|
||||
use Amp\Http\Server\Request;
|
||||
use Amp\Http\Server\Response;
|
||||
|
||||
use Throwable;
|
||||
|
||||
abstract class AbstractController implements ControllerInterface {
|
||||
private int $httpStatus = HttpStatus::OK;
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param array $headers
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function __construct(protected Request $request, protected array $headers = []) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
*/
|
||||
public function __invoke(): Response {
|
||||
return new Response(status: $this->httpStatus, headers: $this->headers, body: $this->getBody());
|
||||
}
|
||||
|
||||
abstract public function getBody(): string;
|
||||
|
||||
public function setHeaders(array $headers): AbstractController {
|
||||
$this->headers = $headers;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $httpStatus
|
||||
*/
|
||||
public function setHttpStatus(int $httpStatus): void {
|
||||
$this->httpStatus = $httpStatus;
|
||||
}
|
||||
|
||||
public function redirect(string $url, bool $permanently = false): void {
|
||||
$this->httpStatus = $permanently ? HttpStatus::MOVED_PERMANENTLY : HttpStatus::SEE_OTHER;
|
||||
$this->headers = array_merge($this->headers ?? [], ['location' => $url]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBaseURL(): string {
|
||||
$schemePort = ['http' => 80, 'https' => 443];
|
||||
return $this->request->getUri()->getScheme() .
|
||||
'://' .
|
||||
$this->request->getUri()->getHost() .
|
||||
($schemePort[$this->request->getUri()->getScheme()] !== $this->request->getUri()->getPort()
|
||||
? ':' . $this->request->getUri()->getPort()
|
||||
: '');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user