Aplikasi Keuangan yang mengatur lalu lintas transaksi antara bank dan aplikasi internal Kampus
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

87 lines
3.3 KiB

<?php
namespace App\UI\Actions;
use App\Attributes\LayoutData;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
class ApplyLayoutMetadata
{
public function __construct(
protected ResolveDynamicText $textResolver
) {}
public function execute(LayoutData $attributeInstance, array|object $dataContext): void
{
$contextTarget = $attributeInstance->context;
// Use data_get to extract nested context if dot-notation is used (e.g., 'project.owner')
// Falls back to the entire data context if the target path isn't found or is null.
$resolvedContext = ! empty($contextTarget)
? data_get($dataContext, $contextTarget, $dataContext)
: $dataContext;
$breadcrumbs = [];
foreach ($attributeInstance->breadcrumbs as $labelKey => $routeConfig) {
// 1. Resolve the text label using our resolved nested context
$label = $this->textResolver->execute($labelKey, $resolvedContext, $contextTarget);
$url = null;
if (! empty($routeConfig)) {
$routeName = '';
$routeParams = [];
// SCENARIO A: Route configuration has parameters array: ["route.name", ["id" => "{user.id}"]]
if (is_array($routeConfig)) {
$routeName = $routeConfig[0] ?? '';
$rawParams = $routeConfig[1] ?? [];
foreach ($rawParams as $paramKey => $paramValue) {
// Pass the nested context down to your string resolver
$resolvedValue = $this->textResolver->execute($paramValue, $resolvedContext, $contextTarget);
$routeParams[$paramKey] = $resolvedValue;
}
}
// SCENARIO B: Route configuration is a simple flat string: "identity.dashboard"
elseif (is_string($routeConfig)) {
$routeName = $routeConfig;
}
// 2. Compile URL safely via route name if it exists in Laravel's routing registry
if (! empty($routeName) && Route::has($routeName)) {
$url = route($routeName, $routeParams);
} else {
// Fallback to treat as a raw URI link path if it's not a registered route name
$url = ! empty($routeName) ? url($routeName) : null;
}
}
$breadcrumbs[] = [
'label' => $label,
'url' => $url,
];
}
// 3. Resolve layout headline text patterns using our nested context
$header = null;
if ($attributeInstance->header) {
$header = $this->textResolver->execute($attributeInstance->header, $resolvedContext, $contextTarget);
}
if (empty($header) && ! empty($breadcrumbs)) {
$lastCrumb = end($breadcrumbs);
$header = $lastCrumb['label'] ?? '';
}
// 4. Inject properties into the layout view instance memory
View::composer([
'components.layouts.nav.topbar',
'components.layouts.guest',
], fn ($view) => $view->with([
'breadcrumbs' => $breadcrumbs,
'header' => $header ?? config('app.name', 'Antigravity App'),
]));
}
}