PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/joebushi/joomla
PHP | 144 lines | 62 code | 21 blank | 61 comment | 12 complexity | ef87790a1db095d6f4ae2e0cde9788e4 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * Joomla! Cache view type object
  13. *
  14. * @package Joomla.Framework
  15. * @subpackage Cache
  16. * @since 1.5
  17. */
  18. class JCacheView extends JCache
  19. {
  20. /**
  21. * Get the cached view data
  22. *
  23. * @access public
  24. * @param object $view The view object to cache output for
  25. * @param string $method The method name of the view method to cache output for
  26. * @param string $group The cache data group
  27. * @param string $id The cache data id
  28. * @return boolean True if the cache is hit (false else)
  29. * @since 1.5
  30. */
  31. function get(&$view, $method, $id=false)
  32. {
  33. // Initialise variables.
  34. $app = &JFactory::getApplication();
  35. $data = false;
  36. // If an id is not given generate it from the request
  37. if ($id == false) {
  38. $id = $this->_makeId($view, $method);
  39. }
  40. $data = parent::get($id);
  41. if ($data !== false) {
  42. $data = unserialize($data);
  43. $document = &JFactory::getDocument();
  44. // Get the document head out of the cache.
  45. $document->setHeadData((isset($data['head'])) ? $data['head'] : array());
  46. // If the pathway buffer is set in the cache data, get it.
  47. if (isset($data['pathway']) && is_array($data['pathway']))
  48. {
  49. // Push the pathway data into the pathway object.
  50. $pathway = &$app->getPathWay();
  51. $pathway->setPathway($data['pathway']);
  52. }
  53. // If a module buffer is set in the cache data, get it.
  54. if (isset($data['module']) && is_array($data['module']))
  55. {
  56. // Iterate through the module positions and push them into the document buffer.
  57. foreach ($data['module'] as $name => $contents) {
  58. $document->setBuffer($contents, 'module', $name);
  59. }
  60. }
  61. // Get the document body out of the cache.
  62. echo (isset($data['body'])) ? $data['body'] : null;
  63. return true;
  64. }
  65. /*
  66. * No hit so we have to execute the view
  67. */
  68. if (method_exists($view, $method))
  69. {
  70. $document = &JFactory::getDocument();
  71. // Get the modules buffer before component execution.
  72. $buffer1 = $document->getBuffer();
  73. // Make sure the module buffer is an array.
  74. if (!isset($buffer1['module']) || !is_array($buffer1['module'])) {
  75. $buffer1['module'] = array();
  76. }
  77. // Capture and echo output
  78. ob_start();
  79. ob_implicit_flush(false);
  80. $view->$method();
  81. $data = ob_get_contents();
  82. ob_end_clean();
  83. echo $data;
  84. /*
  85. * For a view we have a special case. We need to cache not only the output from the view, but the state
  86. * of the document head after the view has been rendered. This will allow us to properly cache any attached
  87. * scripts or stylesheets or links or any other modifications that the view has made to the document object
  88. */
  89. $cached = array();
  90. // View body data
  91. $cached['body'] = $data;
  92. // Document head data
  93. $cached['head'] = $document->getHeadData();
  94. // Pathway data
  95. $pathway = &$app->getPathWay();
  96. $cached['pathway'] = $pathway->getPathway();
  97. // Get the module buffer after component execution.
  98. $buffer2 = $document->getBuffer();
  99. // Make sure the module buffer is an array.
  100. if (!isset($buffer2['module']) || !is_array($buffer2['module'])) {
  101. $buffer2['module'] = array();
  102. }
  103. // Compare the second module buffer against the first buffer.
  104. $cached['module'] = array_diff_assoc($buffer2['module'], $buffer1['module']);
  105. // Store the cache data
  106. $this->store(serialize($cached), $id);
  107. }
  108. return false;
  109. }
  110. /**
  111. * Generate a view cache id
  112. *
  113. * @access private
  114. * @param object $view The view object to cache output for
  115. * @param string $method The method name to cache for the view object
  116. * @return string MD5 Hash : view cache id
  117. * @since 1.5
  118. */
  119. function _makeId(&$view, $method)
  120. {
  121. return md5(serialize(array(JRequest::getURI(), get_class($view), $method)));
  122. }
  123. }