Files
kiri-core/HttpServer/Controller.php
T

101 lines
1.7 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer;
2021-02-20 17:30:03 +08:00
use HttpServer\Abstracts\HttpService;
2020-08-31 01:27:08 +08:00
use HttpServer\Http\HttpHeaders;
use HttpServer\Http\HttpParams;
use HttpServer\Http\Request;
use Exception;
2021-03-03 13:03:11 +08:00
use ReflectionException;
2021-02-08 17:01:03 +08:00
use Snowflake\Abstracts\TraitApplication;
2020-11-10 18:04:42 +08:00
use Snowflake\Exception\ComponentException;
2021-03-03 13:03:11 +08:00
use Snowflake\Exception\NotFindClassException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
/**
* Class WebController
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Web
2021-04-22 14:37:28 +08:00
* @property-read HttpParams $input
* @property-read HttpHeaders $headers
* @property-read Request $request
2020-08-31 01:27:08 +08:00
*/
2021-02-20 17:30:03 +08:00
class Controller extends HttpService
2020-08-31 01:27:08 +08:00
{
2021-02-08 17:01:03 +08:00
use TraitApplication;
2020-09-10 15:54:14 +08:00
2020-11-10 18:04:42 +08:00
/**
* Controller constructor.
* @param array $config
2021-03-29 17:29:39 +08:00
* @throws Exception
2020-11-10 18:04:42 +08:00
*/
2020-09-10 15:54:14 +08:00
public function __construct($config = [])
{
parent::__construct($config);
}
2021-03-29 17:29:39 +08:00
/**
* @throws Exception
*/
public function init()
{
$annotation = Snowflake::getAnnotation();
$annotation->injectProperty($this);
}
2020-08-31 01:27:08 +08:00
/**
2020-12-15 17:17:10 +08:00
* @return ?HttpParams
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-15 17:17:10 +08:00
public function getInput(): ?HttpParams
2020-08-31 01:27:08 +08:00
{
2021-04-22 14:37:28 +08:00
return \request()->params;
2020-08-31 01:27:08 +08:00
}
/**
2020-12-15 17:17:10 +08:00
* @return ?HttpHeaders
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-15 17:17:10 +08:00
public function getHeaders(): ?HttpHeaders
2020-08-31 01:27:08 +08:00
{
2021-04-22 14:37:28 +08:00
return \request()->headers;
2020-08-31 01:27:08 +08:00
}
/**
2020-12-15 17:17:29 +08:00
* @return Request|null
2020-08-31 01:27:08 +08:00
*/
2020-12-15 17:17:10 +08:00
public function getRequest(): ?Request
2020-08-31 01:27:08 +08:00
{
2021-04-23 02:14:30 +08:00
return \request();
2020-08-31 01:27:08 +08:00
}
2020-11-10 18:04:42 +08:00
/**
2020-12-17 14:09:14 +08:00
* @param $name
2020-11-10 18:04:42 +08:00
* @return mixed
2021-04-22 14:37:28 +08:00
* @throws Exception
2020-11-10 18:04:42 +08:00
*/
2020-12-17 14:09:14 +08:00
public function __get($name): mixed
2020-11-10 18:04:42 +08:00
{
// TODO: Change the autogenerated stub
2020-12-17 14:09:14 +08:00
if (property_exists($this, $name)) {
return $this->$name;
2020-11-10 18:04:42 +08:00
}
2020-12-17 14:09:14 +08:00
$method = 'get' . ucfirst($name);
2020-11-10 18:04:42 +08:00
if (method_exists($this, $method)) {
return $this->{$method}();
}
2020-12-17 14:09:14 +08:00
return Snowflake::app()->get($name);
2020-11-10 18:04:42 +08:00
}
2020-08-31 01:27:08 +08:00
}