Files
lucent-laravel/src/Svelte/Svelte.php
T

59 lines
1.6 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?php
namespace Lucent\Svelte;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
2023-10-17 22:57:25 +03:00
use Lucent\Account\AccountService;
2026-04-20 21:07:35 +03:00
use Lucent\Account\AuthService;
2023-10-02 23:10:49 +03:00
use Lucent\Channel\ChannelService;
2026-04-20 21:07:35 +03:00
class Svelte
2023-10-02 23:10:49 +03:00
{
public function __construct(
2023-10-17 22:57:25 +03:00
public ChannelService $channelService,
2026-04-20 21:07:35 +03:00
public AccountService $accountService,
public AuthService $authService,
) {}
function render(
string $layout,
string $view,
string $title = "",
mixed $data = [],
): View|Factory {
2023-10-02 23:10:49 +03:00
$context = [];
2026-04-20 21:07:35 +03:00
$context["user"] = toArray($this->authService->getCurrentUser());
2023-10-02 23:10:49 +03:00
$context["view"] = $view;
$context["layout"] = $layout;
$context["title"] = $title;
$context["data"] = $data;
$context["channel"] = $this->channelService->channel;
2026-04-20 21:07:35 +03:00
$context[
"readableSchemas"
] = $this->accountService->currentReadableSchemas();
2023-10-02 23:10:49 +03:00
$json = json_encode($context);
2026-04-20 21:07:35 +03:00
$divTag = sprintf(
'<div class="lucent-component" data-layout="%s"></div>',
$layout,
);
$jsonTag = sprintf(
'<script type="application/json" id="json-%s">%s</script>',
$layout,
$json,
);
2023-10-02 23:10:49 +03:00
$svelte = $divTag . $jsonTag;
2026-04-20 21:07:35 +03:00
return view("lucent::svelte", [
"svelte" => $svelte,
"view" => $view,
"data" => $data,
"title" => $title,
"layout" => $layout,
"channel" => $this->channelService->channel,
2023-10-02 23:10:49 +03:00
]);
}
}