From 1034dc5ee52d6d8afd9e509296ae9c21e407acf0 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:50:02 +1300 Subject: [PATCH] ENH Improve type safety to support refactored template layer (#3010) --- code/Controllers/ContentController.php | 24 +++----- code/Model/SiteTree.php | 4 +- tests/behat/src/ThemeContext.php | 2 +- .../php/Controllers/ContentControllerTest.php | 31 ++++++---- .../DummyTemplateContentController.php | 15 +++++ .../DummyTemplateEngine.php | 56 +++++++++++++++++++ 6 files changed, 101 insertions(+), 31 deletions(-) create mode 100644 tests/php/Controllers/ContentControllerTest/DummyTemplateContentController.php create mode 100644 tests/php/Controllers/ContentControllerTest/DummyTemplateEngine.php diff --git a/code/Controllers/ContentController.php b/code/Controllers/ContentController.php index ad3397e061..f92dca3368 100644 --- a/code/Controllers/ContentController.php +++ b/code/Controllers/ContentController.php @@ -290,16 +290,6 @@ public function getMenu($level = 1) return new ArrayList($visible); } - /** - * @return ArrayList - * @deprecated 5.4.0 Use getMenu() instead. You can continue to use $Menu in templates. - */ - public function Menu($level) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Use getMenu() instead. You can continue to use $Menu in templates.'); - return $this->getMenu($level); - } - /** * Returns the default log-in form. * @@ -416,23 +406,23 @@ public function getViewer($action) $action = $action === 'index' ? '' : '_' . $action; $templatesFound = []; - // Find templates for the record + action together - e.g. Page_action.ss + // Find templates for the record + action together - e.g. Page_action if ($this->dataRecord instanceof SiteTree) { $templatesFound[] = $this->dataRecord->getViewerTemplates($action); } - // Find templates for the controller + action together - e.g. PageController_action.ss - $templatesFound[] = SSViewer::get_templates_by_class(static::class, $action, Controller::class); + // Find templates for the controller + action together - e.g. PageController_action + $templatesFound[] = SSViewer::get_templates_by_class(static::class, $action ?? '', Controller::class); - // Find templates for the record without an action - e.g. Page.ss + // Find templates for the record without an action - e.g. Page if ($this->dataRecord instanceof SiteTree) { $templatesFound[] = $this->dataRecord->getViewerTemplates(); } - // Find the templates for the controller without an action - e.g. PageController.ss - $templatesFound[] = SSViewer::get_templates_by_class(static::class, "", Controller::class); + // Find the templates for the controller without an action - e.g. PageController + $templatesFound[] = SSViewer::get_templates_by_class(static::class, '', Controller::class); $templates = array_merge(...$templatesFound); - return SSViewer::create($templates); + return SSViewer::create($templates, $this->getTemplateEngine()); } } diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 2ee8e6b1d5..4d053e5fa0 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -1454,7 +1454,7 @@ public function MetaComponents() $tags['title'] = [ 'tag' => 'title', - 'content' => $this->obj('Title')->forTemplate() + 'content' => $this->obj('Title')?->forTemplate() ]; $generator = $this->getGenerator(); @@ -1601,7 +1601,7 @@ public function MetaTags($includeTitle = true) $tagString = implode("\n", $tags); if ($this->ExtraMeta) { - $tagString .= $this->obj('ExtraMeta')->forTemplate(); + $tagString .= $this->obj('ExtraMeta')?->forTemplate(); } $this->extend('updateMetaTags', $tagString); diff --git a/tests/behat/src/ThemeContext.php b/tests/behat/src/ThemeContext.php index ead1f8265d..3da77ee2a3 100644 --- a/tests/behat/src/ThemeContext.php +++ b/tests/behat/src/ThemeContext.php @@ -33,7 +33,7 @@ public function stepCreateTheme($theme) } /** - * Create a template within a test theme + * Create a template within a test theme. Only ss templates are supported. * * @Given /^a template "(?