Регистр файлов
This commit is contained in:
parent
4fd0187ea6
commit
c8958cbee0
83 changed files with 25 additions and 53 deletions
40
src/View/Composite.php
Normal file
40
src/View/Composite.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
// CompositeView+PHPTAL
|
||||
class View_Composite extends View_Top
|
||||
{
|
||||
private $tal;
|
||||
|
||||
function __construct($file)
|
||||
{
|
||||
parent::__construct($file);
|
||||
|
||||
$this->tal = new PHPTAL($file);
|
||||
$this->tal->stripComments(true);
|
||||
}
|
||||
|
||||
function set($key, $val)
|
||||
{
|
||||
if ($key == 'title') {
|
||||
$this->setTitle($val);
|
||||
}
|
||||
$this->tal->set($key, $val);
|
||||
}
|
||||
|
||||
function __set($key, $val)
|
||||
{
|
||||
$this->tal->set($key, $val);
|
||||
}
|
||||
|
||||
function setTranslator($tr)
|
||||
{
|
||||
$this->tal->setTranslator($tr);
|
||||
}
|
||||
|
||||
function execute()
|
||||
{
|
||||
parent::execute();
|
||||
// postProcess
|
||||
return $this->tal->execute();
|
||||
}
|
||||
}
|
||||
259
src/View/Top.php
Normal file
259
src/View/Top.php
Normal file
|
|
@ -0,0 +1,259 @@
|
|||
<?php
|
||||
|
||||
class View_Top // AbstractCompositeView
|
||||
{
|
||||
protected $_section = array(); // Вложенные шаблоны
|
||||
// Блоки
|
||||
protected $_stylesheet = array(); // Массив стилей текущего шаблона
|
||||
protected $_script = array(); // Массив скриптов текущего шаблона
|
||||
protected $_scriptstring = array();
|
||||
protected $_startup = array();
|
||||
|
||||
protected $_title = null; // Заголовок текущего шаблона
|
||||
public $alias = array();
|
||||
|
||||
function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Связывет переменную с вложенным шаблоном
|
||||
*
|
||||
* @param string $section переменная шаблона
|
||||
* @param CompositeView $view вложенный шаблон
|
||||
*/
|
||||
public function setView($section, /*CompositeView*/ $view)
|
||||
{
|
||||
$this->_section [$section] = $view;
|
||||
}
|
||||
|
||||
public function jGrowl($message, $args)
|
||||
{
|
||||
$this->addScriptRaw('$.jGrowl("' . $message . '", ' . json::encode($args) . ");\n", true);
|
||||
}
|
||||
|
||||
public function setGlobal($name, $args)
|
||||
{
|
||||
$this->addScriptRaw("var " . $name . " = " . json::encode($args) . ";\n", false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавляет скипт к текущему шаблону
|
||||
*
|
||||
* @param string $name путь к скрипту
|
||||
*/
|
||||
public function addScript($name)
|
||||
{
|
||||
$this->_script [] = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавляет код скипта к текущему шаблону
|
||||
*
|
||||
* @param string $name строка javascript кода
|
||||
*/
|
||||
public function addScriptRaw($name, $startup = false)
|
||||
{
|
||||
if ($startup) {
|
||||
$this->_startup [] = $name;
|
||||
} else {
|
||||
$this->_scriptstring [] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавляет стили к текущему шаблону
|
||||
*
|
||||
* @param string $name путь к стилю
|
||||
*/
|
||||
public function addStyleSheet($name)
|
||||
{
|
||||
$this->_stylesheet [] = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Рекурсивно извлекает из значение свойства обьекта
|
||||
*
|
||||
* @param string $list Имя свойства
|
||||
* @param boolean $flatten
|
||||
*/
|
||||
private function doTree($list, $flatten = true) {
|
||||
$result = ($flatten == true) ? $this->$list : array($this->$list);
|
||||
foreach ($this->_section as $key => $value) {
|
||||
if (is_object($value)) $result = array_merge($value->doTree($list, $flatten), $result);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Массив имен файлов скриптов
|
||||
*
|
||||
* return array
|
||||
*/
|
||||
public function getScripts()
|
||||
{
|
||||
return $this->doTree('_script');
|
||||
}
|
||||
|
||||
function resolveAlias($alias, $list)
|
||||
{
|
||||
$result = array();
|
||||
foreach($list as $item) {
|
||||
$result [] = strtr($item, $alias);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Строка со скриптом
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getScriptRaw()
|
||||
{
|
||||
return implode("\n", $this->doTree('_scriptstring'));
|
||||
}
|
||||
|
||||
public function getScriptStartup()
|
||||
{
|
||||
return implode("\n", $this->doTree('_startup'));
|
||||
}
|
||||
|
||||
/*abstract*/ public function set($key, $value)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Массив имен файлов стилей
|
||||
*
|
||||
* return array
|
||||
*/
|
||||
public function getStyleSheet()
|
||||
{
|
||||
return $this->doTree('_stylesheet');
|
||||
}
|
||||
|
||||
/**
|
||||
* Обработка всех вложенных шаблонов
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
private function isNotNull($title)
|
||||
{
|
||||
return $title !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Общая строка заголовка
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return implode(" - ", array_filter($this->doTree('_title', false), array($this, 'isNotNull')));
|
||||
}
|
||||
|
||||
private function findGroup($groups, $file)
|
||||
{
|
||||
foreach($groups as $key => $group) {
|
||||
if(in_array($file, $group)) {
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function groupFiles(array $list, $debug)
|
||||
{
|
||||
$debug = ($debug) ? 'debug=1' : '';
|
||||
$path = parse_url(WWW_PATH, PHP_URL_PATH);
|
||||
$list = array_reverse($list);
|
||||
// Группы нужно передвавать как параметр !!!
|
||||
$groups = array(
|
||||
'table' => array($path . '/js/table.js', $path . '/js/listtable.js',
|
||||
$path . '/js/page.js', $path . '/js/pagemenu.js'),
|
||||
'base' => array($path . '/js/admin.js', $path . '/js/cookie.js'),
|
||||
);
|
||||
$use = array();
|
||||
|
||||
$result = array();
|
||||
foreach ($list as $file) {
|
||||
$name = $this->findGroup($groups, $file);
|
||||
if($name) {
|
||||
$use [$name] = 1;
|
||||
} else {
|
||||
$result [] = $file;
|
||||
}
|
||||
}
|
||||
$list = array();
|
||||
foreach ($use as $name => $value) {
|
||||
$list [] = WWW_PATH . "/min/?$debug&f=" . implode(",", $groups[$name]);
|
||||
}
|
||||
return array_merge($list, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Обработка шаблона
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
|
||||
$alias = $this->doTree('alias');
|
||||
|
||||
// require_once 'minify.php';
|
||||
// Скрипты и стили
|
||||
$this->set('scripts', array_unique($this->groupFiles($this->resolveAlias($alias, $this->getScripts()), false)));
|
||||
$this->set('stylesheet', array_unique($this->groupFiles($this->resolveAlias($alias, $this->getStyleSheet()), false)));
|
||||
|
||||
$this->set('scriptstring', $this->getScriptRaw());
|
||||
$this->set('startup', $this->getScriptStartup());
|
||||
$this->set('title', $this->getTitle());
|
||||
//
|
||||
return $this->execute(); // execute+phptal ??
|
||||
}
|
||||
|
||||
function setAlias($alias)
|
||||
{
|
||||
$this->alias = $alias;
|
||||
}
|
||||
|
||||
function addAlias($name, $path)
|
||||
{
|
||||
$this->alias['${' . $name . '}'] = $path;
|
||||
$this->set($name, $path);
|
||||
}
|
||||
|
||||
function loadImports($importFile)
|
||||
{
|
||||
// Подключение стилей и скриптов
|
||||
if (file_exists($importFile)) {
|
||||
$import = file_get_contents($importFile);
|
||||
$files = explode("\n", $import);
|
||||
foreach ($files as $file) {
|
||||
if (strpos($file, ".js") !== false) {
|
||||
$this->addScript(strtr(trim($file), $this->alias));
|
||||
} else if(strpos($file, ".css") !== false) {
|
||||
$this->addStyleSheet(strtr(trim($file), $this->alias));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
40
src/View/View.php
Normal file
40
src/View/View.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
// Класс отображения
|
||||
// CompositeView !! Composite pattern
|
||||
|
||||
class View
|
||||
{
|
||||
protected $document;
|
||||
protected $values;
|
||||
|
||||
public function __construct ($document)
|
||||
{
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->values[$key] = $value;
|
||||
}
|
||||
|
||||
public function __set($key, $value)
|
||||
{
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$result = $this->values;
|
||||
return self::getTemplateContent ($this->document, $result);
|
||||
}
|
||||
|
||||
static function getTemplateContent($document, $result)
|
||||
{
|
||||
ob_start ();
|
||||
include ($document);
|
||||
$result = ob_get_contents ();
|
||||
ob_clean ();
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue