Skip to content

Commit

Permalink
perf: Improved event subscriber registration and initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Feb 14, 2024
1 parent 0d11931 commit febf372
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
public static function getSubscribedEvents(): array
{
return [
'workflow.node.completed' => ['onAutomaticWebhook'],
'workflow.node.completed' => 'onAutomaticWebhook',
NodeVisibilityChangedEvent::class => 'onAutomaticWebhook',
NodesSourcesPreUpdatedEvent::class => 'onAutomaticWebhook',
NodesSourcesDeletedEvent::class => 'onAutomaticWebhook',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function getSubscribedEvents(): array
{
return [
// must be registered just after Symfony\Component\HttpKernel\EventListener\LocaleListener
RequestEvent::class => [['onKernelRequest', 16]],
RequestEvent::class => ['onKernelRequest', 16],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use RZ\Roadiz\CoreBundle\Routing\NodesSourcesUrlGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class NodeSourcePathSubscriber implements EventSubscriberInterface
final class NodeSourcePathSubscriber implements EventSubscriberInterface
{
public function __construct(
protected readonly NodesSourcesPathAggregator $pathAggregator
private readonly NodesSourcesPathAggregator $pathAggregator
) {
}

Expand All @@ -22,7 +22,7 @@ public function __construct(
public static function getSubscribedEvents(): array
{
return [
NodesSourcesPathGeneratingEvent::class => [['onNodesSourcesPath', -100]],
NodesSourcesPathGeneratingEvent::class => ['onNodesSourcesPath', -100],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@
use Symfony\Component\WebLink\GenericLinkProvider;
use Symfony\Component\WebLink\Link;

class NodesSourcesLinkHeaderEventSubscriber implements EventSubscriberInterface
final class NodesSourcesLinkHeaderEventSubscriber implements EventSubscriberInterface
{
private ManagerRegistry $managerRegistry;
private UrlGeneratorInterface $urlGenerator;

public function __construct(
ManagerRegistry $managerRegistry,
UrlGeneratorInterface $urlGenerator
private readonly ManagerRegistry $managerRegistry,
private readonly UrlGeneratorInterface $urlGenerator
) {
$this->managerRegistry = $managerRegistry;
$this->urlGenerator = $urlGenerator;
}

/**
Expand All @@ -40,33 +35,35 @@ public static function getSubscribedEvents(): array
public function onKernelView(ViewEvent $event): void
{
$request = $event->getRequest();
$resources = $request->attributes->get('data', null);
$resources = $request->attributes->get('data');
$linkProvider = $request->attributes->get('_links', new GenericLinkProvider());

if ($resources instanceof NodesSources && $linkProvider instanceof EvolvableLinkProviderInterface) {
/*
* Preview and authentication is handled at repository level.
*/
/** @var NodesSources[] $allSources */
$allSources = $this->managerRegistry
->getRepository(get_class($resources))
->findByNode($resources->getNode());
if (!$resources instanceof NodesSources || !$linkProvider instanceof EvolvableLinkProviderInterface) {
return;
}

/*
* Preview and authentication is handled at repository level.
*/
/** @var NodesSources[] $allSources */
$allSources = $this->managerRegistry
->getRepository(get_class($resources))
->findByNode($resources->getNode());

foreach ($allSources as $singleSource) {
$linkProvider = $linkProvider->withLink(
(new Link(
'alternate',
$this->urlGenerator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
RouteObjectInterface::ROUTE_OBJECT => $singleSource
])
))
->withAttribute('hreflang', $singleSource->getTranslation()->getLocale())
// Must encode translation name in base64 because headers are ASCII only
->withAttribute('title', \base64_encode($singleSource->getTranslation()->getName()))
->withAttribute('type', 'text/html')
);
}
$request->attributes->set('_links', $linkProvider);
foreach ($allSources as $singleSource) {
$linkProvider = $linkProvider->withLink(
(new Link(
'alternate',
$this->urlGenerator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
RouteObjectInterface::ROUTE_OBJECT => $singleSource
])
))
->withAttribute('hreflang', $singleSource->getTranslation()->getLocale())
// Must encode translation name in base64 because headers are ASCII only
->withAttribute('title', \base64_encode($singleSource->getTranslation()->getName()))
->withAttribute('type', 'text/html')
);
}
$request->attributes->set('_links', $linkProvider);
}
}
18 changes: 7 additions & 11 deletions lib/RoadizCoreBundle/src/EventSubscriber/SignatureSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@

final class SignatureSubscriber implements EventSubscriberInterface
{
private string $version;
private bool $debug;
private bool $hideRoadizVersion;

public function __construct(string $cmsVersion, bool $hideRoadizVersion, bool $debug = false)
{
$this->version = $cmsVersion;
$this->debug = $debug;
$this->hideRoadizVersion = $hideRoadizVersion;
public function __construct(
private readonly string $cmsVersion,
private readonly bool $hideRoadizVersion,
private readonly bool $debug = false
) {
}
/**
* Filters the Response.
Expand All @@ -34,8 +30,8 @@ public function onKernelResponse(ResponseEvent $event): void
$response = $event->getResponse();
$response->headers->add(['X-Powered-By' => 'Roadiz CMS']);

if ($this->debug && $this->version) {
$response->headers->add(['X-Version' => $this->version]);
if ($this->debug && $this->cmsVersion) {
$response->headers->add(['X-Version' => $this->cmsVersion]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public static function getSubscribedEvents(): array

public function onTagUpdatedEvent(TagUpdatedEvent $event): void
{
$tag = $event->getTag();
if ($tag instanceof AbstractDateTimed) {
$tag->setUpdatedAt(new \DateTime());
}
$event->getTag()->setUpdatedAt(new \DateTime());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
/**
* Subscribe to Translation event to clear result cache.
*/
class TranslationSubscriber implements EventSubscriberInterface
final class TranslationSubscriber implements EventSubscriberInterface
{
protected ManagerRegistry $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry)
public function __construct(private readonly ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
}

public static function getSubscribedEvents(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@

final class UserLocaleSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
private TokenStorageInterface $tokenStorage;

public function __construct(
RequestStack $requestStack,
TokenStorageInterface $tokenStorage
private readonly RequestStack $requestStack,
private readonly TokenStorageInterface $tokenStorage
) {
$this->requestStack = $requestStack;
$this->tokenStorage = $tokenStorage;
}

/**
Expand All @@ -34,7 +29,7 @@ public static function getSubscribedEvents(): array
// must be registered after the default Locale listener
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
UserUpdatedEvent::class => [['onUserUpdated']],
UserUpdatedEvent::class => 'onUserUpdated',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,21 @@

use RZ\Roadiz\CoreBundle\Preview\Exception\PreviewNotAllowedException;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Bundle\SecurityBundle\Security;

class PreviewModeSubscriber implements EventSubscriberInterface
final class PreviewModeSubscriber implements EventSubscriberInterface
{
public const QUERY_PARAM_NAME = '_preview';

protected PreviewResolverInterface $previewResolver;
protected TokenStorageInterface $tokenStorage;
protected Security $security;

public function __construct(
PreviewResolverInterface $previewResolver,
TokenStorageInterface $tokenStorage,
Security $security
private readonly PreviewResolverInterface $previewResolver,
private readonly Security $security
) {
$this->previewResolver = $previewResolver;
$this->tokenStorage = $tokenStorage;
$this->security = $security;
}

/**
Expand All @@ -39,7 +29,7 @@ public function __construct(
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 9999],
KernelEvents::REQUEST => ['onKernelRequest', 2047],
KernelEvents::CONTROLLER => ['onControllerMatched', 10],
KernelEvents::RESPONSE => 'onResponse',
];
Expand All @@ -61,9 +51,9 @@ public function onKernelRequest(RequestEvent $event): void
$request = $event->getRequest();
if (
$event->isMainRequest() &&
$request->query->has(static::QUERY_PARAM_NAME) &&
$request->query->has(self::QUERY_PARAM_NAME) &&
\in_array(
$request->query->get(static::QUERY_PARAM_NAME, 0),
$request->query->get(self::QUERY_PARAM_NAME, 0),
['true', true, '1', 1, 'on', 'yes', 'y'],
true
)
Expand Down

0 comments on commit febf372

Please sign in to comment.