initial commit for soko-web
soko-web/pipeline/head This commit looks good

This commit is contained in:
bence committed 2023-04-07 19:32:15 +02:00
commit a17a88e9d4
71 files changed
+7241

No files matched your search

+22
View File
@@ -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;
}
+35
View File
@@ -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';
}
}
+23
View File
@@ -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';
}
}
+41
View File
@@ -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;
}
}
}