-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiffyneManager.php
More file actions
209 lines (179 loc) · 6.09 KB
/
Copy pathDiffyneManager.php
File metadata and controls
209 lines (179 loc) · 6.09 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
namespace Diffyne;
use Diffyne\Attributes\Lazy;
use Diffyne\State\ComponentHydrator;
use Diffyne\VirtualDOM\Renderer;
use Illuminate\Contracts\Foundation\Application;
use ReflectionClass;
class DiffyneManager
{
protected Application $app;
protected Renderer $renderer;
protected ComponentHydrator $hydrator;
/**
* Registered component aliases.
*
* @var array<string, string>
*/
protected array $components = [];
public function __construct(Application $app)
{
$this->app = $app;
$this->renderer = $app->make(Renderer::class);
$this->hydrator = $app->make(ComponentHydrator::class);
}
/**
* Register a component alias.
*/
public function component(string $alias, string $class): void
{
$this->components[$alias] = $class;
}
/**
* Mount a component and return its initial HTML.
*
* @param array<string, mixed> $params
*/
public function mount(string $component, array $params = []): string
{
$componentClass = $this->resolveComponent($component);
if (! is_string($componentClass) || ! class_exists($componentClass)) {
throw new \InvalidArgumentException("Component [{$component}] not found.");
}
/** @var class-string $componentClass */
$reflection = new ReflectionClass($componentClass);
$lazyAttributes = $reflection->getAttributes(Lazy::class);
if (! empty($lazyAttributes)) {
return $this->mountLazy($componentClass, $params, $lazyAttributes[0]->newInstance());
}
$instance = $this->hydrator->mount($componentClass, $params);
$rendered = $this->renderer->renderInitial($instance);
return $this->wrapComponent($rendered, $componentClass);
}
/**
* Mount a lazy-loaded component with placeholder.
*
* @param array<string, mixed> $params
*/
protected function mountLazy(string $componentClass, array $params, Lazy $lazyAttr): string
{
$id = 'diffyne-'.\Illuminate\Support\Str::random(16);
$paramsJson = json_encode($params);
$paramsEncoded = htmlspecialchars($paramsJson !== false ? $paramsJson : '{}', ENT_QUOTES, 'UTF-8');
// For nested components, use the full path as component name
$namespace = config('diffyne.component_namespace', 'App\\Diffyne');
$componentName = str_replace($namespace.'\\', '', $componentClass);
$componentName = str_replace('\\', '/', $componentName);
// Default placeholder
$placeholder = $lazyAttr->placeholder ?? $this->getDefaultPlaceholder();
return <<<HTML
<div
diff:id="{$id}"
diff:class="{$componentClass}"
diff:name="{$componentName}"
diff:params="{$paramsEncoded}"
data-diffyne-lazy
data-diffyne-component
>
{$placeholder}
</div>
HTML;
}
/**
* Get default lazy loading placeholder.
*/
protected function getDefaultPlaceholder(): string
{
return <<<'HTML'
<div class="diffyne-lazy-placeholder" style="padding: 2rem; text-align: center; color: #666;">
<div class="diffyne-spinner" style="display: inline-block; width: 24px; height: 24px; border: 3px solid #f3f3f3; border-top: 3px solid #3498db; border-radius: 50%; animation: diffyne-spin 1s linear infinite;"></div>
<style>
@keyframes diffyne-spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</div>
HTML;
}
/**
* Resolve component class from alias or fully qualified name.
*/
protected function resolveComponent(string $component): ?string
{
// Check registered aliases
if (isset($this->components[$component])) {
return $this->components[$component];
}
// Check if it's a fully qualified class name
if (class_exists($component)) {
return $component;
}
// Try to find in default namespace (support nested paths with forward slashes)
$defaultNamespace = config('diffyne.component_namespace', 'App\\Diffyne');
$componentPath = str_replace('/', '\\', $component);
$fullClass = $defaultNamespace.'\\'.$componentPath;
if (class_exists($fullClass)) {
return $fullClass;
}
return null;
}
/**
* Wrap rendered component in container div with metadata.
*
* @param array<string, mixed> $rendered
*/
protected function wrapComponent(array $rendered, string $componentClass): string
{
$id = $rendered['id'];
$html = $rendered['html'];
$stateJson = json_encode($rendered['state']);
$state = htmlspecialchars($stateJson !== false ? $stateJson : '{}', ENT_QUOTES, 'UTF-8');
$fingerprint = $rendered['fingerprint'];
$signature = $rendered['signature'];
$eventListenersJson = json_encode($rendered['eventListeners'] ?? []);
$eventListeners = htmlspecialchars($eventListenersJson !== false ? $eventListenersJson : '[]', ENT_QUOTES, 'UTF-8');
// For nested components, use the full path as component name
$namespace = config('diffyne.component_namespace', 'App\\Diffyne');
$componentName = str_replace($namespace.'\\', '', $componentClass);
$componentName = str_replace('\\', '/', $componentName);
return <<<HTML
<div
diff:id="{$id}"
diff:class="{$componentClass}"
diff:name="{$componentName}"
diff:state="{$state}"
diff:fingerprint="{$fingerprint}"
diff:listeners="{$eventListeners}"
diff:signature="{$signature}"
data-diffyne-component
data-diffyne-loaded
>
{$html}
</div>
HTML;
}
/**
* Get the renderer instance.
*/
public function getRenderer(): Renderer
{
return $this->renderer;
}
/**
* Get the hydrator instance.
*/
public function getHydrator(): ComponentHydrator
{
return $this->hydrator;
}
/**
* Get all registered components.
*
* @return array<string, string>
*/
public function getComponents(): array
{
return $this->components;
}
}