| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Contracts; |
| 4: | |
| 5: | use Illuminate\Http\Request; |
| 6: | use LaravelUi5\Core\Middleware\ResolveUi5Context; |
| 7: | use LaravelUi5\Core\Ui5\Contracts\Ui5ArtifactInterface; |
| 8: | |
| 9: | /** |
| 10: | * Represents the runtime context of a UI5 artifact request. |
| 11: | * |
| 12: | * This context encapsulates all scoped information (tenant, partner, artifact, |
| 13: | * request, locale) required to evaluate settings, resolve permissions, and |
| 14: | * execute providers consistently. |
| 15: | * |
| 16: | * **Scope** |
| 17: | * - *HTTP requests*: Constructed by middleware from the current Request/Route. |
| 18: | * - *Console/Queue/Test*: Can be created manually without a Request |
| 19: | * (Request is nullable). Useful for jobs, batch reports, migrations, or tests. |
| 20: | * |
| 21: | * **Guidelines** |
| 22: | * - Do not directly depend on $request being non-null in providers. |
| 23: | * - Always access tenant/partner/artifact/locale via this context, not via globals. |
| 24: | * - Treat as immutable: one Ui5Context per logical execution. |
| 25: | * |
| 26: | * Access via dependency injection or the service container: |
| 27: | * |
| 28: | * <code> |
| 29: | * $context = app(Ui5Context::class); |
| 30: | * </code> |
| 31: | * |
| 32: | * @see ResolveUi5Context |
| 33: | */ |
| 34: | final readonly class Ui5Context |
| 35: | { |
| 36: | /** |
| 37: | * @param Request|null $request The current HTTP request (null if running in console) |
| 38: | * @param Ui5ArtifactInterface $artifact The current UI5 artifact (App, Card, Report) |
| 39: | * @param TenantInterface|null $tenant The tenant the app is running in (if applicable) |
| 40: | * @param BusinessPartnerInterface|null $partner The acting business partner (can be impersonated) |
| 41: | * @param BusinessPartnerInterface|null $authPartner The originally authenticated partner (if different) |
| 42: | * @param string|null $locale Effective locale (overrides tenant->getLocale() if provided) |
| 43: | */ |
| 44: | public function __construct( |
| 45: | public ?Request $request = null, |
| 46: | public Ui5ArtifactInterface $artifact, |
| 47: | public ?TenantInterface $tenant = null, |
| 48: | public ?BusinessPartnerInterface $partner = null, |
| 49: | public ?BusinessPartnerInterface $authPartner = null, |
| 50: | public ?string $locale = null, |
| 51: | ) |
| 52: | { |
| 53: | } |
| 54: | |
| 55: | /** |
| 56: | * Effective locale, falling back to tenant->getLocale() if not explicitly set. |
| 57: | * |
| 58: | * @return string|null |
| 59: | */ |
| 60: | public function effectiveLocale(): ?string |
| 61: | { |
| 62: | return $this->locale ?? $this->tenant?->getLocale(); |
| 63: | } |
| 64: | } |
| 65: |