diff options
| author | 2023-05-11 12:53:32 +0200 | |
|---|---|---|
| committer | 2023-05-11 12:53:32 +0200 | |
| commit | fe7d9bbcd68660a59b813346c236b61b25a51c80 (patch) | |
| tree | 95be837c2838659a3ea72974c9f9b18b1ee634ca /lib | |
| parent | 2343f0ded14ac6970575c23e4de34e536241b623 (diff) | |
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
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Minz/ActionController.php | 21 |
1 files changed, 16 insertions, 5 deletions
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; } |
