From 78665cd56da1ff9ab5b7aa76f9c91e3c3362d187 Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 21 Dec 2020 06:41:45 -0800 Subject: [PATCH] Performance: Reduced e107::getFolder() usage from 2500+ to less than 30. --- e107_handlers/e_parse_class.php | 70 +++++++++++++++------------ e107_tests/tests/unit/e_parseTest.php | 24 ++++++++- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 1d16df4db..75ebe2727 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -66,6 +66,9 @@ class e_parse extends e_parser protected $staticUrl = null; + /** @var array Stored relative paths - used by replaceConstants() */ + private $relativePaths = array(); + // BBcode that contain preformatted code. private $preformatted = array('html', 'markdown'); @@ -3296,37 +3299,44 @@ class e_parse extends e_parser return $new; } - if($mode != '') + + if(!empty($mode)) { $e107 = e107::getInstance(); - $replace_relative = array( - $e107::getFolder('media_files'), - $e107::getFolder('media_video'), - $e107::getFolder('media_image'), - $e107::getFolder('media_icon'), - $e107::getFolder('avatars'), - $e107::getFolder('web_js'), - $e107::getFolder('web_css'), - $e107::getFolder('web_image'), - //$e107->getFolder('web_pack'), - e_IMAGE_ABS, - e_THEME_ABS, - $e107::getFolder('images'), - $e107::getFolder('plugins'), - $e107::getFolder('files'), - $e107::getFolder('themes'), - // $e107->getFolder('downloads'), - $e107::getFolder('handlers'), - $e107::getFolder('media'), - $e107::getFolder('web'), - $e107->site_theme ? $e107::getFolder('themes').$e107->site_theme.'/' : '', - defset('THEME_ABS'), - (ADMIN ? $e107::getFolder('admin') : ''), - '', - $e107::getFolder('core'), - $e107::getFolder('system'), - ); + if(empty($this->relativePaths)) // prevent multiple lookups. + { + + $this->relativePaths = array( + $e107::getFolder('media_files'), + $e107::getFolder('media_video'), + $e107::getFolder('media_image'), + $e107::getFolder('media_icon'), + $e107::getFolder('avatars'), + $e107::getFolder('web_js'), + $e107::getFolder('web_css'), + $e107::getFolder('web_image'), + //$e107->getFolder('web_pack'), + e_IMAGE_ABS, + e_THEME_ABS, + $e107::getFolder('images'), + $e107::getFolder('plugins'), + $e107::getFolder('files'), + $e107::getFolder('themes'), + // $e107->getFolder('downloads'), + $e107::getFolder('handlers'), + $e107::getFolder('media'), + $e107::getFolder('web'), + $e107->site_theme ? $e107::getFolder('themes').$e107->site_theme.'/' : '', + defset('THEME_ABS'), + (ADMIN ? $e107::getFolder('admin') : ''), + '', + $e107::getFolder('core'), + $e107::getFolder('system'), + ); + } + + $replace_relative = $this->relativePaths; switch ($mode) { @@ -3815,8 +3825,8 @@ class e_parse extends e_parser * Cameron's DOM-based parser. * * @method replaceConstants($text, $mode = '', $all = false) - * @method toAttribute($title) - * @method thumbUrl($icon) + * @method toAttribute($text) + * @method thumbUrl($url) * @method thumbDimensions() */ class e_parser diff --git a/e107_tests/tests/unit/e_parseTest.php b/e107_tests/tests/unit/e_parseTest.php index 8f5f511bf..6ea6722d0 100644 --- a/e107_tests/tests/unit/e_parseTest.php +++ b/e107_tests/tests/unit/e_parseTest.php @@ -505,10 +505,30 @@ while($row = $sql->fetch()) */ public function testReplaceConstants() { - $actual = $this->tp->replaceConstants('{e_BASE}news','abs'); + $tests = array( + 0 => array( + 'path' => '{e_BASE}news', + 'type' => 'abs', + 'match' => e_HTTP, + ), + 1 => array( + 'path' => '{e_BASE}news.php', + 'type' => 'full', + 'match' => 'https://localhost/e107/news.php', + ), + 2 => array( + 'path' => '{e_PLUGIN}news/index.php', + 'type' => null, + 'match' => 'e107_plugins/news/index.php', + ), + ); - $this->assertStringContainsString(e_HTTP,$actual); + foreach($tests as $var) + { + $actual = $this->tp->replaceConstants($var['path'],$var['type']); + $this->assertStringContainsString($var['match'], $actual); + } } /*