Add method to cache the replacements for each object
authorJoshua Rüsweg <josh@bastelstu.be>
Sun, 9 Jul 2017 14:12:37 +0000 (16:12 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Sun, 9 Jul 2017 14:12:37 +0000 (16:12 +0200)
See #2315

wcfsetup/install/files/lib/data/user/trophy/UserTrophy.class.php
wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php

index 6dcc147497af3dff56fbfb1335af9e1812533d72..7bd6bb488efbeea48fb57a6796f251e229748e12 100644 (file)
@@ -1,10 +1,10 @@
 <?php
 namespace wcf\data\user\trophy;
-use wcf\data\DatabaseObject;
 use wcf\data\trophy\Trophy;
 use wcf\data\trophy\TrophyCache;
 use wcf\data\user\User;
 use wcf\data\user\UserProfile;
+use wcf\data\DatabaseObject;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\event\EventHandler;
 use wcf\system\exception\SystemException;
@@ -28,6 +28,12 @@ use wcf\util\StringUtil;
  * @property-read      string          $useCustomDescription           `1`, iif the trophy use a custom description
  */
 class UserTrophy extends DatabaseObject {
+       /**
+        * The description text replacements. 
+        * @var string[]
+        */
+       private $replacements; 
+       
        /**
         * Returns the trophy for the user trophy. 
         * 
@@ -56,17 +62,7 @@ class UserTrophy extends DatabaseObject {
                        return $this->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; 
+       }
 }
index 16f0092edd595a3c9456fa2bccf6604d8d73c789..c6fc1d0003338789e916ca6b04abf99071321d16 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace wcf\data\user\trophy;
-use wcf\data\AbstractDatabaseObjectAction;
 use wcf\data\user\UserAction;
+use wcf\data\AbstractDatabaseObjectAction;
 
 /**
  * Provides user trophy actions.