From 97e6a8a4e60d6258c916c39ccba7fafb25594158 Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Thu, 19 Jan 2023 15:07:15 -0800 Subject: [PATCH] Make renderWithTiwg a static function There's zero need to create a new instance of the class before calling it, so this removes a very small amount of overhead, but more importantly removes the need to create and define a new class of it each time, which makes the code very slightly cleaner --- phpstan.neon | 1 + src/AntCMS/AntCMS.php | 3 +-- src/AntCMS/AntPages.php | 3 +-- src/AntCMS/AntTwig.php | 2 +- src/Plugins/Admin/AdminPlugin.php | 9 +++------ 5 files changed, 7 insertions(+), 11 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 2298851..b9d8473 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,3 +5,4 @@ parameters: excludePaths: analyse: - src/Vendor + - src/Cache diff --git a/src/AntCMS/AntCMS.php b/src/AntCMS/AntCMS.php index db61324..f00314b 100644 --- a/src/AntCMS/AntCMS.php +++ b/src/AntCMS/AntCMS.php @@ -19,7 +19,6 @@ class AntCMS { $start_time = microtime(true); $content = $this->getPage($page); - $antTwig = new AntTwig; if (!$content || !is_array($content)) { $this->renderException("404"); @@ -36,7 +35,7 @@ class AntCMS 'AntCMSKeywords' => $content['keywords'], ); $pageTemplate = str_replace('', $markdown, $pageTemplate); - $pageTemplate = $antTwig->renderWithTiwg($pageTemplate, $params); + $pageTemplate = AntTwig::renderWithTiwg($pageTemplate, $params); $end_time = microtime(true); $elapsed_time = round($end_time - $start_time, 4); diff --git a/src/AntCMS/AntPages.php b/src/AntCMS/AntPages.php index 356ad86..9ab9724 100644 --- a/src/AntCMS/AntPages.php +++ b/src/AntCMS/AntPages.php @@ -94,8 +94,7 @@ class AntPages } } - $antTwig = new AntTwig; - $navHTML = $antTwig->renderWithTiwg($navTemplate, array('pages' => $pages)); + $navHTML = AntTwig::renderWithTiwg($navTemplate, array('pages' => $pages)); $antCache->setCache($cacheKey, $navHTML); return $navHTML; diff --git a/src/AntCMS/AntTwig.php b/src/AntCMS/AntTwig.php index 8f2017a..3377958 100644 --- a/src/AntCMS/AntTwig.php +++ b/src/AntCMS/AntTwig.php @@ -12,7 +12,7 @@ class AntTwig * @param string|null $theme * @return string */ - public function renderWithTiwg(string $content = '', array $params = array(), string $theme = null) + public static function renderWithTiwg(string $content = '', array $params = array(), string $theme = null) { $twigCache = AntConfig::currentConfig('enableCache') ? AntCachePath : false; $theme = $theme ?? AntConfig::currentConfig('activeTheme'); diff --git a/src/Plugins/Admin/AdminPlugin.php b/src/Plugins/Admin/AdminPlugin.php index 9e66d61..50d93a5 100644 --- a/src/Plugins/Admin/AdminPlugin.php +++ b/src/Plugins/Admin/AdminPlugin.php @@ -38,7 +38,6 @@ class AdminPlugin extends AntPlugin $this->managePages($route); default: - $antTwig = new AntTwig; $params = array( 'AntCMSTitle' => 'AntCMS Admin Dashboard', 'AntCMSDescription' => 'The AntCMS admin dashboard', @@ -50,7 +49,7 @@ class AdminPlugin extends AntPlugin $HTMLTemplate .= "AntCMS Configuration
\n"; $HTMLTemplate .= "Page management
\n"; $pageTemplate = str_replace('', $HTMLTemplate, $pageTemplate); - $pageTemplate = $antTwig->renderWithTiwg($pageTemplate, $params); + $pageTemplate = AntTwig::renderWithTiwg($pageTemplate, $params); echo $pageTemplate; break; @@ -68,7 +67,6 @@ class AdminPlugin extends AntPlugin $HTMLTemplate = $antCMS->getThemeTemplate('textarea_edit_layout'); $currentConfig = AntConfig::currentConfig(); $currentConfigFile = file_get_contents(antConfigFile); - $antTwig = new AntTwig; $params = array( 'AntCMSTitle' => 'AntCMS Configuration', 'AntCMSDescription' => 'The AntCMS configuration screen', @@ -119,7 +117,7 @@ class AdminPlugin extends AntPlugin } $pageTemplate = str_replace('', $HTMLTemplate, $pageTemplate); - $pageTemplate = $antTwig->renderWithTiwg($pageTemplate, $params); + $pageTemplate = AntTwig::renderWithTiwg($pageTemplate, $params); echo $pageTemplate; exit; @@ -135,7 +133,6 @@ class AdminPlugin extends AntPlugin $pageTemplate = $antCMS->getPageLayout(); $HTMLTemplate = $antCMS->getThemeTemplate('markdown_edit_layout'); $pages = AntPages::getPages(); - $antTwig = new AntTwig; $params = array( 'AntCMSTitle' => 'AntCMS Page Management', 'AntCMSDescription' => 'The AntCMS page management screen', @@ -247,7 +244,7 @@ class AdminPlugin extends AntPlugin } $pageTemplate = str_replace('', $HTMLTemplate, $pageTemplate); - $pageTemplate = $antTwig->renderWithTiwg($pageTemplate, $params); + $pageTemplate = AntTwig::renderWithTiwg($pageTemplate, $params); echo $pageTemplate; exit;