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 all 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": "^3.3",
"kimai/user-bundle": "^1.1",
"laravolt/avatar": "^3.0",
"league/csv": "^9.4",
Expand Down
38 changes: 20 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);
}
}
16 changes: 9 additions & 7 deletions src/EventSubscriber/UserEnvironmentSubscriber.php
Expand Up @@ -16,7 +16,7 @@
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 @@ -40,20 +40,22 @@ public static function getSubscribedEvents(): array
];
}

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

if (null === $this->storage->getToken()) {
// the locale depends on the request, not on the user configuration
\Locale::setDefault($event->getRequest()->getLocale());

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

$user = $this->storage->getToken()->getUser();

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

if ($user instanceof User) {
date_default_timezone_set($user->getTimezone());
Expand Down
46 changes: 13 additions & 33 deletions src/EventSubscriber/UserPreferenceSubscriber.php
Expand Up @@ -28,20 +28,20 @@
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Validator\Constraints\Range;

class UserPreferenceSubscriber implements EventSubscriberInterface
final class UserPreferenceSubscriber implements EventSubscriberInterface
{
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
private $eventDispatcher;
/**
* @var AuthorizationCheckerInterface
*/
protected $voter;
private $voter;
/**
* @var SystemConfiguration
*/
protected $configuration;
private $configuration;

public function __construct(EventDispatcherInterface $dispatcher, AuthorizationCheckerInterface $voter, SystemConfiguration $formConfig)
{
Expand All @@ -57,43 +57,23 @@ public static function getSubscribedEvents(): array
];
}

private function getDefaultTheme(): ?string
{
return $this->configuration->getUserDefaultTheme();
}

private function getDefaultCurrency(): string
{
return $this->configuration->getUserDefaultCurrency();
}

private function getDefaultLanguage(): string
{
return $this->configuration->getUserDefaultLanguage();
}

private function getDefaultTimezone(): string
{
$timezone = $this->configuration->getUserDefaultTimezone();
if (null === $timezone) {
$timezone = date_default_timezone_get();
}

return $timezone;
}

/**
* @param User $user
* @return UserPreference[]
*/
public function getDefaultPreferences(User $user)
{
$timezone = $this->configuration->getUserDefaultTimezone();
if (null === $timezone) {
$timezone = date_default_timezone_get();
}

$enableHourlyRate = false;
$hourlyRateOptions = [];

if ($this->voter->isGranted('hourly-rate', $user)) {
$enableHourlyRate = true;
$hourlyRateOptions = ['currency' => $this->getDefaultCurrency()];
$hourlyRateOptions = ['currency' => $this->configuration->getUserDefaultCurrency()];
}

return [
Expand All @@ -119,14 +99,14 @@ public function getDefaultPreferences(User $user)

(new UserPreference())
->setName(UserPreference::TIMEZONE)
->setValue($this->getDefaultTimezone())
->setValue($timezone)
->setOrder(200)
->setSection('locale')
->setType(TimezoneType::class),

(new UserPreference())
->setName(UserPreference::LOCALE)
->setValue($this->getDefaultLanguage())
->setValue($this->configuration->getUserDefaultLanguage())
->setOrder(250)
->setSection('locale')
->setType(LanguageType::class),
Expand All @@ -140,7 +120,7 @@ public function getDefaultPreferences(User $user)

(new UserPreference())
->setName(UserPreference::SKIN)
->setValue($this->getDefaultTheme())
->setValue($this->configuration->getUserDefaultTheme())
->setOrder(400)
->setSection('theme')
->setType(SkinType::class),
Expand Down
29 changes: 9 additions & 20 deletions src/EventSubscriber/UserProfileSubscriber.php
Expand Up @@ -43,32 +43,21 @@ public static function getSubscribedEvents(): array

public function prepareUserProfile(KernelEvent $event): void
{
if (!$this->canHandleEvent($event)) {
return;
}

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

$event = new PrepareUserEvent($user);
$this->eventDispatcher->dispatch($event);
}

private function canHandleEvent(KernelEvent $event): bool
{
// Ignore sub-requests
// ignore sub-requests
if (!$event->isMasterRequest()) {
return false;
return;
}

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

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

return ($user instanceof User);
if ($user instanceof User) {
$event = new PrepareUserEvent($user);
$this->eventDispatcher->dispatch($event);
}
}
}