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.

107 lines
3.2 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(ACTION_TPL_PATH.'/'.$this->template_dir);
  44. $this->prepare();
  45. return $this->templater->fetch($this->template.'.tpl');
  46. }
  47. /**
  48. * Инициализация данных действия.
  49. * Тут должна быть ВСЯ работа с установкой данных,
  50. * дабы потом они нормально закешировались.
  51. * Этот метод НЕ исполняется при срабатывании кеша.
  52. *
  53. */
  54. abstract protected function init();
  55. /**
  56. * Подготовка данных для шаблона.
  57. * Этот метод выполняется ВСЕГДА
  58. */
  59. protected function prepare() {}
  60. /**
  61. * Возвращает имя файла для кеширования
  62. * Переопределяется в доченрих классах.
  63. *
  64. * @return false/string - имя файла кеша либо false если кеш запрещен
  65. */
  66. protected function getCacheKey()
  67. {
  68. return false;
  69. }
  70. /**
  71. * Возвращает время жизни кеша в секундах
  72. * При отрицательном значении кеш вечен.
  73. * Переопределяется в доченрих классах.
  74. *
  75. * @return false/integer - время жизни кеша либо false если кеш запрещен
  76. */
  77. protected function getCacheTime()
  78. {
  79. return 0;
  80. }
  81. /**
  82. * Выдает строку для кеширования.
  83. */
  84. protected function store()
  85. {
  86. return serialize(get_object_vars($this));
  87. }
  88. /**
  89. * Разбирает строку кеша, созданную методом store
  90. */
  91. protected function restore($data)
  92. {
  93. foreach(unserialize($data) as $key => $val) {
  94. $this->$key = $val;
  95. }
  96. }
  97. }
  98. ?>