julienmonnerie/kirby/src/Panel/Document.php

73 lines
1.8 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
namespace Kirby\Panel;
2022-08-31 15:02:43 +02:00
use Kirby\Cms\App;
2022-06-17 17:51:59 +02:00
use Kirby\Http\Response;
use Kirby\Http\Uri;
use Kirby\Toolkit\Tpl;
use Throwable;
/**
* The Document is used by the View class to render
* the full Panel HTML document in Fiber calls that
* should not return just JSON objects
* @since 3.6.0
*
* @package Kirby Panel
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class Document
{
2022-08-31 15:02:43 +02:00
/**
* Renders the panel document
*/
2022-12-19 14:56:05 +01:00
public static function response(array $fiber): Response
2022-08-31 15:02:43 +02:00
{
2025-04-21 18:57:21 +02:00
$kirby = App::instance();
$assets = new Assets();
2022-08-31 15:02:43 +02:00
// Full HTML response
// @codeCoverageIgnoreStart
try {
2025-04-21 18:57:21 +02:00
if ($assets->link() === true) {
2022-08-31 15:02:43 +02:00
usleep(1);
Response::go($kirby->url('base') . '/' . $kirby->path());
}
} catch (Throwable $e) {
die('The Panel assets cannot be installed properly. ' . $e->getMessage());
}
// @codeCoverageIgnoreEnd
// get the uri object for the panel url
2025-04-21 18:57:21 +02:00
$uri = new Uri($kirby->url('panel'));
2022-08-31 15:02:43 +02:00
// proper response code
$code = $fiber['$view']['code'] ?? 200;
// load the main Panel view template
$body = Tpl::load($kirby->root('kirby') . '/views/panel.php', [
2025-04-21 18:57:21 +02:00
'assets' => $assets->external(),
'icons' => $assets->icons(),
2022-08-31 15:02:43 +02:00
'nonce' => $kirby->nonce(),
'fiber' => $fiber,
'panelUrl' => $uri->path()->toString(true) . '/',
]);
2024-12-20 12:37:52 +01:00
$frameAncestors = $kirby->option('panel.frameAncestors');
$frameAncestors = match (true) {
$frameAncestors === true => "'self'",
is_array($frameAncestors) => "'self' " . implode(' ', $frameAncestors),
is_string($frameAncestors) => $frameAncestors,
default => "'none'"
};
return new Response($body, 'text/html', $code, [
'Content-Security-Policy' => 'frame-ancestors ' . $frameAncestors
]);
2022-08-31 15:02:43 +02:00
}
2022-06-17 17:51:59 +02:00
}