phplibrary/src/View/View.php

208 lines
5.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace ctiso\View;
use Exception;
class View extends \stdClass
{
protected $_section = array(); // Вложенные шаблоны
// Блоки
protected $_stylesheet = array(); // Массив стилей текущего шаблона
protected $_script = array(); // Массив скриптов текущего шаблона
public $_scriptstring = array();
protected $_startup = array();
protected $_values = array();
protected $_title = null; // Заголовок текущего шаблона
public $active_module;
public $module_action;
public $prefix;
public $suggestions; //подсказки
public $alias = array();
public $codeGenerator = null;
public $parent_view = null;
function __construct() {
}
/**
* Связывет переменную с вложенным шаблоном
*
* @param string $section переменная шаблона
* @param View|string $view вложенный шаблон
*/
public function setView($section, $view/*: View|string*/)
{
$this->_section [$section] = $view;
if (is_object($view)) {
$view->parent_view = $this;
}
}
public function assignValues($values)
{
$this->_values = $values;
$this->_values["suggestions"] = $this->suggestions;
}
public function setGlobal($name, $args)
{
$this->addScriptRaw("var " . $name . " = " . json_encode($args) . ";\n", false);
}
/**
* Добавляет скипт к текущему шаблону
*
* @param string $name путь к скрипту
*/
public function addScript($name)
{
$output = $this->resolveName($this->alias, $name . '?' . http_build_query($this->prefix));
$this->_script [] = $output;
}
/**
* Добавляет код скипта к текущему шаблону
*
* @param string $name строка javascript кода
*/
public function addScriptRaw($name, $startup = false)
{
if ($startup) {
$this->_startup [] = $name;
} else {
$this->_scriptstring [] = $name;
}
}
public function setPrefix($name, $val) {
$this->prefix[$name] = $val;
}
/**
* Добавляет стили к текущему шаблону
*
* @param string $name путь к стилю
*/
public function addStyleSheet($name)
{
$output = $this->resolveName($this->alias, $name . '?' . http_build_query($this->prefix));
$this->_stylesheet [] = $output;
}
/**
* Рекурсивно извлекает из значение свойства обьекта
*
* @param string $list Имя свойства
* @param boolean $flatten
*/
protected function doTree($list, $flatten = true)
{
$result = ($flatten == true) ? $this->$list : array($this->$list);
foreach ($this->_section as $value) {
if (is_object($value)) {
if ($list == '_script' || $list == '_stylesheet') {
$result = array_merge($result, $value->doTree($list, $flatten));
} else {
$result = array_merge($value->doTree($list, $flatten), $result);
}
}
}
return $result;
}
/*abstract*/ public function set($key, $value)
{
}
/**
* Обработка всех вложенных шаблонов
*
* @return string
*/
public function execute()
{
foreach ($this->_section as $key => $value) {
$this->set($key, (is_object($value)) ? $value->execute() : $value); // ?
}
}
/**
* Установка заголовка шаблона
*
* @param string $title
*/
public function setTitle($title)
{
$this->_title = $title;
}
protected function isNotNull($title)
{
return $title !== null;
}
function setAlias($alias)
{
$this->alias = $alias;
}
function addAlias($name, $path)
{
$this->alias[$name] = $path;
$this->set($name, $path);
}
function find_file($pathlist, $file) {
foreach($pathlist as $key => $www) {
if (file_exists($key . '/' . $file)) {
return $www . '/' . $file;
}
}
throw new Exception("file not found: $file");
}
function resolveName($alias, $file) {
list($type, $filename) = explode(":", $file, 2);
// Сделать поиск а не просто замену папки при совпадении имени
if (is_array($alias[$type])) {
$output = $this->find_file($alias[$type], $filename);
} else {
$output = $alias[$type] . '/' . $filename;
}
return $output;
}
function loadImports($importFile)
{
$types = array(
'js' => array($this, 'addScript'),
'css' => array($this, 'addStyleSheet')
);
// Подключение стилей и скриптов
if (file_exists($importFile)) {
$files = file($importFile);
foreach ($files as $file) {
// Получить расширение вместо strpos
$file = trim($file);
if (!empty($file)) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
call_user_func($types[$ext], $file);
}
}
}
}
public function resolveAllNames($alias, $list) {
$result = array();
foreach($list as $item) {
$result [] = $this->resolveName($alias, $item);
}
return $result;
}
}