Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bundle/Core/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ services:
- '@ibexa.config.resolver'
calls:
- [setSiteAccess, ['@Ibexa\Core\MVC\Symfony\SiteAccess']]
tags:
- { name: kernel.event_subscriber }

Ibexa\Core\MVC\Symfony\SiteAccess\SiteAccessServiceInterface:
alias: 'Ibexa\Core\MVC\Symfony\SiteAccess\SiteAccessService'
Expand Down
45 changes: 40 additions & 5 deletions src/lib/MVC/Symfony/SiteAccess/SiteAccessService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
use Ibexa\Core\Base\Exceptions\NotFoundException;
use Ibexa\Core\MVC\Symfony\Event\ScopeChangeEvent;
use Ibexa\Core\MVC\Symfony\MVCEvents;
use Ibexa\Core\MVC\Symfony\SiteAccess;
use function iterator_to_array;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SiteAccessService implements SiteAccessServiceInterface, SiteAccessAware
class SiteAccessService implements SiteAccessServiceInterface, SiteAccessAware, EventSubscriberInterface
{
private SiteAccessProviderInterface $provider;

private ?SiteAccess $siteAccess = null;
/** @var list<\Ibexa\Core\MVC\Symfony\SiteAccess> */
private array $siteAccessStack = [];

private ConfigResolverInterface $configResolver;

Expand All @@ -30,9 +34,40 @@ public function __construct(
$this->configResolver = $configResolver;
}

public static function getSubscribedEvents(): array
{
return [
MVCEvents::CONFIG_SCOPE_CHANGE => 'onConfigScopeChange',
MVCEvents::CONFIG_SCOPE_RESTORE => 'onConfigScopeRestore',
];
}

public function setSiteAccess(?SiteAccess $siteAccess = null): void
{
$this->siteAccess = $siteAccess;
$this->siteAccessStack = $siteAccess !== null ? [$siteAccess] : [];
}

/**
* Pushes the new SiteAccess onto the stack, so that getCurrent() reflects it until the
* matching restore happens.
*/
public function onConfigScopeChange(ScopeChangeEvent $event): void
{
$this->siteAccessStack[] = $event->getSiteAccess();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $event->getSiteAccess() returns array, shouldn't its name be plural?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Event returns a single SiteAccess.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🫣

}
Comment thread
Steveb-p marked this conversation as resolved.

/**
* Pops the SiteAccess pushed by the matching onConfigScopeChange(), but never below one
* remaining entry: the bottom-most entry (the current request's SiteAccess) must survive an
* unbalanced restore.
*/
public function onConfigScopeRestore(ScopeChangeEvent $event): void
{
if (count($this->siteAccessStack) <= 1) {
return;
}

array_pop($this->siteAccessStack);
}

public function exists(string $name): bool
Expand All @@ -56,12 +91,12 @@ public function getAll(): iterable

public function getCurrent(): ?SiteAccess
{
return $this->siteAccess ?? null;
return $this->siteAccessStack !== [] ? end($this->siteAccessStack) : null;
}

public function getSiteAccessesRelation(?SiteAccess $siteAccess = null): array
{
$siteAccess = $siteAccess ?? $this->siteAccess;
$siteAccess = $siteAccess ?? $this->getCurrent();
if ($siteAccess === null) {
throw new InvalidArgumentException('siteAccess', 'no SiteAccess given and none currently set');
}
Expand Down
87 changes: 87 additions & 0 deletions tests/lib/MVC/Symfony/SiteAccess/SiteAccessServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use ArrayIterator;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Core\MVC\Symfony\Event\ScopeChangeEvent;
use Ibexa\Core\MVC\Symfony\MVCEvents;
use Ibexa\Core\MVC\Symfony\SiteAccess;
use Ibexa\Core\MVC\Symfony\SiteAccess\Provider\StaticSiteAccessProvider;
use Ibexa\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface;
Expand Down Expand Up @@ -65,6 +67,91 @@ public function testGetCurrentSiteAccess(): void
self::assertNull($service->getCurrent());
}

public function testGetSubscribedEvents(): void
{
self::assertSame(
[
MVCEvents::CONFIG_SCOPE_CHANGE => 'onConfigScopeChange',
MVCEvents::CONFIG_SCOPE_RESTORE => 'onConfigScopeRestore',
],
SiteAccessService::getSubscribedEvents()
);
}

public function testOnConfigScopeChangeMakesGetCurrentReflectTheNewSiteAccess(): void
{
$service = new SiteAccessService(
$this->createStub(SiteAccessProviderInterface::class),
$this->createStub(ConfigResolverInterface::class)
);

$baseSiteAccess = new SiteAccess('base');
$service->setSiteAccess($baseSiteAccess);

$previewSiteAccess = new SiteAccess('preview');
$service->onConfigScopeChange(new ScopeChangeEvent($previewSiteAccess));

self::assertSame($previewSiteAccess, $service->getCurrent());
}

public function testOnConfigScopeRestoreBringsBackThePreviousSiteAccess(): void
{
$service = new SiteAccessService(
$this->createStub(SiteAccessProviderInterface::class),
$this->createStub(ConfigResolverInterface::class)
);

$baseSiteAccess = new SiteAccess('base');
$service->setSiteAccess($baseSiteAccess);

$previewSiteAccess = new SiteAccess('preview');
$service->onConfigScopeChange(new ScopeChangeEvent($previewSiteAccess));
$service->onConfigScopeRestore(new ScopeChangeEvent($baseSiteAccess));

self::assertSame($baseSiteAccess, $service->getCurrent());
}

public function testOnConfigScopeRestoreNeverDropsTheBaseSiteAccess(): void
{
$service = new SiteAccessService(
$this->createStub(SiteAccessProviderInterface::class),
$this->createStub(ConfigResolverInterface::class)
);

$baseSiteAccess = new SiteAccess('base');
$service->setSiteAccess($baseSiteAccess);

$service->onConfigScopeRestore(new ScopeChangeEvent($baseSiteAccess));

self::assertSame($baseSiteAccess, $service->getCurrent());
}

public function testNestedConfigScopeChangesAndRestoresRoundTripLikeAStack(): void
{
$service = new SiteAccessService(
$this->createStub(SiteAccessProviderInterface::class),
$this->createStub(ConfigResolverInterface::class)
);

$baseSiteAccess = new SiteAccess('base');
$service->setSiteAccess($baseSiteAccess);

$firstSiteAccess = new SiteAccess('first');
$secondSiteAccess = new SiteAccess('second');

$service->onConfigScopeChange(new ScopeChangeEvent($firstSiteAccess));
self::assertSame($firstSiteAccess, $service->getCurrent());

$service->onConfigScopeChange(new ScopeChangeEvent($secondSiteAccess));
self::assertSame($secondSiteAccess, $service->getCurrent());

$service->onConfigScopeRestore(new ScopeChangeEvent($firstSiteAccess));
self::assertSame($firstSiteAccess, $service->getCurrent());

$service->onConfigScopeRestore(new ScopeChangeEvent($baseSiteAccess));
self::assertSame($baseSiteAccess, $service->getCurrent());
}

public function testGetSiteAccess(): void
{
$staticSiteAccessProvider = new StaticSiteAccessProvider(
Expand Down