Repository navigation
Expand file tree
/
Copy pathSSRHandler.php
More file actions
183 lines (158 loc) · 4.12 KB
/
Copy pathSSRHandler.php
File metadata and controls
183 lines (158 loc) · 4.12 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace BitApps\BitConnect\SSR;
use BitApps\BitConnect\SSR\Helper\ContextHelper;
use BitApps\BitConnect\SSR\Helper\StateHelper;
use BitApps\BitConnect\SSR\View\ViewManager;
if (!defined('ABSPATH')) {
exit;
}
class SSRHandler
{
private ViewManager $viewManager;
private StateHelper $stateHelper;
private ContextHelper $contextHelper;
private string $interactiveNamespace;
private string $rootElementId;
private array $rootElementAttributes;
public function __construct(
$interactiveNamespace = 'bitConnectStore',
$rootElementId = 'bit-connect-u-root',
$rootElementAttributes = ['data-wp-init' => 'callbacks.postWatcher']
) {
$this->interactiveNamespace = $interactiveNamespace;
$this->rootElementId = $rootElementId;
$this->rootElementAttributes = $rootElementAttributes;
// Initialize ViewManager with configuration
$this->viewManager = ViewManager::getInstance(
null, // Use default SSRView class
$this->interactiveNamespace,
$this->rootElementId,
$this->rootElementAttributes
);
$this->stateHelper = new StateHelper($interactiveNamespace);
$this->contextHelper = new ContextHelper();
}
/**
* Generate view based on route with wp_interactivity state and context.
*
* @param string $route Route to render
* @param array $state Optional state data
* @param array $context Optional context data
* @param array $viewData Optional view data
*
* @return string
*/
public function generateView($route, $state = [], $context = [], $viewData = [])
{
return $this->viewManager->generateView($route, $state, $context, $viewData);
}
/**
* Get the view manager instance.
*
* @return ViewManager
*/
public function getViewManager()
{
return $this->viewManager;
}
/**
* Get the state helper instance.
*
* @return StateHelper
*/
public function getStateHelper()
{
return $this->stateHelper;
}
/**
* Get the context helper instance.
*
* @return ContextHelper
*/
public function getContextHelper()
{
return $this->contextHelper;
}
/**
* Set the interactive namespace.
*
* @param string $namespace
*
* @return self
*/
public function setInteractiveNamespace($namespace)
{
$this->interactiveNamespace = $namespace;
$this->stateHelper = new StateHelper($namespace, 'init');
$this->viewManager = ViewManager::getInstance(
null,
$namespace,
$this->rootElementId,
$this->rootElementAttributes
);
return $this;
}
/**
* Get the interactive namespace.
*
* @return string
*/
public function getInteractiveNamespace()
{
return $this->interactiveNamespace;
}
/**
* Set the root element ID.
*
* @param string $id
*
* @return self
*/
public function setRootElementId($id)
{
$this->rootElementId = $id;
$this->viewManager = ViewManager::getInstance(
null,
$this->interactiveNamespace,
$id,
$this->rootElementAttributes
);
return $this;
}
/**
* Get the root element ID.
*
* @return string
*/
public function getRootElementId()
{
return $this->rootElementId;
}
/**
* Set root element attributes.
*
* @param array $attributes
*
* @return self
*/
public function setRootElementAttributes($attributes)
{
$this->rootElementAttributes = $attributes;
$this->viewManager = ViewManager::getInstance(
null,
$this->interactiveNamespace,
$this->rootElementId,
$attributes
);
return $this;
}
/**
* Get root element attributes.
*
* @return array
*/
public function getRootElementAttributes()
{
return $this->rootElementAttributes;
}
}