This commit is contained in:
71 files changed
+7241
No files matched your search
@@ -0,0 +1,22 @@
|
||||
<?php namespace SokoWeb\Response;
|
||||
|
||||
use SokoWeb\Interfaces\Response\IContent;
|
||||
|
||||
abstract class ContentBase implements IContent
|
||||
{
|
||||
protected array $data;
|
||||
|
||||
public function setData(array $data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
abstract public function render(): void;
|
||||
|
||||
abstract public function getContentType(): string;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php namespace SokoWeb\Response;
|
||||
|
||||
use SokoWeb\View\Linker;
|
||||
|
||||
class HtmlContent extends ContentBase
|
||||
{
|
||||
private string $view;
|
||||
|
||||
public function __construct(string $view, array $data = [])
|
||||
{
|
||||
$this->view = $view;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function render(): void
|
||||
{
|
||||
if (!empty($_ENV['DEV'])) {
|
||||
$generator = new Linker($this->view);
|
||||
$generator->generate();
|
||||
}
|
||||
|
||||
extract($this->data);
|
||||
|
||||
if (defined('SCRIPT_STARTED')) {
|
||||
$__debug_runtime = round((hrtime(true) - SCRIPT_STARTED) / 1e+6, 1);
|
||||
}
|
||||
|
||||
require ROOT . '/cache/views/' . $this->view . '.php';
|
||||
}
|
||||
|
||||
public function getContentType(): string
|
||||
{
|
||||
return 'text/html';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php namespace SokoWeb\Response;
|
||||
|
||||
class JsonContent extends ContentBase
|
||||
{
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function render(): void
|
||||
{
|
||||
if (defined('SCRIPT_STARTED')) {
|
||||
$this->data['__debug_runtime'] = round((hrtime(true) - SCRIPT_STARTED) / 1e+6, 1);
|
||||
}
|
||||
|
||||
echo json_encode($this->data);
|
||||
}
|
||||
|
||||
public function getContentType(): string
|
||||
{
|
||||
return 'application/json';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php namespace SokoWeb\Response;
|
||||
|
||||
use SokoWeb\Interfaces\Response\IRedirect;
|
||||
|
||||
class Redirect implements IRedirect
|
||||
{
|
||||
private string $target;
|
||||
|
||||
private int $type;
|
||||
|
||||
public function __construct(string $target, int $type = IRedirect::TEMPORARY)
|
||||
{
|
||||
$this->target = $target;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function getUrl(): string
|
||||
{
|
||||
if (preg_match('/^http(s)?/', $this->target) === 1) {
|
||||
$link = $this->target;
|
||||
} else {
|
||||
$link = \Container::$request->getBase() . '/' . $this->target;
|
||||
}
|
||||
|
||||
return $link;
|
||||
}
|
||||
|
||||
public function getHttpCode(): int
|
||||
{
|
||||
switch ($this->type) {
|
||||
case IRedirect::PERMANENT:
|
||||
return 301;
|
||||
|
||||
case IRedirect::TEMPORARY:
|
||||
return 302;
|
||||
|
||||
default:
|
||||
return 302;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user