@php $diff = $getState() ?? ['summary' => [], 'added' => [], 'removed' => [], 'changed' => []]; $summary = $diff['summary'] ?? []; $policyType = $diff['policy_type'] ?? null; $groupByBlock = static function (array $items): array { $groups = []; foreach ($items as $path => $value) { if (! is_string($path) || $path === '') { continue; } $parts = explode(' > ', $path, 2); if (count($parts) === 2) { [$group, $label] = $parts; } else { $group = 'Other'; $label = $path; } $groups[$group][$label] = $value; } ksort($groups); return $groups; }; $stringify = static function (mixed $value): string { if ($value === null) { return '—'; } if (is_bool($value)) { return $value ? 'Enabled' : 'Disabled'; } if (is_scalar($value)) { return (string) $value; } return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ?: ''; }; $isExpandable = static function (mixed $value): bool { if (is_array($value)) { return true; } return is_string($value) && strlen($value) > 160; }; $isScriptKey = static function (mixed $name): bool { return in_array((string) $name, ['scriptContent', 'detectionScriptContent', 'remediationScriptContent'], true); }; $canHighlightScripts = static function (?string $policyType): bool { return (bool) config('tenantpilot.display.show_script_content', false) && in_array($policyType, ['deviceManagementScript', 'deviceShellScript', 'deviceHealthScript'], true); }; $selectGrammar = static function (?string $policyType, string $code): string { if ($policyType === 'deviceShellScript') { $firstLine = strtok($code, "\n") ?: ''; $shebang = trim($firstLine); if (str_starts_with($shebang, '#!')) { if (str_contains($shebang, 'zsh')) { return 'zsh'; } if (str_contains($shebang, 'bash')) { return 'bash'; } return 'sh'; } return 'sh'; } return 'powershell'; }; $highlight = static function (?string $policyType, string $code, string $fallbackClass = '') use ($selectGrammar): ?string { if (! class_exists(\Torchlight\Engine\Engine::class)) { return null; } try { return (new \Torchlight\Engine\Engine())->codeToHtml( code: $code, grammar: $selectGrammar($policyType, $code), theme: [ 'light' => 'github-light', 'dark' => 'github-dark', ], withGutter: false, withWrapper: true, ); } catch (\Throwable $e) { return null; } }; $highlightInline = static function (?string $policyType, string $code) use ($selectGrammar): ?string { if (! class_exists(\Torchlight\Engine\Engine::class)) { return null; } if ($code === '') { return ''; } try { $html = (new \Torchlight\Engine\Engine())->codeToHtml( code: $code, grammar: $selectGrammar($policyType, $code), theme: [ 'light' => 'github-light', 'dark' => 'github-dark', ], withGutter: false, withWrapper: false, ); $html = (string) preg_replace('//', '', $html); if (! preg_match('/]*>.*?<\\/code>/s', $html, $matches)) { return null; } return trim((string) ($matches[0] ?? '')); } catch (\Throwable $e) { return null; } }; $splitLines = static function (string $text): array { $text = str_replace(["\r\n", "\r"], "\n", $text); return $text === '' ? [] : explode("\n", $text); }; $myersLineDiff = static function (array $a, array $b): array { $n = count($a); $m = count($b); $max = $n + $m; $v = [1 => 0]; $trace = []; for ($d = 0; $d <= $max; $d++) { $trace[$d] = $v; for ($k = -$d; $k <= $d; $k += 2) { $kPlus = $v[$k + 1] ?? 0; $kMinus = $v[$k - 1] ?? 0; if ($k === -$d || ($k !== $d && $kMinus < $kPlus)) { $x = $kPlus; } else { $x = $kMinus + 1; } $y = $x - $k; while ($x < $n && $y < $m && $a[$x] === $b[$y]) { $x++; $y++; } $v[$k] = $x; if ($x >= $n && $y >= $m) { break 2; } } } $ops = []; $x = $n; $y = $m; for ($d = count($trace) - 1; $d >= 0; $d--) { $v = $trace[$d]; $k = $x - $y; $kPlus = $v[$k + 1] ?? 0; $kMinus = $v[$k - 1] ?? 0; if ($k === -$d || ($k !== $d && $kMinus < $kPlus)) { $prevK = $k + 1; } else { $prevK = $k - 1; } $prevX = $v[$prevK] ?? 0; $prevY = $prevX - $prevK; while ($x > $prevX && $y > $prevY) { $ops[] = ['type' => 'equal', 'line' => $a[$x - 1]]; $x--; $y--; } if ($d === 0) { break; } if ($x === $prevX) { $ops[] = ['type' => 'insert', 'line' => $b[$y - 1] ?? '']; $y--; } else { $ops[] = ['type' => 'delete', 'line' => $a[$x - 1] ?? '']; $x--; } } return array_reverse($ops); }; $scriptLineDiff = static function (string $fromText, string $toText) use ($splitLines, $myersLineDiff): array { return $myersLineDiff($splitLines($fromText), $splitLines($toText)); }; @endphp
{{ (int) ($summary['added'] ?? 0) }} added {{ (int) ($summary['removed'] ?? 0) }} removed {{ (int) ($summary['changed'] ?? 0) }} changed
@foreach (['changed' => ['label' => 'Changed', 'collapsed' => false], 'added' => ['label' => 'Added', 'collapsed' => true], 'removed' => ['label' => 'Removed', 'collapsed' => true]] as $key => $meta) @php $items = $diff[$key] ?? []; $groups = $groupByBlock(is_array($items) ? $items : []); @endphp @if ($groups !== [])
@foreach ($groups as $group => $groupItems)
{{ $group }}
{{ count($groupItems) }}
@foreach ($groupItems as $name => $value)
@if ($key === 'changed' && is_array($value) && array_key_exists('from', $value) && array_key_exists('to', $value)) @php $from = $value['from']; $to = $value['to']; $fromText = $stringify($from); $toText = $stringify($to); $isScriptContent = $canHighlightScripts($policyType) && $isScriptKey($name); $ops = $isScriptContent ? $scriptLineDiff((string) $fromText, (string) $toText) : []; $useTorchlight = $isScriptContent && class_exists(\Torchlight\Engine\Engine::class); $rows = []; if ($isScriptContent) { $count = count($ops); for ($i = 0; $i < $count; $i++) { $op = $ops[$i]; $next = $ops[$i + 1] ?? null; $type = $op['type'] ?? null; $line = (string) ($op['line'] ?? ''); if ($type === 'equal') { $rows[] = [ 'left' => ['type' => 'equal', 'line' => $line], 'right' => ['type' => 'equal', 'line' => $line], ]; continue; } if ($type === 'delete' && is_array($next) && ($next['type'] ?? null) === 'insert') { $rows[] = [ 'left' => ['type' => 'delete', 'line' => $line], 'right' => ['type' => 'insert', 'line' => (string) ($next['line'] ?? '')], ]; $i++; continue; } if ($type === 'delete') { $rows[] = [ 'left' => ['type' => 'delete', 'line' => $line], 'right' => ['type' => 'blank', 'line' => ''], ]; continue; } if ($type === 'insert') { $rows[] = [ 'left' => ['type' => 'blank', 'line' => ''], 'right' => ['type' => 'insert', 'line' => $line], ]; continue; } } } @endphp
{{ (string) $name }}
@if ($isScriptContent)
Script
View
Diff Before After ⤢ Fullscreen
Old
@php
foreach ($rows as $row) {
    $left = $row['left'];
    $leftType = $left['type'];
    $leftLine = (string) ($left['line'] ?? '');

    $leftHighlighted = $useTorchlight ? $highlightInline($policyType, $leftLine) : null;
    $leftRendered = (is_string($leftHighlighted) && $leftHighlighted !== '') ? $leftHighlighted : e($leftLine);

    if ($leftType === 'equal') {
        if ($useTorchlight) {
            @endphp
            @once
                @include('filament.partials.torchlight-dark-overrides')
                
            @endonce
            @php
        }

        echo ''.$leftRendered."\n";
        continue;
    }

    if ($leftType === 'delete') {
        if ($useTorchlight) {
            @endphp
            @once
                @include('filament.partials.torchlight-dark-overrides')
                
            @endonce
            @php
        }

        echo '- '.$leftRendered."\n";
        continue;
    }

    echo "\n";
}
@endphp
New
@php
foreach ($rows as $row) {
    $right = $row['right'];
    $rightType = $right['type'];
    $rightLine = (string) ($right['line'] ?? '');

    $rightHighlighted = $useTorchlight ? $highlightInline($policyType, $rightLine) : null;
    $rightRendered = (is_string($rightHighlighted) && $rightHighlighted !== '') ? $rightHighlighted : e($rightLine);

    if ($rightType === 'equal') {
        if ($useTorchlight) {
            @endphp
            @once
                @include('filament.partials.torchlight-dark-overrides')
                
            @endonce
            @php
        }

        echo ''.$rightRendered."\n";
        continue;
    }

    if ($rightType === 'insert') {
        if ($useTorchlight) {
            @endphp
            @once
                @include('filament.partials.torchlight-dark-overrides')
                
            @endonce
            @php
        }

        echo '+ '.$rightRendered."\n";
        continue;
    }

    echo "\n";
}
@endphp
Before
@php $highlightedBefore = $useTorchlight ? $highlight($policyType, (string) $fromText) : null; @endphp @if (is_string($highlightedBefore) && $highlightedBefore !== '') @once @include('filament.partials.torchlight-dark-overrides') @endonce
{!! $highlightedBefore !!}
@else
{{ (string) $fromText }}
@endif
After
@php $highlightedAfter = $useTorchlight ? $highlight($policyType, (string) $toText) : null; @endphp @if (is_string($highlightedAfter) && $highlightedAfter !== '') @once @include('filament.partials.torchlight-dark-overrides') @endonce
{!! $highlightedAfter !!}
@else
{{ (string) $toText }}
@endif
Script diff
Close
Diff Before After
Old
@php
foreach ($rows as $row) {
    $left = $row['left'];
    $leftType = $left['type'];
    $leftLine = (string) ($left['line'] ?? '');

    $leftHighlighted = $useTorchlight ? $highlightInline($policyType, $leftLine) : null;
    $leftRendered = (is_string($leftHighlighted) && $leftHighlighted !== '') ? $leftHighlighted : e($leftLine);

    if ($leftType === 'equal') {
        if ($useTorchlight) {
            @endphp
            @once
                @include('filament.partials.torchlight-dark-overrides')
                
            @endonce
            @php
        }

        echo ''.$leftRendered."\n";
        continue;
    }

    if ($leftType === 'delete') {
        if ($useTorchlight) {
            @endphp
            @once
                @include('filament.partials.torchlight-dark-overrides')
                
            @endonce
            @php
        }

        echo '- '.$leftRendered."\n";
        continue;
    }

    echo "\n";
}
@endphp
New
@php
foreach ($rows as $row) {
    $right = $row['right'];
    $rightType = $right['type'];
    $rightLine = (string) ($right['line'] ?? '');

    $rightHighlighted = $useTorchlight ? $highlightInline($policyType, $rightLine) : null;
    $rightRendered = (is_string($rightHighlighted) && $rightHighlighted !== '') ? $rightHighlighted : e($rightLine);

    if ($rightType === 'equal') {
        if ($useTorchlight) {
            @endphp
            @once
                @include('filament.partials.torchlight-dark-overrides')
                
            @endonce
            @php
        }

        echo ''.$rightRendered."\n";
        continue;
    }

    if ($rightType === 'insert') {
        if ($useTorchlight) {
            @endphp
            @once
                @include('filament.partials.torchlight-dark-overrides')
                
            @endonce
            @php
        }

        echo '+ '.$rightRendered."\n";
        continue;
    }

    echo "\n";
}
@endphp
Before
@php $highlightedBeforeFullscreen = $useTorchlight ? $highlight($policyType, (string) $fromText) : null; @endphp @if (is_string($highlightedBeforeFullscreen) && $highlightedBeforeFullscreen !== '') @once @include('filament.partials.torchlight-dark-overrides') @endonce
{!! $highlightedBeforeFullscreen !!}
@else
{{ (string) $fromText }}
@endif
After
@php $highlightedAfterFullscreen = $useTorchlight ? $highlight($policyType, (string) $toText) : null; @endphp @if (is_string($highlightedAfterFullscreen) && $highlightedAfterFullscreen !== '') @once @include('filament.partials.torchlight-dark-overrides') @endonce
{!! $highlightedAfterFullscreen !!}
@else
{{ (string) $toText }}
@endif
@else
From @if ($isExpandable($from))
View
{{ $fromText }}
@else
{{ $fromText }}
@endif
To @if ($isExpandable($to))
View
{{ $toText }}
@else
{{ $toText }}
@endif
@endif
@else @php $text = $stringify($value); @endphp
{{ (string) $name }}
@if ($isExpandable($value))
View @php $isScriptContent = $canHighlightScripts($policyType) && $isScriptKey($name); $highlighted = $isScriptContent ? $highlight($policyType, (string) $text) : null; @endphp @if (is_string($highlighted) && $highlighted !== '') @once @include('filament.partials.torchlight-dark-overrides') @endonce
{!! $highlighted !!}
@else
{{ $text }}
@endif
@else
{{ $text }}
@endif
@endif
@endforeach
@endforeach
@endif @endforeach