PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 137 lines | 54 code | 19 blank | 64 comment | 10 complexity | c433ed3667597d3a58d57420dc8a6529 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: view.php 9764 2007-12-30 07:48:11Z ircmaxell $
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @copyright Copyright (C) 2005 - 2008 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. * @author Louis Landry <louis.landry@joomla.org>
  20. * @package Joomla.Framework
  21. * @subpackage Cache
  22. * @since 1.5
  23. */
  24. class JCacheView extends JCache
  25. {
  26. /**
  27. * Get the cached view data
  28. *
  29. * @access public
  30. * @param object $view The view object to cache output for
  31. * @param string $method The method name of the view method to cache output for
  32. * @param string $group The cache data group
  33. * @param string $id The cache data id
  34. * @return boolean True if the cache is hit (false else)
  35. * @since 1.5
  36. */
  37. function get( &$view, $method, $id=false )
  38. {
  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 a module buffer is set in the cache data, get it.
  52. if (isset($data['module']) && is_array($data['module']))
  53. {
  54. // Iterate through the module positions and push them into the document buffer.
  55. foreach ($data['module'] as $name => $contents) {
  56. $document->setBuffer($contents, 'module', $name);
  57. }
  58. }
  59. // Get the document body out of the cache.
  60. echo (isset($data['body'])) ? $data['body'] : null;
  61. return true;
  62. }
  63. /*
  64. * No hit so we have to execute the view
  65. */
  66. if (method_exists($view, $method))
  67. {
  68. $document = &JFactory::getDocument();
  69. // Get the modules buffer before component execution.
  70. $buffer1 = $document->getBuffer();
  71. // Make sure the module buffer is an array.
  72. if (!isset($buffer1['module']) || !is_array($buffer1['module'])) {
  73. $buffer1['module'] = array();
  74. }
  75. // Capture and echo output
  76. ob_start();
  77. ob_implicit_flush( false );
  78. $view->$method();
  79. $data = ob_get_contents();
  80. ob_end_clean();
  81. echo $data;
  82. /*
  83. * For a view we have a special case. We need to cache not only the output from the view, but the state
  84. * of the document head after the view has been rendered. This will allow us to properly cache any attached
  85. * scripts or stylesheets or links or any other modifications that the view has made to the document object
  86. */
  87. $cached = array();
  88. // View body data
  89. $cached['body'] = $data;
  90. // Document head data
  91. $cached['head'] = $document->getHeadData();
  92. // Get the module buffer after component execution.
  93. $buffer2 = $document->getBuffer();
  94. // Make sure the module buffer is an array.
  95. if (!isset($buffer2['module']) || !is_array($buffer2['module'])) {
  96. $buffer2['module'] = array();
  97. }
  98. // Compare the second module buffer against the first buffer.
  99. $cached['module'] = array_diff_assoc($buffer2['module'], $buffer1['module']);
  100. // Store the cache data
  101. $this->store(serialize($cached), $id);
  102. }
  103. return false;
  104. }
  105. /**
  106. * Generate a view cache id
  107. *
  108. * @access private
  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. * @return string MD5 Hash : view cache id
  112. * @since 1.5
  113. */
  114. function _makeId(&$view, $method)
  115. {
  116. return md5(serialize(array(JRequest::getURI(), get_class($view), $method)));
  117. }
  118. }