You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.3 KiB

  1. <?php
  2. /**
  3. * Рутовый класс для любого действия.
  4. * Описывает основной функционал для работы с классами действий.
  5. *
  6. * @copyright
  7. * @link
  8. * @package Majestic
  9. * @subpackage Action
  10. * @since
  11. * @version SVN: $Id$
  12. * @filesource $URL$
  13. */
  14. abstract class Action
  15. {
  16. public $template; //шаблон действия
  17. public $template_dir = '.'; //путь к шаблону действия
  18. public $class; //имя дочернего класса
  19. protected $templater; //шаблонизатор
  20. public function __construct()
  21. {
  22. $this->class = get_class($this);
  23. $this->template = substr($this->class, 0, -strlen(ACTION_POSTFIX));
  24. if (CACHE_ENABLE && ($cache_name = $this->getCacheKey()) !== false) {
  25. $cache = new Cache($this->class.'_'.$cache_name, $this->getCacheTime());
  26. if ($cache->isCached()) {
  27. $this->restore($cache->load());
  28. } else {
  29. $this->init();
  30. $cache->save($this->store());
  31. }
  32. } else {
  33. $this->init();
  34. }
  35. }
  36. /**
  37. * Выдает результат действия.
  38. *
  39. * @return string
  40. */
  41. public function display()
  42. {
  43. $this->templater = Load::templater();
  44. $this->prepare();
  45. $this->templater->setPath(ACTION_TPL_PATH.'/'.$this->template_dir);
  46. return $this->templater->fetch($this->template.'.tpl');
  47. }
  48. /**
  49. * Инициализация данных действия.
  50. * Тут должна быть ВСЯ работа с установкой данных,
  51. * дабы потом они нормально закешировались.
  52. * Этот метод НЕ исполняется при срабатывании кеша.
  53. *
  54. */
  55. abstract protected function init();
  56. /**
  57. * Подготовка данных для шаблона.
  58. * Этот метод выполняется ВСЕГДА
  59. */
  60. protected function prepare() {}
  61. /**
  62. * Возвращает имя файла для кеширования
  63. * Переопределяется в доченрих классах.
  64. *
  65. * @return false/string - имя файла кеша либо false если кеш запрещен
  66. */
  67. protected function getCacheKey()
  68. {
  69. return false;
  70. }
  71. /**
  72. * Возвращает время жизни кеша в секундах
  73. * При отрицательном значении кеш вечен.
  74. * Переопределяется в доченрих классах.
  75. *
  76. * @return false/integer - время жизни кеша либо false если кеш запрещен
  77. */
  78. protected function getCacheTime()
  79. {
  80. return 0;
  81. }
  82. /**
  83. * Выдает строку для кеширования.
  84. */
  85. protected function store()
  86. {
  87. return serialize(get_object_vars($this));
  88. }
  89. /**
  90. * Разбирает строку кеша, созданную методом store
  91. */
  92. protected function restore($data)
  93. {
  94. foreach(unserialize($data) as $key => $val) {
  95. $this->$key = $val;
  96. }
  97. }
  98. }
  99. ?>