10-04-2026
This commit is contained in:
parent
4d6b4930b2
commit
4bb89aad8c
836 changed files with 52961 additions and 5950 deletions
197
app/Services/CmsFluxEditorHtmlTransformer.php
Normal file
197
app/Services/CmsFluxEditorHtmlTransformer.php
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMNode;
|
||||
use DOMXPath;
|
||||
|
||||
/**
|
||||
* Der Flux-Editor (TipTap) nutzt für Hervorhebungen das Element `mark` (Highlight-Extension).
|
||||
* Im Frontend wird derselbe optische Zweck oft mit `span.text-secondary` umgesetzt.
|
||||
* Diese Klasse wandelt beim Laden in den Editor und beim Speichern zwischen beiden Formen um.
|
||||
*/
|
||||
final class CmsFluxEditorHtmlTransformer
|
||||
{
|
||||
/**
|
||||
* JSON-Felder, die im CMS-Modal mit flux:editor (Rich-Text) bearbeitet werden.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
public const RICH_TEXT_JSON_FIELDS = [
|
||||
'description',
|
||||
'text',
|
||||
'content',
|
||||
'help',
|
||||
'answer',
|
||||
'quote',
|
||||
];
|
||||
|
||||
public static function toEditor(string $html): string
|
||||
{
|
||||
if ($html === '' || ! str_contains($html, 'text-secondary')) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
return self::spansWithClassToMarks($html);
|
||||
}
|
||||
|
||||
public static function fromEditor(string $html): string
|
||||
{
|
||||
if ($html === '' || ! str_contains($html, '<mark')) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
return self::marksToSecondarySpans($html);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>|array{_value: string}> $items
|
||||
* @return array<int, array<string, mixed>|array{_value: string}>
|
||||
*/
|
||||
public static function toEditorJsonItems(array $items, bool $isStringArray): array
|
||||
{
|
||||
if ($isStringArray) {
|
||||
return $items;
|
||||
}
|
||||
|
||||
return array_map(function ($item) {
|
||||
if (! is_array($item)) {
|
||||
return $item;
|
||||
}
|
||||
$out = [];
|
||||
foreach ($item as $key => $value) {
|
||||
if (in_array($key, self::RICH_TEXT_JSON_FIELDS, true) && is_string($value)) {
|
||||
$out[$key] = self::toEditor($value);
|
||||
} else {
|
||||
$out[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}, $items);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>|array{_value: string}> $items
|
||||
* @return array<int, array<string, mixed>|array{_value: string}>
|
||||
*/
|
||||
public static function fromEditorJsonItems(array $items, bool $isStringArray): array
|
||||
{
|
||||
if ($isStringArray) {
|
||||
return $items;
|
||||
}
|
||||
|
||||
return array_map(function ($item) {
|
||||
if (! is_array($item)) {
|
||||
return $item;
|
||||
}
|
||||
$out = [];
|
||||
foreach ($item as $key => $value) {
|
||||
if (in_array($key, self::RICH_TEXT_JSON_FIELDS, true) && is_string($value)) {
|
||||
$out[$key] = self::fromEditor($value);
|
||||
} else {
|
||||
$out[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}, $items);
|
||||
}
|
||||
|
||||
private static function spansWithClassToMarks(string $html): string
|
||||
{
|
||||
libxml_use_internal_errors(true);
|
||||
$dom = new DOMDocument;
|
||||
$wrapped = '<?xml encoding="utf-8"?><div id="cms-flux-root">'.$html.'</div>';
|
||||
$dom->loadHTML($wrapped, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
||||
libxml_clear_errors();
|
||||
|
||||
$root = $dom->getElementById('cms-flux-root');
|
||||
if (! $root instanceof DOMElement) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$xpath = new DOMXPath($dom);
|
||||
$expression = '//span[contains(concat(" ", normalize-space(@class), " "), " text-secondary ")]';
|
||||
$nodes = [];
|
||||
foreach ($xpath->query($expression) ?? [] as $node) {
|
||||
$nodes[] = $node;
|
||||
}
|
||||
|
||||
usort($nodes, fn ($a, $b) => self::nodeDepth($b) <=> self::nodeDepth($a));
|
||||
|
||||
foreach ($nodes as $span) {
|
||||
if (! $span instanceof DOMElement || $span->tagName !== 'span') {
|
||||
continue;
|
||||
}
|
||||
$mark = $dom->createElement('mark');
|
||||
while ($span->firstChild) {
|
||||
$mark->appendChild($span->firstChild);
|
||||
}
|
||||
$span->parentNode?->replaceChild($mark, $span);
|
||||
}
|
||||
|
||||
return self::extractInnerHtml($dom, $root);
|
||||
}
|
||||
|
||||
private static function marksToSecondarySpans(string $html): string
|
||||
{
|
||||
libxml_use_internal_errors(true);
|
||||
$dom = new DOMDocument;
|
||||
$wrapped = '<?xml encoding="utf-8"?><div id="cms-flux-root">'.$html.'</div>';
|
||||
$dom->loadHTML($wrapped, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
||||
libxml_clear_errors();
|
||||
|
||||
$root = $dom->getElementById('cms-flux-root');
|
||||
if (! $root instanceof DOMElement) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$xpath = new DOMXPath($dom);
|
||||
$nodes = [];
|
||||
foreach ($xpath->query('//mark') ?? [] as $node) {
|
||||
$nodes[] = $node;
|
||||
}
|
||||
|
||||
usort($nodes, fn ($a, $b) => self::nodeDepth($b) <=> self::nodeDepth($a));
|
||||
|
||||
foreach ($nodes as $mark) {
|
||||
if (! $mark instanceof DOMElement) {
|
||||
continue;
|
||||
}
|
||||
$span = $dom->createElement('span');
|
||||
$span->setAttribute('class', 'text-secondary');
|
||||
while ($mark->firstChild) {
|
||||
$span->appendChild($mark->firstChild);
|
||||
}
|
||||
$mark->parentNode?->replaceChild($span, $mark);
|
||||
}
|
||||
|
||||
return self::extractInnerHtml($dom, $root);
|
||||
}
|
||||
|
||||
private static function nodeDepth(DOMNode $node): int
|
||||
{
|
||||
$d = 0;
|
||||
while ($node->parentNode) {
|
||||
$d++;
|
||||
$node = $node->parentNode;
|
||||
}
|
||||
|
||||
return $d;
|
||||
}
|
||||
|
||||
private static function extractInnerHtml(DOMDocument $dom, DOMElement $root): string
|
||||
{
|
||||
$html = '';
|
||||
foreach ($root->childNodes as $child) {
|
||||
$html .= $dom->saveHTML($child);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
119
app/Services/DisplayMediaService.php
Normal file
119
app/Services/DisplayMediaService.php
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\DisplayMedia;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class DisplayMediaService
|
||||
{
|
||||
/**
|
||||
* Store an uploaded file and create a DisplayMedia record.
|
||||
*/
|
||||
public function storeUpload(UploadedFile $file, ?string $collection = null): DisplayMedia
|
||||
{
|
||||
$filename = $file->getClientOriginalName();
|
||||
$extension = strtolower($file->getClientOriginalExtension());
|
||||
$storageName = Str::uuid().'.'.$extension;
|
||||
$datePath = now()->format('Y/m');
|
||||
$relativePath = "display-media/{$datePath}/{$storageName}";
|
||||
|
||||
Storage::disk('public')->putFileAs("display-media/{$datePath}", $file, $storageName);
|
||||
|
||||
$type = in_array($extension, ['mp4', 'webm', 'mov']) ? 'video' : 'image';
|
||||
|
||||
$metadata = [];
|
||||
if ($type === 'image') {
|
||||
$dimensions = @getimagesize($file->getRealPath());
|
||||
if ($dimensions) {
|
||||
$metadata['width'] = $dimensions[0];
|
||||
$metadata['height'] = $dimensions[1];
|
||||
}
|
||||
}
|
||||
|
||||
return DisplayMedia::create([
|
||||
'filename' => $filename,
|
||||
'disk' => 'public',
|
||||
'path' => $relativePath,
|
||||
'source_type' => 'upload',
|
||||
'type' => $type,
|
||||
'mime_type' => $file->getMimeType(),
|
||||
'file_size' => $file->getSize(),
|
||||
'collection' => $collection,
|
||||
'metadata' => ! empty($metadata) ? $metadata : null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a DisplayMedia record from an external URL.
|
||||
*/
|
||||
public function createFromUrl(string $url, string $type = 'video', ?string $title = null, ?string $collection = null): DisplayMedia
|
||||
{
|
||||
$filename = $title ?: $this->extractFilenameFromUrl($url);
|
||||
|
||||
return DisplayMedia::create([
|
||||
'filename' => $filename,
|
||||
'disk' => 'public',
|
||||
'path' => null,
|
||||
'external_url' => $url,
|
||||
'source_type' => 'external',
|
||||
'type' => $type,
|
||||
'mime_type' => null,
|
||||
'file_size' => 0,
|
||||
'title' => $title,
|
||||
'collection' => $collection,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that an external URL is accessible.
|
||||
*/
|
||||
public function validateExternalUrl(string $url): bool
|
||||
{
|
||||
try {
|
||||
$response = Http::timeout(10)
|
||||
->withOptions(['allow_redirects' => true])
|
||||
->head($url);
|
||||
|
||||
return $response->successful() || $response->status() === 302 || $response->status() === 301;
|
||||
} catch (\Throwable) {
|
||||
// Some services block HEAD requests, try GET with stream
|
||||
try {
|
||||
$response = Http::timeout(10)
|
||||
->withOptions(['allow_redirects' => true, 'stream' => true])
|
||||
->get($url);
|
||||
|
||||
return $response->successful();
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a DisplayMedia record and its associated files.
|
||||
*/
|
||||
public function delete(DisplayMedia $media): void
|
||||
{
|
||||
if ($media->isUpload() && $media->path) {
|
||||
Storage::disk($media->disk)->delete($media->path);
|
||||
}
|
||||
|
||||
if ($media->thumbnail_path) {
|
||||
Storage::disk($media->disk)->delete($media->thumbnail_path);
|
||||
}
|
||||
|
||||
$media->delete();
|
||||
}
|
||||
|
||||
private function extractFilenameFromUrl(string $url): string
|
||||
{
|
||||
$parsed = parse_url($url, PHP_URL_PATH);
|
||||
$basename = $parsed ? basename($parsed) : 'external-media';
|
||||
|
||||
return Str::limit($basename, 100);
|
||||
}
|
||||
}
|
||||
90
app/Services/ProjectDocumentationContent.php
Normal file
90
app/Services/ProjectDocumentationContent.php
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
use League\CommonMark\Environment\Environment;
|
||||
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
||||
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
|
||||
use League\CommonMark\MarkdownConverter;
|
||||
|
||||
class ProjectDocumentationContent
|
||||
{
|
||||
public static function markdownPath(): string
|
||||
{
|
||||
return base_path('dev/entwicklung.md');
|
||||
}
|
||||
|
||||
public static function html(): string
|
||||
{
|
||||
$mdPath = self::markdownPath();
|
||||
|
||||
if (! file_exists($mdPath)) {
|
||||
return '<p class="text-red-600">Dokumentation nicht gefunden.</p>';
|
||||
}
|
||||
|
||||
$markdown = file_get_contents($mdPath);
|
||||
|
||||
$environment = new Environment([
|
||||
'html_input' => 'allow',
|
||||
'allow_unsafe_links' => false,
|
||||
]);
|
||||
|
||||
$environment->addExtension(new CommonMarkCoreExtension);
|
||||
$environment->addExtension(new GithubFlavoredMarkdownExtension);
|
||||
|
||||
$converter = new MarkdownConverter($environment);
|
||||
|
||||
return $converter->convert($markdown)->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{level: int, title: string, slug: string}>
|
||||
*/
|
||||
public static function tableOfContents(): array
|
||||
{
|
||||
$mdPath = self::markdownPath();
|
||||
|
||||
if (! file_exists($mdPath)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$markdown = file_get_contents($mdPath);
|
||||
$toc = [];
|
||||
|
||||
preg_match_all('/^(#{2,3})\s+(.+)$/m', $markdown, $matches, PREG_SET_ORDER);
|
||||
|
||||
foreach ($matches as $match) {
|
||||
$level = strlen($match[1]);
|
||||
$title = trim($match[2]);
|
||||
$slug = Str::slug($title);
|
||||
|
||||
$toc[] = [
|
||||
'level' => $level,
|
||||
'title' => $title,
|
||||
'slug' => $slug,
|
||||
];
|
||||
}
|
||||
|
||||
return $toc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{size: string, modified: string, lines: int}|null
|
||||
*/
|
||||
public static function fileInfo(): ?array
|
||||
{
|
||||
$mdPath = self::markdownPath();
|
||||
|
||||
if (! file_exists($mdPath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'size' => round(filesize($mdPath) / 1024, 1).' KB',
|
||||
'modified' => Carbon::parse(filemtime($mdPath))->format('d.m.Y H:i'),
|
||||
'lines' => count(file($mdPath)),
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue