PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

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