diff options
Diffstat (limited to 'ui/include/classes/mvc/CView.php')
-rw-r--r-- | ui/include/classes/mvc/CView.php | 128 |
1 files changed, 61 insertions, 67 deletions
diff --git a/ui/include/classes/mvc/CView.php b/ui/include/classes/mvc/CView.php index 92f0a769c16..89dfaef5093 100644 --- a/ui/include/classes/mvc/CView.php +++ b/ui/include/classes/mvc/CView.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types = 0); /* ** Zabbix ** Copyright (C) 2001-2022 Zabbix SIA @@ -26,61 +26,45 @@ class CView { /** * Directory list of MVC views ordered by search priority. - * - * @static - * - * @var array */ - private static $directories = ['local/app/views', 'app/views', 'include/views']; + private static array $directories = ['local/app/views', 'app/views', 'include/views']; /** * Indicates support of web layout modes. - * - * @var boolean */ - private $layout_modes_enabled = false; + private bool $layout_modes_enabled = false; /** * Explicitly set layout mode. - * - * @var int */ - private $layout_mode; + private ?int $layout_mode = null; /** - * View name. - * - * @var string + * Directory where the view file was found. */ - private $name; + private ?string $directory = null; + + private string $assets_path = 'assets'; /** - * Data provided for view. - * - * @var array + * View name. */ - private $data; + private string $name; /** - * Directory where the view file was found. - * - * @var string + * List of JavaScript files for inclusion into HTML page using <script src="...">. */ - private $directory; + private array $js_files = []; /** - * List of JavaScript files for inclusion into a HTML page using <script src="...">. - * - * @var array + * List of CSS files for inclusion into HTML page using <link rel="stylesheet" type="text/css" src="...">. */ - private $js_files = []; + private array $css_files = []; /** - * List of CSS files for inclusion into a HTML page using <link rel="stylesheet" type="text/css" src="...">. - * - * @var array + * Data provided for view. */ - private $css_files = []; + private array $data; /** * Create a view based on view name and data. @@ -91,7 +75,7 @@ class CView { * @throws InvalidArgumentException if view name not valid. * @throws RuntimeException if view not found or not readable. */ - public function __construct($name, array $data = []) { + public function __construct(string $name, array $data = []) { if (!preg_match('/^[a-z]+(\/[a-z]+)*(\.[a-z]+)*$/', $name)) { throw new InvalidArgumentException(sprintf('Invalid view name: "%s".', $name)); } @@ -100,6 +84,7 @@ class CView { foreach (self::$directories as $directory) { $file_path = $directory.'/'.$name.'.php'; + if (is_file($file_path)) { $this->directory = $directory; break; @@ -118,15 +103,27 @@ class CView { $this->data = $data; } + public function getDirectory(): string { + return $this->directory; + } + + public function setAssetsPath(string $asset_path): self { + $this->assets_path = $asset_path; + + return $this; + } + + public function getAssetsPath(): string { + RETURN $this->assets_path; + } + /** * Render view and return the output. * Note: view should only output textual content like HTML, JSON, scripts or similar. * * @throws RuntimeException if view not found, not readable or returned false. - * - * @return string */ - public function getOutput() { + public function getOutput(): string { $data = $this->data; $file_path = $this->directory.'/'.$this->name.'.php'; @@ -148,17 +145,15 @@ class CView { * - JavaScript file will be searched in the "js" subdirectory of the view file. * - A copy of $data variable will be available for using within the file. * - * @param string $file_name - * @param array $data - * - * @throws RuntimeException if the file not found, not readable or returned false. + * @param string $file_name + * @param array|null $data * * @return string */ - public function readJsFile(string $file_name, ?array $data = null): string { - $data = ($data === null) ? $this->data : $data; + public function readJsFile(string $file_name, array $data = null, $relative_dir = '/js'): string { + $data = $data ?? $this->data; - $file_path = $this->directory.'/js/'.$file_name; + $file_path = $this->directory.$relative_dir.'/'.$file_name; ob_start(); @@ -177,40 +172,37 @@ class CView { * - JavaScript file will be searched in the "js" subdirectory of the view file. * - A copy of $data variable will be available for using within the file. * - * @param string $file_name - * @param array $data - * * @throws RuntimeException if the file not found, not readable or returned false. */ - public function includeJsFile(string $file_name, array $data = null) { + public function includeJsFile(string $file_name, array $data = null): self { echo $this->readJsFile($file_name, $data); + + return $this; } /** * Add a native JavaScript file to this view. - * - * @param string $src */ - public function addJsFile($src) { - $this->js_files[] = $src; + public function addJsFile(string $js): self { + $this->js_files[] = $js; + + return $this; } /** * Get list of native JavaScript files added to this view. - * - * @return array */ - public function getJsFiles() { + public function getJsFiles(): array { return $this->js_files; } /** * Add a CSS file to this view. - * - * @param string $src */ - public function addCssFile($src) { - $this->css_files[] = $src; + public function addCssFile($css): self { + $this->css_files[] = $css; + + return $this; } /** @@ -218,15 +210,17 @@ class CView { * * @return array */ - public function getCssFiles() { + public function getCssFiles(): array { return $this->css_files; } /** * Enable support of web layout modes. */ - public function enableLayoutModes() { + public function enableLayoutModes(): self { $this->layout_modes_enabled = true; + + return $this; } /** @@ -234,8 +228,10 @@ class CView { * * @param int $layout_mode ZBX_LAYOUT_NORMAL | ZBX_LAYOUT_KIOSKMODE */ - public function setLayoutMode(int $layout_mode): void { + public function setLayoutMode(int $layout_mode): self { $this->layout_mode = $layout_mode; + + return $this; } /** @@ -243,9 +239,9 @@ class CView { * * @return int ZBX_LAYOUT_NORMAL | ZBX_LAYOUT_KIOSKMODE */ - public function getLayoutMode() { + public function getLayoutMode(): int { if ($this->layout_modes_enabled) { - return ($this->layout_mode !== null) ? $this->layout_mode : CViewHelper::loadLayoutMode(); + return $this->layout_mode ?? CViewHelper::loadLayoutMode(); } return ZBX_LAYOUT_NORMAL; @@ -253,11 +249,9 @@ class CView { /** * Register custom directory of MVC views. The last registered will have the first priority. - * - * @param string $directory */ - public static function registerDirectory($directory) { - if (!in_array($directory, self::$directories)) { + public static function registerDirectory(string $directory): void { + if (!in_array($directory, self::$directories, true)) { array_unshift(self::$directories, $directory); } } |