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

[Optimization] Skip loading user and path if not needed #171

Merged
merged 2 commits into from
May 14, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions bundle/EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,23 @@ public function onKernelRequest(GetResponseEvent $event)
}

try {
$apiUser = $this->repository->getUserService()->loadUser($session->get('eZUserLoggedInID'));
$legacyUserId = (int)$session->get('eZUserLoggedInID');
$token = $this->tokenStorage->getToken();

// Check if token is already legacy token, and user already loaded by Platform
if ($token instanceof LegacyToken &&
$token->getUser() instanceof User &&
$token->getUser()->getAPIUserReference()->getUserId() === $legacyUserId &&
$this->repository->getCurrentUserReference()->getUserId() === $legacyUserId
) {
// All seems ok, we can skip loading anything here
return;
}

// Load user and set as current
$apiUser = $this->repository->getUserService()->loadUser($legacyUserId);
$this->repository->setCurrentUser($apiUser);

$token = $this->tokenStorage->getToken();
if ($token instanceof TokenInterface) {
$token->setUser(new User($apiUser));
// Don't embed if we already have a LegacyToken, to avoid nested session storage.
Expand Down
29 changes: 19 additions & 10 deletions bundle/LegacyMapper/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,7 @@ private function getMultiSiteSettings()
return $result;
}

$pathPrefix = '';

try {
$pathPrefix = trim($this->urlAliasGenerator->getPathPrefixByRootLocationId($rootLocationId), '/');
} catch (Exception $e) {
// Ignore any errors
// Most probable cause for error is database not being ready yet,
// i.e. initial install of the project which includes eZ Publish Legacy
}

$pathPrefix = $this->loadPathPrefix($rootLocationId);
$pathPrefixExcludeItems = array_map(
static function ($value) {
return trim($value, '/');
Expand Down Expand Up @@ -340,4 +331,22 @@ private function getClusterSettings()

return $clusterSettings;
}

private function loadPathPrefix($rootLocationId)
{
// If root location is 2 we know path is empty, so we can skip loading location + urlAlias data
if ($rootLocationId === 2) {
return '';
}

try {
return trim($this->urlAliasGenerator->getPathPrefixByRootLocationId($rootLocationId), '/');
} catch (Exception $e) {
// Ignore any errors
// Most probable cause for error is database not being ready yet,
// i.e. initial install of the project which includes eZ Publish Legacy
}

return '';
}
}