Skip to content

Commit d5cbca5

Browse files
committed
Major refactor kind of working
1 parent 72e3efd commit d5cbca5

File tree

17 files changed

+643
-156
lines changed

17 files changed

+643
-156
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ phpliteadmin.*
1414
.php-cs-fixer.cache
1515
sysadm/
1616
vendor/*
17+
public/php_error.log

public/index.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
use HCP\Init;
1212

13-
echo new Init(new class()
14-
{
13+
echo new Init(new class() {
1514
public $cfg = [
1615
'email' => 'admin@example.com',
1716
'admpw' => 'admin123',
@@ -31,7 +30,7 @@
3130
'm' => 'list', // Method (action)
3231
'o' => 'Home', // Object (content)
3332
'r' => 'local', // Remotes (local)
34-
't' => 'TopNav', // Theme (Default)
33+
't' => 'SideBar', // Theme (Default)
3534
'x' => '', // XHR (request)
3635
];
3736

@@ -135,14 +134,16 @@
135134

136135
function elog(string $content): void
137136
{
138-
if (DBG) {
137+
if (DBG)
138+
{
139139
error_log($content);
140140
}
141141
}
142142

143143
function dbg($var = null): void
144144
{
145-
if (is_object($var)) {
145+
if (is_object($var))
146+
{
146147
$refobj = new \ReflectionObject($var);
147148
// get all public and protected properties
148149
$var = $refobj->getProperties(\ReflectionProperty::IS_PUBLIC);

src/Config/AppConfig.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HCP\Config;
6+
7+
class AppConfig
8+
{
9+
private array $config;
10+
private static ?AppConfig $instance = null;
11+
12+
private function __construct(array $initialConfig)
13+
{
14+
$this->config = $initialConfig;
15+
}
16+
17+
public static function getInstance(array $initialConfig = []): self
18+
{
19+
if (self::$instance === null)
20+
{
21+
self::$instance = new self($initialConfig);
22+
}
23+
return self::$instance;
24+
}
25+
26+
public function get(string $key, $default = null)
27+
{
28+
return $this->config[$key] ?? $default;
29+
}
30+
31+
public function set(string $key, $value): void
32+
{
33+
$this->config[$key] = $value;
34+
}
35+
36+
public function getAll(): array
37+
{
38+
return $this->config;
39+
}
40+
}

src/Http/Request.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HCP\Http;
6+
7+
class Request
8+
{
9+
private array $get;
10+
private array $post;
11+
private array $server;
12+
private array $session;
13+
14+
public function __construct()
15+
{
16+
$this->get = $_GET;
17+
$this->post = $_POST;
18+
$this->server = $_SERVER;
19+
$this->session = $_SESSION ?? [];
20+
}
21+
22+
public function getParam(string $key, $default = null)
23+
{
24+
return $this->get[$key] ?? $default;
25+
}
26+
27+
public function getPostParam(string $key, $default = null)
28+
{
29+
return $this->post[$key] ?? $default;
30+
}
31+
32+
public function isPost(): bool
33+
{
34+
return $this->server['REQUEST_METHOD'] === 'POST';
35+
}
36+
37+
public function getMethod(): string
38+
{
39+
return $this->server['REQUEST_METHOD'];
40+
}
41+
42+
public function getServerParam(string $key, $default = null)
43+
{
44+
return $this->server[$key] ?? $default;
45+
}
46+
47+
public function getSessionParam(string $key, $default = null)
48+
{
49+
return $this->session[$key] ?? $default;
50+
}
51+
52+
public function getAllParams(): array
53+
{
54+
return [
55+
'get' => $this->get,
56+
'post' => $this->post,
57+
'server' => $this->server,
58+
'session' => $this->session
59+
];
60+
}
61+
}

src/Http/Response.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HCP\Http;
6+
7+
class Response
8+
{
9+
private string $content = '';
10+
private array $headers = [];
11+
private int $statusCode = 200;
12+
private string $contentType = 'text/html';
13+
14+
public function setContent(string $content): self
15+
{
16+
$this->content = $content;
17+
return $this;
18+
}
19+
20+
public function addHeader(string $name, string $value): self
21+
{
22+
$this->headers[$name] = $value;
23+
return $this;
24+
}
25+
26+
public function setStatusCode(int $code): self
27+
{
28+
$this->statusCode = $code;
29+
return $this;
30+
}
31+
32+
public function setContentType(string $type): self
33+
{
34+
$this->contentType = $type;
35+
$this->addHeader('Content-Type', $type);
36+
return $this;
37+
}
38+
39+
public function getContent(): string
40+
{
41+
return $this->content;
42+
}
43+
44+
public function getStatusCode(): int
45+
{
46+
return $this->statusCode;
47+
}
48+
49+
public function send(): void
50+
{
51+
if (!headers_sent()) {
52+
foreach ($this->headers as $name => $value) {
53+
header("$name: $value");
54+
}
55+
http_response_code($this->statusCode);
56+
}
57+
echo $this->content;
58+
}
59+
60+
public function json(mixed $data): self
61+
{
62+
$this->setContentType('application/json');
63+
$this->content = json_encode($data, JSON_PRETTY_PRINT);
64+
return $this;
65+
}
66+
67+
public function text(string $content): self
68+
{
69+
$this->setContentType('text/plain');
70+
$this->content = $content;
71+
return $this;
72+
}
73+
74+
public function html(string $content): self
75+
{
76+
$this->setContentType('text/html');
77+
$this->content = $content;
78+
return $this;
79+
}
80+
}

src/Init.php

+58-44
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,43 @@
44

55
namespace HCP;
66

7+
use HCP\Config\AppConfig;
8+
use HCP\Http\Request;
9+
use HCP\Http\Response;
10+
use HCP\Plugin\PluginManager;
711
use HCP\Util;
812

913
class Init
1014
{
11-
private $theme;
15+
private object $theme;
16+
private Request $request;
17+
private Response $response;
18+
private AppConfig $config;
19+
private PluginManager $pluginManager;
1220

1321
public function __construct(object $g)
1422
{
1523
elog(__METHOD__);
1624

25+
$this->initializeCore($g);
26+
$this->processRequest($g);
27+
$this->processOutput($g);
28+
}
29+
30+
private function initializeCore(object $g): void
31+
{
32+
$this->config = AppConfig::getInstance($g->cfg);
33+
$this->request = new Request();
34+
$this->response = new Response();
35+
$this->pluginManager = new PluginManager();
36+
1737
$this->initializeSession($g);
38+
Theme::setGlobal($g);
39+
40+
if (!empty($g->in['t'])) {
41+
Theme::setTheme($g->in['t']);
42+
}
1843
$this->loadTheme($g);
19-
$this->processPlugin($g);
20-
$this->processOutput($g);
2144
}
2245

2346
private function initializeSession(object $g): void
@@ -26,13 +49,10 @@ private function initializeSession(object $g): void
2649

2750
session_start();
2851

29-
//elog(var_export($_SESSION, true));
30-
//$_SESSION = [];
31-
3252
$g->cfg['host'] ??= getenv('HOSTNAME');
3353
Util::cfg($g);
3454
$g->in = Util::esc($g->in);
35-
$g->cfg['self'] = str_replace('index.php', '', $_SERVER['PHP_SELF']);
55+
$g->cfg['self'] = str_replace('index.php', '', $this->request->getServerParam('PHP_SELF'));
3656

3757
if (!isset($_SESSION['c'])) {
3858
$_SESSION['c'] = Util::random_token(32);
@@ -57,25 +77,24 @@ private function loadTheme(object $g): void
5777
if (class_exists($viewClass)) {
5878
$this->theme = new $viewClass($g);
5979
} else {
60-
// Fallback to default Theme
61-
$this->theme = new Theme($g);
80+
// Fallback to TopNav theme
81+
$this->theme = Theme::getTheme();
6282
}
6383

6484
// Assign theme instance to g->t for access in plugins
6585
$g->t = $this->theme;
6686
}
6787

68-
private function processPlugin(object $g): void
88+
private function processRequest(object $g): void
6989
{
7090
elog(__METHOD__);
7191

72-
$pluginClass = "HCP\\Plugins\\{$g->in['o']}\\Model";
92+
$pluginName = $g->in['o'];
93+
$plugin = $this->pluginManager->loadPlugin($pluginName, $this->theme);
7394

74-
elog(__METHOD__ . " pluginClass=$pluginClass");
75-
76-
if (class_exists($pluginClass)) {
95+
if ($plugin) {
7796
$g->in['a'] ? Util::chkapi($g) : Util::remember($g);
78-
$g->out['main'] = (string) new $pluginClass($this->theme);
97+
$g->out['main'] = (string) $plugin;
7998
} else {
8099
$g->out['main'] = 'Error: no plugin object!';
81100
}
@@ -90,43 +109,38 @@ private function processOutput(object $g): void
90109
$g->out[$k] = method_exists($this->theme, $k) ? $this->theme->{$k}() : $v;
91110
}
92111
}
112+
113+
$x = $g->in['x'];
114+
$content = '';
115+
116+
if ('text' === $x) {
117+
$content = preg_replace('/^\h*\v+/m', '', strip_tags($g->out['main']));
118+
$this->response->text($content);
119+
} elseif ('json' === $x) {
120+
$this->response->json($g->out['main']);
121+
} elseif ($x) {
122+
if ($x === 'html') {
123+
$this->response->html($g->out['main']);
124+
} else {
125+
$out = $g->out[$x] ?? '';
126+
if ($out) {
127+
$this->response->json($out);
128+
}
129+
}
130+
} else {
131+
$this->response->html($this->theme->html());
132+
}
93133
}
94134

95135
public function __destruct()
96136
{
97-
elog(__FILE__ . ' ' . $_SERVER['REMOTE_ADDR'] . ' ' .
98-
round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 4) . "\n");
137+
elog(__FILE__ . ' ' . $this->request->getServerParam('REMOTE_ADDR') . ' ' .
138+
round((microtime(true) - $this->request->getServerParam('REQUEST_TIME_FLOAT')), 4) . "\n");
99139
}
100140

101141
public function __toString(): string
102142
{
103143
elog(__METHOD__);
104-
105-
$g = $this->theme->g;
106-
107-
$x = $g->in['x'];
108-
109-
if ('text' === $x) {
110-
return preg_replace('/^\h*\v+/m', '', strip_tags($g->out['main']));
111-
}
112-
113-
if ('json' === $x) {
114-
header('Content-Type: application/json');
115-
return $g->out['main'];
116-
}
117-
118-
if ($x) {
119-
if ($x === 'html') {
120-
return $g->out['main'];
121-
}
122-
123-
$out = $g->out[$x] ?? '';
124-
if ($out) {
125-
header('Content-Type: application/json');
126-
return json_encode($out, JSON_PRETTY_PRINT);
127-
}
128-
}
129-
130-
return $this->theme->html();
144+
return $this->response->getContent();
131145
}
132146
}

0 commit comments

Comments
 (0)