PageRenderTime 43ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/cache/controller/view.php

https://bitbucket.org/eternaware/joomus
PHP | 137 lines | 66 code | 18 blank | 53 comment | 17 complexity | 4747483b1be5d751b26d65f319c52e20 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Cache
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Joomla! Cache view type object
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Cache
  15. * @since 11.1
  16. */
  17. class JCacheControllerView extends JCacheController
  18. {
  19. /**
  20. * Get the cached view data
  21. *
  22. * @param object &$view The view object to cache output for
  23. * @param string $method The method name of the view method to cache output for
  24. * @param string $id The cache data id
  25. * @param boolean $wrkarounds True to enable workarounds.
  26. *
  27. * @return boolean True if the cache is hit (false else)
  28. *
  29. * @since 11.1
  30. */
  31. public function get(&$view, $method, $id = false, $wrkarounds = true)
  32. {
  33. // If an id is not given generate it from the request
  34. if ($id == false)
  35. {
  36. $id = $this->_makeId($view, $method);
  37. }
  38. $data = $this->cache->get($id);
  39. $locktest = new stdClass;
  40. $locktest->locked = null;
  41. $locktest->locklooped = null;
  42. if ($data === false)
  43. {
  44. $locktest = $this->cache->lock($id, null);
  45. // If the loop is completed and returned true it means the lock has been set.
  46. // If looped is true try to get the cached data again; it could exist now.
  47. if ($locktest->locked == true && $locktest->locklooped == true)
  48. {
  49. $data = $this->cache->get($id);
  50. }
  51. // False means that locking is either turned off or maxtime has been exceeded.
  52. // Execute the view.
  53. }
  54. if ($data !== false)
  55. {
  56. $data = unserialize(trim($data));
  57. if ($wrkarounds === true)
  58. {
  59. echo JCache::getWorkarounds($data);
  60. }
  61. else
  62. {
  63. // No workarounds, so all data is stored in one piece
  64. echo (isset($data)) ? $data : null;
  65. }
  66. if ($locktest->locked == true)
  67. {
  68. $this->cache->unlock($id);
  69. }
  70. return true;
  71. }
  72. /*
  73. * No hit so we have to execute the view
  74. */
  75. if (method_exists($view, $method))
  76. {
  77. // If previous lock failed try again
  78. if ($locktest->locked == false)
  79. {
  80. $locktest = $this->cache->lock($id);
  81. }
  82. // Capture and echo output
  83. ob_start();
  84. ob_implicit_flush(false);
  85. $view->$method();
  86. $data = ob_get_contents();
  87. ob_end_clean();
  88. echo $data;
  89. /*
  90. * For a view we have a special case. We need to cache not only the output from the view, but the state
  91. * of the document head after the view has been rendered. This will allow us to properly cache any attached
  92. * scripts or stylesheets or links or any other modifications that the view has made to the document object
  93. */
  94. $cached = array();
  95. $cached = $wrkarounds == true ? JCache::setWorkarounds($data) : $data;
  96. // Store the cache data
  97. $this->cache->store(serialize($cached), $id);
  98. if ($locktest->locked == true)
  99. {
  100. $this->cache->unlock($id);
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * Generate a view cache id.
  107. *
  108. * @param object &$view The view object to cache output for
  109. * @param string $method The method name to cache for the view object
  110. *
  111. * @return string MD5 Hash : view cache id
  112. *
  113. * @since 11.1
  114. */
  115. protected function _makeId(&$view, $method)
  116. {
  117. return md5(serialize(array(JCache::makeId(), get_class($view), $method)));
  118. }
  119. }