Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements #2329

Merged
merged 10 commits into from Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -30,7 +30,7 @@
"hslavich/oneloginsaml-bundle": "^1.4",
"jms/metadata": "^2.0",
"jms/serializer-bundle": "^3.2",
"kevinpapst/adminlte-bundle": "^3.0",
"kevinpapst/adminlte-bundle": "dev-master",
kevinpapst marked this conversation as resolved.
Show resolved Hide resolved
"kimai/user-bundle": "^1.1",
"laravolt/avatar": "^3.0",
"league/csv": "^9.4",
Expand Down
41 changes: 23 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 0 additions & 39 deletions src/Controller/LayoutController.php

This file was deleted.

51 changes: 15 additions & 36 deletions src/EventSubscriber/ThemeOptionsSubscriber.php
Expand Up @@ -20,22 +20,17 @@
/**
* Allows dynamic injection of theme related options.
*/
class ThemeOptionsSubscriber implements EventSubscriberInterface
final class ThemeOptionsSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
protected $storage;

private $storage;
/**
* @var ContextHelper
*/
protected $helper;
private $helper;

/**
* @param TokenStorageInterface $storage
* @param ContextHelper $helper
*/
public function __construct(TokenStorageInterface $storage, ContextHelper $helper)
{
$this->storage = $storage;
Expand All @@ -52,18 +47,24 @@ public static function getSubscribedEvents(): array
];
}

/**
* @param KernelEvent $event
*/
public function setThemeOptions(KernelEvent $event)
public function setThemeOptions(KernelEvent $event): void
{
if (!$this->canHandleEvent($event)) {
// Ignore sub-requests
if (!$event->isMasterRequest()) {
return;
}

// ignore events like the toolbar where we do not have a token
if (null === $this->storage->getToken()) {
return;
}

/** @var User $user */
$user = $this->storage->getToken()->getUser();

if (!($user instanceof User)) {
return;
}

/** @var UserPreference $ref */
foreach ($user->getPreferences() as $ref) {
$name = $ref->getName();
Expand All @@ -90,26 +91,4 @@ public function setThemeOptions(KernelEvent $event)
}
}
}

/**
* @param KernelEvent $event
* @return bool
*/
protected function canHandleEvent(KernelEvent $event): bool
{
// Ignore sub-requests
if (!$event->isMasterRequest()) {
return false;
}

// ignore events like the toolbar where we do not have a token
if (null === $this->storage->getToken()) {
return false;
}

/** @var User $user */
$user = $this->storage->getToken()->getUser();

return ($user instanceof User);
}
}
31 changes: 21 additions & 10 deletions src/EventSubscriber/UserEnvironmentSubscriber.php
Expand Up @@ -10,13 +10,15 @@
namespace App\EventSubscriber;

use App\Entity\User;
use App\Event\PrepareUserEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class UserEnvironmentSubscriber implements EventSubscriberInterface
final class UserEnvironmentSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
Expand All @@ -26,38 +28,47 @@ class UserEnvironmentSubscriber implements EventSubscriberInterface
* @var AuthorizationCheckerInterface
*/
private $auth;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

public function __construct(TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $auth)
public function __construct(TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $auth, EventDispatcherInterface $dispatcher)
{
$this->storage = $tokenStorage;
$this->auth = $auth;
$this->eventDispatcher = $dispatcher;
}

public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['prepareEnvironment', -100],
KernelEvents::REQUEST => ['prepareEnvironment', 200],
];
}

public function prepareEnvironment(RequestEvent $event)
public function prepareEnvironment(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
}

// the locale depends on the request, not on the user configuration
\Locale::setDefault($event->getRequest()->getLocale());

if (null === $this->storage->getToken()) {
return;
}

$user = $this->storage->getToken()->getUser();
if (!($user instanceof User)) {
return;
}

// the locale depends on the request, not on the user configuration
\Locale::setDefault($event->getRequest()->getLocale());
date_default_timezone_set($user->getTimezone());
$user->initCanSeeAllData($this->auth->isGranted('view_all_data'));

if ($user instanceof User) {
date_default_timezone_set($user->getTimezone());
$user->initCanSeeAllData($this->auth->isGranted('view_all_data'));
}
$event = new PrepareUserEvent($user);
$this->eventDispatcher->dispatch($event);
}
}