- WIP still on the live module

This commit is contained in:
mcndave committed 2025-11-11 13:35:05 -05:00
1 parent b7cd369c12
commit af4fa89127
7 files changed
+411 -77

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
<?php
namespace Lean\Live\Lib;
class Html
{
public static function nodeToArray(\Dom\Node $node) : array
{
return $list ?? [];
}
}
+88
View File
@@ -0,0 +1,88 @@
<?php
namespace Lean\Live\Middleware;
use DI\Attribute\Inject;
use Lean\Factory\HttpFactoryInterface;
use Psr\Http\{Message\ResponseInterface,
Message\ServerRequestInterface,
Server\MiddlewareInterface,
Server\RequestHandlerInterface};
class RenderPartOf implements MiddlewareInterface
{
#[Inject]
protected HttpFactoryInterface $factory;
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$handling = $handler->handle($request);
if (
in_array($handling->getStatusCode(), [ 200, 201, ], true) &&
str_starts_with($handling->getHeaderLine('content-type'), 'text/html')
) {
# Subject to change
$uiLiveRequest = $request->getHeaderLine("X-Ui-Live");
if ($uiLiveRequest) {
$decoded = json_decode($uiLiveRequest, true);
$dom = \DOM\HTMLDocument::createFromString($handling->getBody()->getContents(), 32);
$list = array_map(fn($e) => $e['name'], $decoded['elements']);
if ($list) {
foreach ($list as $name) {
$node = $dom->querySelector("ui-live[data-name=\"{$name}\"]");
if ($node && !$this->isAlreadyRendering($node, $list)) {
$elements[$name] = $this->renderElement($node);
}
}
}
if ($decoded['includeHead'] ?? false) {
foreach($dom->querySelector("head")->childNodes as $node) {
if ($node instanceof \DOM\HTMLElement) {
$head[] = $dom->saveHTML($node);
}
}
}
$jsonResponse = array_filter([
'head' => $head ?? false,
'elements' => $elements ?? false,
]);
if ($elements) {
return $this->factory->createJsonResponse($jsonResponse, $handling->getStatusCode());
}
else {
return $this->factory->createEmptyResponse();
}
}
}
return $handling;
}
protected function isAlreadyRendering(\DOM\Element $element, array $list) : bool
{
$parent = $element->parentElement;
do {
if (strtolower($parent->tagName) === "ui-live") {
if ($parent->hasAttribute('data-name') && in_array($parent->getAttribute('data-name'), $list)) {
return true;
}
}
} while($parent = $parent->parentElement);
return false;
}
protected function renderElement(\DOM\Element $element) : string
{
return $element->innerHTML;
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace Lean\Live\Picea;
use Picea\Extension\Extension,
Picea\Extension\ExtensionTrait;
use Picea\Compiler\Context;
use Picea\Extension\FunctionExtension;
use Psr\Http\Message\ServerRequestInterface;
class Live implements Extension, FunctionExtension {
use ExtensionTrait;
public function __construct(
protected ServerRequestInterface $request
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { }
public function exportFunctions(): array
{
return [
"live" => [ $this, 'liveElement' ],
];
}
public function liveElement(string $inputName) : void
{
}
}