From 143364630589518d1e618230b712cf548cbbe1e1 Mon Sep 17 00:00:00 2001 From: Cameron Date: Tue, 22 Dec 2020 09:29:12 -0800 Subject: [PATCH] Deprecated some old themeHandler methods in favor of e_theme class. Should see a speed increase in theme manager. --- e107_handlers/theme_handler.php | 216 ++++++++++---------------- e107_tests/tests/unit/e_themeTest.php | 41 ++++- 2 files changed, 119 insertions(+), 138 deletions(-) diff --git a/e107_handlers/theme_handler.php b/e107_handlers/theme_handler.php index 0cd40a64e..fec36ffff 100644 --- a/e107_handlers/theme_handler.php +++ b/e107_handlers/theme_handler.php @@ -298,7 +298,7 @@ class e_theme { $themeArray = array(); - $tloop = 1; + $cacheTag = self::CACHETAG; @@ -308,11 +308,13 @@ class e_theme return $this; } - $array = scandir(e_THEME); + // $array = scandir(e_THEME); + $array = e107::getFile()->get_dirs(e_THEME); + $tloop = 1; foreach($array as $file) { - if($file != "." && $file != ".." && $file != "CVS" && $file != "templates" && is_dir(e_THEME.$file) && is_readable(e_THEME.$file."/theme.php")) + if($file != "CVS" && $file != "templates" && is_readable(e_THEME.$file."/theme.php")) { $themeArray[$file] = self::getThemeInfo($file); @@ -334,8 +336,8 @@ class e_theme /** - * Return a var from the current theme. - * @param $var + * Return a var from the current theme or all vars if $var is empty. + * @param string|null $var * @param null $key * @return array|bool */ @@ -504,39 +506,57 @@ class e_theme /** * Return a list of all local themes in various formats. * Replaces getThemeList - * @param null|string $mode null, 'version' | 'id' + * @param null|string $mode null, 'version' | 'id' | 'xml' * @return array|bool a list or false if no results */ public function getList($mode=null) { $arr = array(); - if($mode === 'version') + switch ($mode) { - foreach($this->_data as $dir=>$v) - { - $arr[$dir] = array('version'=>$v['version'], 'author'=>$v['author']); - } + case "version": + foreach($this->_data as $dir=>$v) + { + $arr[$dir] = array('version'=>$v['version'], 'author'=>$v['author']); + } + break; - } - elseif($mode === 'id') - { - foreach($this->_data as $dir=>$v) - { - $arr[] = $dir; - } - } - else - { - $arr = $this->_data; + case "id": + $count = 1; + foreach($this->_data as $dir=>$v) + { + $arr[$count] = $dir; + $count++; + } + break; + case 'xml': + $count = 1; + foreach($this->_data as $dir=>$v) + { + if($v['legacy'] === true) + { + continue; + } + + $v['id'] = $count; // reset the counter. + $arr[$dir] = $v; + + $count++; + } + break; + + default: + $arr = $this->_data; } return !empty($arr) ? $arr : false; - } + + /** * Get a list of all themes in theme folder and its data. * @deprecated @@ -549,7 +569,6 @@ class e_theme { trigger_error(''.__METHOD__.' is deprecated.', E_USER_DEPRECATED); // NO LAN - $themeArray = array(); $tloop = 1; @@ -618,6 +637,7 @@ class e_theme $handle2 = e107::getFile()->get_files(e_THEME.$file."/", "\.php|\.css|\.xml|preview\.jpg|preview\.png", $reject, 1); $themeArray = array(); + $themeArray[$file] = array(); foreach ($handle2 as $fln) { @@ -658,13 +678,16 @@ class e_theme // Load Theme information and merge with existing array. theme.xml (v2.x theme) is given priority over theme.php (v1.x). - if(in_array("theme.xml", $themeArray[$file]['files'])) + if(!empty($themeArray[$file]['files'])) { - $themeArray[$file] = array_merge($themeArray[$file], self::parse_theme_xml($file)); - } - elseif(in_array("theme.php", $themeArray[$file]['files'])) - { - $themeArray[$file] = array_merge($themeArray[$file], self::parse_theme_php($file)); + if(in_array("theme.xml", $themeArray[$file]['files'])) + { + $themeArray[$file] = array_merge($themeArray[$file], self::parse_theme_xml($file)); + } + elseif(in_array("theme.php", $themeArray[$file]['files'])) + { + $themeArray[$file] = array_merge($themeArray[$file], self::parse_theme_php($file)); + } } if(!empty($themeArray[$file]['css']) && count($themeArray[$file]['css']) > 1) @@ -1029,7 +1052,7 @@ class e_theme private static function initThemePreview($id) { $themeobj = new themeHandler; - $themeArray = $themeobj->getThemes('id'); + $themeArray = e107::getTheme()->getList('id'); $id = (int) $id; $themeDef = $themeobj->findDefault($themeArray[$id]); @@ -1232,7 +1255,7 @@ class themeHandler if(!empty($_POST['setUploadTheme']) && !empty($unzippedTheme)) { - $themeArray = $this->getThemes(); + $themeArray = e107::getTheme()->getList(); $this->id = $themeArray[$unzippedTheme]['id']; if($this->setTheme()) @@ -1253,7 +1276,7 @@ class themeHandler } - $this->themeArray = (defined('E107_INSTALL')) ? $this->getThemes('xml') : $this->getThemes(); + $this->themeArray = (defined('E107_INSTALL')) ?e107::getTheme()->getList('xml') : e107::getTheme()->getList(); // print_a($this -> themeArray); @@ -1362,111 +1385,30 @@ class themeHandler } - - function getThemes($mode = FALSE) - { - $themeArray = array(); - - $tloop = 1; - $fl = e107::getFile(); - $array = $fl->get_dirs(e_THEME); - - foreach($array as $file) - { - - if(($mode == 'xml') && !is_readable(e_THEME.$file."/theme.xml")) - { - continue; - } - - if($file != "." && $file != ".." && $file != "CVS" && $file != "templates" && is_dir(e_THEME.$file) && is_readable(e_THEME.$file."/theme.php")) - { - if($mode == "id") - { - $themeArray[$tloop] = $file; - } - else - { - $themeArray[$file] = $this->getThemeInfo($file); - $themeArray[$file]['id'] = $tloop; - } - $tloop++; - } - } - - // echo "
";
-	//	 print_r($themeArray);
-	//	 echo "
"; - - return $themeArray; + /** + * Returns a list of themes and their information. + * @deprecated + * @param false $mode + * @return array|bool + */ + public function getThemes($mode = FALSE) + { + trigger_error(''.__METHOD__.' is deprecated. Use e107::getTheme()->getList($mode); instead. ', E_USER_DEPRECATED); + + return e107::getTheme()->getList($mode); + } - + /** + * @deprecated + * @param string $file - theme folder name. + * @return array|mixed + */ function getThemeInfo($file) { - // return e_theme::getThemeInfo($file); - - $mes = e107::getMessage(); - $reject = array('e_.*'); - $handle2 = e107::getFile()->get_files(e_THEME.$file."/", "\.php|\.css|\.xml|preview\.jpg|preview\.png", $reject, 1); - - foreach ($handle2 as $fln) - { - $file2 = str_replace(e_THEME.$file."/", "", $fln['path']).$fln['fname']; - - $themeArray[$file]['files'][] = $file2; - - if(strpos($file2, "preview.") !== false) - { - $themeArray[$file]['preview'] = e_THEME.$file."/".$file2; - } - - // ---------------- get information string for css file - Legacy mode (no theme.xml) - - if(strpos($file2, ".css") !== false && strpos($file2, "menu.css") === false && strpos($file2, "e_") !== 0) - { - if($cssContents = file_get_contents(e_THEME.$file."/".$file2)) - { - $nonadmin = preg_match('/\* Non-Admin(.*?)\*\//', $cssContents) ? true : false; - preg_match('/\* info:(.*?)\*\//', $cssContents, $match); - $match[1] = varset($match[1], ''); - $scope = ($nonadmin == true) ? 'front' : ''; - - - $themeArray[$file]['css'][] = array("name"=>$file2, "info"=>$match[1], "scope"=>$scope, "nonadmin"=>$nonadmin); - - } - //else - // { - // $mes->addDebug("Couldn't read file: ".e_THEME.$file."/".$file2); - // } - } - - - } // end while.. - - // Load Theme information and merge with existing array. theme.xml (v2.x theme) is given priority over theme.php (v1.x). - - if(in_array("theme.xml", $themeArray[$file]['files'])) - { - $themeArray[$file] = array_merge($themeArray[$file], $this->parse_theme_xml($file)); - } - elseif(in_array("theme.php", $themeArray[$file]['files'])) - { - $themeArray[$file] = array_merge($themeArray[$file], $this->parse_theme_php($file)); - } - - if(!empty($themeArray[$file]['css']) && count($themeArray[$file]['css']) > 1) - { - $themeArray[$file]['multipleStylesheets'] = true; - } - - - - return $themeArray[$file]; - - + trigger_error(''.__METHOD__.' is deprecated. Use e107::getTheme($themedir)->get(); instead. ', E_USER_DEPRECATED); + return e107::getTheme($file)->get(); } /** @@ -2685,7 +2627,7 @@ class themeHandler foreach ($adminstyles as $as) { $style = str_replace(".php", "", $as['fname']); - $astext .= "\n"; + $astext .= "\n"; } $astext .= ""; @@ -3061,7 +3003,7 @@ class themeHandler $sql = e107::getDb(); $mes = e107::getMessage(); - $themeArray = $this->getThemes("id"); + $themeArray = e107::getTheme()->getList("id"); $name = ($name) ? $name : vartrue($themeArray[$this->id]); $layout = $pref['sitetheme_layouts'] = is_array($this->themeArray[$name]['layouts']) ? $this->themeArray[$name]['layouts'] : array(); @@ -3225,7 +3167,7 @@ class themeHandler if(!$l) { - $l = $this->getThemeInfo($theme); + $l = e107::getTheme($theme)->get(); // $this->getThemeInfo($theme); } @@ -3252,7 +3194,7 @@ class themeHandler $ns = e107::getRender(); $mes = e107::getMessage(); - $themeArray = $this->getThemes("id"); + $themeArray = e107::getTheme()->getList('id'); // $this->getThemes("id"); $pref['admintheme'] = $themeArray[$this->id]; $pref['admincss'] = file_exists(e_THEME.$pref['admintheme'].'/admin_dark.css') ? 'admin_dark.css' : 'admin_light.css'; $e107cache->clear_sys(); diff --git a/e107_tests/tests/unit/e_themeTest.php b/e107_tests/tests/unit/e_themeTest.php index c8914fe4b..83b8bd565 100644 --- a/e107_tests/tests/unit/e_themeTest.php +++ b/e107_tests/tests/unit/e_themeTest.php @@ -20,7 +20,7 @@ try { $this->tm = $this->make('e_theme'); - $this->tm->__construct(); + } catch (Exception $e) { @@ -176,8 +176,47 @@ } +/* + public function testGetThemesMigrations() + { + $thm = e107::getSingleton('themeHandler'); + + $tests = array(null, 'id', 'xml'); + + foreach($tests as $mode) + { + $old = $thm->getThemes($mode); + + $this->tm->__construct(['force'=>true]); + $new = $this->tm->getThemes($mode); + + $this->assertSame($old,$new); + } + } +*/ +/* + public function testThemeInfoMigration() + { + $thm = e107::getSingleton('themeHandler'); + + $name = 'bootstrap3'; + + $this->tm->__construct(['themedir'=>$name, 'force'=>true]); + $old = $thm->getThemeInfo($name); + + $new = $this->tm->get(); + + $this->assertNotEmpty($new, "New parsing of ".$name." returned null"); + $this->assertNotEmpty($old, "Old parsing of ".$name." returned null"); + + // unset($new['id']); // introduced. + + $this->assertSame($old, $new); + + } +*/ }