PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ankursaxena_2/joomla-platform
PHP | 124 lines | 57 code | 17 blank | 50 comment | 18 complexity | 2d0e77db3fcc52d930e2afe2f876dee1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Cache
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 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 $group The cache data group
  25. * @param string $id The cache data id
  26. *
  27. * @return boolean True if the cache is hit (false else)
  28. * @since 11.1
  29. */
  30. public function get(&$view, $method, $id=false, $wrkarounds=true)
  31. {
  32. // If an id is not given generate it from the request
  33. if ($id == false) {
  34. $id = $this->_makeId($view, $method);
  35. }
  36. $data = false;
  37. $data = $this->cache->get($id);
  38. $locktest = new stdClass;
  39. $locktest->locked = null;
  40. $locktest->locklooped = null;
  41. if ($data === false) {
  42. $locktest = $this->cache->lock($id,null);
  43. // If the loop is completed and returned true it means the lock has been set
  44. // If looped is true try to get the cached data again; it could exist now
  45. if ($locktest->locked == true && $locktest->locklooped == true) {
  46. $data = $this->cache->get($id);
  47. }
  48. // False means that locking is either turned off or maxtime has been exceeded.
  49. // Execute the view.
  50. }
  51. if ($data !== false) {
  52. $data = unserialize(trim($data));
  53. if ($wrkarounds === true) {
  54. echo JCache::getWorkarounds($data);
  55. }
  56. else { // No workarounds, so all data is stored in one piece
  57. echo (isset($data)) ? $data : null;
  58. }
  59. if ($locktest->locked == true) {
  60. $this->cache->unlock($id);
  61. }
  62. return true;
  63. }
  64. /*
  65. * No hit so we have to execute the view
  66. */
  67. if (method_exists($view, $method)) {
  68. // If previous lock failed try again
  69. if ($locktest->locked == false) {
  70. $locktest = $this->cache->lock($id);
  71. }
  72. // Capture and echo output
  73. ob_start();
  74. ob_implicit_flush(false);
  75. $view->$method();
  76. $data = ob_get_contents();
  77. ob_end_clean();
  78. echo $data;
  79. /*
  80. * For a view we have a special case. We need to cache not only the output from the view, but the state
  81. * of the document head after the view has been rendered. This will allow us to properly cache any attached
  82. * scripts or stylesheets or links or any other modifications that the view has made to the document object
  83. */
  84. $cached = array();
  85. $cached = $wrkarounds == true ? JCache::setWorkarounds($data) : $data;
  86. // Store the cache data
  87. $this->cache->store(serialize($cached), $id);
  88. if ($locktest->locked == true) {
  89. $this->cache->unlock($id);
  90. }
  91. }
  92. return false;
  93. }
  94. /**
  95. * Generate a view cache id.
  96. *
  97. * @param object $view The view object to cache output for
  98. * @param string $method The method name to cache for the view object
  99. *
  100. * @return string MD5 Hash : view cache id
  101. * @since 11.1
  102. */
  103. private function _makeId(&$view, $method)
  104. {
  105. return md5(serialize(array(JCache::makeId(), get_class($view), $method)));
  106. }
  107. }