Source
<?php declare(strict_types = 0);
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* Class for rendering views.
*/
class CView {
/**
* Directory list of MVC views ordered by search priority.
*/
private static array $directories = ['local/app/views', 'app/views', 'include/views'];
/**
* Indicates support of web layout modes.
*/
private bool $layout_modes_enabled = false;
/**
* Explicitly set layout mode.
*/
private ?int $layout_mode = null;
/**
* Directory where the view file was found.
*/
private ?string $directory = null;
private string $assets_path = 'assets';
/**
* View name.
*/
private string $name;
/**
* List of JavaScript files for inclusion into HTML page using <script src="...">.
*/
private array $js_files = [];
/**
* List of CSS files for inclusion into HTML page using <link rel="stylesheet" type="text/css" src="...">.
*/
private array $css_files = [];
/**
* Data provided for view.
*/
private array $data;
/**
* Create a view based on view name and data.
*
* @param string $name View name to search for.
* @param array $data Accessible data within the view.
*
* @throws InvalidArgumentException if view name not valid.
* @throws RuntimeException if view not found or not readable.
*/
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));
}
$file_path = null;
foreach (self::$directories as $directory) {
$file_path = $directory.'/'.$name.'.php';
if (is_file($file_path)) {
$this->directory = $directory;
break;
}
}
if ($this->directory === null) {
throw new RuntimeException(sprintf('View not found: "%s".', $name));
}