IBX-8074: Made SiteAccessService react to CONFIG_SCOPE_CHANGE/RESTORE - #806
IBX-8074: Made SiteAccessService react to CONFIG_SCOPE_CHANGE/RESTORE#806Steveb-p wants to merge 4 commits into
Conversation
getCurrent() only ever reflected the value set once via the shared SiteAccessAware::setSiteAccess() singleton, so it never picked up a scope change dispatched by ContentPreviewHelper or ConsoleCommandListener. It now keeps a small stack, pushing on CONFIG_SCOPE_CHANGE and popping on CONFIG_SCOPE_RESTORE.
konradoboza
left a comment
There was a problem hiding this comment.
Nice one! +1 as long as CI + regressions agree.
| if (count($this->siteAccessStack) > 1) { | ||
| array_pop($this->siteAccessStack); | ||
| } |
There was a problem hiding this comment.
I'd argue that this is one of the rare cases where early return makes it harder to read, because the original code does not contain a logical "negation", and the primary purpose of the code is to execute this logic check.
Changed anyway.
There was a problem hiding this comment.
Pull request overview
Makes SiteAccessService track configuration scope changes so getCurrent() reflects preview and CLI SiteAccess contexts.
Changes:
- Adds event subscriptions and SiteAccess stack handling.
- Registers the service as an event subscriber.
- Adds unit coverage for stack behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/lib/MVC/Symfony/SiteAccess/SiteAccessService.php |
Implements scope-aware SiteAccess tracking. |
src/bundle/Core/Resources/config/routing.yml |
Registers the event subscriber. |
tests/lib/MVC/Symfony/SiteAccess/SiteAccessServiceTest.php |
Tests change, restore, and nesting behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| private SiteAccessProviderInterface $provider; | ||
|
|
||
| private ?SiteAccess $siteAccess = null; | ||
| /** @var \Ibexa\Core\MVC\Symfony\SiteAccess[] */ |
There was a problem hiding this comment.
| /** @var \Ibexa\Core\MVC\Symfony\SiteAccess[] */ | |
| /** @var list<SiteAccess> */ |
| */ | ||
| public function onConfigScopeChange(ScopeChangeEvent $event): void | ||
| { | ||
| $this->siteAccessStack[] = $event->getSiteAccess(); |
There was a problem hiding this comment.
If $event->getSiteAccess() returns array, shouldn't its name be plural?
There was a problem hiding this comment.
Event returns a single SiteAccess.
There was a problem hiding this comment.
I don't know how I connected [] on the left of the assignment with the returned array on the right 😅
I was still before morning coffee then 🫣
| $this->createMock(SiteAccessProviderInterface::class), | ||
| $this->createMock(ConfigResolverInterface::class) |
There was a problem hiding this comment.
These could be mocks I guess, as long as we don't put expectation one them 😉
here, and in other added test cases
…d updated PHPDoc syntax
|



Related PRs:
IBX-12204, 6.0) — the full stack feature this is a narrow slice ofDescription:
Same fix as #805, backported one branch further.
SiteAccessService::getCurrent()only ever returned whatever was set once via the (shared, singleton)SiteAccessAware::setSiteAccess()call.ContentPreviewHelper::changeConfigScope()/restoreConfigScope()andConsoleCommandListener::onConsoleCommand()build a newSiteAccessand only dispatch it as aScopeChangeEventunderMVCEvents::CONFIG_SCOPE_CHANGE/CONFIG_SCOPE_RESTORE— they never touch that shared object. So during content preview or a--siteaccessCLI run,getCurrent()kept returning the outer/main siteaccess instead of the one that's actually active.SiteAccessServicenow implementsEventSubscriberInterfaceand reacts to those two events directly, keeping a small stack: push onCONFIG_SCOPE_CHANGE, pop onCONFIG_SCOPE_RESTORE(never below the base entry, so an unbalanced restore can't null out the current siteaccess).setSiteAccess()still seeds the base of that stack, so the existing DI wiring inrouting.ymland theSiteAccessAwarecontract are untouched.Deliberately not the full
IBX-12204/#798 rework — nochangeSiteAccess()/restoreSiteAccess()API, noSiteAccessAwaredeprecation, no migratingContentPreviewHelper/ConsoleCommandListener/etc. off it, noMVCEvents::SITEACCESS/FINISH_REQUESTsub-request handling. Just the reactive fix, sinceContentPreviewHelperandConsoleCommandListeneralready dispatch the events we need — zero changes required on their end.For QA:
Unit coverage is in
SiteAccessServiceTest(change/restore, the never-drop-base guard, nested change/restore round-tripping). To see it manually:$container->get(SiteAccessServiceInterface::class)->getCurrent()->name(e.g. from a debug template block, or a breakpoint inContentPreviewHelper::changeConfigScope()right after the call) — it should be the previewed siteaccess, not the one you started from.getCurrent()should be back to the original siteaccess.Same idea for CLI: run any command with
--siteaccess=<other>and confirmSiteAccessServiceInterface::getCurrent()->namereflects it instead of the default.