PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 137 lines | 67 code | 17 blank | 53 comment | 17 complexity | a23ef3c397af5f0b7ab1ac41a036d65a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  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 = false;
  39. $data = $this->cache->get($id);
  40. $locktest = new stdClass;
  41. $locktest->locked = null;
  42. $locktest->locklooped = null;
  43. if ($data === false)
  44. {
  45. $locktest = $this->cache->lock($id, null);
  46. // If the loop is completed and returned true it means the lock has been set
  47. // If looped is true try to get the cached data again; it could exist now
  48. if ($locktest->locked == true && $locktest->locklooped == true)
  49. {
  50. $data = $this->cache->get($id);
  51. }
  52. // False means that locking is either turned off or maxtime has been exceeded.
  53. // Execute the view.
  54. }
  55. if ($data !== false)
  56. {
  57. $data = unserialize(trim($data));
  58. if ($wrkarounds === true)
  59. {
  60. echo JCache::getWorkarounds($data);
  61. }
  62. else
  63. {
  64. // No workarounds, so all data is stored in one piece
  65. echo (isset($data)) ? $data : null;
  66. }
  67. if ($locktest->locked == true)
  68. {
  69. $this->cache->unlock($id);
  70. }
  71. return true;
  72. }
  73. /*
  74. * No hit so we have to execute the view
  75. */
  76. if (method_exists($view, $method))
  77. {
  78. // If previous lock failed try again
  79. if ($locktest->locked == false)
  80. {
  81. $locktest = $this->cache->lock($id);
  82. }
  83. // Capture and echo output
  84. ob_start();
  85. ob_implicit_flush(false);
  86. $view->$method();
  87. $data = ob_get_contents();
  88. ob_end_clean();
  89. echo $data;
  90. /*
  91. * For a view we have a special case. We need to cache not only the output from the view, but the state
  92. * of the document head after the view has been rendered. This will allow us to properly cache any attached
  93. * scripts or stylesheets or links or any other modifications that the view has made to the document object
  94. */
  95. $cached = array();
  96. $cached = $wrkarounds == true ? JCache::setWorkarounds($data) : $data;
  97. // Store the cache data
  98. $this->cache->store(serialize($cached), $id);
  99. if ($locktest->locked == true)
  100. {
  101. $this->cache->unlock($id);
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Generate a view cache id.
  108. *
  109. * @param object &$view The view object to cache output for
  110. * @param string $method The method name to cache for the view object
  111. *
  112. * @return string MD5 Hash : view cache id
  113. *
  114. * @since 11.1
  115. */
  116. protected function _makeId(&$view, $method)
  117. {
  118. return md5(serialize(array(JCache::makeId(), get_class($view), $method)));
  119. }
  120. }