From 6e44b4080a6356fbf7c27e0d5e7acfcad5ebf577 Mon Sep 17 00:00:00 2001 From: thybag Date: Tue, 4 Sep 2012 16:09:56 +0100 Subject: [PATCH 1/5] Cache application.encoding within HTML class to avoid unnecessary calls to Config::get(); Calling the "Config::get('application.encoding')" is expensive and within a large form (using the form builder) having it requested multiple times can result in a significant performance drag. Caching this value reduced calls to Config:get within our project from 1200+ to 125. All core tests appear to pass with this change in place. --- laravel/html.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/laravel/html.php b/laravel/html.php index fd5bf82e..71a89080 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -9,6 +9,13 @@ class HTML { */ public static $macros = array(); + /** + * Cache application encoding locally to save expensive calls to config::get(). + * + * @var string + */ + public static $encoding = null; + /** * Registers a custom macro. * @@ -31,7 +38,8 @@ public static function macro($name, $macro) */ public static function entities($value) { - return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false); + if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); + return htmlentities($value, ENT_QUOTES, static::$encoding, false); } /** @@ -42,7 +50,8 @@ public static function entities($value) */ public static function decode($value) { - return html_entity_decode($value, ENT_QUOTES, Config::get('application.encoding')); + if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); + return html_entity_decode($value, ENT_QUOTES, static::$encoding); } /** @@ -55,7 +64,8 @@ public static function decode($value) */ public static function specialchars($value) { - return htmlspecialchars($value, ENT_QUOTES, Config::get('application.encoding'), false); + if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); + return htmlspecialchars($value, ENT_QUOTES, static::$encoding, false); } /** From a9be66d41a01ba09cc07e772b5ee57088a0ee1d4 Mon Sep 17 00:00:00 2001 From: thybag Date: Wed, 5 Sep 2012 09:35:54 +0100 Subject: [PATCH 2/5] Refactor changes into single get_encoding() method within the HTML Class (in order to remove duplication of logic). All tests continue to pass. --- laravel/html.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/laravel/html.php b/laravel/html.php index 71a89080..ec6b3ae2 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -38,8 +38,7 @@ public static function macro($name, $macro) */ public static function entities($value) { - if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); - return htmlentities($value, ENT_QUOTES, static::$encoding, false); + return htmlentities($value, ENT_QUOTES, static::get_encoding(), false); } /** @@ -50,8 +49,7 @@ public static function entities($value) */ public static function decode($value) { - if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); - return html_entity_decode($value, ENT_QUOTES, static::$encoding); + return html_entity_decode($value, ENT_QUOTES, static::get_encoding()); } /** @@ -64,8 +62,7 @@ public static function decode($value) */ public static function specialchars($value) { - if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); - return htmlspecialchars($value, ENT_QUOTES, static::$encoding, false); + return htmlspecialchars($value, ENT_QUOTES, static::get_encoding(), false); } /** @@ -417,6 +414,18 @@ protected static function obfuscate($value) return $safe; } + /** + * Get the appliction.encoding without needing to request it from Config::get() each time. + * + * @return string + */ + public static function get_encoding(){ + + if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); + + return static::$encoding; + } + /** * Dynamically handle calls to custom macros. * From f55bb85c6f146a972bbdc6c9591dfefc8de2d661 Mon Sep 17 00:00:00 2001 From: thybag Date: Wed, 5 Sep 2012 09:49:04 +0100 Subject: [PATCH 3/5] Update to conform better with laravels coding style. --- laravel/html.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/laravel/html.php b/laravel/html.php index ec6b3ae2..7a602de9 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -419,8 +419,8 @@ protected static function obfuscate($value) * * @return string */ - public static function get_encoding(){ - + protected static function get_encoding() + { if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); return static::$encoding; From aa32e4cf72304564663ed069fa60b1b74e63ec01 Mon Sep 17 00:00:00 2001 From: thybag Date: Sun, 9 Sep 2012 14:24:52 +0100 Subject: [PATCH 4/5] Rename static::get_encoding() method to static::encoding() to fit better with style. --- laravel/html.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/laravel/html.php b/laravel/html.php index 7a602de9..157ef48e 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -38,7 +38,7 @@ public static function macro($name, $macro) */ public static function entities($value) { - return htmlentities($value, ENT_QUOTES, static::get_encoding(), false); + return htmlentities($value, ENT_QUOTES, static::encoding(), false); } /** @@ -49,7 +49,7 @@ public static function entities($value) */ public static function decode($value) { - return html_entity_decode($value, ENT_QUOTES, static::get_encoding()); + return html_entity_decode($value, ENT_QUOTES, static::encoding()); } /** @@ -62,7 +62,7 @@ public static function decode($value) */ public static function specialchars($value) { - return htmlspecialchars($value, ENT_QUOTES, static::get_encoding(), false); + return htmlspecialchars($value, ENT_QUOTES, static::encoding(), false); } /** @@ -419,8 +419,8 @@ protected static function obfuscate($value) * * @return string */ - protected static function get_encoding() - { + protected static function encoding() + { if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); return static::$encoding; From f02e7dc4ca0c9f3dd16c8dc49324854090d8d443 Mon Sep 17 00:00:00 2001 From: Alex Andrews Date: Mon, 10 Sep 2012 12:27:50 +0100 Subject: [PATCH 5/5] Reformatted return Changed the return of the method in line with suggestions from @JoostK. --- laravel/html.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/laravel/html.php b/laravel/html.php index 157ef48e..3d7f775e 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -420,10 +420,8 @@ protected static function obfuscate($value) * @return string */ protected static function encoding() - { - if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); - - return static::$encoding; + { + return static::$encoding ?: static::$encoding = Config::get('application.encoding'); } /**