4
4
5
5
namespace HCP ;
6
6
7
+ use HCP \Config \AppConfig ;
8
+ use HCP \Http \Request ;
9
+ use HCP \Http \Response ;
10
+ use HCP \Plugin \PluginManager ;
7
11
use HCP \Util ;
8
12
9
13
class Init
10
14
{
11
- private $ theme ;
15
+ private object $ theme ;
16
+ private Request $ request ;
17
+ private Response $ response ;
18
+ private AppConfig $ config ;
19
+ private PluginManager $ pluginManager ;
12
20
13
21
public function __construct (object $ g )
14
22
{
15
23
elog (__METHOD__ );
16
24
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
+
17
37
$ this ->initializeSession ($ g );
38
+ Theme::setGlobal ($ g );
39
+
40
+ if (!empty ($ g ->in ['t ' ])) {
41
+ Theme::setTheme ($ g ->in ['t ' ]);
42
+ }
18
43
$ this ->loadTheme ($ g );
19
- $ this ->processPlugin ($ g );
20
- $ this ->processOutput ($ g );
21
44
}
22
45
23
46
private function initializeSession (object $ g ): void
@@ -26,13 +49,10 @@ private function initializeSession(object $g): void
26
49
27
50
session_start ();
28
51
29
- //elog(var_export($_SESSION, true));
30
- //$_SESSION = [];
31
-
32
52
$ g ->cfg ['host ' ] ??= getenv ('HOSTNAME ' );
33
53
Util::cfg ($ g );
34
54
$ 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 ' ) );
36
56
37
57
if (!isset ($ _SESSION ['c ' ])) {
38
58
$ _SESSION ['c ' ] = Util::random_token (32 );
@@ -57,25 +77,24 @@ private function loadTheme(object $g): void
57
77
if (class_exists ($ viewClass )) {
58
78
$ this ->theme = new $ viewClass ($ g );
59
79
} else {
60
- // Fallback to default Theme
61
- $ this ->theme = new Theme ( $ g );
80
+ // Fallback to TopNav theme
81
+ $ this ->theme = Theme:: getTheme ( );
62
82
}
63
83
64
84
// Assign theme instance to g->t for access in plugins
65
85
$ g ->t = $ this ->theme ;
66
86
}
67
87
68
- private function processPlugin (object $ g ): void
88
+ private function processRequest (object $ g ): void
69
89
{
70
90
elog (__METHOD__ );
71
91
72
- $ pluginClass = "HCP \\Plugins \\{$ g ->in ['o ' ]}\\Model " ;
92
+ $ pluginName = $ g ->in ['o ' ];
93
+ $ plugin = $ this ->pluginManager ->loadPlugin ($ pluginName , $ this ->theme );
73
94
74
- elog (__METHOD__ . " pluginClass= $ pluginClass " );
75
-
76
- if (class_exists ($ pluginClass )) {
95
+ if ($ plugin ) {
77
96
$ g ->in ['a ' ] ? Util::chkapi ($ g ) : Util::remember ($ g );
78
- $ g ->out ['main ' ] = (string ) new $ pluginClass ( $ this -> theme ) ;
97
+ $ g ->out ['main ' ] = (string ) $ plugin ;
79
98
} else {
80
99
$ g ->out ['main ' ] = 'Error: no plugin object! ' ;
81
100
}
@@ -90,43 +109,38 @@ private function processOutput(object $g): void
90
109
$ g ->out [$ k ] = method_exists ($ this ->theme , $ k ) ? $ this ->theme ->{$ k }() : $ v ;
91
110
}
92
111
}
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
+ }
93
133
}
94
134
95
135
public function __destruct ()
96
136
{
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" );
99
139
}
100
140
101
141
public function __toString (): string
102
142
{
103
143
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 ();
131
145
}
132
146
}
0 commit comments