Files
majestic/classes/Action.class.php
akulikov 83d25fa24b News are coming
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/trunk@38 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
2008-12-29 12:42:57 +00:00

109 lines
3.3 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
/**
* Рутовый класс для любого действия.
* Описывает основной функционал для работы с классами действий.
*
* @copyright
* @link
* @package Majestic
* @subpackage Action
* @since
* @version SVN: $Id$
* @filesource $URL$
*/
abstract class Action
{
public $template; //шаблон действия
public $template_dir = '.'; //путь к шаблону действия
public $class; //имя дочернего класса
protected $templater; //шаблонизатор
public function __construct()
{
$this->class = get_class($this);
$this->template = substr($this->class, 0, -strlen(ACTION_POSTFIX));
if (CACHE_ENABLE && ($cache_name = $this->getCacheKey()) !== false) {
$cache = new Cache($this->class.'_'.$cache_name, $this->getCacheTime());
if ($cache->isCached()) {
$this->restore($cache->load());
} else {
$this->init();
$cache->save($this->store());
}
} else {
$this->init();
}
}
/**
* Выдает результат действия.
*
* @return string
*/
public function display()
{
$this->templater = Load::templater();
$this->prepare();
$this->templater->setPath(ACTION_TPL_PATH.'/'.$this->template_dir);
return $this->templater->fetch($this->template.'.tpl');
}
/**
* Инициализация данных действия.
* Тут должна быть ВСЯ работа с установкой данных,
* дабы потом они нормально закешировались.
* Этот метод НЕ исполняется при срабатывании кеша.
*
*/
abstract protected function init();
/**
* Подготовка данных для шаблона.
* Этот метод выполняется ВСЕГДА
*/
protected function prepare() {}
/**
* Возвращает имя файла для кеширования
* Переопределяется в доченрих классах.
*
* @return false/string - имя файла кеша либо false если кеш запрещен
*/
protected function getCacheKey()
{
return false;
}
/**
* Возвращает время жизни кеша в секундах
* При отрицательном значении кеш вечен.
* Переопределяется в доченрих классах.
*
* @return false/integer - время жизни кеша либо false если кеш запрещен
*/
protected function getCacheTime()
{
return 0;
}
/**
* Выдает строку для кеширования.
*/
protected function store()
{
return serialize(get_object_vars($this));
}
/**
* Разбирает строку кеша, созданную методом store
*/
protected function restore($data)
{
foreach(unserialize($data) as $key => $val) {
$this->$key = $val;
}
}
}
?>