PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/nooku/administrator/components/com_default/controllers/behaviors/cacheable.php

https://github.com/bhar1red/anahita
PHP | 196 lines | 89 code | 23 blank | 84 comment | 14 complexity | c6e30ac3f8ce49d2b7247ac87a1c5d0a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: cacheable.php 4628 2012-05-06 19:56:43Z johanjanssens $
  4. * @package Nooku_Components
  5. * @subpackage Default
  6. * @copyright Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved.
  7. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  8. * @link http://www.nooku.org
  9. */
  10. /**
  11. * Default Controller Cacheable Behavior
  12. *
  13. * @author Johan Janssens <johan@nooku.org>
  14. * @package Nooku_Components
  15. * @subpackage Default
  16. */
  17. class ComDefaultControllerBehaviorCacheable extends KControllerBehaviorAbstract
  18. {
  19. /**
  20. * The cached state of the resource
  21. *
  22. * @var boolean
  23. */
  24. protected $_output = '';
  25. /**
  26. * Fetch the unrendered view data from the cache
  27. *
  28. * @param KCommandContext A command context object
  29. * @return void
  30. */
  31. protected function _beforeControllerGet(KCommandContext $context)
  32. {
  33. $view = $this->getView();
  34. $cache = JFactory::getCache($this->_getGroup(), 'output');
  35. $key = $this->_getKey();
  36. if($data = $cache->get($key))
  37. {
  38. $data = unserialize($data);
  39. //Render the view output
  40. if($view instanceof KViewTemplate)
  41. {
  42. $context->result = $view->getTemplate()
  43. ->loadString($data['component'], array(), false)
  44. ->render();
  45. }
  46. else $context->result = $data['component'];
  47. $this->_output = $context->result;
  48. }
  49. }
  50. /**
  51. * Store the unrendered view data in the cache
  52. *
  53. * @param KCommandContext A command context object
  54. * @return void
  55. */
  56. protected function _afterControllerGet(KCommandContext $context)
  57. {
  58. if(empty($this->_output))
  59. {
  60. $view = $this->getView();
  61. $cache = JFactory::getCache($this->_getGroup(), 'output');
  62. $key = $this->_getKey();
  63. $data = array();
  64. //Store the unrendered view output
  65. if($view instanceof KViewTemplate) {
  66. $data['component'] = (string) $view->getTemplate();
  67. } else {
  68. $data['component'] = $context->result;
  69. }
  70. $cache->store(serialize($data), $key);
  71. }
  72. }
  73. /**
  74. * Return the cached data after read
  75. *
  76. * Only if cached data was found return it but allow the chain to continue to allow
  77. * processing all the read commands
  78. *
  79. * @param KCommandContext A command context object
  80. * @return void
  81. */
  82. protected function _afterControllerRead(KCommandContext $context)
  83. {
  84. if(!empty($this->_output)) {
  85. $context->result = $this->_output;
  86. }
  87. }
  88. /**
  89. * Return the cached data before browse
  90. *
  91. * Only if cached data was fetch return it and break the chain to dissallow any
  92. * further processing to take place
  93. *
  94. * @param KCommandContext A command context object
  95. * @return void
  96. */
  97. protected function _beforeControllerBrowse(KCommandContext $context)
  98. {
  99. if(!empty($this->_output))
  100. {
  101. $context->result = $this->_output;
  102. return false;
  103. }
  104. }
  105. /**
  106. * Clean the cache
  107. *
  108. * @param KCommandContext A command context object
  109. * @return boolean
  110. */
  111. protected function _afterControllerAdd(KCommandContext $context)
  112. {
  113. $status = $context->result->getStatus();
  114. if($status == KDatabase::STATUS_CREATED) {
  115. JFactory::getCache()->clean($this->_getGroup());
  116. }
  117. return true;
  118. }
  119. /**
  120. * Clean the cache
  121. *
  122. * @param KCommandContext A command context object
  123. * @return boolean
  124. */
  125. protected function _afterControllerDelete(KCommandContext $context)
  126. {
  127. $status = $context->result->getStatus();
  128. if($status == KDatabase::STATUS_DELETED) {
  129. JFactory::getCache()->clean($this->_getGroup());
  130. }
  131. return true;
  132. }
  133. /**
  134. * Clean the cache
  135. *
  136. * @param KCommandContext A command context object
  137. * @return boolean
  138. */
  139. protected function _afterControllerEdit(KCommandContext $context)
  140. {
  141. $status = $context->result->getStatus();
  142. if($status == KDatabase::STATUS_UPDATED) {
  143. JFactory::getCache()->clean($this->_getGroup());
  144. }
  145. return true;
  146. }
  147. /**
  148. * Generate a cache key
  149. *
  150. * The key is based on the layout, format and model state
  151. *
  152. * @return string
  153. */
  154. protected function _getKey()
  155. {
  156. $view = $this->getView();
  157. $state = $this->getModel()->getState()->toArray();
  158. $key = $view->getLayout().'-'.$view->getFormat().':'.md5(http_build_query($state));
  159. return $key;
  160. }
  161. /**
  162. * Generate a cache group
  163. *
  164. * The group is based on the component identifier
  165. *
  166. * @return string
  167. */
  168. protected function _getGroup()
  169. {
  170. $group = $this->_mixer->getIdentifier();
  171. return $group;
  172. }
  173. }