From fe7d9bbcd68660a59b813346c236b61b25a51c80 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Thu, 11 May 2023 12:53:32 +0200 Subject: Typed view model classes (#5380) * Typed view model classes * Add ability to provide a typed view model class to a controller * Use `::class` instead of string for referring to classes * Examplified with `stats` and `javascript` controllers / views (more to do) * Also useful for extensions (my usecase today), which did not have the ability to define own view model attributes before. * Typo --- lib/Minz/ActionController.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'lib/Minz/ActionController.php') diff --git a/lib/Minz/ActionController.php b/lib/Minz/ActionController.php index c9551a016..553862bab 100644 --- a/lib/Minz/ActionController.php +++ b/lib/Minz/ActionController.php @@ -21,16 +21,27 @@ class Minz_ActionController { protected $view; /** - * Gives the possibility to override the default View type. + * Gives the possibility to override the default view model type. * @var class-string + * @deprecated Use constructor with view type instead */ - public static $viewType = 'Minz_View'; + public static $defaultViewType = Minz_View::class; - public function __construct () { + /** + * @phpstan-param class-string|'' $viewType + * @param string $viewType Name of the class (inheriting from Minz_View) to use for the view model + */ + public function __construct(string $viewType = '') { $this->csp_policies = self::$csp_default; $view = null; - if (class_exists(self::$viewType)) { - $view = new self::$viewType(); + if ($viewType !== '' && class_exists($viewType)) { + $view = new $viewType(); + if (!($view instanceof Minz_View)) { + $view = null; + } + } + if ($view === null && class_exists(self::$defaultViewType)) { + $view = new self::$defaultViewType(); if (!($view instanceof Minz_View)) { $view = null; } -- cgit v1.2.3