From cc224811a07f1e5374f15facee07cfc8cdedc82d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Sun, 9 Jul 2017 16:12:37 +0200 Subject: [PATCH] Add method to cache the replacements for each object See #2315 --- .../lib/data/user/trophy/UserTrophy.class.php | 41 +++++++++++++------ .../user/trophy/UserTrophyAction.class.php | 2 +- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/wcfsetup/install/files/lib/data/user/trophy/UserTrophy.class.php b/wcfsetup/install/files/lib/data/user/trophy/UserTrophy.class.php index 6dcc147497..7bd6bb488e 100644 --- a/wcfsetup/install/files/lib/data/user/trophy/UserTrophy.class.php +++ b/wcfsetup/install/files/lib/data/user/trophy/UserTrophy.class.php @@ -1,10 +1,10 @@ getTrophy()->getDescription(); } - $replacements = [ - '{$username}' => $this->getUserProfile()->username - ]; - - $parameters = ['replacements' => $replacements]; - - EventHandler::getInstance()->fireAction($this, 'getDescription', $parameters); - - $replacements = $parameters['replacements']; - - return nl2br(StringUtil::encodeHTML(strtr($this->description, $replacements))); + return nl2br(StringUtil::encodeHTML(strtr($this->description, $this->getReplacements()))); } /** @@ -98,4 +94,25 @@ class UserTrophy extends DatabaseObject { // @TODO check user option canViewTrophies return true; } + + /** + * Returns an array with replacements for the trophy. + * + * @return string[] + */ + protected function getReplacements() { + if ($this->replacements == null) { + $replacements = [ + '{$username}' => $this->getUserProfile()->username + ]; + + $parameters = ['replacements' => $replacements]; + + EventHandler::getInstance()->fireAction($this, 'getDescription', $parameters); + + $this->replacements = $parameters['replacements']; + } + + return $this->replacements; + } } diff --git a/wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php b/wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php index 16f0092edd..c6fc1d0003 100644 --- a/wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php +++ b/wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php @@ -1,7 +1,7 @@