1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108:
<?php
namespace Baguette;
/**
* Base class of web application
*
* @author USAMI Kenta <tadsan@zonu.me>
* @copyright 2016 Baguette HQ
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*
* @property-read array $server $_SERVER
* @property-read array $cookie $_COOKIE
* @property-read array $get $_GET
* @property-read array $post $_POST
* @property-read \DateTimeImmutable $now
*/
abstract class Application
{
/** @var array $_SERVER */
protected $server;
/** @var array $_COOKIE */
protected $cookie;
/** @var array $_GET */
protected $get;
/** @var array $_POST */
protected $post;
/** @var \DateTimeImmutable */
protected $now;
/**
* @param array $server $_SERVER
* @param array $cookie $_COOKIE
* @param array $get $_GET
* @param array $post $_POST
* @param \DateTimeImmutable $now
*/
public function __construct(array $server, array $cookie, array $get, array $post, \DateTimeImmutable $now = null)
{
$this->server = $server;
$this->cookie = $cookie;
$this->get = $get;
$this->post = $post;
$this->now = $now ?: new \DateTimeImmutable;
}
/**
* @param \Teto\Routing\Action $action
* @return \Baguette\Response\ResponseInterface
*/
abstract public function execute(\Teto\Routing\Action $action);
public function __get($name)
{
return $this->$name;
}
/**
* @param \Baguette\Response\ResponseInterface $response
* @return string
*/
public function renderResponse(Response\ResponseInterface $response)
{
if (!static::headers_sent()) {
static::http_response_code($response->getHttpStatusCode());
foreach ($response->getResponseHeaders() as $type => $values) {
foreach ($values as $v) {
static::header("{$type}: {$v}");
}
}
}
return $response->render($this);
}
/**
* @return bool
*/
protected function headers_sent()
{
return \headers_sent();
}
/**
* @param string $string
* @param bool $replace
* @param int $http_response_code
*/
protected function header($string)
{
\header($string);
}
/**
* @param int $response_code
* @return int
*/
protected function http_response_code($response_code = null)
{
if ($response_code === null) {
return \http_response_code();
} else {
return \http_response_code($response_code);
}
}
}