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
|
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-12-15 17:17:10 +08:00
|
|
|
/** @var null|HttpParams $input */
|
|
|
|
|
public null|HttpParams $input;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
2020-12-15 17:17:10 +08:00
|
|
|
/** @var null|HttpHeaders */
|
|
|
|
|
public null|HttpHeaders $headers;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
2020-12-15 17:17:10 +08:00
|
|
|
/** @var null|Request */
|
|
|
|
|
public null|Request $request;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-09-10 15:54:14 +08:00
|
|
|
|
2020-11-10 18:04:42 +08:00
|
|
|
/**
|
|
|
|
|
* Controller constructor.
|
|
|
|
|
* @param array $config
|
|
|
|
|
*/
|
2020-09-10 15:54:14 +08:00
|
|
|
public function __construct($config = [])
|
|
|
|
|
{
|
|
|
|
|
parent::__construct($config);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
/**
|
2020-12-15 17:17:10 +08:00
|
|
|
* @param null|HttpParams $input
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-12-15 17:17:10 +08:00
|
|
|
public function setInput(?HttpParams $input): void
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
$this->input = $input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-15 17:17:10 +08:00
|
|
|
* @param ?HttpHeaders $headers
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-12-15 17:17:10 +08:00
|
|
|
public function setHeaders(?HttpHeaders $headers): void
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
$this->headers = $headers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-15 17:17:10 +08:00
|
|
|
* @param ?Request $request
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-12-15 17:17:10 +08:00
|
|
|
public function setRequest(?Request $request): void
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
$this->request = $request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
|
|
|
|
if (!$this->input) {
|
|
|
|
|
$this->input = $this->getRequest()->params;
|
|
|
|
|
}
|
|
|
|
|
return $this->input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2020-12-15 17:18:37 +08:00
|
|
|
if (!$this->headers && $this->getRequest()) {
|
2020-08-31 01:27:08 +08:00
|
|
|
$this->headers = $this->getRequest()->headers;
|
|
|
|
|
}
|
|
|
|
|
return $this->headers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
|
|
|
|
if (!$this->request) {
|
2020-09-03 11:39:20 +08:00
|
|
|
$this->request = Snowflake::app()->request;
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
return $this->request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
* @throws ComponentException
|
2021-03-03 13:03:11 +08:00
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
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
|
|
|
}
|