Loading core/modules/workspaces/src/WorkspaceManager.php +12 −8 Changes for core/modules/workspaces/src/WorkspaceManager.php: 12 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -153,14 +153,16 @@ public function executeInWorkspace($workspace_id, callable $function) { if ($should_switch_workspace) { $this->doSwitchWorkspace($workspace, TRUE); } $result = $function(); // Switch back if needed. try { return $function(); } finally { // Switch back if needed, even when the callback throws an exception. if ($should_switch_workspace) { $this->doSwitchWorkspace($previous_active_workspace, TRUE); } return $result; } } /** Loading @@ -173,14 +175,16 @@ public function executeOutsideWorkspace(callable $function) { if ($previous_active_workspace) { $this->doSwitchWorkspace(NULL, TRUE); } $result = $function(); // Switch back if needed. try { return $function(); } finally { // Switch back if needed, even when the callback throws an exception. if ($previous_active_workspace) { $this->doSwitchWorkspace($previous_active_workspace, TRUE); } return $result; } } } core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php +29 Changes for core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php: 29 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -891,6 +891,35 @@ function (WorkspaceSwitchEvent $event) use (&$switch_events): void { $switch_events = []; $this->workspaceManager->executeOutsideWorkspace(function (): void {}); $this->assertSame([TRUE, TRUE], $switch_events); // A callback that throws an exception should not change the active // workspace, and the switch back should still be temporary. $switch_events = []; try { $this->workspaceManager->executeOutsideWorkspace(function (): void { throw new \RuntimeException('Failed outside the workspace.'); }); $this->fail('The exception thrown by the callback was not propagated.'); } catch (\RuntimeException $e) { $this->assertSame('Failed outside the workspace.', $e->getMessage()); } $this->assertSame('stage', $this->workspaceManager->getActiveWorkspace()?->id()); $this->assertSame([TRUE, TRUE], $switch_events); $this->workspaceManager->switchToLive(); $switch_events = []; try { $this->workspaceManager->executeInWorkspace('stage', function (): void { throw new \RuntimeException('Failed in the workspace.'); }); $this->fail('The exception thrown by the callback was not propagated.'); } catch (\RuntimeException $e) { $this->assertSame('Failed in the workspace.', $e->getMessage()); } $this->assertFalse($this->workspaceManager->hasActiveWorkspace()); $this->assertSame([TRUE, TRUE], $switch_events); } /** Loading Loading
core/modules/workspaces/src/WorkspaceManager.php +12 −8 Changes for core/modules/workspaces/src/WorkspaceManager.php: 12 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -153,14 +153,16 @@ public function executeInWorkspace($workspace_id, callable $function) { if ($should_switch_workspace) { $this->doSwitchWorkspace($workspace, TRUE); } $result = $function(); // Switch back if needed. try { return $function(); } finally { // Switch back if needed, even when the callback throws an exception. if ($should_switch_workspace) { $this->doSwitchWorkspace($previous_active_workspace, TRUE); } return $result; } } /** Loading @@ -173,14 +175,16 @@ public function executeOutsideWorkspace(callable $function) { if ($previous_active_workspace) { $this->doSwitchWorkspace(NULL, TRUE); } $result = $function(); // Switch back if needed. try { return $function(); } finally { // Switch back if needed, even when the callback throws an exception. if ($previous_active_workspace) { $this->doSwitchWorkspace($previous_active_workspace, TRUE); } return $result; } } }
core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php +29 Changes for core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php: 29 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -891,6 +891,35 @@ function (WorkspaceSwitchEvent $event) use (&$switch_events): void { $switch_events = []; $this->workspaceManager->executeOutsideWorkspace(function (): void {}); $this->assertSame([TRUE, TRUE], $switch_events); // A callback that throws an exception should not change the active // workspace, and the switch back should still be temporary. $switch_events = []; try { $this->workspaceManager->executeOutsideWorkspace(function (): void { throw new \RuntimeException('Failed outside the workspace.'); }); $this->fail('The exception thrown by the callback was not propagated.'); } catch (\RuntimeException $e) { $this->assertSame('Failed outside the workspace.', $e->getMessage()); } $this->assertSame('stage', $this->workspaceManager->getActiveWorkspace()?->id()); $this->assertSame([TRUE, TRUE], $switch_events); $this->workspaceManager->switchToLive(); $switch_events = []; try { $this->workspaceManager->executeInWorkspace('stage', function (): void { throw new \RuntimeException('Failed in the workspace.'); }); $this->fail('The exception thrown by the callback was not propagated.'); } catch (\RuntimeException $e) { $this->assertSame('Failed in the workspace.', $e->getMessage()); } $this->assertFalse($this->workspaceManager->hasActiveWorkspace()); $this->assertSame([TRUE, TRUE], $switch_events); } /** Loading