/libraries/joomla/cache/handler/view.php

https://bitbucket.org/stager94/skmz-joomla · PHP · 150 lines · 62 code · 22 blank · 66 comment · 12 complexity · 40ec4c3e452ead17ce7948e9758fac73 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: view.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * Joomla! Cache view type object
  18. *
  19. * @package Joomla.Framework
  20. * @subpackage Cache
  21. * @since 1.5
  22. */
  23. class JCacheView extends JCache
  24. {
  25. /**
  26. * Get the cached view data
  27. *
  28. * @access public
  29. * @param object $view The view object to cache output for
  30. * @param string $method The method name of the view method to cache output for
  31. * @param string $group The cache data group
  32. * @param string $id The cache data id
  33. * @return boolean True if the cache is hit (false else)
  34. * @since 1.5
  35. */
  36. function get( &$view, $method, $id=false )
  37. {
  38. global $mainframe;
  39. // Initialize variables
  40. $data = false;
  41. // If an id is not given generate it from the request
  42. if ($id == false) {
  43. $id = $this->_makeId($view, $method);
  44. }
  45. $data = parent::get($id);
  46. if ($data !== false) {
  47. $data = unserialize($data);
  48. $document = &JFactory::getDocument();
  49. // Get the document head out of the cache.
  50. $document->setHeadData((isset($data['head'])) ? $data['head'] : array());
  51. // If the pathway buffer is set in the cache data, get it.
  52. if (isset($data['pathway']) && is_array($data['pathway']))
  53. {
  54. // Push the pathway data into the pathway object.
  55. $pathway = &$mainframe->getPathWay();
  56. $pathway->setPathway($data['pathway']);
  57. }
  58. // If a module buffer is set in the cache data, get it.
  59. if (isset($data['module']) && is_array($data['module']))
  60. {
  61. // Iterate through the module positions and push them into the document buffer.
  62. foreach ($data['module'] as $name => $contents) {
  63. $document->setBuffer($contents, 'module', $name);
  64. }
  65. }
  66. // Get the document body out of the cache.
  67. echo (isset($data['body'])) ? $data['body'] : null;
  68. return true;
  69. }
  70. /*
  71. * No hit so we have to execute the view
  72. */
  73. if (method_exists($view, $method))
  74. {
  75. $document = &JFactory::getDocument();
  76. // Get the modules buffer before component execution.
  77. $buffer1 = $document->getBuffer();
  78. // Make sure the module buffer is an array.
  79. if (!isset($buffer1['module']) || !is_array($buffer1['module'])) {
  80. $buffer1['module'] = array();
  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. // View body data
  96. $cached['body'] = $data;
  97. // Document head data
  98. $cached['head'] = $document->getHeadData();
  99. // Pathway data
  100. $pathway = &$mainframe->getPathWay();
  101. $cached['pathway'] = $pathway->getPathway();
  102. // Get the module buffer after component execution.
  103. $buffer2 = $document->getBuffer();
  104. // Make sure the module buffer is an array.
  105. if (!isset($buffer2['module']) || !is_array($buffer2['module'])) {
  106. $buffer2['module'] = array();
  107. }
  108. // Compare the second module buffer against the first buffer.
  109. $cached['module'] = array_diff_assoc($buffer2['module'], $buffer1['module']);
  110. // Store the cache data
  111. $this->store(serialize($cached), $id);
  112. }
  113. return false;
  114. }
  115. /**
  116. * Generate a view cache id
  117. *
  118. * @access private
  119. * @param object $view The view object to cache output for
  120. * @param string $method The method name to cache for the view object
  121. * @return string MD5 Hash : view cache id
  122. * @since 1.5
  123. */
  124. function _makeId(&$view, $method)
  125. {
  126. return md5(serialize(array(JRequest::getURI(), get_class($view), $method)));
  127. }
  128. }