|null */ private ?array $pending = null; private bool $active = true; public function __construct( private readonly Application $app, private readonly Router $router, private readonly ExtensionSettingsRegistry $settings, private readonly ExtensionPermissionRegistry $permissions, private readonly ExtensionActionDecorators $actions, private readonly ExtensionConsoleRegistry $console, private readonly ExtensionHeadTags $headTags, private readonly ExtensionFieldRegistry $fields, ) {} public function begin(): void { $this->pending = []; $this->active = false; } /** @param Closure(): void $registration */ public function defer(Closure $registration): void { if ($this->pending === null) { $registration(); return; } $this->pending[] = $registration; } public function commit(string $identifier): void { if ($this->pending === null) { return; } $definition = $this->settings->get($identifier); $permissions = $this->permissions->all()[ExtensionPermissionRegistry::group($identifier)] ?? null; $actions = $this->actions->snapshot(); $console = $this->console->snapshot(); $headTags = $this->headTags->snapshot(); $fields = $this->fields->snapshot(); $router = new ExtensionStagingRouter($this->router); Route::swap($router); $registrations = $this->pending; $this->pending = null; try { foreach ($registrations as $registration) { $registration(); } $router->apply(); $this->active = true; } catch (Throwable $throwable) { $this->settings->unregister($identifier); if ($definition instanceof ExtensionSettingsDefinition) { $this->settings->register($identifier, $definition); } $this->permissions->unregister($identifier); if ($permissions !== null) { $this->permissions->register($identifier, $permissions['description'], $permissions['keys']); } $this->actions->restore($actions); $this->console->restore($console); $this->headTags->restore($headTags); $this->fields->restore($fields); throw $throwable; } finally { Route::swap($this->router); $routes = $this->router->getRoutes(); if ($routes instanceof RouteCollection) { $this->router->setRoutes($routes); } $this->app->instance('routes', $routes); $this->pending = null; } } public function discard(): void { $this->pending = null; $this->active = false; } public function isActive(): bool { return $this->active; } }